Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
MotionProperties.inl
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
8
9void MotionProperties::MoveKinematic(Vec3Arg inDeltaPosition, QuatArg inDeltaRotation, float inDeltaTime)
10{
11 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::ReadWrite));
12 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess(), BodyAccess::EAccess::Read));
13 JPH_ASSERT(mCachedBodyType == EBodyType::RigidBody);
14 JPH_ASSERT(mCachedMotionType != EMotionType::Static);
15
16 // Calculate required linear velocity
17 mLinearVelocity = LockTranslation(inDeltaPosition / inDeltaTime);
18
19 // Calculate required angular velocity
20 Vec3 axis;
21 float angle;
22 inDeltaRotation.GetAxisAngle(axis, angle);
23 mAngularVelocity = LockAngular(axis * (angle / inDeltaTime));
24}
25
27{
28 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::ReadWrite));
29
30 float len_sq = mLinearVelocity.LengthSq();
31 JPH_ASSERT(isfinite(len_sq));
32 if (len_sq > Square(mMaxLinearVelocity))
33 mLinearVelocity *= mMaxLinearVelocity / sqrt(len_sq);
34}
35
37{
38 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::ReadWrite));
39
40 float len_sq = mAngularVelocity.LengthSq();
41 JPH_ASSERT(isfinite(len_sq));
42 if (len_sq > Square(mMaxAngularVelocity))
43 mAngularVelocity *= mMaxAngularVelocity / sqrt(len_sq);
44}
45
47{
48 Mat44 rotation = Mat44::sRotation(mInertiaRotation);
49 Mat44 rotation_mul_scale_transposed(mInvInertiaDiagonal.SplatX() * rotation.GetColumn4(0), mInvInertiaDiagonal.SplatY() * rotation.GetColumn4(1), mInvInertiaDiagonal.SplatZ() * rotation.GetColumn4(2), Vec4(0, 0, 0, 1));
50 return rotation.Multiply3x3RightTransposed(rotation_mul_scale_transposed);
51}
52
53inline void MotionProperties::ScaleToMass(float inMass)
54{
55 JPH_ASSERT(mInvMass > 0.0f, "Body must have finite mass");
56 JPH_ASSERT(inMass > 0.0f, "New mass cannot be zero");
57
58 float new_inv_mass = 1.0f / inMass;
59 mInvInertiaDiagonal *= new_inv_mass / mInvMass;
60 mInvMass = new_inv_mass;
61}
62
68
70{
71 JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic);
72
73 Mat44 rotation = inRotation.Multiply3x3(Mat44::sRotation(mInertiaRotation));
74 Mat44 rotation_mul_scale_transposed(mInvInertiaDiagonal.SplatX() * rotation.GetColumn4(0), mInvInertiaDiagonal.SplatY() * rotation.GetColumn4(1), mInvInertiaDiagonal.SplatZ() * rotation.GetColumn4(2), Vec4(0, 0, 0, 1));
75 Mat44 inverse_inertia = rotation.Multiply3x3RightTransposed(rotation_mul_scale_transposed);
76
77 // We need to mask out both the rows and columns of DOFs that are not allowed
78 Vec4 angular_dofs_mask = GetAngularDOFsMask().ReinterpretAsFloat();
79 inverse_inertia.SetColumn4(0, Vec4::sAnd(inverse_inertia.GetColumn4(0), Vec4::sAnd(angular_dofs_mask, angular_dofs_mask.SplatX())));
80 inverse_inertia.SetColumn4(1, Vec4::sAnd(inverse_inertia.GetColumn4(1), Vec4::sAnd(angular_dofs_mask, angular_dofs_mask.SplatY())));
81 inverse_inertia.SetColumn4(2, Vec4::sAnd(inverse_inertia.GetColumn4(2), Vec4::sAnd(angular_dofs_mask, angular_dofs_mask.SplatZ())));
82
83 return inverse_inertia;
84}
85
87{
88 JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic);
89
90 // Mask out columns of DOFs that are not allowed
91 Vec3 angular_dofs_mask = Vec3(GetAngularDOFsMask().ReinterpretAsFloat());
92 Vec3 v = Vec3::sAnd(inV, angular_dofs_mask);
93
94 // Multiply vector by inverse inertia
95 Mat44 rotation = Mat44::sRotation(inBodyRotation * mInertiaRotation);
96 Vec3 result = rotation.Multiply3x3(mInvInertiaDiagonal * rotation.Multiply3x3Transposed(v));
97
98 // Mask out rows of DOFs that are not allowed
99 return Vec3::sAnd(result, angular_dofs_mask);
100}
101
102void MotionProperties::ApplyGyroscopicForceInternal(QuatArg inBodyRotation, float inDeltaTime)
103{
104 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::ReadWrite));
105 JPH_ASSERT(mCachedBodyType == EBodyType::RigidBody);
106 JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic);
107
108 // Calculate local space inertia tensor (a diagonal in local space)
109 UVec4 is_zero = Vec3::sEquals(mInvInertiaDiagonal, Vec3::sZero());
110 Vec3 denominator = Vec3::sSelect(mInvInertiaDiagonal, Vec3::sReplicate(1.0f), is_zero);
111 Vec3 nominator = Vec3::sSelect(Vec3::sReplicate(1.0f), Vec3::sZero(), is_zero);
112 Vec3 local_inertia = nominator / denominator; // Avoid dividing by zero, inertia in this axis will be zero
113
114 // Calculate local space angular momentum
115 Quat inertia_space_to_world_space = inBodyRotation * mInertiaRotation;
116 Vec3 local_angular_velocity = inertia_space_to_world_space.Conjugated() * mAngularVelocity;
117 Vec3 local_momentum = local_inertia * local_angular_velocity;
118
119 // The gyroscopic force applies a torque: T = -w x I w where w is angular velocity and I the inertia tensor
120 // Calculate the new angular momentum by applying the gyroscopic force and make sure the new magnitude is the same as the old one
121 // to avoid introducing energy into the system due to the Euler step
122 Vec3 new_local_momentum = local_momentum - inDeltaTime * local_angular_velocity.Cross(local_momentum);
123 float new_local_momentum_len_sq = new_local_momentum.LengthSq();
124 new_local_momentum = new_local_momentum_len_sq > 0.0f? new_local_momentum * sqrt(local_momentum.LengthSq() / new_local_momentum_len_sq) : Vec3::sZero();
125
126 // Convert back to world space angular velocity
127 mAngularVelocity = inertia_space_to_world_space * (mInvInertiaDiagonal * new_local_momentum);
128}
129
130void MotionProperties::ApplyForceTorqueAndDragInternal(QuatArg inBodyRotation, Vec3Arg inGravity, float inDeltaTime)
131{
132 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::ReadWrite));
133 JPH_ASSERT(mCachedBodyType == EBodyType::RigidBody);
134 JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic);
135
136 // Update linear velocity
137 mLinearVelocity = LockTranslation(mLinearVelocity + inDeltaTime * (mGravityFactor * inGravity + mInvMass * GetAccumulatedForce()));
138
139 // Update angular velocity
140 mAngularVelocity += inDeltaTime * MultiplyWorldSpaceInverseInertiaByVector(inBodyRotation, GetAccumulatedTorque());
141
142 // Linear damping: dv/dt = -c * v
143 // Solution: v(t) = v(0) * e^(-c * t) or v2 = v1 * e^(-c * dt)
144 // Taylor expansion of e^(-c * dt) = 1 - c * dt + ...
145 // Since dt is usually in the order of 1/60 and c is a low number too this approximation is good enough
146 mLinearVelocity *= max(0.0f, 1.0f - mLinearDamping * inDeltaTime);
147 mAngularVelocity *= max(0.0f, 1.0f - mAngularDamping * inDeltaTime);
148
149 // Clamp velocities
152}
153
155{
156#ifdef JPH_DOUBLE_PRECISION
157 // Make spheres relative to the first point and initialize them to zero radius
158 DVec3 offset = inPoints[0];
159 offset.StoreDouble3(&mSleepTestOffset);
160 mSleepTestSpheres[0] = Sphere(Vec3::sZero(), 0.0f);
161 for (int i = 1; i < 3; ++i)
162 mSleepTestSpheres[i] = Sphere(Vec3(inPoints[i] - offset), 0.0f);
163#else
164 // Initialize the spheres to zero radius around the supplied points
165 for (int i = 0; i < 3; ++i)
166 mSleepTestSpheres[i] = Sphere(inPoints[i], 0.0f);
167#endif
168
169 mSleepTestTimer = 0.0f;
170}
171
172ECanSleep MotionProperties::AccumulateSleepTime(float inDeltaTime, float inTimeBeforeSleep)
173{
174 mSleepTestTimer += inDeltaTime;
175 return mSleepTestTimer >= inTimeBeforeSleep? ECanSleep::CanSleep : ECanSleep::CannotSleep;
176}
177
@ RigidBody
Rigid body consisting of a rigid shape.
#define JPH_NAMESPACE_END
Definition Core.h:379
#define JPH_NAMESPACE_BEGIN
Definition Core.h:373
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
JPH_INLINE constexpr T Square(T inV)
Square a value.
Definition Math.h:52
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.
@ Static
Non movable.
@ Dynamic
Responds to forces as a normal physics object.
Definition DVec3.h:14
JPH_INLINE void StoreDouble3(Double3 *outV) const
Store 3 doubles to memory.
Definition DVec3.inl:171
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition Mat44.h:13
JPH_INLINE Vec3 Multiply3x3Transposed(Vec3Arg inV) const
Multiply vector by only 3x3 part of the transpose of the matrix ( )
Definition Mat44.inl:336
JPH_INLINE Mat44 Multiply3x3RightTransposed(Mat44Arg inM) const
Multiply 3x3 matrix by the transpose of a 3x3 matrix ( )
Definition Mat44.inl:397
JPH_INLINE Vec4 GetColumn4(uint inCol) const
Definition Mat44.h:160
JPH_INLINE Vec3 Multiply3x3(Vec3Arg inV) const
Multiply vector by only 3x3 part of the matrix.
Definition Mat44.inl:316
static JPH_INLINE Mat44 sRotation(Vec3Arg inAxis, float inAngle)
Rotate around arbitrary axis.
Definition Mat44.inl:139
JPH_INLINE void SetColumn4(uint inCol, Vec4Arg inV)
Definition Mat44.h:161
void ClampAngularVelocity()
Definition MotionProperties.inl:36
void ScaleToMass(float inMass)
Definition MotionProperties.inl:53
ECanSleep AccumulateSleepTime(float inDeltaTime, float inTimeBeforeSleep)
Accumulate sleep time and return if a body can go to sleep.
Definition MotionProperties.inl:172
void ClampLinearVelocity()
Clamp velocity according to limit.
Definition MotionProperties.inl:26
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:176
JPH_INLINE Vec3 GetAccumulatedTorque() const
Definition MotionProperties.h:139
void ApplyGyroscopicForceInternal(QuatArg inBodyRotation, float inDeltaTime)
Apply the gyroscopic force (aka Dzhanibekov effect, see https://en.wikipedia.org/wiki/Tennis_racket_t...
Definition MotionProperties.inl:102
Mat44 GetLocalSpaceInverseInertiaUnchecked() const
Same as GetLocalSpaceInverseInertia() but doesn't check if the body is dynamic.
Definition MotionProperties.inl:46
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:86
void ResetSleepTestSpheres(const RVec3 *inPoints)
Reset spheres to center around inPoints with radius 0.
Definition MotionProperties.inl:154
JPH_INLINE Vec3 GetAccumulatedForce() const
Definition MotionProperties.h:136
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:163
Mat44 GetInverseInertiaForRotation(Mat44Arg inRotation) const
Get inverse inertia matrix ( ) for a given object rotation (translation will be ignored)....
Definition MotionProperties.inl:69
Mat44 GetLocalSpaceInverseInertia() const
Get inverse inertia matrix ( ). Will be a matrix of zeros for a static or kinematic object.
Definition MotionProperties.inl:63
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:169
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:130
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:9
Definition Quat.h:33
JPH_INLINE void GetAxisAngle(Vec3 &outAxis, float &outAngle) const
Get axis and angle that represents this quaternion, outAngle will always be in the range .
Definition Quat.inl:83
JPH_INLINE Quat Conjugated() const
The conjugate [w, -x, -y, -z] is the same as the inverse for unit quaternions.
Definition Quat.h:178
Definition Sphere.h:12
Definition UVec4.h:12
JPH_INLINE Vec4 ReinterpretAsFloat() const
Reinterpret UVec4 as a Vec4 (doesn't change the bits)
Definition UVec4.inl:340
Definition Vec3.h:17
JPH_INLINE Vec4 SplatX() const
Replicate the X component to all components.
Definition Vec3.inl:533
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition Vec3.inl:594
JPH_INLINE Vec4 SplatZ() const
Replicate the Z component to all components.
Definition Vec3.inl:555
static JPH_INLINE Vec3 sAnd(Vec3Arg inV1, Vec3Arg inV2)
Logical and (component wise)
Definition Vec3.inl:314
JPH_INLINE Vec4 SplatY() const
Replicate the Y component to all components.
Definition Vec3.inl:544
JPH_INLINE float LengthSq() const
Squared length of vector.
Definition Vec3.inl:665
static JPH_INLINE UVec4 sEquals(Vec3Arg inV1, Vec3Arg inV2)
Equals (component wise)
Definition Vec3.inl:177
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition Vec3.inl:107
static JPH_INLINE Vec3 sReplicate(float inV)
Replicate inV across all components.
Definition Vec3.inl:118
static JPH_INLINE Vec3 sSelect(Vec3Arg inNotSet, Vec3Arg inSet, UVec4Arg inControl)
Component wise select, returns inNotSet when highest bit of inControl = 0 and inSet when highest bit ...
Definition Vec3.inl:269
Definition Vec4.h:14
JPH_INLINE Vec4 SplatX() const
Replicate the X component to all components.
Definition Vec4.inl:558
static JPH_INLINE Vec4 sAnd(Vec4Arg inV1, Vec4Arg inV2)
Logical and (component wise)
Definition Vec4.inl:293
JPH_INLINE Vec4 SplatY() const
Replicate the Y component to all components.
Definition Vec4.inl:569
JPH_INLINE Vec4 SplatZ() const
Replicate the Z component to all components.
Definition Vec4.inl:580