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
12
14
43{
45 template <EMotionType Type1, EMotionType Type2>
46 JPH_INLINE bool ApplyVelocityStep(MotionProperties *ioMotionProperties1, float inInvMass1, MotionProperties *ioMotionProperties2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inLambda) const
47 {
48 // Apply impulse if delta is not zero
49 if (inLambda != 0.0f)
50 {
51 // Calculate velocity change due to constraint
52 //
53 // Impulse:
54 // P = J^T lambda
55 //
56 // Euler velocity integration:
57 // v' = v + M^-1 P
58 if constexpr (Type1 == EMotionType::Dynamic)
59 {
60 ioMotionProperties1->SubLinearVelocityStep((inLambda * inInvMass1) * inWorldSpaceAxis);
61 ioMotionProperties1->SubAngularVelocityStep(inLambda * Vec3::sLoadFloat3Unsafe(mInvI1_R1PlusUxAxis));
62 }
63 if constexpr (Type2 == EMotionType::Dynamic)
64 {
65 ioMotionProperties2->AddLinearVelocityStep((inLambda * inInvMass2) * inWorldSpaceAxis);
66 ioMotionProperties2->AddAngularVelocityStep(inLambda * Vec3::sLoadFloat3Unsafe(mInvI2_R2xAxis));
67 }
68 return true;
69 }
70
71 return false;
72 }
73
75 template <EMotionType Type1, EMotionType Type2>
76 JPH_INLINE float TemplatedCalculateInverseEffectiveMass(float inInvMass1, Mat44Arg inInvI1, Vec3Arg inR1PlusU, float inInvMass2, Mat44Arg inInvI2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis)
77 {
78 JPH_ASSERT(inWorldSpaceAxis.IsNormalized(1.0e-5f));
79
80 // Calculate properties used below
81 Vec3 r1_plus_u_x_axis;
82 if constexpr (Type1 != EMotionType::Static)
83 {
84 r1_plus_u_x_axis = inR1PlusU.Cross(inWorldSpaceAxis);
85 r1_plus_u_x_axis.StoreFloat3(&mR1PlusUxAxis);
86 }
87 else
88 {
89 #ifdef _DEBUG
90 Vec3::sNaN().StoreFloat3(&mR1PlusUxAxis);
91 #endif
92 }
93
94 Vec3 r2_x_axis;
95 if constexpr (Type2 != EMotionType::Static)
96 {
97 r2_x_axis = inR2.Cross(inWorldSpaceAxis);
98 r2_x_axis.StoreFloat3(&mR2xAxis);
99 }
100 else
101 {
102 #ifdef _DEBUG
103 Vec3::sNaN().StoreFloat3(&mR2xAxis);
104 #endif
105 }
106
107 // Calculate inverse effective mass: K = J M^-1 J^T
108 float inv_effective_mass;
109
110 if constexpr (Type1 == EMotionType::Dynamic)
111 {
112 Vec3 invi1_r1_plus_u_x_axis = inInvI1.Multiply3x3(r1_plus_u_x_axis);
113 invi1_r1_plus_u_x_axis.StoreFloat3(&mInvI1_R1PlusUxAxis);
114 inv_effective_mass = inInvMass1 + invi1_r1_plus_u_x_axis.Dot(r1_plus_u_x_axis);
115 }
116 else
117 {
118 (void)r1_plus_u_x_axis; // Fix compiler warning: Not using this (it's not calculated either)
119 JPH_IF_DEBUG(Vec3::sNaN().StoreFloat3(&mInvI1_R1PlusUxAxis);)
120 inv_effective_mass = 0.0f;
121 }
122
123 if constexpr (Type2 == EMotionType::Dynamic)
124 {
125 Vec3 invi2_r2_x_axis = inInvI2.Multiply3x3(r2_x_axis);
126 invi2_r2_x_axis.StoreFloat3(&mInvI2_R2xAxis);
127 inv_effective_mass += inInvMass2 + invi2_r2_x_axis.Dot(r2_x_axis);
128 }
129 else
130 {
131 (void)r2_x_axis; // Fix compiler warning: Not using this (it's not calculated either)
132 JPH_IF_DEBUG(Vec3::sNaN().StoreFloat3(&mInvI2_R2xAxis);)
133 }
134
135 return inv_effective_mass;
136 }
137
139 JPH_INLINE float CalculateInverseEffectiveMass(const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis)
140 {
141 // Dispatch to the correct templated form
142 switch (inBody1.GetMotionType())
143 {
144 case EMotionType::Dynamic:
145 {
146 const MotionProperties *mp1 = inBody1.GetMotionPropertiesUnchecked();
147 float inv_m1 = mp1->GetInverseMass();
148 Mat44 inv_i1 = inBody1.GetInverseInertia();
149 switch (inBody2.GetMotionType())
150 {
151 case EMotionType::Dynamic:
152 return TemplatedCalculateInverseEffectiveMass<EMotionType::Dynamic, EMotionType::Dynamic>(inv_m1, inv_i1, inR1PlusU, inBody2.GetMotionPropertiesUnchecked()->GetInverseMass(), inBody2.GetInverseInertia(), inR2, inWorldSpaceAxis);
153
154 case EMotionType::Kinematic:
155 return TemplatedCalculateInverseEffectiveMass<EMotionType::Dynamic, EMotionType::Kinematic>(inv_m1, inv_i1, inR1PlusU, 0 /* Will not be used */, Mat44() /* Will not be used */, inR2, inWorldSpaceAxis);
156
157 case EMotionType::Static:
158 return TemplatedCalculateInverseEffectiveMass<EMotionType::Dynamic, EMotionType::Static>(inv_m1, inv_i1, inR1PlusU, 0 /* Will not be used */, Mat44() /* Will not be used */, inR2, inWorldSpaceAxis);
159
160 default:
161 break;
162 }
163 break;
164 }
165
166 case EMotionType::Kinematic:
167 JPH_ASSERT(inBody2.IsDynamic());
168 return TemplatedCalculateInverseEffectiveMass<EMotionType::Kinematic, EMotionType::Dynamic>(0 /* Will not be used */, Mat44() /* Will not be used */, inR1PlusU, inBody2.GetMotionPropertiesUnchecked()->GetInverseMass(), inBody2.GetInverseInertia(), inR2, inWorldSpaceAxis);
169
170 case EMotionType::Static:
171 JPH_ASSERT(inBody2.IsDynamic());
172 return TemplatedCalculateInverseEffectiveMass<EMotionType::Static, EMotionType::Dynamic>(0 /* Will not be used */, Mat44() /* Will not be used */, inR1PlusU, inBody2.GetMotionPropertiesUnchecked()->GetInverseMass(), inBody2.GetInverseInertia(), inR2, inWorldSpaceAxis);
173
174 default:
175 break;
176 }
177
178 JPH_ASSERT(false);
179 return 0.0f;
180 }
181
183 JPH_INLINE float CalculateInverseEffectiveMassWithMassOverride(const Body &inBody1, float inInvMass1, float inInvInertiaScale1, Vec3Arg inR1PlusU, const Body &inBody2, float inInvMass2, float inInvInertiaScale2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis)
184 {
185 // Dispatch to the correct templated form
186 switch (inBody1.GetMotionType())
187 {
188 case EMotionType::Dynamic:
189 {
190 Mat44 inv_i1 = inInvInertiaScale1 * inBody1.GetInverseInertia();
191 switch (inBody2.GetMotionType())
192 {
193 case EMotionType::Dynamic:
194 return TemplatedCalculateInverseEffectiveMass<EMotionType::Dynamic, EMotionType::Dynamic>(inInvMass1, inv_i1, inR1PlusU, inInvMass2, inInvInertiaScale2 * inBody2.GetInverseInertia(), inR2, inWorldSpaceAxis);
195
196 case EMotionType::Kinematic:
197 return TemplatedCalculateInverseEffectiveMass<EMotionType::Dynamic, EMotionType::Kinematic>(inInvMass1, inv_i1, inR1PlusU, 0 /* Will not be used */, Mat44() /* Will not be used */, inR2, inWorldSpaceAxis);
198
199 case EMotionType::Static:
200 return TemplatedCalculateInverseEffectiveMass<EMotionType::Dynamic, EMotionType::Static>(inInvMass1, inv_i1, inR1PlusU, 0 /* Will not be used */, Mat44() /* Will not be used */, inR2, inWorldSpaceAxis);
201
202 default:
203 break;
204 }
205 break;
206 }
207
208 case EMotionType::Kinematic:
209 JPH_ASSERT(inBody2.IsDynamic());
210 return TemplatedCalculateInverseEffectiveMass<EMotionType::Kinematic, EMotionType::Dynamic>(0 /* Will not be used */, Mat44() /* Will not be used */, inR1PlusU, inInvMass2, inInvInertiaScale2 * inBody2.GetInverseInertia(), inR2, inWorldSpaceAxis);
211
212 case EMotionType::Static:
213 JPH_ASSERT(inBody2.IsDynamic());
214 return TemplatedCalculateInverseEffectiveMass<EMotionType::Static, EMotionType::Dynamic>(0 /* Will not be used */, Mat44() /* Will not be used */, inR1PlusU, inInvMass2, inInvInertiaScale2 * inBody2.GetInverseInertia(), inR2, inWorldSpaceAxis);
215
216 default:
217 break;
218 }
219
220 JPH_ASSERT(false);
221 return 0.0f;
222 }
223
224public:
226 template <EMotionType Type1, EMotionType Type2>
227 JPH_INLINE void TemplatedCalculateConstraintProperties(float inInvMass1, Mat44Arg inInvI1, Vec3Arg inR1PlusU, float inInvMass2, Mat44Arg inInvI2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias = 0.0f)
228 {
229 float inv_effective_mass = TemplatedCalculateInverseEffectiveMass<Type1, Type2>(inInvMass1, inInvI1, inR1PlusU, inInvMass2, inInvI2, inR2, inWorldSpaceAxis);
230
231 if (inv_effective_mass == 0.0f)
232 Deactivate();
233 else
234 {
235 mEffectiveMass = 1.0f / inv_effective_mass;
236 mSpringPart.CalculateSpringPropertiesWithBias(inBias);
237 }
238
239 JPH_DET_LOG("TemplatedCalculateConstraintProperties: invM1: " << inInvMass1 << " invI1: " << inInvI1 << " r1PlusU: " << inR1PlusU << " invM2: " << inInvMass2 << " invI2: " << inInvI2 << " r2: " << inR2 << " bias: " << inBias << " r1PlusUxAxis: " << mR1PlusUxAxis << " r2xAxis: " << mR2xAxis << " invI1_R1PlusUxAxis: " << mInvI1_R1PlusUxAxis << " invI2_R2xAxis: " << mInvI2_R2xAxis << " effectiveMass: " << mEffectiveMass << " totalLambda: " << mTotalLambda);
240 }
241
249 inline void CalculateConstraintProperties(const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias = 0.0f)
250 {
251 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
252
253 if (inv_effective_mass == 0.0f)
254 Deactivate();
255 else
256 {
257 mEffectiveMass = 1.0f / inv_effective_mass;
258 mSpringPart.CalculateSpringPropertiesWithBias(inBias);
259 }
260 }
261
273 inline void CalculateConstraintPropertiesWithMassOverride(const Body &inBody1, float inInvMass1, float inInvInertiaScale1, Vec3Arg inR1PlusU, const Body &inBody2, float inInvMass2, float inInvInertiaScale2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias = 0.0f)
274 {
275 float inv_effective_mass = CalculateInverseEffectiveMassWithMassOverride(inBody1, inInvMass1, inInvInertiaScale1, inR1PlusU, inBody2, inInvMass2, inInvInertiaScale2, inR2, inWorldSpaceAxis);
276
277 if (inv_effective_mass == 0.0f)
278 Deactivate();
279 else
280 {
281 mEffectiveMass = 1.0f / inv_effective_mass;
282 mSpringPart.CalculateSpringPropertiesWithBias(inBias);
283 }
284 }
285
297 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)
298 {
299 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
300
301 if (inv_effective_mass == 0.0f)
302 Deactivate();
303 else
304 mSpringPart.CalculateSpringPropertiesWithFrequencyAndDamping(inDeltaTime, inv_effective_mass, inBias, inC, inFrequency, inDamping, mEffectiveMass);
305 }
306
318 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)
319 {
320 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
321
322 if (inv_effective_mass == 0.0f)
323 Deactivate();
324 else
325 mSpringPart.CalculateSpringPropertiesWithStiffnessAndDamping(inDeltaTime, inv_effective_mass, inBias, inC, inStiffness, inDamping, mEffectiveMass);
326 }
327
329 inline void CalculateConstraintPropertiesWithSettings(float inDeltaTime, const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias, float inC, const SpringSettings &inSpringSettings)
330 {
331 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
332
333 if (inv_effective_mass == 0.0f)
334 Deactivate();
335 else if (inSpringSettings.mMode == ESpringMode::FrequencyAndDamping)
336 mSpringPart.CalculateSpringPropertiesWithFrequencyAndDamping(inDeltaTime, inv_effective_mass, inBias, inC, inSpringSettings.mFrequency, inSpringSettings.mDamping, mEffectiveMass);
337 else
338 mSpringPart.CalculateSpringPropertiesWithStiffnessAndDamping(inDeltaTime, inv_effective_mass, inBias, inC, inSpringSettings.mStiffness, inSpringSettings.mDamping, mEffectiveMass);
339 }
340
342 inline void Deactivate()
343 {
344 mEffectiveMass = 0.0f;
345 mTotalLambda = 0.0f;
346 }
347
349 inline bool IsActive() const
350 {
351 return mEffectiveMass != 0.0f;
352 }
353
355 template <EMotionType Type1, EMotionType Type2>
356 inline void TemplatedWarmStart(MotionProperties *ioMotionProperties1, float inInvMass1, MotionProperties *ioMotionProperties2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inWarmStartImpulseRatio)
357 {
358 mTotalLambda *= inWarmStartImpulseRatio;
359
360 ApplyVelocityStep<Type1, Type2>(ioMotionProperties1, inInvMass1, ioMotionProperties2, inInvMass2, inWorldSpaceAxis, mTotalLambda);
361 }
362
368 inline void WarmStart(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inWarmStartImpulseRatio)
369 {
370 EMotionType motion_type1 = ioBody1.GetMotionType();
371 MotionProperties *motion_properties1 = ioBody1.GetMotionPropertiesUnchecked();
372
373 EMotionType motion_type2 = ioBody2.GetMotionType();
374 MotionProperties *motion_properties2 = ioBody2.GetMotionPropertiesUnchecked();
375
376 // Dispatch to the correct templated form
377 // Note: Warm starting doesn't differentiate between kinematic/static bodies so we handle both as static bodies
378 if (motion_type1 == EMotionType::Dynamic)
379 {
380 if (motion_type2 == EMotionType::Dynamic)
381 TemplatedWarmStart<EMotionType::Dynamic, EMotionType::Dynamic>(motion_properties1, motion_properties1->GetInverseMass(), motion_properties2, motion_properties2->GetInverseMass(), inWorldSpaceAxis, inWarmStartImpulseRatio);
382 else
383 TemplatedWarmStart<EMotionType::Dynamic, EMotionType::Static>(motion_properties1, motion_properties1->GetInverseMass(), motion_properties2, 0.0f /* Unused */, inWorldSpaceAxis, inWarmStartImpulseRatio);
384 }
385 else
386 {
387 JPH_ASSERT(motion_type2 == EMotionType::Dynamic);
388 TemplatedWarmStart<EMotionType::Static, EMotionType::Dynamic>(motion_properties1, 0.0f /* Unused */, motion_properties2, motion_properties2->GetInverseMass(), inWorldSpaceAxis, inWarmStartImpulseRatio);
389 }
390 }
391
393 template <EMotionType Type1, EMotionType Type2>
394 JPH_INLINE float TemplatedSolveVelocityConstraintGetTotalLambda(const MotionProperties *ioMotionProperties1, const MotionProperties *ioMotionProperties2, Vec3Arg inWorldSpaceAxis) const
395 {
396 // Calculate jacobian multiplied by linear velocity
397 float jv;
398 if constexpr (Type1 != EMotionType::Static && Type2 != EMotionType::Static)
399 jv = inWorldSpaceAxis.Dot(ioMotionProperties1->GetLinearVelocity() - ioMotionProperties2->GetLinearVelocity());
400 else if constexpr (Type1 != EMotionType::Static)
401 jv = inWorldSpaceAxis.Dot(ioMotionProperties1->GetLinearVelocity());
402 else if constexpr (Type2 != EMotionType::Static)
403 jv = inWorldSpaceAxis.Dot(-ioMotionProperties2->GetLinearVelocity());
404 else
405 JPH_ASSERT(false); // Static vs static is nonsensical!
406
407 // Calculate jacobian multiplied by angular velocity
408 if constexpr (Type1 != EMotionType::Static)
409 jv += Vec3::sLoadFloat3Unsafe(mR1PlusUxAxis).Dot(ioMotionProperties1->GetAngularVelocity());
410 if constexpr (Type2 != EMotionType::Static)
411 jv -= Vec3::sLoadFloat3Unsafe(mR2xAxis).Dot(ioMotionProperties2->GetAngularVelocity());
412
413 // Lagrange multiplier is:
414 //
415 // lambda = -K^-1 (J v + b)
416 float lambda = mEffectiveMass * (jv - mSpringPart.GetBias(mTotalLambda));
417
418 // Return the total accumulated lambda
419 return mTotalLambda + lambda;
420 }
421
423 template <EMotionType Type1, EMotionType Type2>
424 JPH_INLINE bool TemplatedSolveVelocityConstraintApplyLambda(MotionProperties *ioMotionProperties1, float inInvMass1, MotionProperties *ioMotionProperties2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inTotalLambda)
425 {
426 float delta_lambda = inTotalLambda - mTotalLambda; // Calculate change in lambda
427 mTotalLambda = inTotalLambda; // Store accumulated impulse
428
429 return ApplyVelocityStep<Type1, Type2>(ioMotionProperties1, inInvMass1, ioMotionProperties2, inInvMass2, inWorldSpaceAxis, delta_lambda);
430 }
431
433 template <EMotionType Type1, EMotionType Type2>
434 inline bool TemplatedSolveVelocityConstraint(MotionProperties *ioMotionProperties1, float inInvMass1, MotionProperties *ioMotionProperties2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
435 {
436 float total_lambda = TemplatedSolveVelocityConstraintGetTotalLambda<Type1, Type2>(ioMotionProperties1, ioMotionProperties2, inWorldSpaceAxis);
437
438 // Clamp impulse to specified range
439 total_lambda = Clamp(total_lambda, inMinLambda, inMaxLambda);
440
441 return TemplatedSolveVelocityConstraintApplyLambda<Type1, Type2>(ioMotionProperties1, inInvMass1, ioMotionProperties2, inInvMass2, inWorldSpaceAxis, total_lambda);
442 }
443
450 inline bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
451 {
452 EMotionType motion_type1 = ioBody1.GetMotionType();
453 MotionProperties *motion_properties1 = ioBody1.GetMotionPropertiesUnchecked();
454
455 EMotionType motion_type2 = ioBody2.GetMotionType();
456 MotionProperties *motion_properties2 = ioBody2.GetMotionPropertiesUnchecked();
457
458 // Dispatch to the correct templated form
459 switch (motion_type1)
460 {
461 case EMotionType::Dynamic:
462 switch (motion_type2)
463 {
464 case EMotionType::Dynamic:
465 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Dynamic>(motion_properties1, motion_properties1->GetInverseMass(), motion_properties2, motion_properties2->GetInverseMass(), inWorldSpaceAxis, inMinLambda, inMaxLambda);
466
467 case EMotionType::Kinematic:
468 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Kinematic>(motion_properties1, motion_properties1->GetInverseMass(), motion_properties2, 0.0f /* Unused */, inWorldSpaceAxis, inMinLambda, inMaxLambda);
469
470 case EMotionType::Static:
471 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Static>(motion_properties1, motion_properties1->GetInverseMass(), motion_properties2, 0.0f /* Unused */, inWorldSpaceAxis, inMinLambda, inMaxLambda);
472
473 default:
474 JPH_ASSERT(false);
475 break;
476 }
477 break;
478
479 case EMotionType::Kinematic:
480 JPH_ASSERT(motion_type2 == EMotionType::Dynamic);
481 return TemplatedSolveVelocityConstraint<EMotionType::Kinematic, EMotionType::Dynamic>(motion_properties1, 0.0f /* Unused */, motion_properties2, motion_properties2->GetInverseMass(), inWorldSpaceAxis, inMinLambda, inMaxLambda);
482
483 case EMotionType::Static:
484 JPH_ASSERT(motion_type2 == EMotionType::Dynamic);
485 return TemplatedSolveVelocityConstraint<EMotionType::Static, EMotionType::Dynamic>(motion_properties1, 0.0f /* Unused */, motion_properties2, motion_properties2->GetInverseMass(), inWorldSpaceAxis, inMinLambda, inMaxLambda);
486
487 default:
488 JPH_ASSERT(false);
489 break;
490 }
491
492 return false;
493 }
494
503 inline bool SolveVelocityConstraintWithMassOverride(Body &ioBody1, float inInvMass1, Body &ioBody2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
504 {
505 EMotionType motion_type1 = ioBody1.GetMotionType();
506 MotionProperties *motion_properties1 = ioBody1.GetMotionPropertiesUnchecked();
507
508 EMotionType motion_type2 = ioBody2.GetMotionType();
509 MotionProperties *motion_properties2 = ioBody2.GetMotionPropertiesUnchecked();
510
511 // Dispatch to the correct templated form
512 switch (motion_type1)
513 {
514 case EMotionType::Dynamic:
515 switch (motion_type2)
516 {
517 case EMotionType::Dynamic:
518 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Dynamic>(motion_properties1, inInvMass1, motion_properties2, inInvMass2, inWorldSpaceAxis, inMinLambda, inMaxLambda);
519
520 case EMotionType::Kinematic:
521 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Kinematic>(motion_properties1, inInvMass1, motion_properties2, 0.0f /* Unused */, inWorldSpaceAxis, inMinLambda, inMaxLambda);
522
523 case EMotionType::Static:
524 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Static>(motion_properties1, inInvMass1, motion_properties2, 0.0f /* Unused */, inWorldSpaceAxis, inMinLambda, inMaxLambda);
525
526 default:
527 JPH_ASSERT(false);
528 break;
529 }
530 break;
531
532 case EMotionType::Kinematic:
533 JPH_ASSERT(motion_type2 == EMotionType::Dynamic);
534 return TemplatedSolveVelocityConstraint<EMotionType::Kinematic, EMotionType::Dynamic>(motion_properties1, 0.0f /* Unused */, motion_properties2, inInvMass2, inWorldSpaceAxis, inMinLambda, inMaxLambda);
535
536 case EMotionType::Static:
537 JPH_ASSERT(motion_type2 == EMotionType::Dynamic);
538 return TemplatedSolveVelocityConstraint<EMotionType::Static, EMotionType::Dynamic>(motion_properties1, 0.0f /* Unused */, motion_properties2, inInvMass2, inWorldSpaceAxis, inMinLambda, inMaxLambda);
539
540 default:
541 JPH_ASSERT(false);
542 break;
543 }
544
545 return false;
546 }
547
554 inline bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inC, float inBaumgarte) const
555 {
556 // Only apply position constraint when the constraint is hard, otherwise the velocity bias will fix the constraint
557 if (inC != 0.0f && !mSpringPart.IsActive())
558 {
559 // Calculate lagrange multiplier (lambda) for Baumgarte stabilization:
560 //
561 // lambda = -K^-1 * beta / dt * C
562 //
563 // We should divide by inDeltaTime, but we should multiply by inDeltaTime in the Euler step below so they're cancelled out
564 float lambda = -mEffectiveMass * inBaumgarte * inC;
565
566 // Directly integrate velocity change for one time step
567 //
568 // Euler velocity integration:
569 // dv = M^-1 P
570 //
571 // Impulse:
572 // P = J^T lambda
573 //
574 // Euler position integration:
575 // x' = x + dv * dt
576 //
577 // Note we don't accumulate velocities for the stabilization. This is using the approach described in 'Modeling and
578 // Solving Constraints' by Erin Catto presented at GDC 2007. On slide 78 it is suggested to split up the Baumgarte
579 // stabilization for positional drift so that it does not actually add to the momentum. We combine an Euler velocity
580 // integrate + a position integrate and then discard the velocity change.
581 if (ioBody1.IsDynamic())
582 {
583 ioBody1.SubPositionStep((lambda * ioBody1.GetMotionProperties()->GetInverseMass()) * inWorldSpaceAxis);
584 ioBody1.SubRotationStep(lambda * Vec3::sLoadFloat3Unsafe(mInvI1_R1PlusUxAxis));
585 }
586 if (ioBody2.IsDynamic())
587 {
588 ioBody2.AddPositionStep((lambda * ioBody2.GetMotionProperties()->GetInverseMass()) * inWorldSpaceAxis);
589 ioBody2.AddRotationStep(lambda * Vec3::sLoadFloat3Unsafe(mInvI2_R2xAxis));
590 }
591 return true;
592 }
593
594 return false;
595 }
596
605 inline bool SolvePositionConstraintWithMassOverride(Body &ioBody1, float inInvMass1, Body &ioBody2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inC, float inBaumgarte) const
606 {
607 // Only apply position constraint when the constraint is hard, otherwise the velocity bias will fix the constraint
608 if (inC != 0.0f && !mSpringPart.IsActive())
609 {
610 // Calculate lagrange multiplier (lambda) for Baumgarte stabilization:
611 //
612 // lambda = -K^-1 * beta / dt * C
613 //
614 // We should divide by inDeltaTime, but we should multiply by inDeltaTime in the Euler step below so they're cancelled out
615 float lambda = -mEffectiveMass * inBaumgarte * inC;
616
617 // Directly integrate velocity change for one time step
618 //
619 // Euler velocity integration:
620 // dv = M^-1 P
621 //
622 // Impulse:
623 // P = J^T lambda
624 //
625 // Euler position integration:
626 // x' = x + dv * dt
627 //
628 // Note we don't accumulate velocities for the stabilization. This is using the approach described in 'Modeling and
629 // Solving Constraints' by Erin Catto presented at GDC 2007. On slide 78 it is suggested to split up the Baumgarte
630 // stabilization for positional drift so that it does not actually add to the momentum. We combine an Euler velocity
631 // integrate + a position integrate and then discard the velocity change.
632 if (ioBody1.IsDynamic())
633 {
634 ioBody1.SubPositionStep((lambda * inInvMass1) * inWorldSpaceAxis);
635 ioBody1.SubRotationStep(lambda * Vec3::sLoadFloat3Unsafe(mInvI1_R1PlusUxAxis));
636 }
637 if (ioBody2.IsDynamic())
638 {
639 ioBody2.AddPositionStep((lambda * inInvMass2) * inWorldSpaceAxis);
640 ioBody2.AddRotationStep(lambda * Vec3::sLoadFloat3Unsafe(mInvI2_R2xAxis));
641 }
642 return true;
643 }
644
645 return false;
646 }
647
649 inline void SetTotalLambda(float inLambda)
650 {
651 mTotalLambda = inLambda;
652 }
653
655 inline float GetTotalLambda() const
656 {
657 return mTotalLambda;
658 }
659
661 void SaveState(StateRecorder &inStream) const
662 {
663 inStream.Write(mTotalLambda);
664 }
665
668 {
669 inStream.Read(mTotalLambda);
670 }
671
672private:
673 Float3 mR1PlusUxAxis;
674 Float3 mR2xAxis;
675 Float3 mInvI1_R1PlusUxAxis;
676 Float3 mInvI2_R2xAxis;
677 float mEffectiveMass = 0.0f;
678 SpringPart mSpringPart;
679 float mTotalLambda = 0.0f;
680};
681
#define JPH_IF_DEBUG(...)
Definition: Core.h:486
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
#define JPH_DET_LOG(...)
By default we log nothing.
Definition: DeterminismLog.h:155
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
constexpr T Clamp(T inV, T inMin, T inMax)
Clamp a value between two values.
Definition: Math.h:45
EMotionType
Motion type of a physics body.
Definition: MotionType.h:11
Definition: AxisConstraintPart.h:43
float GetTotalLambda() const
Return lagrange multiplier.
Definition: AxisConstraintPart.h:655
bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
Definition: AxisConstraintPart.h:450
void CalculateConstraintPropertiesWithSettings(float inDeltaTime, const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias, float inC, const SpringSettings &inSpringSettings)
Selects one of the above functions based on the spring settings.
Definition: AxisConstraintPart.h:329
bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inC, float inBaumgarte) const
Definition: AxisConstraintPart.h:554
bool IsActive() const
Check if constraint is active.
Definition: AxisConstraintPart.h:349
void SetTotalLambda(float inLambda)
Override total lagrange multiplier, can be used to set the initial value for warm starting.
Definition: AxisConstraintPart.h:649
bool TemplatedSolveVelocityConstraint(MotionProperties *ioMotionProperties1, float inInvMass1, MotionProperties *ioMotionProperties2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
Templated form of SolveVelocityConstraint with the motion types baked in.
Definition: AxisConstraintPart.h:434
JPH_INLINE void TemplatedCalculateConstraintProperties(float inInvMass1, Mat44Arg inInvI1, Vec3Arg inR1PlusU, float inInvMass2, Mat44Arg inInvI2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias=0.0f)
Templated form of CalculateConstraintProperties with the motion types baked in.
Definition: AxisConstraintPart.h:227
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:297
void WarmStart(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inWarmStartImpulseRatio)
Definition: AxisConstraintPart.h:368
void Deactivate()
Deactivate this constraint.
Definition: AxisConstraintPart.h:342
void CalculateConstraintPropertiesWithMassOverride(const Body &inBody1, float inInvMass1, float inInvInertiaScale1, Vec3Arg inR1PlusU, const Body &inBody2, float inInvMass2, float inInvInertiaScale2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias=0.0f)
Definition: AxisConstraintPart.h:273
bool SolvePositionConstraintWithMassOverride(Body &ioBody1, float inInvMass1, Body &ioBody2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inC, float inBaumgarte) const
Definition: AxisConstraintPart.h:605
JPH_INLINE float TemplatedSolveVelocityConstraintGetTotalLambda(const MotionProperties *ioMotionProperties1, const MotionProperties *ioMotionProperties2, Vec3Arg inWorldSpaceAxis) const
Templated form of SolveVelocityConstraint with the motion types baked in, part 1: get the total lambd...
Definition: AxisConstraintPart.h:394
bool SolveVelocityConstraintWithMassOverride(Body &ioBody1, float inInvMass1, Body &ioBody2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
Definition: AxisConstraintPart.h:503
JPH_INLINE bool TemplatedSolveVelocityConstraintApplyLambda(MotionProperties *ioMotionProperties1, float inInvMass1, MotionProperties *ioMotionProperties2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inTotalLambda)
Templated form of SolveVelocityConstraint with the motion types baked in, part 2: apply new lambda.
Definition: AxisConstraintPart.h:424
void SaveState(StateRecorder &inStream) const
Save state of this constraint part.
Definition: AxisConstraintPart.h:661
void TemplatedWarmStart(MotionProperties *ioMotionProperties1, float inInvMass1, MotionProperties *ioMotionProperties2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inWarmStartImpulseRatio)
Templated form of WarmStart with the motion types baked in.
Definition: AxisConstraintPart.h:356
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:318
void CalculateConstraintProperties(const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias=0.0f)
Definition: AxisConstraintPart.h:249
void RestoreState(StateRecorder &inStream)
Restore state of this constraint part.
Definition: AxisConstraintPart.h:667
Definition: Body.h:35
const MotionProperties * GetMotionProperties() const
Access to the motion properties.
Definition: Body.h:255
EMotionType GetMotionType() const
Get the bodies motion type.
Definition: Body.h:118
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 (using during position integrate & constraint solving)
Definition: Body.inl:81
void SubPositionStep(Vec3Arg inLinearVelocityTimesDeltaTime)
Definition: Body.h:290
Mat44 GetInverseInertia() const
Get inverse inertia tensor in world space.
Definition: Body.inl:120
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:259
void AddPositionStep(Vec3Arg inLinearVelocityTimesDeltaTime)
Update position using an Euler step (used during position integrate & constraint solving)
Definition: Body.h:289
Class that holds 3 floats. Used as a storage class. Convert to Vec3 for calculations.
Definition: Float3.h:13
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 Multiply3x3(Vec3Arg inV) const
Multiply vector by only 3x3 part of the matrix.
Definition: Mat44.inl:316
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:191
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:192
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:194
void AddAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition: MotionProperties.h:193
Class used in other constraint parts to calculate the required bias factor in the lagrange multiplier...
Definition: SpringPart.h:14
void CalculateSpringPropertiesWithFrequencyAndDamping(float inDeltaTime, float inInvEffectiveMass, float inBias, float inC, float inFrequency, float inDamping, float &outEffectiveMass)
Definition: SpringPart.h:86
void CalculateSpringPropertiesWithStiffnessAndDamping(float inDeltaTime, float inInvEffectiveMass, float inBias, float inC, float inStiffness, float inDamping, float &outEffectiveMass)
Definition: SpringPart.h:116
float GetBias(float inTotalLambda) const
Get total bias b, including supplied bias and bias for spring: lambda = J v + b.
Definition: SpringPart.h:137
void CalculateSpringPropertiesWithBias(float inBias)
Definition: SpringPart.h:71
bool IsActive() const
Returns if this spring is active.
Definition: SpringPart.h:131
Settings for a linear or angular spring.
Definition: SpringSettings.h:23
float mStiffness
Definition: SpringSettings.h:60
float mDamping
Definition: SpringSettings.h:67
ESpringMode mMode
Definition: SpringSettings.h:44
float mFrequency
Definition: SpringSettings.h:51
Definition: StateRecorder.h:48
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:16
JPH_INLINE float Dot(Vec3Arg inV2) const
Dot product.
Definition: Vec3.inl:637
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition: Vec3.inl:582
JPH_INLINE bool IsNormalized(float inTolerance=1.0e-6f) const
Test if vector is normalized.
Definition: Vec3.inl:737
JPH_INLINE void StoreFloat3(Float3 *outV) const
Store 3 floats to memory.
Definition: Vec3.inl:757
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
static JPH_INLINE Vec3 sNaN()
Vector with all NaN's.
Definition: Vec3.inl:129