RavEngine
Loading...
Searching...
No Matches
PxsRigidBody.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef PXS_RIGID_BODY_H
30#define PXS_RIGID_BODY_H
31
32#include "PxvDynamics.h"
33#include "CmSpatialVector.h"
34
35namespace physx
36{
37struct PxsCCDBody;
38
39#define PX_INTERNAL_LOCK_FLAG_START 8
40
41PX_ALIGN_PREFIX(16)
43{
44 public:
45
46 enum PxsRigidBodyFlag
47 {
48 eFROZEN = 1 << 0, //This flag indicates that the stabilization is enabled and the body is
49 //"frozen". By "frozen", we mean that the body's transform is unchanged
50 //from the previous frame. This permits various optimizations.
51 eFREEZE_THIS_FRAME = 1 << 1,
52 eUNFREEZE_THIS_FRAME = 1 << 2,
53 eACTIVATE_THIS_FRAME = 1 << 3,
54 eDEACTIVATE_THIS_FRAME = 1 << 4,
55 // PT: this flag is now only used on the GPU. For the CPU the data is now stored directly in PxsBodyCore.
56 eDISABLE_GRAVITY_GPU = 1 << 5,
57 eSPECULATIVE_CCD = 1 << 6,
58 eENABLE_GYROSCROPIC = 1 << 7,
59 //KS - copied here for GPU simulation to avoid needing to pass another set of flags around.
60 eLOCK_LINEAR_X = 1 << (PX_INTERNAL_LOCK_FLAG_START),
61 eLOCK_LINEAR_Y = 1 << (PX_INTERNAL_LOCK_FLAG_START + 1),
62 eLOCK_LINEAR_Z = 1 << (PX_INTERNAL_LOCK_FLAG_START + 2),
63 eLOCK_ANGULAR_X = 1 << (PX_INTERNAL_LOCK_FLAG_START + 3),
64 eLOCK_ANGULAR_Y = 1 << (PX_INTERNAL_LOCK_FLAG_START + 4),
65 eLOCK_ANGULAR_Z = 1 << (PX_INTERNAL_LOCK_FLAG_START + 5),
66 eRETAIN_ACCELERATION = 1 << 14,
67 eFIRST_BODY_COPY_GPU = 1 << 15 // Flag to raise to indicate that the body is DMA'd to the GPU for the first time
68 };
69
70 PX_FORCE_INLINE PxsRigidBody(PxsBodyCore* core, PxReal freeze_count) :
71 // PT: TODO: unify naming conventions
72 mLastTransform (core->body2World),
73 mInternalFlags (0),
74 solverIterationCounts (core->solverIterationCounts),
75 mCCD (NULL),
76 mCore (core),
77 sleepLinVelAcc (PxVec3(0.0f)),
78 freezeCount (freeze_count),
79 sleepAngVelAcc (PxVec3(0.0f)),
80 accelScale (1.0f)
81 {}
82
84
85 PX_FORCE_INLINE const PxTransform& getPose() const { PX_ASSERT(mCore->body2World.isSane()); return mCore->body2World; }
86
87 PX_FORCE_INLINE const PxVec3& getLinearVelocity() const { PX_ASSERT(mCore->linearVelocity.isFinite()); return mCore->linearVelocity; }
88 PX_FORCE_INLINE const PxVec3& getAngularVelocity() const { PX_ASSERT(mCore->angularVelocity.isFinite()); return mCore->angularVelocity; }
89
90 PX_FORCE_INLINE void setVelocity(const PxVec3& linear,
91 const PxVec3& angular) { PX_ASSERT(linear.isFinite()); PX_ASSERT(angular.isFinite());
92 mCore->linearVelocity = linear;
93 mCore->angularVelocity = angular; }
94 PX_FORCE_INLINE void setLinearVelocity(const PxVec3& linear) { PX_ASSERT(linear.isFinite()); mCore->linearVelocity = linear; }
95 PX_FORCE_INLINE void setAngularVelocity(const PxVec3& angular) { PX_ASSERT(angular.isFinite()); mCore->angularVelocity = angular; }
96
97 PX_FORCE_INLINE void constrainLinearVelocity();
98 PX_FORCE_INLINE void constrainAngularVelocity();
99
100 PX_FORCE_INLINE PxU32 getIterationCounts() { return mCore->solverIterationCounts; }
101 PX_FORCE_INLINE PxReal getReportThreshold() const { return mCore->contactReportThreshold; }
102
103 PX_FORCE_INLINE const PxTransform& getLastCCDTransform() const { return mLastTransform; }
104 PX_FORCE_INLINE void saveLastCCDTransform() { mLastTransform = mCore->body2World; }
105
106 PX_FORCE_INLINE bool isKinematic() const { return mCore->inverseMass == 0.0f; }
107
108 PX_FORCE_INLINE void setPose(const PxTransform& pose) { mCore->body2World = pose; }
109 PX_FORCE_INLINE void setPosition(const PxVec3& position) { mCore->body2World.p = position; }
110 PX_FORCE_INLINE PxReal getInvMass() const { return mCore->inverseMass; }
111 PX_FORCE_INLINE PxVec3 getInvInertia() const { return mCore->inverseInertia; }
112 PX_FORCE_INLINE PxReal getMass() const { return 1.0f/mCore->inverseMass; }
113 PX_FORCE_INLINE PxVec3 getInertia() const { return PxVec3(1.0f/mCore->inverseInertia.x,
114 1.0f/mCore->inverseInertia.y,
115 1.0f/mCore->inverseInertia.z); }
116 PX_FORCE_INLINE PxsBodyCore& getCore() { return *mCore; }
117 PX_FORCE_INLINE const PxsBodyCore& getCore() const { return *mCore; }
118
119 PX_FORCE_INLINE PxU32 isActivateThisFrame() const { return PxU32(mInternalFlags & eACTIVATE_THIS_FRAME); }
120 PX_FORCE_INLINE PxU32 isDeactivateThisFrame() const { return PxU32(mInternalFlags & eDEACTIVATE_THIS_FRAME); }
121 PX_FORCE_INLINE PxU32 isFreezeThisFrame() const { return PxU32(mInternalFlags & eFREEZE_THIS_FRAME); }
122 PX_FORCE_INLINE PxU32 isUnfreezeThisFrame() const { return PxU32(mInternalFlags & eUNFREEZE_THIS_FRAME); }
123 PX_FORCE_INLINE void clearFreezeFlag() { mInternalFlags &= ~eFREEZE_THIS_FRAME; }
124 PX_FORCE_INLINE void clearUnfreezeFlag() { mInternalFlags &= ~eUNFREEZE_THIS_FRAME; }
125 PX_FORCE_INLINE void clearAllFrameFlags() { mInternalFlags &= ~(eFREEZE_THIS_FRAME | eUNFREEZE_THIS_FRAME | eACTIVATE_THIS_FRAME | eDEACTIVATE_THIS_FRAME); }
126
127 // PT: implemented in PxsCCD.cpp:
128 void advanceToToi(PxReal toi, PxReal dt, bool clip);
129 void advancePrevPoseToToi(PxReal toi);
130// PxTransform getAdvancedTransform(PxReal toi) const;
131 Cm::SpatialVector getPreSolverVelocities() const;
132
133 PxTransform mLastTransform; //28 (28)
134
135 PxU16 mInternalFlags; //30 (30)
136 PxU16 solverIterationCounts; //32 (32)
137
138 PxsCCDBody* mCCD; //36 (40) // only valid during CCD
139
140 PxsBodyCore* mCore; //40 (48)
141#if !PX_P64_FAMILY
142 PxU32 alignmentPad[2]; //48 (48)
143#endif
144 PxVec3 sleepLinVelAcc; //60 (60)
145 PxReal freezeCount; //64 (64)
146
147 PxVec3 sleepAngVelAcc; //76 (76)
148 PxReal accelScale; //80 (80)
149}
150PX_ALIGN_SUFFIX(16);
151PX_COMPILE_TIME_ASSERT(0 == (sizeof(PxsRigidBody) & 0x0f));
152
153void PxsRigidBody::constrainLinearVelocity()
154{
155 const PxU32 lockFlags = mCore->lockFlags;
156 if(lockFlags)
157 {
158 if(lockFlags & PxRigidDynamicLockFlag::eLOCK_LINEAR_X)
159 mCore->linearVelocity.x = 0.0f;
160 if(lockFlags & PxRigidDynamicLockFlag::eLOCK_LINEAR_Y)
161 mCore->linearVelocity.y = 0.0f;
162 if(lockFlags & PxRigidDynamicLockFlag::eLOCK_LINEAR_Z)
163 mCore->linearVelocity.z = 0.0f;
164 }
165}
166
167void PxsRigidBody::constrainAngularVelocity()
168{
169 const PxU32 lockFlags = mCore->lockFlags;
170 if(lockFlags)
171 {
172 if(lockFlags & PxRigidDynamicLockFlag::eLOCK_ANGULAR_X)
173 mCore->angularVelocity.x = 0.0f;
174 if(lockFlags & PxRigidDynamicLockFlag::eLOCK_ANGULAR_Y)
175 mCore->angularVelocity.y = 0.0f;
176 if(lockFlags & PxRigidDynamicLockFlag::eLOCK_ANGULAR_Z)
177 mCore->angularVelocity.z = 0.0f;
178 }
179}
180
181}
182
183#endif
Definition CmSpatialVector.h:46
class representing a rigid euclidean transform as a quaternion and a vector
Definition PxTransform.h:49
3 Element vector class.
Definition PxVec3.h:50
PX_CUDA_CALLABLE PX_INLINE bool isFinite() const
returns true if all 3 elems of the vector are finite (not NAN or INF, etc.)
Definition PxVec3.h:156
Definition PxsRigidBody.h:43
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
#define PX_COMPILE_TIME_ASSERT(exp)
Definition PxPreprocessor.h:428
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
Definition PxvDynamics.h:74
Structure to represent a body in the CCD system.
Definition PxsCCD.h:123