Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
RackAndPinionConstraintPart.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()->SubLinearVelocityStep(inLambda * mRatio_InvM2_B);
56 return true;
57 }
58
59 return false;
60 }
61
62public:
69 inline void CalculateConstraintProperties(const Body &inBody1, Vec3Arg inWorldSpaceHingeAxis, const Body &inBody2, Vec3Arg inWorldSpaceSliderAxis, float inRatio)
70 {
71 JPH_ASSERT(inWorldSpaceHingeAxis.IsNormalized(1.0e-4f));
72 JPH_ASSERT(inWorldSpaceSliderAxis.IsNormalized(1.0e-4f));
73
74 // Calculate: I1^-1 a
75 mInvI1_A = inBody1.GetMotionProperties()->MultiplyWorldSpaceInverseInertiaByVector(inBody1.GetRotation(), inWorldSpaceHingeAxis);
76
77 // Calculate: r/m2 b
78 float inv_m2 = inBody2.GetMotionProperties()->GetInverseMass();
79 mRatio_InvM2_B = inRatio * inv_m2 * inWorldSpaceSliderAxis;
80
81 // K^-1 = 1 / (J M^-1 J^T) = 1 / (a^T I1^-1 a + 1/m2 * r^2 * b . b)
82 mEffectiveMass = 1.0f / (inWorldSpaceHingeAxis.Dot(mInvI1_A) + inv_m2 * Square(inRatio));
83 }
84
86 inline void Deactivate()
87 {
88 mEffectiveMass = 0.0f;
89 mTotalLambda = 0.0f;
90 }
91
93 inline bool IsActive() const
94 {
95 return mEffectiveMass != 0.0f;
96 }
97
102 inline void WarmStart(Body &ioBody1, Body &ioBody2, float inWarmStartImpulseRatio)
103 {
104 mTotalLambda *= inWarmStartImpulseRatio;
105 ApplyVelocityStep(ioBody1, ioBody2, mTotalLambda);
106 }
107
114 inline bool SolveVelocityConstraint(Body &ioBody1, Vec3Arg inWorldSpaceHingeAxis, Body &ioBody2, Vec3Arg inWorldSpaceSliderAxis, float inRatio)
115 {
116 // Lagrange multiplier is:
117 //
118 // lambda = -K^-1 (J v + b)
119 float lambda = mEffectiveMass * (inRatio * inWorldSpaceSliderAxis.Dot(ioBody2.GetLinearVelocity()) - inWorldSpaceHingeAxis.Dot(ioBody1.GetAngularVelocity()));
120 mTotalLambda += lambda; // Store accumulated impulse
121
122 return ApplyVelocityStep(ioBody1, ioBody2, lambda);
123 }
124
126 float GetTotalLambda() const
127 {
128 return mTotalLambda;
129 }
130
136 inline bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, float inC, float inBaumgarte) const
137 {
138 // Only apply position constraint when the constraint is hard, otherwise the velocity bias will fix the constraint
139 if (inC != 0.0f)
140 {
141 // Calculate lagrange multiplier (lambda) for Baumgarte stabilization:
142 //
143 // lambda = -K^-1 * beta / dt * C
144 //
145 // We should divide by inDeltaTime, but we should multiply by inDeltaTime in the Euler step below so they're cancelled out
146 float lambda = -mEffectiveMass * inBaumgarte * inC;
147
148 // Directly integrate velocity change for one time step
149 //
150 // Euler velocity integration:
151 // dv = M^-1 P
152 //
153 // Impulse:
154 // P = J^T lambda
155 //
156 // Euler position integration:
157 // x' = x + dv * dt
158 //
159 // Note we don't accumulate velocities for the stabilization. This is using the approach described in 'Modeling and
160 // Solving Constraints' by Erin Catto presented at GDC 2007. On slide 78 it is suggested to split up the Baumgarte
161 // stabilization for positional drift so that it does not actually add to the momentum. We combine an Euler velocity
162 // integrate + a position integrate and then discard the velocity change.
163 if (ioBody1.IsDynamic())
164 ioBody1.AddRotationStep(lambda * mInvI1_A);
165 if (ioBody2.IsDynamic())
166 ioBody2.SubPositionStep(lambda * mRatio_InvM2_B);
167 return true;
168 }
169
170 return false;
171 }
172
174 void SaveState(StateRecorder &inStream) const
175 {
176 inStream.Write(mTotalLambda);
177 }
178
181 {
182 inStream.Read(mTotalLambda);
183 }
184
185private:
186 Vec3 mInvI1_A;
187 Vec3 mRatio_InvM2_B;
188 float mEffectiveMass = 0.0f;
189 float mTotalLambda = 0.0f;
190};
191
#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
Definition: Body.h:33
const MotionProperties * GetMotionProperties() const
Access to the motion properties.
Definition: Body.h:205
bool IsDynamic() const
Check if this body is dynamic, which means that it moves and forces can act on it.
Definition: Body.h:56
void AddRotationStep(Vec3Arg inAngularVelocityTimesDeltaTime)
Update rotation using an Euler step (using during position integrate & constraint solving)
Definition: Body.inl:75
void SubPositionStep(Vec3Arg inLinearVelocityTimesDeltaTime)
Definition: Body.h:237
Vec3 GetLinearVelocity() const
Get world space linear velocity of the center of mass (unit: m/s)
Definition: Body.h:109
Quat GetRotation() const
World space rotation of the body.
Definition: Body.h:187
Vec3 GetAngularVelocity() const
Get world space angular velocity of the center of mass (unit: rad/s)
Definition: Body.h:118
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
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 AddAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition: MotionProperties.h:128
Definition: RackAndPinionConstraintPart.h:40
void CalculateConstraintProperties(const Body &inBody1, Vec3Arg inWorldSpaceHingeAxis, const Body &inBody2, Vec3Arg inWorldSpaceSliderAxis, float inRatio)
Definition: RackAndPinionConstraintPart.h:69
bool SolveVelocityConstraint(Body &ioBody1, Vec3Arg inWorldSpaceHingeAxis, Body &ioBody2, Vec3Arg inWorldSpaceSliderAxis, float inRatio)
Definition: RackAndPinionConstraintPart.h:114
float GetTotalLambda() const
Return lagrange multiplier.
Definition: RackAndPinionConstraintPart.h:126
void WarmStart(Body &ioBody1, Body &ioBody2, float inWarmStartImpulseRatio)
Definition: RackAndPinionConstraintPart.h:102
void SaveState(StateRecorder &inStream) const
Save state of this constraint part.
Definition: RackAndPinionConstraintPart.h:174
bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, float inC, float inBaumgarte) const
Definition: RackAndPinionConstraintPart.h:136
void RestoreState(StateRecorder &inStream)
Restore state of this constraint part.
Definition: RackAndPinionConstraintPart.h:180
void Deactivate()
Deactivate this constraint.
Definition: RackAndPinionConstraintPart.h:86
bool IsActive() const
Check if constraint is active.
Definition: RackAndPinionConstraintPart.h:93
Definition: StateRecorder.h:15
void Read(T &outT)
Read a primitive (e.g. float, int, etc.) from the binary stream.
Definition: StreamIn.h:27
void Write(const T &inT)
Write a primitive (e.g. float, int, etc.) to the binary stream.
Definition: StreamOut.h:24
Definition: Vec3.h:16
JPH_INLINE float Dot(Vec3Arg inV2) const
Dot product.
Definition: Vec3.inl:637
JPH_INLINE bool IsNormalized(float inTolerance=1.0e-6f) const
Test if vector is normalized.
Definition: Vec3.inl:737