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
13
15
16class StateRecorder;
17
20{
21public:
23
25 EMotionQuality GetMotionQuality() const { return mMotionQuality; }
26
28 inline Vec3 GetLinearVelocity() const { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess, BodyAccess::EAccess::Read)); return mLinearVelocity; }
29
31 void SetLinearVelocity(Vec3Arg inLinearVelocity) { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess, BodyAccess::EAccess::ReadWrite)); JPH_ASSERT(inLinearVelocity.Length() <= mMaxLinearVelocity); mLinearVelocity = inLinearVelocity; }
32
34 void SetLinearVelocityClamped(Vec3Arg inLinearVelocity) { mLinearVelocity = inLinearVelocity; ClampLinearVelocity(); }
35
37 inline Vec3 GetAngularVelocity() const { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess, BodyAccess::EAccess::Read)); return mAngularVelocity; }
38
40 void SetAngularVelocity(Vec3Arg inAngularVelocity) { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess, BodyAccess::EAccess::ReadWrite)); JPH_ASSERT(inAngularVelocity.Length() <= mMaxAngularVelocity); mAngularVelocity = inAngularVelocity; }
41
43 void SetAngularVelocityClamped(Vec3Arg inAngularVelocity) { mAngularVelocity = inAngularVelocity; ClampAngularVelocity(); }
44
46 inline void MoveKinematic(Vec3Arg inDeltaPosition, QuatArg inDeltaRotation, float inDeltaTime);
47
50
52 inline float GetMaxLinearVelocity() const { return mMaxLinearVelocity; }
53 inline void SetMaxLinearVelocity(float inLinearVelocity) { JPH_ASSERT(inLinearVelocity >= 0.0f); mMaxLinearVelocity = inLinearVelocity; }
54
56 inline float GetMaxAngularVelocity() const { return mMaxAngularVelocity; }
57 inline void SetMaxAngularVelocity(float inAngularVelocity) { JPH_ASSERT(inAngularVelocity >= 0.0f); mMaxAngularVelocity = inAngularVelocity; }
59
61 inline void ClampLinearVelocity();
62 inline void ClampAngularVelocity();
63
65 inline float GetLinearDamping() const { return mLinearDamping; }
66 void SetLinearDamping(float inLinearDamping) { JPH_ASSERT(inLinearDamping >= 0.0f); mLinearDamping = inLinearDamping; }
67
69 inline float GetAngularDamping() const { return mAngularDamping; }
70 void SetAngularDamping(float inAngularDamping) { JPH_ASSERT(inAngularDamping >= 0.0f); mAngularDamping = inAngularDamping; }
71
73 inline float GetGravityFactor() const { return mGravityFactor; }
74 void SetGravityFactor(float inGravityFactor) { mGravityFactor = inGravityFactor; }
75
77 inline void SetMassProperties(const MassProperties &inMassProperties);
78
80 inline float GetInverseMass() const { JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic); return mInvMass; }
81 inline float GetInverseMassUnchecked() const { return mInvMass; }
82
86 void SetInverseMass(float inInverseMass) { mInvMass = inInverseMass; }
87
89 inline Vec3 GetInverseInertiaDiagonal() const { JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic); return mInvInertiaDiagonal; }
90
92 inline Quat GetInertiaRotation() const { return mInertiaRotation; }
93
97 void SetInverseInertia(Vec3Arg inDiagonal, QuatArg inRot) { mInvInertiaDiagonal = inDiagonal; mInertiaRotation = inRot; }
98
100 inline Mat44 GetLocalSpaceInverseInertia() const;
101
104
106 inline Mat44 GetInverseInertiaForRotation(Mat44Arg inRotation) const;
107
109 JPH_INLINE Vec3 MultiplyWorldSpaceInverseInertiaByVector(QuatArg inBodyRotation, Vec3Arg inV) const;
110
112 JPH_INLINE Vec3 GetPointVelocityCOM(Vec3Arg inPointRelativeToCOM) const { return mLinearVelocity + mAngularVelocity.Cross(inPointRelativeToCOM); }
113
114 // 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.
115 JPH_INLINE Vec3 GetAccumulatedForce() const { return Vec3::sLoadFloat3Unsafe(mForce); }
116
117 // 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.
118 JPH_INLINE Vec3 GetAccumulatedTorque() const { return Vec3::sLoadFloat3Unsafe(mTorque); }
119
121 // FUNCTIONS BELOW THIS LINE ARE FOR INTERNAL USE ONLY
123
126 inline void AddLinearVelocityStep(Vec3Arg inLinearVelocityChange) { JPH_DET_LOG("AddLinearVelocityStep: " << inLinearVelocityChange); JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess, BodyAccess::EAccess::ReadWrite)); mLinearVelocity += inLinearVelocityChange; JPH_ASSERT(!mLinearVelocity.IsNaN()); }
127 inline void SubLinearVelocityStep(Vec3Arg inLinearVelocityChange) { JPH_DET_LOG("SubLinearVelocityStep: " << inLinearVelocityChange); JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess, BodyAccess::EAccess::ReadWrite)); mLinearVelocity -= inLinearVelocityChange; JPH_ASSERT(!mLinearVelocity.IsNaN()); }
128 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()); }
129 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()); }
131
133 inline void ApplyForceTorqueAndDragInternal(QuatArg inBodyRotation, Vec3Arg inGravity, float inDeltaTime);
134
136 inline void ResetForceAndTorqueInternal() { mForce = Float3(0, 0, 0); mTorque = Float3(0, 0, 0); }
137
139 uint32 GetIslandIndexInternal() const { return mIslandIndex; }
140 void SetIslandIndexInternal(uint32 inIndex) { mIslandIndex = inIndex; }
141
143 uint32 GetIndexInActiveBodiesInternal() const { return mIndexInActiveBodies; }
144
145#ifdef JPH_DOUBLE_PRECISION
146 inline DVec3 GetSleepTestOffset() const { return DVec3::sLoadDouble3Unsafe(mSleepTestOffset); }
147#endif // JPH_DOUBLE_PRECISION
148
150 inline void ResetSleepTestSpheres(const RVec3 *inPoints);
151
153 void SaveState(StateRecorder &inStream) const;
154
156 void RestoreState(StateRecorder &inStream);
157
158private:
159 friend class BodyManager;
160 friend class Body;
161
162 // 1st cache line
163 // 16 byte aligned
164 Vec3 mLinearVelocity { Vec3::sZero() };
165 Vec3 mAngularVelocity { Vec3::sZero() };
166 Vec3 mInvInertiaDiagonal;
167 Quat mInertiaRotation;
168
169 // 2nd cache line
170 // 4 byte aligned
171 Float3 mForce { 0, 0, 0 };
172 Float3 mTorque { 0, 0, 0 };
173 float mInvMass;
174 float mLinearDamping;
175 float mAngularDamping;
176 float mMaxLinearVelocity;
177 float mMaxAngularVelocity;
178 float mGravityFactor;
179 uint32 mIndexInActiveBodies;
180 uint32 mIslandIndex;
181
182 // 1 byte aligned
183 EMotionQuality mMotionQuality;
184 bool mAllowSleeping;
185
186 // 3rd cache line (least frequently used)
187 // 4 byte aligned (or 8 byte if running in double precision)
188#ifdef JPH_DOUBLE_PRECISION
189 Double3 mSleepTestOffset;
190#endif // JPH_DOUBLE_PRECISION
191 Sphere mSleepTestSpheres[3];
192 float mSleepTestTimer;
193
194#ifdef JPH_ENABLE_ASSERTS
195 EMotionType mCachedMotionType;
196#endif
197};
198
200
201#include "MotionProperties.inl"
uint32_t uint32
Definition: Core.h:312
#define JPH_NAMESPACE_END
Definition: Core.h:240
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
#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
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:33
Class that contains all bodies.
Definition: BodyManager.h:30
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:20
void SetLinearVelocity(Vec3Arg inLinearVelocity)
Set world space linear velocity of the center of mass.
Definition: MotionProperties.h:31
uint32 GetIslandIndexInternal() const
Access to the island index.
Definition: MotionProperties.h:139
void ClampAngularVelocity()
Definition: MotionProperties.inl:58
void RestoreState(StateRecorder &inStream)
Restoring state for replay.
Definition: MotionProperties.cpp:33
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:43
void ClampLinearVelocity()
Clamp velocity according to limit.
Definition: MotionProperties.inl:48
Quat GetInertiaRotation() const
Rotation (R) that takes inverse inertia diagonal to local space: .
Definition: MotionProperties.h:92
float GetMaxLinearVelocity() const
Maximum linear velocity that a body can achieve. Used to prevent the system from exploding.
Definition: MotionProperties.h:52
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:112
void SaveState(StateRecorder &inStream) const
Saving state for replay.
Definition: MotionProperties.cpp:12
Vec3 GetInverseInertiaDiagonal() const
Diagonal of inverse inertia matrix: D. Should only be called on a dynamic object (static or kinematic...
Definition: MotionProperties.h:89
void SetInverseInertia(Vec3Arg inDiagonal, QuatArg inRot)
Definition: MotionProperties.h:97
float GetGravityFactor() const
Get gravity factor (1 = normal gravity, 0 = no gravity)
Definition: MotionProperties.h:73
void AddLinearVelocityStep(Vec3Arg inLinearVelocityChange)
Definition: MotionProperties.h:126
float GetMaxAngularVelocity() const
Maximum angular velocity that a body can achieve. Used to prevent the system from exploding.
Definition: MotionProperties.h:56
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:34
float GetInverseMassUnchecked() const
Definition: MotionProperties.h:81
Vec3 GetLinearVelocity() const
Get world space linear velocity of the center of mass.
Definition: MotionProperties.h:28
Vec3 GetAngularVelocity() const
Get world space angular velocity of the center of mass.
Definition: MotionProperties.h:37
void SetMaxAngularVelocity(float inAngularVelocity)
Definition: MotionProperties.h:57
JPH_INLINE Vec3 GetAccumulatedTorque() const
Definition: MotionProperties.h:118
void SetIslandIndexInternal(uint32 inIndex)
Definition: MotionProperties.h:140
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:69
void SubLinearVelocityStep(Vec3Arg inLinearVelocityChange)
Definition: MotionProperties.h:127
float GetInverseMass() const
Get inverse mass (1 / mass). Should only be called on a dynamic object (static or kinematic bodies ha...
Definition: MotionProperties.h:80
void SetGravityFactor(float inGravityFactor)
Definition: MotionProperties.h:74
uint32 GetIndexInActiveBodiesInternal() const
Access to the index in the active bodies array.
Definition: MotionProperties.h:143
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:65
void SubAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition: MotionProperties.h:129
Mat44 GetLocalSpaceInverseInertiaUnchecked() const
Same as GetLocalSpaceInverseInertia() but doesn't check if the body is dynamic.
Definition: MotionProperties.inl:68
void SetInverseMass(float inInverseMass)
Definition: MotionProperties.h:86
void SetAngularVelocity(Vec3Arg inAngularVelocity)
Set world space angular velocity of the center of mass.
Definition: MotionProperties.h:40
JPH_INLINE Vec3 MultiplyWorldSpaceInverseInertiaByVector(QuatArg inBodyRotation, Vec3Arg inV) const
Multiply a vector with the inverse world space inertia tensor ( ). Zero if object is static or kinema...
Definition: MotionProperties.inl:90
void ResetSleepTestSpheres(const RVec3 *inPoints)
Reset spheres to center around inPoints with radius 0.
Definition: MotionProperties.inl:121
JPH_INLINE Vec3 GetAccumulatedForce() const
Definition: MotionProperties.h:115
void MoveKinematic(Vec3Arg inDeltaPosition, QuatArg inDeltaRotation, float inDeltaTime)
Set velocity of body such that it will be rotate/translate by inDeltaPosition/Rotation in inDeltaTime...
Definition: MotionProperties.inl:32
Mat44 GetInverseInertiaForRotation(Mat44Arg inRotation) const
Get inverse inertia matrix ( ) for a given object rotation (translation will be ignored)....
Definition: MotionProperties.inl:81
void SetMaxLinearVelocity(float inLinearVelocity)
Definition: MotionProperties.h:53
JPH_OVERRIDE_NEW_DELETE EMotionQuality GetMotionQuality() const
Motion quality, or how well it detects collisions when it has a high velocity.
Definition: MotionProperties.h:25
void SetLinearDamping(float inLinearDamping)
Definition: MotionProperties.h:66
Mat44 GetLocalSpaceInverseInertia() const
Get inverse inertia matrix ( ). Will be a matrix of zeros for a static or kinematic object.
Definition: MotionProperties.inl:75
void ResetForceAndTorqueInternal()
At the end of a simulation update the forces and torques need to be reset for the next frame.
Definition: MotionProperties.h:136
void SetAngularDamping(float inAngularDamping)
Definition: MotionProperties.h:70
void ApplyForceTorqueAndDragInternal(QuatArg inBodyRotation, Vec3Arg inGravity, float inDeltaTime)
Apply all accumulated forces, torques and drag (should only be called by the PhysicsSystem)
Definition: MotionProperties.inl:98
void SetMassProperties(const MassProperties &inMassProperties)
Set the mass and inertia tensor.
Definition: MotionProperties.inl:9
void AddAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition: MotionProperties.h:128
Definition: Quat.h:33
Definition: Sphere.h:12
Definition: StateRecorder.h:15
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 sZero()
Vector with all zeros.
Definition: Vec3.inl:107
JPH_INLINE bool IsNaN() const
Test if vector contains NaN elements.
Definition: Vec3.inl:742
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