Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
MotionProperties.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <Jolt/Core/Atomics.h>
17
19
20class StateRecorder;
21
23enum class ECanSleep
24{
25 CannotSleep = 0,
26 CanSleep = 1,
27};
28
31{
32public:
34
36 EMotionQuality GetMotionQuality() const { return mMotionQuality; }
37
39 inline EAllowedDOFs GetAllowedDOFs() const { return mAllowedDOFs; }
40
42 inline bool GetAllowSleeping() const { return mAllowSleeping; }
43
45 inline Vec3 GetLinearVelocity() const { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::Read)); return mLinearVelocity; }
46
50 void SetLinearVelocity(Vec3Arg inLinearVelocity) { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::ReadWrite)); JPH_ASSERT(inLinearVelocity.Length() <= mMaxLinearVelocity); mLinearVelocity = LockTranslation(inLinearVelocity); }
51
55 void SetLinearVelocityClamped(Vec3Arg inLinearVelocity) { mLinearVelocity = LockTranslation(inLinearVelocity); ClampLinearVelocity(); }
56
58 inline Vec3 GetAngularVelocity() const { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::Read)); return mAngularVelocity; }
59
63 void SetAngularVelocity(Vec3Arg inAngularVelocity) { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::ReadWrite)); JPH_ASSERT(inAngularVelocity.Length() <= mMaxAngularVelocity); mAngularVelocity = LockAngular(inAngularVelocity); }
64
68 void SetAngularVelocityClamped(Vec3Arg inAngularVelocity) { mAngularVelocity = LockAngular(inAngularVelocity); ClampAngularVelocity(); }
69
73 inline void MoveKinematic(Vec3Arg inDeltaPosition, QuatArg inDeltaRotation, float inDeltaTime);
74
77
79 inline float GetMaxLinearVelocity() const { return mMaxLinearVelocity; }
80 inline void SetMaxLinearVelocity(float inLinearVelocity) { JPH_ASSERT(inLinearVelocity >= 0.0f); mMaxLinearVelocity = inLinearVelocity; }
81
83 inline float GetMaxAngularVelocity() const { return mMaxAngularVelocity; }
84 inline void SetMaxAngularVelocity(float inAngularVelocity) { JPH_ASSERT(inAngularVelocity >= 0.0f); mMaxAngularVelocity = inAngularVelocity; }
86
88 inline void ClampLinearVelocity();
89 inline void ClampAngularVelocity();
90
92 inline float GetLinearDamping() const { return mLinearDamping; }
93 void SetLinearDamping(float inLinearDamping) { JPH_ASSERT(inLinearDamping >= 0.0f); mLinearDamping = inLinearDamping; }
94
96 inline float GetAngularDamping() const { return mAngularDamping; }
97 void SetAngularDamping(float inAngularDamping) { JPH_ASSERT(inAngularDamping >= 0.0f); mAngularDamping = inAngularDamping; }
98
100 inline float GetGravityFactor() const { return mGravityFactor; }
101 void SetGravityFactor(float inGravityFactor) { mGravityFactor = inGravityFactor; }
102
104 void SetMassProperties(EAllowedDOFs inAllowedDOFs, const MassProperties &inMassProperties);
105
107 inline float GetInverseMass() const { JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic); return mInvMass; }
108 inline float GetInverseMassUnchecked() const { return mInvMass; }
109
114 void SetInverseMass(float inInverseMass) { mInvMass = inInverseMass; }
115
117 inline Vec3 GetInverseInertiaDiagonal() const { JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic); return mInvInertiaDiagonal; }
118
120 inline Quat GetInertiaRotation() const { return mInertiaRotation; }
121
126 void SetInverseInertia(Vec3Arg inDiagonal, QuatArg inRot) { mInvInertiaDiagonal = inDiagonal; mInertiaRotation = inRot; }
127
130 void ScaleToMass(float inMass);
131
133 inline Mat44 GetLocalSpaceInverseInertia() const;
134
136 inline Mat44 GetLocalSpaceInverseInertiaUnchecked() const;
137
139 inline Mat44 GetInverseInertiaForRotation(Mat44Arg inRotation) const;
140
142 JPH_INLINE Vec3 MultiplyWorldSpaceInverseInertiaByVector(QuatArg inBodyRotation, Vec3Arg inV) const;
143
145 JPH_INLINE Vec3 GetPointVelocityCOM(Vec3Arg inPointRelativeToCOM) const { return mLinearVelocity + mAngularVelocity.Cross(inPointRelativeToCOM); }
146
147 // Get the total amount of force applied to the center of mass this time step (through Body::AddForce calls). Note that it will reset to zero after PhysicsSystem::Update.
148 JPH_INLINE Vec3 GetAccumulatedForce() const { return Vec3::sLoadFloat3Unsafe(mForce); }
149
150 // Get the total amount of torque applied to the center of mass this time step (through Body::AddForce/Body::AddTorque calls). Note that it will reset to zero after PhysicsSystem::Update.
151 JPH_INLINE Vec3 GetAccumulatedTorque() const { return Vec3::sLoadFloat3Unsafe(mTorque); }
152
153 // Reset the total accumulated force, note that this will be done automatically after every time step.
154 JPH_INLINE void ResetForce() { mForce = Float3(0, 0, 0); }
155
156 // Reset the total accumulated torque, note that this will be done automatically after every time step.
157 JPH_INLINE void ResetTorque() { mTorque = Float3(0, 0, 0); }
158
159 // Reset the current velocity and accumulated force and torque.
160 JPH_INLINE void ResetMotion()
161 {
162 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::ReadWrite));
163 mLinearVelocity = mAngularVelocity = Vec3::sZero();
164 mForce = mTorque = Float3(0, 0, 0);
165 }
166
173
175 JPH_INLINE Vec3 LockTranslation(Vec3Arg inV) const
176 {
177 return Vec3::sAnd(inV, Vec3(GetLinearDOFsMask().ReinterpretAsFloat()));
178 }
179
186
188 JPH_INLINE Vec3 LockAngular(Vec3Arg inV) const
189 {
190 return Vec3::sAnd(inV, Vec3(GetAngularDOFsMask().ReinterpretAsFloat()));
191 }
192
194 void SetNumVelocityStepsOverride(uint inN) { JPH_ASSERT(inN < 256); mNumVelocityStepsOverride = uint8(inN); }
195 uint GetNumVelocityStepsOverride() const { return mNumVelocityStepsOverride; }
196
198 void SetNumPositionStepsOverride(uint inN) { JPH_ASSERT(inN < 256); mNumPositionStepsOverride = uint8(inN); }
199 uint GetNumPositionStepsOverride() const { return mNumPositionStepsOverride; }
200
201#ifdef JPH_TRACK_SIMULATION_STATS
203 struct SimulationStats
204 {
205 void Reset() { mBroadPhaseTicks = 0; mNarrowPhaseTicks.store(0, memory_order_relaxed); mVelocityConstraintTicks = 0; mPositionConstraintTicks = 0; mUpdateBoundsTicks = 0; mCCDTicks.store(0, memory_order_relaxed); mNumContactConstraints.store(0, memory_order_relaxed); mNumCollisionSteps = 0; mNumVelocitySteps = 0; mNumPositionSteps = 0; mIsLargeIsland = false; }
206
207 uint64 mBroadPhaseTicks = 0;
208 atomic<uint64> mNarrowPhaseTicks = 0;
209 uint64 mVelocityConstraintTicks = 0;
210 uint64 mPositionConstraintTicks = 0;
211 uint64 mUpdateBoundsTicks = 0;
212 atomic<uint64> mCCDTicks = 0;
213 atomic<uint32> mNumContactConstraints = 0;
214 uint8 mNumCollisionSteps = 0;
215 uint8 mNumVelocitySteps = 0;
216 uint8 mNumPositionSteps = 0;
217 bool mIsLargeIsland = false;
218 };
219
220 const SimulationStats & GetSimulationStats() const { return mSimulationStats; }
221 SimulationStats & GetSimulationStats() { return mSimulationStats; }
222#endif // JPH_TRACK_SIMULATION_STATS
223
225 // FUNCTIONS BELOW THIS LINE ARE FOR INTERNAL USE ONLY
227
230 inline void ApplyLinearVelocityStep(Vec3Arg inLinearVelocity)
231 {
232 JPH_DET_LOG("ApplyLinearVelocityStep: " << inLinearVelocity);
233 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::ReadWrite));
234 JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic);
235 JPH_ASSERT(!inLinearVelocity.IsNaN());
236 mLinearVelocity = LockTranslation(inLinearVelocity);
237 }
238 inline void AddLinearVelocityStep(Vec3Arg inLinearVelocityChange) { ApplyLinearVelocityStep(mLinearVelocity + inLinearVelocityChange); }
239 inline void SubLinearVelocityStep(Vec3Arg inLinearVelocityChange) { ApplyLinearVelocityStep(mLinearVelocity - inLinearVelocityChange); }
240
241 inline void ApplyAngularVelocityStep(Vec3Arg inAngularVelocity)
242 {
243 JPH_DET_LOG("ApplyAngularVelocityStep: " << inAngularVelocity);
244 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::ReadWrite));
245 JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic);
246 JPH_ASSERT(!inAngularVelocity.IsNaN());
247 mAngularVelocity = inAngularVelocity;
248 }
249 inline void AddAngularVelocityStep(Vec3Arg inAngularVelocityChange) { ApplyAngularVelocityStep(mAngularVelocity + inAngularVelocityChange); }
250 inline void SubAngularVelocityStep(Vec3Arg inAngularVelocityChange) { ApplyAngularVelocityStep(mAngularVelocity - inAngularVelocityChange); }
252
254 inline void ApplyGyroscopicForceInternal(QuatArg inBodyRotation, float inDeltaTime);
255
257 inline void ApplyForceTorqueAndDragInternal(QuatArg inBodyRotation, Vec3Arg inGravity, float inDeltaTime);
258
260 uint32 GetIslandIndexInternal() const { return mIslandIndex; }
261 void SetIslandIndexInternal(uint32 inIndex) { mIslandIndex = inIndex; }
262
264 uint32 GetIndexInActiveBodiesInternal() const { return mIndexInActiveBodies.load(memory_order_relaxed); }
265
266#ifdef JPH_DOUBLE_PRECISION
267 inline DVec3 GetSleepTestOffset() const { return DVec3::sLoadDouble3Unsafe(mSleepTestOffset); }
268#endif // JPH_DOUBLE_PRECISION
269
271 inline void ResetSleepTestSpheres(const RVec3 *inPoints);
272
274 inline void ResetSleepTestTimer() { mSleepTestTimer = 0.0f; }
275
277 inline ECanSleep AccumulateSleepTime(float inDeltaTime, float inTimeBeforeSleep);
278
280 void SaveState(StateRecorder &inStream) const;
281
283 void RestoreState(StateRecorder &inStream);
284
285 static constexpr uint32 cInactiveIndex = uint32(-1);
286
287private:
288 friend class BodyManager;
289 friend class Body;
290
291 // 1st cache line
292 // 16 byte aligned
293 Vec3 mLinearVelocity { Vec3::sZero() };
294 Vec3 mAngularVelocity { Vec3::sZero() };
295 Vec3 mInvInertiaDiagonal;
296 Quat mInertiaRotation;
297
298 // 2nd cache line
299 // 4 byte aligned
300 Float3 mForce { 0, 0, 0 };
301 Float3 mTorque { 0, 0, 0 };
302 float mInvMass;
303 float mLinearDamping;
304 float mAngularDamping;
305 float mMaxLinearVelocity;
306 float mMaxAngularVelocity;
307 float mGravityFactor;
308 atomic<uint32> mIndexInActiveBodies = cInactiveIndex;
309 uint32 mIslandIndex = cInactiveIndex;
310
311 // 1 byte aligned
312 EMotionQuality mMotionQuality;
313 bool mAllowSleeping;
314 EAllowedDOFs mAllowedDOFs = EAllowedDOFs::All;
315 uint8 mNumVelocityStepsOverride = 0;
316 uint8 mNumPositionStepsOverride = 0;
317
318 // 3rd cache line (least frequently used)
319 // 4 byte aligned (or 8 byte if running in double precision)
320#ifdef JPH_DOUBLE_PRECISION
321 Double3 mSleepTestOffset;
322#endif // JPH_DOUBLE_PRECISION
323 Sphere mSleepTestSpheres[3];
324 float mSleepTestTimer;
325
326#ifdef JPH_ENABLE_ASSERTS
327 EBodyType mCachedBodyType;
328 EMotionType mCachedMotionType;
329#endif
330
331#ifdef JPH_TRACK_SIMULATION_STATS
332 SimulationStats mSimulationStats;
333#endif // JPH_TRACK_SIMULATION_STATS
334};
335
337
338#include "MotionProperties.inl"
EAllowedDOFs
Enum used in BodyCreationSettings and MotionProperties to indicate which degrees of freedom a body ha...
Definition AllowedDOFs.h:11
@ RotationX
Body can rotate around world space X axis.
@ TranslationY
Body can move in world space Y axis.
@ RotationY
Body can rotate around world space Y axis.
@ TranslationZ
Body can move in world space Z axis.
@ All
All degrees of freedom are allowed.
@ TranslationX
Body can move in world space X axis.
@ RotationZ
Body can rotate around world space Z axis.
EBodyType
Type of body.
Definition BodyType.h:11
std::uint8_t uint8
Definition Core.h:578
#define JPH_EXPORT
Definition Core.h:310
std::uint64_t uint64
Definition Core.h:582
unsigned int uint
Definition Core.h:577
#define JPH_NAMESPACE_END
Definition Core.h:494
std::uint32_t uint32
Definition Core.h:580
#define JPH_NAMESPACE_BEGIN
Definition Core.h:488
#define JPH_DET_LOG(...)
Definition DeterminismLog.h:177
#define JPH_ASSERT(...)
Definition IssueReporting.h:28
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:51
ECanSleep
Enum that determines if an object can go to sleep.
Definition MotionProperties.h:24
@ CanSleep
Object can go to sleep.
@ CannotSleep
Object cannot go to sleep.
EMotionQuality
Motion quality, or how well it detects collisions when it has a high velocity.
Definition MotionQuality.h:11
EMotionType
Motion type of a physics body.
Definition MotionType.h:11
@ Dynamic
Responds to forces as a normal physics object.
Definition Body.h:39
Definition BodyManager.h:47
Definition DVec3.h:14
static JPH_INLINE DVec3 sLoadDouble3Unsafe(const Double3 &inV)
Load 3 doubles from memory (reads 64 bits extra which it doesn't use)
Definition DVec3.inl:188
Class that holds 3 doubles. Used as a storage class. Convert to DVec3 for calculations.
Definition Double3.h:13
Class that holds 3 floats. Used as a storage class. Convert to Vec3 for calculations.
Definition Float3.h:13
Describes the mass and inertia properties of a body. Used during body construction only.
Definition MassProperties.h:16
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition Mat44.h:13
The Body class only keeps track of state for static bodies, the MotionProperties class keeps the addi...
Definition MotionProperties.h:31
void SetLinearVelocity(Vec3Arg inLinearVelocity)
Definition MotionProperties.h:50
uint32 GetIslandIndexInternal() const
Access to the island index.
Definition MotionProperties.h:260
void SetNumVelocityStepsOverride(uint inN)
Used only when this body is dynamic and colliding. Override for the number of solver velocity iterati...
Definition MotionProperties.h:194
void SetAngularVelocityClamped(Vec3Arg inAngularVelocity)
Definition MotionProperties.h:68
Quat GetInertiaRotation() const
Rotation (R) that takes inverse inertia diagonal to local space: .
Definition MotionProperties.h:120
float GetMaxLinearVelocity() const
Maximum linear velocity that a body can achieve. Used to prevent the system from exploding.
Definition MotionProperties.h:79
JPH_INLINE Vec3 GetPointVelocityCOM(Vec3Arg inPointRelativeToCOM) const
Velocity of point inPoint (in center of mass space, e.g. on the surface of the body) of the body (uni...
Definition MotionProperties.h:145
Vec3 GetInverseInertiaDiagonal() const
Diagonal of inverse inertia matrix: D. Should only be called on a dynamic object (static or kinematic...
Definition MotionProperties.h:117
void ApplyAngularVelocityStep(Vec3Arg inAngularVelocity)
Definition MotionProperties.h:241
uint GetNumPositionStepsOverride() const
Definition MotionProperties.h:199
void SetInverseInertia(Vec3Arg inDiagonal, QuatArg inRot)
Definition MotionProperties.h:126
float GetGravityFactor() const
Get gravity factor (1 = normal gravity, 0 = no gravity)
Definition MotionProperties.h:100
void AddLinearVelocityStep(Vec3Arg inLinearVelocityChange)
Definition MotionProperties.h:238
float GetMaxAngularVelocity() const
Maximum angular velocity that a body can achieve. Used to prevent the system from exploding.
Definition MotionProperties.h:83
void SetLinearVelocityClamped(Vec3Arg inLinearVelocity)
Definition MotionProperties.h:55
uint GetNumVelocityStepsOverride() const
Definition MotionProperties.h:195
float GetInverseMassUnchecked() const
Definition MotionProperties.h:108
Vec3 GetLinearVelocity() const
Get world space linear velocity of the center of mass.
Definition MotionProperties.h:45
Vec3 GetAngularVelocity() const
Get world space angular velocity of the center of mass.
Definition MotionProperties.h:58
void SetMaxAngularVelocity(float inAngularVelocity)
Definition MotionProperties.h:84
JPH_INLINE Vec3 LockAngular(Vec3Arg inV) const
Takes an angular velocity / torque vector inV and returns a vector where the components that are not ...
Definition MotionProperties.h:188
JPH_INLINE void ResetMotion()
Definition MotionProperties.h:160
JPH_INLINE Vec3 GetAccumulatedTorque() const
Definition MotionProperties.h:151
void SetIslandIndexInternal(uint32 inIndex)
Definition MotionProperties.h:261
float GetAngularDamping() const
Get angular damping: dw/dt = -c * w. c. Value should be zero or positive and is usually close to 0.
Definition MotionProperties.h:96
void SubLinearVelocityStep(Vec3Arg inLinearVelocityChange)
Definition MotionProperties.h:239
float GetInverseMass() const
Get inverse mass (1 / mass). Should only be called on a dynamic object (static or kinematic bodies ha...
Definition MotionProperties.h:107
void SetGravityFactor(float inGravityFactor)
Definition MotionProperties.h:101
JPH_INLINE UVec4 GetLinearDOFsMask() const
Returns a vector where the linear components that are not allowed by mAllowedDOFs are set to 0 and th...
Definition MotionProperties.h:168
uint32 GetIndexInActiveBodiesInternal() const
Access to the index in the active bodies array.
Definition MotionProperties.h:264
float GetLinearDamping() const
Get linear damping: dv/dt = -c * v. c. Value should be zero or positive and is usually close to 0.
Definition MotionProperties.h:92
void SubAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition MotionProperties.h:250
JPH_INLINE void ResetForce()
Definition MotionProperties.h:154
void SetInverseMass(float inInverseMass)
Definition MotionProperties.h:114
void ResetSleepTestTimer()
Reset the sleep test timer without resetting the sleep test spheres.
Definition MotionProperties.h:274
void SetAngularVelocity(Vec3Arg inAngularVelocity)
Definition MotionProperties.h:63
void ApplyLinearVelocityStep(Vec3Arg inLinearVelocity)
Definition MotionProperties.h:230
JPH_INLINE Vec3 GetAccumulatedForce() const
Definition MotionProperties.h:148
JPH_INLINE Vec3 LockTranslation(Vec3Arg inV) const
Takes a translation vector inV and returns a vector where the components that are not allowed by mAll...
Definition MotionProperties.h:175
void SetMaxLinearVelocity(float inLinearVelocity)
Definition MotionProperties.h:80
JPH_OVERRIDE_NEW_DELETE EMotionQuality GetMotionQuality() const
Motion quality, or how well it detects collisions when it has a high velocity.
Definition MotionProperties.h:36
void SetLinearDamping(float inLinearDamping)
Definition MotionProperties.h:93
void SetAngularDamping(float inAngularDamping)
Definition MotionProperties.h:97
JPH_INLINE UVec4 GetAngularDOFsMask() const
Returns a vector where the angular components that are not allowed by mAllowedDOFs are set to 0 and t...
Definition MotionProperties.h:181
void SetNumPositionStepsOverride(uint inN)
Used only when this body is dynamic and colliding. Override for the number of solver position iterati...
Definition MotionProperties.h:198
EAllowedDOFs GetAllowedDOFs() const
Get the allowed degrees of freedom that this body has (this can be changed by calling SetMassProperti...
Definition MotionProperties.h:39
bool GetAllowSleeping() const
If this body can go to sleep.
Definition MotionProperties.h:42
void AddAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition MotionProperties.h:249
JPH_INLINE void ResetTorque()
Definition MotionProperties.h:157
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition NonCopyable.h:11
Definition Quat.h:33
Definition Sphere.h:12
Definition StateRecorder.h:110
Definition UVec4.h:12
static JPH_INLINE UVec4 sReplicate(uint32 inV)
Replicate int inV across all components.
Definition UVec4.inl:81
static JPH_INLINE UVec4 sAnd(UVec4Arg inV1, UVec4Arg inV2)
Logical and (component wise)
Definition UVec4.inl:318
static JPH_INLINE UVec4 sEquals(UVec4Arg inV1, UVec4Arg inV2)
Equals (component wise)
Definition UVec4.inl:216
Definition Vec3.h:17
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition Vec3.inl:932
JPH_INLINE float Length() const
Length of vector.
Definition Vec3.inl:1041
static JPH_INLINE Vec3 sAnd(Vec3Arg inV1, Vec3Arg inV2)
Logical and (component wise)
Definition Vec3.inl:484
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition Vec3.inl:133
JPH_INLINE bool IsNaN() const
Test if vector contains NaN elements.
Definition Vec3.inl:1131
static JPH_INLINE Vec3 sLoadFloat3Unsafe(const Float3 &inV)
Load 3 floats from memory (reads 32 bits extra which it doesn't use)
Definition Vec3.inl:179