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
10{
11 // Set inverse mass
12 JPH_ASSERT(inMassProperties.mMass > 0.0f);
13 mInvMass = 1.0f / inMassProperties.mMass;
14
15 // Set inverse inertia
16 Mat44 rotation;
17 Vec3 diagonal;
18 if (inMassProperties.DecomposePrincipalMomentsOfInertia(rotation, diagonal)
19 && !diagonal.IsNearZero())
20 {
21 mInvInertiaDiagonal = diagonal.Reciprocal();
22 mInertiaRotation = rotation.GetQuaternion();
23 }
24 else
25 {
26 // Failed! Fall back to inertia tensor of sphere with radius 1.
27 mInvInertiaDiagonal = Vec3::sReplicate(2.5f * mInvMass);
28 mInertiaRotation = Quat::sIdentity();
29 }
30}
31
32void MotionProperties::MoveKinematic(Vec3Arg inDeltaPosition, QuatArg inDeltaRotation, float inDeltaTime)
33{
34 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess, BodyAccess::EAccess::ReadWrite));
35 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess, BodyAccess::EAccess::Read));
36 JPH_ASSERT(mCachedMotionType != EMotionType::Static);
37
38 // Calculate required linear velocity
39 mLinearVelocity = inDeltaPosition / inDeltaTime;
40
41 // Calculate required angular velocity
42 Vec3 axis;
43 float angle;
44 inDeltaRotation.GetAxisAngle(axis, angle);
45 mAngularVelocity = axis * (angle / inDeltaTime);
46}
47
49{
50 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess, BodyAccess::EAccess::ReadWrite));
51
52 float len_sq = mLinearVelocity.LengthSq();
53 JPH_ASSERT(isfinite(len_sq));
54 if (len_sq > Square(mMaxLinearVelocity))
55 mLinearVelocity *= mMaxLinearVelocity / sqrt(len_sq);
56}
57
59{
60 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess, BodyAccess::EAccess::ReadWrite));
61
62 float len_sq = mAngularVelocity.LengthSq();
63 JPH_ASSERT(isfinite(len_sq));
64 if (len_sq > Square(mMaxAngularVelocity))
65 mAngularVelocity *= mMaxAngularVelocity / sqrt(len_sq);
66}
67
69{
70 Mat44 rotation = Mat44::sRotation(mInertiaRotation);
71 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));
72 return rotation.Multiply3x3RightTransposed(rotation_mul_scale_transposed);
73}
74
80
82{
83 JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic);
84
85 Mat44 rotation = inRotation * Mat44::sRotation(mInertiaRotation);
86 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));
87 return rotation.Multiply3x3RightTransposed(rotation_mul_scale_transposed);
88}
89
91{
92 JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic);
93
94 Mat44 rotation = Mat44::sRotation(inBodyRotation * mInertiaRotation);
95 return rotation.Multiply3x3(mInvInertiaDiagonal * rotation.Multiply3x3Transposed(inV));
96}
97
98void MotionProperties::ApplyForceTorqueAndDragInternal(QuatArg inBodyRotation, Vec3Arg inGravity, float inDeltaTime)
99{
100 JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess, BodyAccess::EAccess::ReadWrite));
101 JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic);
102
103 // Update linear velocity
104 mLinearVelocity += inDeltaTime * (mGravityFactor * inGravity + mInvMass * Vec3::sLoadFloat3Unsafe(mForce));
105
106 // Update angular velocity
107 mAngularVelocity += inDeltaTime * MultiplyWorldSpaceInverseInertiaByVector(inBodyRotation, Vec3::sLoadFloat3Unsafe(mTorque));
108
109 // Linear damping: dv/dt = -c * v
110 // Solution: v(t) = v(0) * e^(-c * t) or v2 = v1 * e^(-c * dt)
111 // Taylor expansion of e^(-c * dt) = 1 - c * dt + ...
112 // Since dt is usually in the order of 1/60 and c is a low number too this approximation is good enough
113 mLinearVelocity *= max(0.0f, 1.0f - mLinearDamping * inDeltaTime);
114 mAngularVelocity *= max(0.0f, 1.0f - mAngularDamping * inDeltaTime);
115
116 // Clamp velocities
119}
120
122{
123#ifdef JPH_DOUBLE_PRECISION
124 // Make spheres relative to the first point and initialize them to zero radius
125 DVec3 offset = inPoints[0];
126 offset.StoreDouble3(&mSleepTestOffset);
127 mSleepTestSpheres[0] = Sphere(Vec3::sZero(), 0.0f);
128 for (int i = 1; i < 3; ++i)
129 mSleepTestSpheres[i] = Sphere(Vec3(inPoints[i] - offset), 0.0f);
130#else
131 // Initialize the spheres to zero radius around the supplied points
132 for (int i = 0; i < 3; ++i)
133 mSleepTestSpheres[i] = Sphere(inPoints[i], 0.0f);
134#endif
135
136 mSleepTestTimer = 0.0f;
137}
138
#define JPH_NAMESPACE_END
Definition Core.h:240
#define JPH_NAMESPACE_BEGIN
Definition Core.h:234
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
constexpr T Square(T inV)
Square a value.
Definition Math.h:52
@ 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
Describes the mass and inertia properties of a body. Used during body construction only.
Definition MassProperties.h:16
bool DecomposePrincipalMomentsOfInertia(Mat44 &outRotation, Vec3 &outDiagonal) const
Definition MassProperties.cpp:24
float mMass
Mass of the shape (kg)
Definition MassProperties.h:52
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 Quat GetQuaternion() const
Convert to quaternion.
Definition Mat44.inl:1046
JPH_INLINE Vec3 Multiply3x3Transposed(Vec3Arg inV) const
Multiply vector by only 3x3 part of the transpose of the matrix ( )
Definition Mat44.inl:327
JPH_INLINE Mat44 Multiply3x3RightTransposed(Mat44Arg inM) const
Multiply 3x3 matrix by the transpose of a 3x3 matrix ( )
Definition Mat44.inl:388
JPH_INLINE Vec4 GetColumn4(uint inCol) const
Definition Mat44.h:156
JPH_INLINE Vec3 Multiply3x3(Vec3Arg inV) const
Multiply vector by only 3x3 part of the matrix.
Definition Mat44.inl:307
static JPH_INLINE Mat44 sRotation(Vec3Arg inAxis, float inAngle)
Rotate around arbitrary axis.
Definition Mat44.inl:139
void ClampAngularVelocity()
Definition MotionProperties.inl:58
void ClampLinearVelocity()
Clamp velocity according to limit.
Definition MotionProperties.inl:48
Mat44 GetLocalSpaceInverseInertiaUnchecked() const
Same as GetLocalSpaceInverseInertia() but doesn't check if the body is dynamic.
Definition MotionProperties.inl:68
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
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
Mat44 GetLocalSpaceInverseInertia() const
Get inverse inertia matrix ( ). Will be a matrix of zeros for a static or kinematic object.
Definition MotionProperties.inl:75
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
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
static JPH_INLINE Quat sIdentity()
Definition Quat.h:93
Definition Vec3.h:16
JPH_INLINE Vec4 SplatX() const
Replicate the X component to all components.
Definition Vec3.inl:521
JPH_INLINE Vec3 Reciprocal() const
Reciprocal vector (1 / value) for each of the components.
Definition Vec3.inl:577
JPH_INLINE Vec4 SplatZ() const
Replicate the Z component to all components.
Definition Vec3.inl:543
JPH_INLINE Vec4 SplatY() const
Replicate the Y component to all components.
Definition Vec3.inl:532
JPH_INLINE float LengthSq() const
Squared length of vector.
Definition Vec3.inl:653
JPH_INLINE bool IsNearZero(float inMaxDistSq=1.0e-12f) const
Test if vector is near zero.
Definition Vec3.inl:347
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 sLoadFloat3Unsafe(const Float3 &inV)
Load 3 floats from memory (reads 32 bits extra which it doesn't use)
Definition Vec3.inl:134
Definition Vec4.h:14