Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
AxisConstraintPart.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
11
13
42{
44 JPH_INLINE bool ApplyVelocityStep(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inLambda) const
45 {
46 // Apply impulse if delta is not zero
47 if (inLambda != 0.0f)
48 {
49 // Calculate velocity change due to constraint
50 //
51 // Impulse:
52 // P = J^T lambda
53 //
54 // Euler velocity integration:
55 // v' = v + M^-1 P
56 if (ioBody1.IsDynamic())
57 {
59 mp1->SubLinearVelocityStep((inLambda * mp1->GetInverseMass()) * inWorldSpaceAxis);
60 mp1->SubAngularVelocityStep(inLambda * Vec3::sLoadFloat3Unsafe(mInvI1_R1PlusUxAxis));
61 }
62 if (ioBody2.IsDynamic())
63 {
65 mp2->AddLinearVelocityStep((inLambda * mp2->GetInverseMass()) * inWorldSpaceAxis);
66 mp2->AddAngularVelocityStep(inLambda * Vec3::sLoadFloat3Unsafe(mInvI2_R2xAxis));
67 }
68 return true;
69 }
70
71 return false;
72 }
73
75 JPH_INLINE float CalculateInverseEffectiveMass(const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis)
76 {
77 JPH_ASSERT(inWorldSpaceAxis.IsNormalized(1.0e-5f));
78
79 // Calculate inverse effective mass: K = J M^-1 J^T
80 float inv_effective_mass;
81
82 if (!inBody1.IsStatic())
83 {
84 Vec3 r1_plus_u_x_axis = inR1PlusU.Cross(inWorldSpaceAxis);
85 r1_plus_u_x_axis.StoreFloat3(&mR1PlusUxAxis);
86
87 if (inBody1.IsDynamic())
88 {
90 Vec3 invi1_r1_plus_u_x_axis = mp1->MultiplyWorldSpaceInverseInertiaByVector(inBody1.GetRotation(), r1_plus_u_x_axis);
91 invi1_r1_plus_u_x_axis.StoreFloat3(&mInvI1_R1PlusUxAxis);
92
93 inv_effective_mass = mp1->GetInverseMass() + invi1_r1_plus_u_x_axis.Dot(r1_plus_u_x_axis);
94 }
95 else
96 {
97 JPH_IF_DEBUG(Vec3::sNaN().StoreFloat3(&mInvI1_R1PlusUxAxis);)
98
99 inv_effective_mass = 0.0f;
100 }
101 }
102 else
103 {
104 JPH_IF_DEBUG(Vec3::sNaN().StoreFloat3(&mR1PlusUxAxis);)
105 JPH_IF_DEBUG(Vec3::sNaN().StoreFloat3(&mInvI1_R1PlusUxAxis);)
106
107 inv_effective_mass = 0.0f;
108 }
109
110 if (!inBody2.IsStatic())
111 {
112 Vec3 r2_x_axis = inR2.Cross(inWorldSpaceAxis);
113 r2_x_axis.StoreFloat3(&mR2xAxis);
114
115 if (inBody2.IsDynamic())
116 {
117 const MotionProperties *mp2 = inBody2.GetMotionPropertiesUnchecked();
118 Vec3 invi2_r2_x_axis = mp2->MultiplyWorldSpaceInverseInertiaByVector(inBody2.GetRotation(), r2_x_axis);
119 invi2_r2_x_axis.StoreFloat3(&mInvI2_R2xAxis);
120
121 inv_effective_mass += mp2->GetInverseMass() + invi2_r2_x_axis.Dot(r2_x_axis);
122 }
123 else
124 {
125 JPH_IF_DEBUG(Vec3::sNaN().StoreFloat3(&mInvI2_R2xAxis);)
126 }
127 }
128 else
129 {
130 JPH_IF_DEBUG(Vec3::sNaN().StoreFloat3(&mR2xAxis);)
131 JPH_IF_DEBUG(Vec3::sNaN().StoreFloat3(&mInvI2_R2xAxis);)
132 }
133
134 return inv_effective_mass;
135 }
136
137public:
145 inline void CalculateConstraintProperties(const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias = 0.0f)
146 {
147 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
148
149 if (inv_effective_mass == 0.0f)
150 Deactivate();
151 else
152 {
153 mEffectiveMass = 1.0f / inv_effective_mass;
154 mSpringPart.CalculateSpringPropertiesWithBias(inBias);
155 }
156 }
157
169 inline void CalculateConstraintPropertiesWithFrequencyAndDamping(float inDeltaTime, const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias, float inC, float inFrequency, float inDamping)
170 {
171 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
172
173 if (inv_effective_mass == 0.0f)
174 Deactivate();
175 else if (inFrequency > 0.0f)
176 mSpringPart.CalculateSpringPropertiesWithFrequencyAndDamping(inDeltaTime, inv_effective_mass, inBias, inC, inFrequency, inDamping, mEffectiveMass);
177 else
178 {
179 mEffectiveMass = 1.0f / inv_effective_mass;
180 mSpringPart.CalculateSpringPropertiesWithBias(inBias);
181 }
182 }
183
195 inline void CalculateConstraintPropertiesWithStiffnessAndDamping(float inDeltaTime, const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias, float inC, float inStiffness, float inDamping)
196 {
197 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
198
199 if (inv_effective_mass == 0.0f)
200 Deactivate();
201 else if (inStiffness > 0.0f || inDamping > 0.0f)
202 mSpringPart.CalculateSpringPropertiesWithStiffnessAndDamping(inDeltaTime, inv_effective_mass, inBias, inC, inStiffness, inDamping, mEffectiveMass);
203 else
204 {
205 mEffectiveMass = 1.0f / inv_effective_mass;
206 mSpringPart.CalculateSpringPropertiesWithBias(inBias);
207 }
208 }
209
212 inline void CalculateConstraintPropertiesWithSettingsForLimit(float inDeltaTime, const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias, float inC, const SpringSettings &inSpringSettings)
213 {
214 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
215
216 if (inv_effective_mass == 0.0f)
217 Deactivate();
218 else if (!inSpringSettings.HasStiffness())
219 {
220 mEffectiveMass = 1.0f / inv_effective_mass;
221 mSpringPart.CalculateSpringPropertiesWithBias(inBias);
222 }
223 else
224 mSpringPart.CalculateSpringPropertiesWithSettings(inDeltaTime, inv_effective_mass, inBias, inC, inSpringSettings, mEffectiveMass);
225 }
226
229 inline void CalculateConstraintPropertiesWithSettingsForMotor(float inDeltaTime, const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias, float inC, const SpringSettings &inSpringSettings)
230 {
231 JPH_ASSERT(inSpringSettings.HasStiffnessOrDamping());
232
233 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
234
235 if (inv_effective_mass == 0.0f)
236 Deactivate();
237 else
238 mSpringPart.CalculateSpringPropertiesWithSettings(inDeltaTime, inv_effective_mass, inBias, inC, inSpringSettings, mEffectiveMass);
239 }
240
242 inline void Deactivate()
243 {
244 mEffectiveMass = 0.0f;
245 mTotalLambda = 0.0f;
246 }
247
249 inline bool IsActive() const
250 {
251 return mEffectiveMass != 0.0f;
252 }
253
259 inline void WarmStart(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inWarmStartImpulseRatio)
260 {
261 mTotalLambda *= inWarmStartImpulseRatio;
262
263 ApplyVelocityStep(ioBody1, ioBody2, inWorldSpaceAxis, mTotalLambda);
264 }
265
272 inline bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
273 {
274 const MotionProperties *mp1 = ioBody1.GetMotionPropertiesUnchecked();
275 const MotionProperties *mp2 = ioBody2.GetMotionPropertiesUnchecked();
276
277 // Calculate jacobian multiplied by linear velocity
278 float jv;
279 if (!ioBody1.IsStatic())
280 {
281 if (!ioBody2.IsStatic())
282 jv = inWorldSpaceAxis.Dot(mp1->GetLinearVelocity() - mp2->GetLinearVelocity());
283 else
284 jv = inWorldSpaceAxis.Dot(mp1->GetLinearVelocity());
285 }
286 else
287 {
288 JPH_ASSERT(!ioBody2.IsStatic());
289 jv = inWorldSpaceAxis.Dot(-mp2->GetLinearVelocity());
290 }
291
292 // Calculate jacobian multiplied by angular velocity
293 if (!ioBody1.IsStatic())
294 jv += Vec3::sLoadFloat3Unsafe(mR1PlusUxAxis).Dot(mp1->GetAngularVelocity());
295 if (!ioBody2.IsStatic())
296 jv -= Vec3::sLoadFloat3Unsafe(mR2xAxis).Dot(mp2->GetAngularVelocity());
297
298 // Lagrange multiplier is:
299 //
300 // lambda = -K^-1 (J v + b)
301 float lambda = mEffectiveMass * (jv - mSpringPart.GetBias(mTotalLambda));
302 float new_lambda = Clamp(mTotalLambda + lambda, inMinLambda, inMaxLambda); // Clamp impulse
303 lambda = new_lambda - mTotalLambda; // Lambda potentially got clamped, calculate the new impulse to apply
304 mTotalLambda = new_lambda; // Store accumulated impulse
305
306 return ApplyVelocityStep(ioBody1, ioBody2, inWorldSpaceAxis, lambda);
307 }
308
310 float GetTotalLambda() const
311 {
312 return mTotalLambda;
313 }
314
321 inline bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inC, float inBaumgarte) const
322 {
323 // Only apply position constraint when the constraint is hard, otherwise the velocity bias will fix the constraint
324 if (inC != 0.0f && !mSpringPart.IsActive())
325 {
326 // Calculate lagrange multiplier (lambda) for Baumgarte stabilization:
327 //
328 // lambda = -K^-1 * beta / dt * C
329 //
330 // We should divide by inDeltaTime, but we should multiply by inDeltaTime in the Euler step below so they're cancelled out
331 float lambda = -mEffectiveMass * inBaumgarte * inC;
332
333 // Directly integrate velocity change for one time step
334 //
335 // Euler velocity integration:
336 // dv = M^-1 P
337 //
338 // Impulse:
339 // P = J^T lambda
340 //
341 // Euler position integration:
342 // x' = x + dv * dt
343 //
344 // Note we don't accumulate velocities for the stabilization. This is using the approach described in 'Modeling and
345 // Solving Constraints' by Erin Catto presented at GDC 2007. On slide 78 it is suggested to split up the Baumgarte
346 // stabilization for positional drift so that it does not actually add to the momentum. We combine an Euler velocity
347 // integrate + a position integrate and then discard the velocity change.
348 if (ioBody1.IsDynamic())
349 {
350 ioBody1.SubPositionStep((lambda * ioBody1.GetMotionProperties()->GetInverseMass()) * inWorldSpaceAxis);
351 ioBody1.SubRotationStep(lambda * Vec3::sLoadFloat3Unsafe(mInvI1_R1PlusUxAxis));
352 }
353 if (ioBody2.IsDynamic())
354 {
355 ioBody2.AddPositionStep((lambda * ioBody2.GetMotionProperties()->GetInverseMass()) * inWorldSpaceAxis);
356 ioBody2.AddRotationStep(lambda * Vec3::sLoadFloat3Unsafe(mInvI2_R2xAxis));
357 }
358 return true;
359 }
360
361 return false;
362 }
363
365 void SaveState(StateRecorder &inStream) const
366 {
367 inStream.Write(mTotalLambda);
368 }
369
372 {
373 inStream.Read(mTotalLambda);
374 }
375
376private:
377 Float3 mR1PlusUxAxis;
378 Float3 mR2xAxis;
379 Float3 mInvI1_R1PlusUxAxis;
380 Float3 mInvI2_R2xAxis;
381 float mEffectiveMass = 0.0f;
382 SpringPart mSpringPart;
383 float mTotalLambda = 0.0f;
384};
385
#define JPH_IF_DEBUG(...)
Definition Core.h:573
#define JPH_NAMESPACE_END
Definition Core.h:433
#define JPH_NAMESPACE_BEGIN
Definition Core.h:427
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
JPH_INLINE constexpr T Clamp(T inV, T inMin, T inMax)
Clamp a value between two values.
Definition Math.h:63
Definition AxisConstraintPart.h:42
float GetTotalLambda() const
Return lagrange multiplier.
Definition AxisConstraintPart.h:310
bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
Definition AxisConstraintPart.h:272
bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inC, float inBaumgarte) const
Definition AxisConstraintPart.h:321
bool IsActive() const
Check if constraint is active.
Definition AxisConstraintPart.h:249
void CalculateConstraintPropertiesWithSettingsForMotor(float inDeltaTime, const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias, float inC, const SpringSettings &inSpringSettings)
Definition AxisConstraintPart.h:229
void CalculateConstraintPropertiesWithFrequencyAndDamping(float inDeltaTime, const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias, float inC, float inFrequency, float inDamping)
Definition AxisConstraintPart.h:169
void WarmStart(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inWarmStartImpulseRatio)
Definition AxisConstraintPart.h:259
void Deactivate()
Deactivate this constraint.
Definition AxisConstraintPart.h:242
void SaveState(StateRecorder &inStream) const
Save state of this constraint part.
Definition AxisConstraintPart.h:365
void CalculateConstraintPropertiesWithSettingsForLimit(float inDeltaTime, const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias, float inC, const SpringSettings &inSpringSettings)
Definition AxisConstraintPart.h:212
void CalculateConstraintPropertiesWithStiffnessAndDamping(float inDeltaTime, const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias, float inC, float inStiffness, float inDamping)
Definition AxisConstraintPart.h:195
void CalculateConstraintProperties(const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias=0.0f)
Definition AxisConstraintPart.h:145
void RestoreState(StateRecorder &inStream)
Restore state of this constraint part.
Definition AxisConstraintPart.h:371
Definition Body.h:39
const MotionProperties * GetMotionProperties() const
Access to the motion properties.
Definition Body.h:308
bool IsDynamic() const
Check if this body is dynamic, which means that it moves and forces can act on it.
Definition Body.h:67
void AddRotationStep(Vec3Arg inAngularVelocityTimesDeltaTime)
Update rotation using an Euler step (used during position integrate & constraint solving)
Definition Body.inl:81
void SubPositionStep(Vec3Arg inLinearVelocityTimesDeltaTime)
Definition Body.h:349
bool IsStatic() const
Check if this body is static (not movable)
Definition Body.h:61
Quat GetRotation() const
World space rotation of the body.
Definition Body.h:271
void SubRotationStep(Vec3Arg inAngularVelocityTimesDeltaTime)
Definition Body.inl:100
const MotionProperties * GetMotionPropertiesUnchecked() const
Access to the motion properties (version that does not check if the object is kinematic or dynamic)
Definition Body.h:312
void AddPositionStep(Vec3Arg inLinearVelocityTimesDeltaTime)
Update position using an Euler step (used during position integrate & constraint solving)
Definition Body.h:348
Class that holds 3 floats. Used as a storage class. Convert to Vec3 for calculations.
Definition Float3.h:13
The Body class only keeps track of state for static bodies, the MotionProperties class keeps the addi...
Definition MotionProperties.h:29
void AddLinearVelocityStep(Vec3Arg inLinearVelocityChange)
Definition MotionProperties.h:226
Vec3 GetLinearVelocity() const
Get world space linear velocity of the center of mass.
Definition MotionProperties.h:43
Vec3 GetAngularVelocity() const
Get world space angular velocity of the center of mass.
Definition MotionProperties.h:52
void SubLinearVelocityStep(Vec3Arg inLinearVelocityChange)
Definition MotionProperties.h:227
float GetInverseMass() const
Get inverse mass (1 / mass). Should only be called on a dynamic object (static or kinematic bodies ha...
Definition MotionProperties.h:95
void SubAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition MotionProperties.h:238
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:83
void AddAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition MotionProperties.h:237
Class used in other constraint parts to calculate the required bias factor in the lagrange multiplier...
Definition SpringPart.h:16
JPH_INLINE bool IsActive() const
Returns if this spring is active.
Definition SpringPart.h:147
JPH_INLINE void CalculateSpringPropertiesWithFrequencyAndDamping(float inDeltaTime, float inInvEffectiveMass, float inBias, float inC, float inFrequency, float inDamping, float &outEffectiveMass)
Definition SpringPart.h:98
JPH_INLINE float GetBias(float inTotalLambda) const
Get total bias b, including supplied bias and bias for spring: lambda = J v + b.
Definition SpringPart.h:153
JPH_INLINE void CalculateSpringPropertiesWithStiffnessAndDamping(float inDeltaTime, float inInvEffectiveMass, float inBias, float inC, float inStiffness, float inDamping, float &outEffectiveMass)
Definition SpringPart.h:36
JPH_INLINE void CalculateSpringPropertiesWithBias(float inBias)
Definition SpringPart.h:21
JPH_INLINE void CalculateSpringPropertiesWithSettings(float inDeltaTime, float inInvEffectiveMass, float inBias, float inC, const SpringSettings &inSpringSettings, float &outEffectiveMass)
Definition SpringPart.h:130
Settings for a linear or angular spring.
Definition SpringSettings.h:24
bool HasStiffnessOrDamping() const
Check if this spring has stiffness or damping (making it active), if not the constraint will be hard.
Definition SpringSettings.h:44
bool HasStiffness() const
Check if the spring has a valid frequency / stiffness, if not the spring will be hard.
Definition SpringSettings.h:41
Definition StateRecorder.h:110
void Read(T &outT)
Read a primitive (e.g. float, int, etc.) from the binary stream.
Definition StreamIn.h:30
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:958
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition Vec3.inl:851
JPH_INLINE bool IsNormalized(float inTolerance=1.0e-6f) const
Test if vector is normalized.
Definition Vec3.inl:1144
JPH_INLINE void StoreFloat3(Float3 *outV) const
Store 3 floats to memory.
Definition Vec3.inl:1169
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:167
static JPH_INLINE Vec3 sNaN()
Vector with all NaN's.
Definition Vec3.inl:162