Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
GearConstraintPart.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
9
11
40{
42 JPH_INLINE bool ApplyVelocityStep(Body &ioBody1, Body &ioBody2, float inLambda) const
43 {
44 // Apply impulse if delta is not zero
45 if (inLambda != 0.0f)
46 {
47 // Calculate velocity change due to constraint
48 //
49 // Impulse:
50 // P = J^T lambda
51 //
52 // Euler velocity integration:
53 // v' = v + M^-1 P
54 ioBody1.GetMotionProperties()->AddAngularVelocityStep(inLambda * mInvI1_A);
55 ioBody2.GetMotionProperties()->AddAngularVelocityStep(inLambda * mInvI2_B);
56 return true;
57 }
58
59 return false;
60 }
61
62public:
69 inline void CalculateConstraintProperties(const Body &inBody1, Vec3Arg inWorldSpaceHingeAxis1, const Body &inBody2, Vec3Arg inWorldSpaceHingeAxis2, float inRatio)
70 {
71 JPH_ASSERT(inWorldSpaceHingeAxis1.IsNormalized(1.0e-4f));
72 JPH_ASSERT(inWorldSpaceHingeAxis2.IsNormalized(1.0e-4f));
73
74 // Calculate: I1^-1 a
75 mInvI1_A = inBody1.GetMotionProperties()->MultiplyWorldSpaceInverseInertiaByVector(inBody1.GetRotation(), inWorldSpaceHingeAxis1);
76
77 // Calculate: I2^-1 b
78 mInvI2_B = inBody2.GetMotionProperties()->MultiplyWorldSpaceInverseInertiaByVector(inBody2.GetRotation(), inWorldSpaceHingeAxis2);
79
80 // K^-1 = 1 / (J M^-1 J^T) = 1 / (a^T I1^-1 a + r^2 * b^T I2^-1 b)
81 float inv_effective_mass = (inWorldSpaceHingeAxis1.Dot(mInvI1_A) + inWorldSpaceHingeAxis2.Dot(mInvI2_B) * Square(inRatio));
82 if (inv_effective_mass == 0.0f)
83 Deactivate();
84 else
85 mEffectiveMass = 1.0f / inv_effective_mass;
86 }
87
89 inline void Deactivate()
90 {
91 mEffectiveMass = 0.0f;
92 mTotalLambda = 0.0f;
93 }
94
96 inline bool IsActive() const
97 {
98 return mEffectiveMass != 0.0f;
99 }
100
105 inline void WarmStart(Body &ioBody1, Body &ioBody2, float inWarmStartImpulseRatio)
106 {
107 mTotalLambda *= inWarmStartImpulseRatio;
108 ApplyVelocityStep(ioBody1, ioBody2, mTotalLambda);
109 }
110
117 inline bool SolveVelocityConstraint(Body &ioBody1, Vec3Arg inWorldSpaceHingeAxis1, Body &ioBody2, Vec3Arg inWorldSpaceHingeAxis2, float inRatio)
118 {
119 // Lagrange multiplier is:
120 //
121 // lambda = -K^-1 (J v + b)
122 float lambda = -mEffectiveMass * (inWorldSpaceHingeAxis1.Dot(ioBody1.GetAngularVelocity()) + inRatio * inWorldSpaceHingeAxis2.Dot(ioBody2.GetAngularVelocity()));
123 mTotalLambda += lambda; // Store accumulated impulse
124
125 return ApplyVelocityStep(ioBody1, ioBody2, lambda);
126 }
127
129 float GetTotalLambda() const
130 {
131 return mTotalLambda;
132 }
133
139 inline bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, float inC, float inBaumgarte) const
140 {
141 // Only apply position constraint when the constraint is hard, otherwise the velocity bias will fix the constraint
142 if (inC != 0.0f)
143 {
144 // Calculate lagrange multiplier (lambda) for Baumgarte stabilization:
145 //
146 // lambda = -K^-1 * beta / dt * C
147 //
148 // We should divide by inDeltaTime, but we should multiply by inDeltaTime in the Euler step below so they're cancelled out
149 float lambda = -mEffectiveMass * inBaumgarte * inC;
150
151 // Directly integrate velocity change for one time step
152 //
153 // Euler velocity integration:
154 // dv = M^-1 P
155 //
156 // Impulse:
157 // P = J^T lambda
158 //
159 // Euler position integration:
160 // x' = x + dv * dt
161 //
162 // Note we don't accumulate velocities for the stabilization. This is using the approach described in 'Modeling and
163 // Solving Constraints' by Erin Catto presented at GDC 2007. On slide 78 it is suggested to split up the Baumgarte
164 // stabilization for positional drift so that it does not actually add to the momentum. We combine an Euler velocity
165 // integrate + a position integrate and then discard the velocity change.
166 if (ioBody1.IsDynamic())
167 ioBody1.AddRotationStep(lambda * mInvI1_A);
168 if (ioBody2.IsDynamic())
169 ioBody2.AddRotationStep(lambda * mInvI2_B);
170 return true;
171 }
172
173 return false;
174 }
175
177 void SaveState(StateRecorder &inStream) const
178 {
179 inStream.Write(mTotalLambda);
180 }
181
184 {
185 inStream.Read(mTotalLambda);
186 }
187
188private:
189 Vec3 mInvI1_A;
190 Vec3 mInvI2_B;
191 float mEffectiveMass = 0.0f;
192 float mTotalLambda = 0.0f;
193};
194
#define JPH_NAMESPACE_END
Definition: Core.h:378
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
JPH_INLINE constexpr T Square(T inV)
Square a value.
Definition: Math.h:52
Definition: Body.h:35
const MotionProperties * GetMotionProperties() const
Access to the motion properties.
Definition: Body.h:263
bool IsDynamic() const
Check if this body is dynamic, which means that it moves and forces can act on it.
Definition: Body.h:63
void AddRotationStep(Vec3Arg inAngularVelocityTimesDeltaTime)
Update rotation using an Euler step (used during position integrate & constraint solving)
Definition: Body.inl:81
Quat GetRotation() const
World space rotation of the body.
Definition: Body.h:245
Vec3 GetAngularVelocity() const
Get world space angular velocity of the center of mass (unit: rad/s)
Definition: Body.h:157
Definition: GearConstraintPart.h:40
float GetTotalLambda() const
Return lagrange multiplier.
Definition: GearConstraintPart.h:129
void WarmStart(Body &ioBody1, Body &ioBody2, float inWarmStartImpulseRatio)
Definition: GearConstraintPart.h:105
bool SolveVelocityConstraint(Body &ioBody1, Vec3Arg inWorldSpaceHingeAxis1, Body &ioBody2, Vec3Arg inWorldSpaceHingeAxis2, float inRatio)
Definition: GearConstraintPart.h:117
bool IsActive() const
Check if constraint is active.
Definition: GearConstraintPart.h:96
void CalculateConstraintProperties(const Body &inBody1, Vec3Arg inWorldSpaceHingeAxis1, const Body &inBody2, Vec3Arg inWorldSpaceHingeAxis2, float inRatio)
Definition: GearConstraintPart.h:69
void Deactivate()
Deactivate this constraint.
Definition: GearConstraintPart.h:89
void RestoreState(StateRecorder &inStream)
Restore state of this constraint part.
Definition: GearConstraintPart.h:183
bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, float inC, float inBaumgarte) const
Definition: GearConstraintPart.h:139
void SaveState(StateRecorder &inStream) const
Save state of this constraint part.
Definition: GearConstraintPart.h:177
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:76
void AddAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition: MotionProperties.h:193
Definition: StateRecorder.h:105
void Read(T &outT)
Read a primitive (e.g. float, int, etc.) from the binary stream.
Definition: StreamIn.h:29
void Write(const T &inT)
Write a primitive (e.g. float, int, etc.) to the binary stream.
Definition: StreamOut.h:26
Definition: Vec3.h:17
JPH_INLINE float Dot(Vec3Arg inV2) const
Dot product.
Definition: Vec3.inl:649
JPH_INLINE bool IsNormalized(float inTolerance=1.0e-6f) const
Test if vector is normalized.
Definition: Vec3.inl:749