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

source code of qtquick3dphysics/src/3rdparty/PhysX/source/lowlevel/software/include/PxsRigidBody.h