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
15
17
18class StateRecorder;
19
21enum class ECanSleep
22{
23 CannotSleep = 0,
24 CanSleep = 1,
25};
26
29{
30public:
32
34 EMotionQuality GetMotionQuality() const { return mMotionQuality; }
35
37 inline EAllowedDOFs GetAllowedDOFs() const { return mAllowedDOFs; }
38
40 inline bool GetAllowSleeping() const { return mAllowSleeping; }
41
43 inline Vec3 GetLinearVelocity() const { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess, BodyAccess::EAccess::Read)); return mLinearVelocity; }
44
46 void SetLinearVelocity(Vec3Arg inLinearVelocity) { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess, BodyAccess::EAccess::ReadWrite)); JPH_ASSERT(inLinearVelocity.Length() <= mMaxLinearVelocity); mLinearVelocity = inLinearVelocity; }
47
49 void SetLinearVelocityClamped(Vec3Arg inLinearVelocity) { mLinearVelocity = inLinearVelocity; ClampLinearVelocity(); }
50
52 inline Vec3 GetAngularVelocity() const { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess, BodyAccess::EAccess::Read)); return mAngularVelocity; }
53
55 void SetAngularVelocity(Vec3Arg inAngularVelocity) { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess, BodyAccess::EAccess::ReadWrite)); JPH_ASSERT(inAngularVelocity.Length() <= mMaxAngularVelocity); mAngularVelocity = inAngularVelocity; }
56
58 void SetAngularVelocityClamped(Vec3Arg inAngularVelocity) { mAngularVelocity = inAngularVelocity; ClampAngularVelocity(); }
59
61 inline void MoveKinematic(Vec3Arg inDeltaPosition, QuatArg inDeltaRotation, float inDeltaTime);
62
65
67 inline float GetMaxLinearVelocity() const { return mMaxLinearVelocity; }
68 inline void SetMaxLinearVelocity(float inLinearVelocity) { JPH_ASSERT(inLinearVelocity >= 0.0f); mMaxLinearVelocity = inLinearVelocity; }
69
71 inline float GetMaxAngularVelocity() const { return mMaxAngularVelocity; }
72 inline void SetMaxAngularVelocity(float inAngularVelocity) { JPH_ASSERT(inAngularVelocity >= 0.0f); mMaxAngularVelocity = inAngularVelocity; }
74
76 inline void ClampLinearVelocity();
77 inline void ClampAngularVelocity();
78
80 inline float GetLinearDamping() const { return mLinearDamping; }
81 void SetLinearDamping(float inLinearDamping) { JPH_ASSERT(inLinearDamping >= 0.0f); mLinearDamping = inLinearDamping; }
82
84 inline float GetAngularDamping() const { return mAngularDamping; }
85 void SetAngularDamping(float inAngularDamping) { JPH_ASSERT(inAngularDamping >= 0.0f); mAngularDamping = inAngularDamping; }
86
88 inline float GetGravityFactor() const { return mGravityFactor; }
89 void SetGravityFactor(float inGravityFactor) { mGravityFactor = inGravityFactor; }
90
92 void SetMassProperties(EAllowedDOFs inAllowedDOFs, const MassProperties &inMassProperties);
93
95 inline float GetInverseMass() const { JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic); return mInvMass; }
96 inline float GetInverseMassUnchecked() const { return mInvMass; }
97
102 void SetInverseMass(float inInverseMass) { mInvMass = inInverseMass; }
103
105 inline Vec3 GetInverseInertiaDiagonal() const { JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic); return mInvInertiaDiagonal; }
106
108 inline Quat GetInertiaRotation() const { return mInertiaRotation; }
109
114 void SetInverseInertia(Vec3Arg inDiagonal, QuatArg inRot) { mInvInertiaDiagonal = inDiagonal; mInertiaRotation = inRot; }
115
117 inline Mat44 GetLocalSpaceInverseInertia() const;
118
120 inline Mat44 GetLocalSpaceInverseInertiaUnchecked() const;
121
123 inline Mat44 GetInverseInertiaForRotation(Mat44Arg inRotation) const;
124
126 JPH_INLINE Vec3 MultiplyWorldSpaceInverseInertiaByVector(QuatArg inBodyRotation, Vec3Arg inV) const;
127
129 JPH_INLINE Vec3 GetPointVelocityCOM(Vec3Arg inPointRelativeToCOM) const { return mLinearVelocity + mAngularVelocity.Cross(inPointRelativeToCOM); }
130
131 // 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 PhysicsSimulation::Update.
132 JPH_INLINE Vec3 GetAccumulatedForce() const { return Vec3::sLoadFloat3Unsafe(mForce); }
133
134 // 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 PhysicsSimulation::Update.
135 JPH_INLINE Vec3 GetAccumulatedTorque() const { return Vec3::sLoadFloat3Unsafe(mTorque); }
136
137 // Reset the total accumulated force, not that this will be done automatically after every time step.
138 JPH_INLINE void ResetForce() { mForce = Float3(0, 0, 0); }
139
140 // Reset the total accumulated torque, not that this will be done automatically after every time step.
141 JPH_INLINE void ResetTorque() { mTorque = Float3(0, 0, 0); }
142
145 {
146 uint32 allowed_dofs = uint32(mAllowedDOFs);
147 UVec4 allowed_dofs_mask = UVec4(allowed_dofs << 31, allowed_dofs << 30, allowed_dofs << 29, 0).ArithmeticShiftRight<31>();
148 return Vec3::sAnd(inV, Vec3(allowed_dofs_mask.ReinterpretAsFloat()));
149 }
150
152 // FUNCTIONS BELOW THIS LINE ARE FOR INTERNAL USE ONLY
154
157 inline void AddLinearVelocityStep(Vec3Arg inLinearVelocityChange) { JPH_DET_LOG("AddLinearVelocityStep: " << inLinearVelocityChange); JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess, BodyAccess::EAccess::ReadWrite)); mLinearVelocity = LockTranslation(mLinearVelocity + inLinearVelocityChange); JPH_ASSERT(!mLinearVelocity.IsNaN()); }
158 inline void SubLinearVelocityStep(Vec3Arg inLinearVelocityChange) { JPH_DET_LOG("SubLinearVelocityStep: " << inLinearVelocityChange); JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess, BodyAccess::EAccess::ReadWrite)); mLinearVelocity = LockTranslation(mLinearVelocity - inLinearVelocityChange); JPH_ASSERT(!mLinearVelocity.IsNaN()); }
159 inline void AddAngularVelocityStep(Vec3Arg inAngularVelocityChange) { JPH_DET_LOG("AddAngularVelocityStep: " << inAngularVelocityChange); JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess, BodyAccess::EAccess::ReadWrite)); mAngularVelocity += inAngularVelocityChange; JPH_ASSERT(!mAngularVelocity.IsNaN()); }
160 inline void SubAngularVelocityStep(Vec3Arg inAngularVelocityChange) { JPH_DET_LOG("SubAngularVelocityStep: " << inAngularVelocityChange); JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess, BodyAccess::EAccess::ReadWrite)); mAngularVelocity -= inAngularVelocityChange; JPH_ASSERT(!mAngularVelocity.IsNaN()); }
162
164 inline void ApplyForceTorqueAndDragInternal(QuatArg inBodyRotation, Vec3Arg inGravity, float inDeltaTime);
165
167 uint32 GetIslandIndexInternal() const { return mIslandIndex; }
168 void SetIslandIndexInternal(uint32 inIndex) { mIslandIndex = inIndex; }
169
171 uint32 GetIndexInActiveBodiesInternal() const { return mIndexInActiveBodies; }
172
173#ifdef JPH_DOUBLE_PRECISION
174 inline DVec3 GetSleepTestOffset() const { return DVec3::sLoadDouble3Unsafe(mSleepTestOffset); }
175#endif // JPH_DOUBLE_PRECISION
176
178 inline void ResetSleepTestSpheres(const RVec3 *inPoints);
179
181 inline void ResetSleepTestTimer() { mSleepTestTimer = 0.0f; }
182
184 inline ECanSleep AccumulateSleepTime(float inDeltaTime, float inTimeBeforeSleep);
185
187 void SaveState(StateRecorder &inStream) const;
188
190 void RestoreState(StateRecorder &inStream);
191
192 static constexpr uint32 cInactiveIndex = uint32(-1);
193
194private:
195 friend class BodyManager;
196 friend class Body;
197
198 // 1st cache line
199 // 16 byte aligned
200 Vec3 mLinearVelocity { Vec3::sZero() };
201 Vec3 mAngularVelocity { Vec3::sZero() };
202 Vec3 mInvInertiaDiagonal;
203 Quat mInertiaRotation;
204
205 // 2nd cache line
206 // 4 byte aligned
207 Float3 mForce { 0, 0, 0 };
208 Float3 mTorque { 0, 0, 0 };
209 float mInvMass;
210 float mLinearDamping;
211 float mAngularDamping;
212 float mMaxLinearVelocity;
213 float mMaxAngularVelocity;
214 float mGravityFactor;
215 uint32 mIndexInActiveBodies = cInactiveIndex;
216 uint32 mIslandIndex = cInactiveIndex;
217
218 // 1 byte aligned
219 EMotionQuality mMotionQuality;
220 bool mAllowSleeping;
221 EAllowedDOFs mAllowedDOFs = EAllowedDOFs::All;
222
223 // 3rd cache line (least frequently used)
224 // 4 byte aligned (or 8 byte if running in double precision)
225#ifdef JPH_DOUBLE_PRECISION
226 Double3 mSleepTestOffset;
227#endif // JPH_DOUBLE_PRECISION
228 Sphere mSleepTestSpheres[3];
229 float mSleepTestTimer;
230
231#ifdef JPH_ENABLE_ASSERTS
232 EBodyType mCachedBodyType;
233 EMotionType mCachedMotionType;
234#endif
235};
236
238
239#include "MotionProperties.inl"
EAllowedDOFs
Enum used in BodyCreationSettings and MotionProperties to indicate which degrees of freedom a body ha...
Definition: AllowedDOFs.h:11
EBodyType
Type of body.
Definition: BodyType.h:11
#define JPH_EXPORT
Definition: Core.h:214
#define JPH_NAMESPACE_END
Definition: Core.h:354
std::uint32_t uint32
Definition: Core.h:429
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:348
#define JPH_DET_LOG(...)
By default we log nothing.
Definition: DeterminismLog.h:155
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
ECanSleep
Enum that determines if an object can go to sleep.
Definition: MotionProperties.h:22
@ 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
Definition: Body.h:35
Class that contains all bodies.
Definition: BodyManager.h:32
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:155
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:29
void SetLinearVelocity(Vec3Arg inLinearVelocity)
Set world space linear velocity of the center of mass.
Definition: MotionProperties.h:46
uint32 GetIslandIndexInternal() const
Access to the island index.
Definition: MotionProperties.h:167
JPH_INLINE Vec3 LockTranslation(Vec3Arg inV)
Takes a translation vector inV and returns a vector where the components that are not allowed by mAll...
Definition: MotionProperties.h:144
void SetAngularVelocityClamped(Vec3Arg inAngularVelocity)
Set world space angular velocity of the center of mass, will make sure the value is clamped against t...
Definition: MotionProperties.h:58
Quat GetInertiaRotation() const
Rotation (R) that takes inverse inertia diagonal to local space: .
Definition: MotionProperties.h:108
float GetMaxLinearVelocity() const
Maximum linear velocity that a body can achieve. Used to prevent the system from exploding.
Definition: MotionProperties.h:67
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:129
Vec3 GetInverseInertiaDiagonal() const
Diagonal of inverse inertia matrix: D. Should only be called on a dynamic object (static or kinematic...
Definition: MotionProperties.h:105
void SetInverseInertia(Vec3Arg inDiagonal, QuatArg inRot)
Definition: MotionProperties.h:114
float GetGravityFactor() const
Get gravity factor (1 = normal gravity, 0 = no gravity)
Definition: MotionProperties.h:88
void AddLinearVelocityStep(Vec3Arg inLinearVelocityChange)
Definition: MotionProperties.h:157
float GetMaxAngularVelocity() const
Maximum angular velocity that a body can achieve. Used to prevent the system from exploding.
Definition: MotionProperties.h:71
void SetLinearVelocityClamped(Vec3Arg inLinearVelocity)
Set world space linear velocity of the center of mass, will make sure the value is clamped against th...
Definition: MotionProperties.h:49
float GetInverseMassUnchecked() const
Definition: MotionProperties.h:96
Vec3 GetLinearVelocity() const
Get world space linear velocity of the center of mass.
Definition: MotionProperties.h:43
Vec3 GetAngularVelocity() const
Get world space angular velocity of the center of mass.
Definition: MotionProperties.h:52
void SetMaxAngularVelocity(float inAngularVelocity)
Definition: MotionProperties.h:72
JPH_INLINE Vec3 GetAccumulatedTorque() const
Definition: MotionProperties.h:135
void SetIslandIndexInternal(uint32 inIndex)
Definition: MotionProperties.h:168
float GetAngularDamping() const
Get angular damping: dw/dt = -c * w. c must be between 0 and 1 but is usually close to 0.
Definition: MotionProperties.h:84
void SubLinearVelocityStep(Vec3Arg inLinearVelocityChange)
Definition: MotionProperties.h:158
float GetInverseMass() const
Get inverse mass (1 / mass). Should only be called on a dynamic object (static or kinematic bodies ha...
Definition: MotionProperties.h:95
void SetGravityFactor(float inGravityFactor)
Definition: MotionProperties.h:89
uint32 GetIndexInActiveBodiesInternal() const
Access to the index in the active bodies array.
Definition: MotionProperties.h:171
float GetLinearDamping() const
Get linear damping: dv/dt = -c * v. c must be between 0 and 1 but is usually close to 0.
Definition: MotionProperties.h:80
void SubAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition: MotionProperties.h:160
JPH_INLINE void ResetForce()
Definition: MotionProperties.h:138
void SetInverseMass(float inInverseMass)
Definition: MotionProperties.h:102
void ResetSleepTestTimer()
Reset the sleep test timer without resetting the sleep test spheres.
Definition: MotionProperties.h:181
void SetAngularVelocity(Vec3Arg inAngularVelocity)
Set world space angular velocity of the center of mass.
Definition: MotionProperties.h:55
JPH_INLINE Vec3 GetAccumulatedForce() const
Definition: MotionProperties.h:132
void SetMaxLinearVelocity(float inLinearVelocity)
Definition: MotionProperties.h:68
JPH_OVERRIDE_NEW_DELETE EMotionQuality GetMotionQuality() const
Motion quality, or how well it detects collisions when it has a high velocity.
Definition: MotionProperties.h:34
void SetLinearDamping(float inLinearDamping)
Definition: MotionProperties.h:81
void SetAngularDamping(float inAngularDamping)
Definition: MotionProperties.h:85
EAllowedDOFs GetAllowedDOFs() const
Get the allowed degrees of freedom that this body has (this can be changed by calling SetMassProperti...
Definition: MotionProperties.h:37
bool GetAllowSleeping() const
If this body can go to sleep.
Definition: MotionProperties.h:40
void AddAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition: MotionProperties.h:159
JPH_INLINE void ResetTorque()
Definition: MotionProperties.h:141
Definition: Quat.h:33
Definition: Sphere.h:12
Definition: StateRecorder.h:48
Definition: UVec4.h:12
JPH_INLINE UVec4 ArithmeticShiftRight() const
Shift all components by Count bits to the right (shifting in the value of the highest bit)
JPH_INLINE Vec4 ReinterpretAsFloat() const
Reinterpret UVec4 as a Vec4 (doesn't change the bits)
Definition: UVec4.inl:332
Definition: Vec3.h:16
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition: Vec3.inl:582
JPH_INLINE float Length() const
Length of vector.
Definition: Vec3.inl:669
static JPH_INLINE Vec3 sAnd(Vec3Arg inV1, Vec3Arg inV2)
Logical and (component wise)
Definition: Vec3.inl:310
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition: Vec3.inl:107
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:134