RavEngine
Loading...
Searching...
No Matches
ScSimStateData.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 SC_SIM_STATE_DATA_H
30#define SC_SIM_STATE_DATA_H
31
32#include "foundation/PxMemory.h"
33#include "ScBodyCore.h"
34
35namespace physx
36{
37namespace Sc
38{
40 {
41 PxTransform targetPose; // The body will move to this pose over the superstep following this getting set.
42 PxU8 targetValid; // User set a kinematic target.
43 PxU8 pad[2];
44 PxU8 type;
45 };
46
48 {
49 // The following members buffer the original body data to restore them when switching back to dynamic body
50 // (for kinematics the corresponding LowLevel properties are set to predefined values)
51 PxVec3 backupInverseInertia; // The inverse of the body space inertia tensor
52 PxReal backupInvMass; // The inverse of the body mass
53 PxReal backupLinearDamping; // The velocity is scaled by (1.0f - this * dt) inside integrateVelocity() every substep.
54 PxReal backupAngularDamping;
55 PxReal backupMaxAngVelSq; // The angular velocity's magnitude is clamped to this maximum value.
56 PxReal backupMaxLinVelSq; // The angular velocity's magnitude is clamped to this maximum value
57 };
58 PX_COMPILE_TIME_ASSERT(0 == (sizeof(Kinematic) & 0x0f));
59
60 enum VelocityModFlags
61 {
62 VMF_GRAVITY_DIRTY = (1 << 0),
63 VMF_ACC_DIRTY = (1 << 1),
64 VMF_VEL_DIRTY = (1 << 2)
65 };
66
67 // Important: Struct is reset in setForcesToDefaults.
69 {
70 PxVec3 linearPerSec; // A request to change the linear velocity by this much each second. The velocity is changed by this * dt inside integrateVelocity().
71 PxU8 pad0[4];
72 PxVec3 angularPerSec;
73 PxU8 pad1[3];
74 PxU8 type;
75 PxVec3 linearPerStep; // A request to change the linear velocity by this much the next step. The velocity is changed inside updateForces().
76 PxU32 pad2;
77 PxVec3 angularPerStep;
78 PxU32 pad3;
79
80 PX_FORCE_INLINE void clear() { linearPerSec = angularPerSec = linearPerStep = angularPerStep = PxVec3(0.0f); }
81
82 PX_FORCE_INLINE void clearPerStep() { linearPerStep = angularPerStep = PxVec3(0.0f); }
83
84 PX_FORCE_INLINE const PxVec3& getLinearVelModPerSec() const { return linearPerSec; }
85 PX_FORCE_INLINE void accumulateLinearVelModPerSec(const PxVec3& v) { linearPerSec += v; }
86 PX_FORCE_INLINE void setLinearVelModPerSec(const PxVec3& v) { linearPerSec = v; }
87 PX_FORCE_INLINE void clearLinearVelModPerSec() { linearPerSec = PxVec3(0.0f); }
88
89 PX_FORCE_INLINE const PxVec3& getLinearVelModPerStep() const { return linearPerStep; }
90 PX_FORCE_INLINE void accumulateLinearVelModPerStep(const PxVec3& v) { linearPerStep += v; }
91 PX_FORCE_INLINE void clearLinearVelModPerStep() { linearPerStep = PxVec3(0.0f); }
92
93 PX_FORCE_INLINE const PxVec3& getAngularVelModPerSec() const { return angularPerSec; }
94 PX_FORCE_INLINE void accumulateAngularVelModPerSec(const PxVec3& v) { angularPerSec += v; }
95 PX_FORCE_INLINE void setAngularVelModPerSec(const PxVec3& v) { angularPerSec = v; }
96 PX_FORCE_INLINE void clearAngularVelModPerSec() { angularPerSec = PxVec3(0.0f); }
97
98 PX_FORCE_INLINE const PxVec3& getAngularVelModPerStep() const { return angularPerStep; }
99 PX_FORCE_INLINE void accumulateAngularVelModPerStep(const PxVec3& v) { angularPerStep += v; }
100 PX_FORCE_INLINE void clearAngularVelModPerStep() { angularPerStep = PxVec3(0.0f); }
101 };
102 PX_COMPILE_TIME_ASSERT(sizeof(VelocityMod) == sizeof(Kinematic));
103
104
105 // Structure to store data either for kinematics (target pose etc.) or for dynamics (vel and accel changes).
106 // note: we do not delete this object for kinematics even if no target is set.
107 struct SimStateData : public PxUserAllocated // TODO: may want to optimize the allocation of this further.
108 {
109 PxU8 data[sizeof(Kinematic)];
110
111 enum Enum
112 {
113 eVelMod=0,
114 eKine
115 };
116
117 SimStateData(){}
118 SimStateData(const PxU8 type)
119 {
120 PxMemZero(data, sizeof(Kinematic));
121 Kinematic* kine = reinterpret_cast<Kinematic*>(data);
122 kine->type = type;
123 }
124
125 PX_FORCE_INLINE PxU32 getType() const { const Kinematic* kine = reinterpret_cast<const Kinematic*>(data); return kine->type;}
126 PX_FORCE_INLINE bool isKine() const {return eKine == getType();}
127 PX_FORCE_INLINE bool isVelMod() const {return eVelMod == getType();}
128
129 PX_FORCE_INLINE Kinematic* getKinematicData() { Kinematic* kine = reinterpret_cast<Kinematic*>(data); PX_ASSERT(eKine == kine->type); return kine;}
130 PX_FORCE_INLINE VelocityMod* getVelocityModData() { VelocityMod* velmod = reinterpret_cast<VelocityMod*>(data); PX_ASSERT(eVelMod == velmod->type); return velmod;}
131 PX_FORCE_INLINE const Kinematic* getKinematicData() const { const Kinematic* kine = reinterpret_cast<const Kinematic*>(data); PX_ASSERT(eKine == kine->type); return kine;}
132 PX_FORCE_INLINE const VelocityMod* getVelocityModData() const { const VelocityMod* velmod = reinterpret_cast<const VelocityMod*>(data); PX_ASSERT(eVelMod == velmod->type); return velmod;}
133 };
134
135 PX_FORCE_INLINE void simStateBackupAndClearBodyProperties(SimStateData* simStateData, PxsBodyCore& core)
136 {
137 PX_ASSERT(simStateData && simStateData->isKine());
138 Kinematic* kine = simStateData->getKinematicData();
139
140 kine->backupLinearDamping = core.linearDamping;
141 kine->backupAngularDamping = core.angularDamping;
142 kine->backupInverseInertia = core.inverseInertia;
143 kine->backupInvMass = core.inverseMass;
144 kine->backupMaxAngVelSq = core.maxAngularVelocitySq;
145 kine->backupMaxLinVelSq = core.maxLinearVelocitySq;
146
147 core.inverseMass = 0.0f;
148 core.inverseInertia = PxVec3(0.0f);
149 core.linearDamping = 0.0f;
150 core.angularDamping = 0.0f;
151 core.maxAngularVelocitySq = PX_MAX_REAL;
152 core.maxLinearVelocitySq = PX_MAX_REAL;
153 }
154
155 PX_FORCE_INLINE void simStateRestoreBodyProperties(const SimStateData* simStateData, PxsBodyCore& core)
156 {
157 PX_ASSERT(simStateData && simStateData->isKine());
158 const Kinematic* kine = simStateData->getKinematicData();
159 core.inverseMass = kine->backupInvMass;
160 core.inverseInertia = kine->backupInverseInertia;
161 core.linearDamping = kine->backupLinearDamping;
162 core.angularDamping = kine->backupAngularDamping;
163 core.maxAngularVelocitySq = kine->backupMaxAngVelSq;
164 core.maxLinearVelocitySq = kine->backupMaxLinVelSq;
165 }
166
167 PX_FORCE_INLINE void simStateClearVelMod(SimStateData* simStateData)
168 {
169 if (simStateData && simStateData->isVelMod())
170 {
171 VelocityMod* velmod = simStateData->getVelocityModData();
172 velmod->clear();
173 }
174 }
175
176 PX_FORCE_INLINE bool simStateGetKinematicTarget(const SimStateData* simStateData, PxTransform& p)
177 {
178 if (simStateData && simStateData->isKine() && simStateData->getKinematicData()->targetValid)
179 {
180 p = simStateData->getKinematicData()->targetPose;
181 return true;
182 }
183 else
184 return false;
185 }
186
187 PX_FORCE_INLINE bool simStateGetHasValidKinematicTarget(const SimStateData* simStateData)
188 {
189 PX_ASSERT(!simStateData || simStateData->isKine());
190 return simStateData && simStateData->isKine() && simStateData->getKinematicData()->targetValid;
191 }
192
193 PX_FORCE_INLINE void simStateSetKinematicTarget(SimStateData* simStateData, const PxTransform& p)
194 {
195 PX_ASSERT(simStateData && simStateData->isKine());
196 // setting the kinematic target is only allowed if the body is part of a scene, at which point the
197 // mSimStateData buffer must exist
198 Kinematic* kine = simStateData->getKinematicData();
199 kine->targetPose = p;
200 kine->targetValid = 1;
201 }
202
203 PX_FORCE_INLINE void simStateInvalidateKinematicTarget(SimStateData* simStateData)
204 {
205 PX_ASSERT(simStateData && simStateData->isKine());
206 simStateData->getKinematicData()->targetValid = 0;
207 }
208} // namespace Sc
209
210} // namespace physx
211
212#endif
213
class representing a rigid euclidean transform as a quaternion and a vector
Definition PxTransform.h:49
Definition PxUserAllocated.h:43
3 Element vector class.
Definition PxVec3.h:50
#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
PX_FORCE_INLINE void * PxMemZero(void *dest, PxU32 count)
Sets the bytes of the provided buffer to zero.
Definition PxMemory.h:53
Definition PxvDynamics.h:74
Definition ScSimStateData.h:40
Definition ScSimStateData.h:48
Definition ScSimStateData.h:108
Definition ScSimStateData.h:69