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 mEffectiveMass = 1.0f / TemplatedCalculateInverseEffectiveMass<Type1, Type2>(inInvMass1, inInvI1, inR1PlusU, inInvMass2, inInvI2, inR2, inWorldSpaceAxis);
230
231 mSpringPart.CalculateSpringPropertiesWithBias(inBias);
232
233 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);
234 }
235
243 inline void CalculateConstraintProperties(const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias = 0.0f)
244 {
245 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
246
247 if (inv_effective_mass == 0.0f)
248 Deactivate();
249 else
250 {
251 mEffectiveMass = 1.0f / inv_effective_mass;
252 mSpringPart.CalculateSpringPropertiesWithBias(inBias);
253 }
254 }
255
267 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)
268 {
269 float inv_effective_mass = CalculateInverseEffectiveMassWithMassOverride(inBody1, inInvMass1, inInvInertiaScale1, inR1PlusU, inBody2, inInvMass2, inInvInertiaScale2, inR2, inWorldSpaceAxis);
270
271 if (inv_effective_mass == 0.0f)
272 Deactivate();
273 else
274 {
275 mEffectiveMass = 1.0f / inv_effective_mass;
276 mSpringPart.CalculateSpringPropertiesWithBias(inBias);
277 }
278 }
279
291 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)
292 {
293 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
294
295 if (inv_effective_mass == 0.0f)
296 Deactivate();
297 else
298 mSpringPart.CalculateSpringPropertiesWithFrequencyAndDamping(inDeltaTime, inv_effective_mass, inBias, inC, inFrequency, inDamping, mEffectiveMass);
299 }
300
312 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)
313 {
314 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
315
316 if (inv_effective_mass == 0.0f)
317 Deactivate();
318 else
319 mSpringPart.CalculateSpringPropertiesWithStiffnessAndDamping(inDeltaTime, inv_effective_mass, inBias, inC, inStiffness, inDamping, mEffectiveMass);
320 }
321
323 inline void CalculateConstraintPropertiesWithSettings(float inDeltaTime, const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias, float inC, const SpringSettings &inSpringSettings)
324 {
325 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
326
327 if (inv_effective_mass == 0.0f)
328 Deactivate();
329 else if (inSpringSettings.mMode == ESpringMode::FrequencyAndDamping)
330 mSpringPart.CalculateSpringPropertiesWithFrequencyAndDamping(inDeltaTime, inv_effective_mass, inBias, inC, inSpringSettings.mFrequency, inSpringSettings.mDamping, mEffectiveMass);
331 else
332 mSpringPart.CalculateSpringPropertiesWithStiffnessAndDamping(inDeltaTime, inv_effective_mass, inBias, inC, inSpringSettings.mStiffness, inSpringSettings.mDamping, mEffectiveMass);
333 }
334
336 inline void Deactivate()
337 {
338 mEffectiveMass = 0.0f;
339 mTotalLambda = 0.0f;
340 }
341
343 inline bool IsActive() const
344 {
345 return mEffectiveMass != 0.0f;
346 }
347
349 template <EMotionType Type1, EMotionType Type2>
350 inline void TemplatedWarmStart(MotionProperties *ioMotionProperties1, float inInvMass1, MotionProperties *ioMotionProperties2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inWarmStartImpulseRatio)
351 {
352 mTotalLambda *= inWarmStartImpulseRatio;
353
354 ApplyVelocityStep<Type1, Type2>(ioMotionProperties1, inInvMass1, ioMotionProperties2, inInvMass2, inWorldSpaceAxis, mTotalLambda);
355 }
356
362 inline void WarmStart(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inWarmStartImpulseRatio)
363 {
364 EMotionType motion_type1 = ioBody1.GetMotionType();
365 MotionProperties *motion_properties1 = ioBody1.GetMotionPropertiesUnchecked();
366
367 EMotionType motion_type2 = ioBody2.GetMotionType();
368 MotionProperties *motion_properties2 = ioBody2.GetMotionPropertiesUnchecked();
369
370 // Dispatch to the correct templated form
371 // Note: Warm starting doesn't differentiate between kinematic/static bodies so we handle both as static bodies
372 if (motion_type1 == EMotionType::Dynamic)
373 {
374 if (motion_type2 == EMotionType::Dynamic)
375 TemplatedWarmStart<EMotionType::Dynamic, EMotionType::Dynamic>(motion_properties1, motion_properties1->GetInverseMass(), motion_properties2, motion_properties2->GetInverseMass(), inWorldSpaceAxis, inWarmStartImpulseRatio);
376 else
377 TemplatedWarmStart<EMotionType::Dynamic, EMotionType::Static>(motion_properties1, motion_properties1->GetInverseMass(), motion_properties2, 0.0f /* Unused */, inWorldSpaceAxis, inWarmStartImpulseRatio);
378 }
379 else
380 {
381 JPH_ASSERT(motion_type2 == EMotionType::Dynamic);
382 TemplatedWarmStart<EMotionType::Static, EMotionType::Dynamic>(motion_properties1, 0.0f /* Unused */, motion_properties2, motion_properties2->GetInverseMass(), inWorldSpaceAxis, inWarmStartImpulseRatio);
383 }
384 }
385
387 template <EMotionType Type1, EMotionType Type2>
388 JPH_INLINE float TemplatedSolveVelocityConstraintGetTotalLambda(const MotionProperties *ioMotionProperties1, const MotionProperties *ioMotionProperties2, Vec3Arg inWorldSpaceAxis) const
389 {
390 // Calculate jacobian multiplied by linear velocity
391 float jv;
392 if constexpr (Type1 != EMotionType::Static && Type2 != EMotionType::Static)
393 jv = inWorldSpaceAxis.Dot(ioMotionProperties1->GetLinearVelocity() - ioMotionProperties2->GetLinearVelocity());
394 else if constexpr (Type1 != EMotionType::Static)
395 jv = inWorldSpaceAxis.Dot(ioMotionProperties1->GetLinearVelocity());
396 else if constexpr (Type2 != EMotionType::Static)
397 jv = inWorldSpaceAxis.Dot(-ioMotionProperties2->GetLinearVelocity());
398 else
399 JPH_ASSERT(false); // Static vs static is nonsensical!
400
401 // Calculate jacobian multiplied by angular velocity
402 if constexpr (Type1 != EMotionType::Static)
403 jv += Vec3::sLoadFloat3Unsafe(mR1PlusUxAxis).Dot(ioMotionProperties1->GetAngularVelocity());
404 if constexpr (Type2 != EMotionType::Static)
405 jv -= Vec3::sLoadFloat3Unsafe(mR2xAxis).Dot(ioMotionProperties2->GetAngularVelocity());
406
407 // Lagrange multiplier is:
408 //
409 // lambda = -K^-1 (J v + b)
410 float lambda = mEffectiveMass * (jv - mSpringPart.GetBias(mTotalLambda));
411
412 // Return the total accumulated lambda
413 return mTotalLambda + lambda;
414 }
415
417 template <EMotionType Type1, EMotionType Type2>
418 JPH_INLINE bool TemplatedSolveVelocityConstraintApplyLambda(MotionProperties *ioMotionProperties1, float inInvMass1, MotionProperties *ioMotionProperties2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inTotalLambda)
419 {
420 float delta_lambda = inTotalLambda - mTotalLambda; // Calculate change in lambda
421 mTotalLambda = inTotalLambda; // Store accumulated impulse
422
423 return ApplyVelocityStep<Type1, Type2>(ioMotionProperties1, inInvMass1, ioMotionProperties2, inInvMass2, inWorldSpaceAxis, delta_lambda);
424 }
425
427 template <EMotionType Type1, EMotionType Type2>
428 inline bool TemplatedSolveVelocityConstraint(MotionProperties *ioMotionProperties1, float inInvMass1, MotionProperties *ioMotionProperties2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
429 {
430 float total_lambda = TemplatedSolveVelocityConstraintGetTotalLambda<Type1, Type2>(ioMotionProperties1, ioMotionProperties2, inWorldSpaceAxis);
431
432 // Clamp impulse to specified range
433 total_lambda = Clamp(total_lambda, inMinLambda, inMaxLambda);
434
435 return TemplatedSolveVelocityConstraintApplyLambda<Type1, Type2>(ioMotionProperties1, inInvMass1, ioMotionProperties2, inInvMass2, inWorldSpaceAxis, total_lambda);
436 }
437
444 inline bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
445 {
446 EMotionType motion_type1 = ioBody1.GetMotionType();
447 MotionProperties *motion_properties1 = ioBody1.GetMotionPropertiesUnchecked();
448
449 EMotionType motion_type2 = ioBody2.GetMotionType();
450 MotionProperties *motion_properties2 = ioBody2.GetMotionPropertiesUnchecked();
451
452 // Dispatch to the correct templated form
453 switch (motion_type1)
454 {
455 case EMotionType::Dynamic:
456 switch (motion_type2)
457 {
458 case EMotionType::Dynamic:
459 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Dynamic>(motion_properties1, motion_properties1->GetInverseMass(), motion_properties2, motion_properties2->GetInverseMass(), inWorldSpaceAxis, inMinLambda, inMaxLambda);
460
461 case EMotionType::Kinematic:
462 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Kinematic>(motion_properties1, motion_properties1->GetInverseMass(), motion_properties2, 0.0f /* Unused */, inWorldSpaceAxis, inMinLambda, inMaxLambda);
463
464 case EMotionType::Static:
465 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Static>(motion_properties1, motion_properties1->GetInverseMass(), motion_properties2, 0.0f /* Unused */, inWorldSpaceAxis, inMinLambda, inMaxLambda);
466
467 default:
468 JPH_ASSERT(false);
469 break;
470 }
471 break;
472
473 case EMotionType::Kinematic:
474 JPH_ASSERT(motion_type2 == EMotionType::Dynamic);
475 return TemplatedSolveVelocityConstraint<EMotionType::Kinematic, EMotionType::Dynamic>(motion_properties1, 0.0f /* Unused */, motion_properties2, motion_properties2->GetInverseMass(), inWorldSpaceAxis, inMinLambda, inMaxLambda);
476
477 case EMotionType::Static:
478 JPH_ASSERT(motion_type2 == EMotionType::Dynamic);
479 return TemplatedSolveVelocityConstraint<EMotionType::Static, EMotionType::Dynamic>(motion_properties1, 0.0f /* Unused */, motion_properties2, motion_properties2->GetInverseMass(), inWorldSpaceAxis, inMinLambda, inMaxLambda);
480
481 default:
482 JPH_ASSERT(false);
483 break;
484 }
485
486 return false;
487 }
488
497 inline bool SolveVelocityConstraintWithMassOverride(Body &ioBody1, float inInvMass1, Body &ioBody2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
498 {
499 EMotionType motion_type1 = ioBody1.GetMotionType();
500 MotionProperties *motion_properties1 = ioBody1.GetMotionPropertiesUnchecked();
501
502 EMotionType motion_type2 = ioBody2.GetMotionType();
503 MotionProperties *motion_properties2 = ioBody2.GetMotionPropertiesUnchecked();
504
505 // Dispatch to the correct templated form
506 switch (motion_type1)
507 {
508 case EMotionType::Dynamic:
509 switch (motion_type2)
510 {
511 case EMotionType::Dynamic:
512 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Dynamic>(motion_properties1, inInvMass1, motion_properties2, inInvMass2, inWorldSpaceAxis, inMinLambda, inMaxLambda);
513
514 case EMotionType::Kinematic:
515 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Kinematic>(motion_properties1, inInvMass1, motion_properties2, 0.0f /* Unused */, inWorldSpaceAxis, inMinLambda, inMaxLambda);
516
517 case EMotionType::Static:
518 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Static>(motion_properties1, inInvMass1, motion_properties2, 0.0f /* Unused */, inWorldSpaceAxis, inMinLambda, inMaxLambda);
519
520 default:
521 JPH_ASSERT(false);
522 break;
523 }
524 break;
525
526 case EMotionType::Kinematic:
527 JPH_ASSERT(motion_type2 == EMotionType::Dynamic);
528 return TemplatedSolveVelocityConstraint<EMotionType::Kinematic, EMotionType::Dynamic>(motion_properties1, 0.0f /* Unused */, motion_properties2, inInvMass2, inWorldSpaceAxis, inMinLambda, inMaxLambda);
529
530 case EMotionType::Static:
531 JPH_ASSERT(motion_type2 == EMotionType::Dynamic);
532 return TemplatedSolveVelocityConstraint<EMotionType::Static, EMotionType::Dynamic>(motion_properties1, 0.0f /* Unused */, motion_properties2, inInvMass2, inWorldSpaceAxis, inMinLambda, inMaxLambda);
533
534 default:
535 JPH_ASSERT(false);
536 break;
537 }
538
539 return false;
540 }
541
548 inline bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inC, float inBaumgarte) const
549 {
550 // Only apply position constraint when the constraint is hard, otherwise the velocity bias will fix the constraint
551 if (inC != 0.0f && !mSpringPart.IsActive())
552 {
553 // Calculate lagrange multiplier (lambda) for Baumgarte stabilization:
554 //
555 // lambda = -K^-1 * beta / dt * C
556 //
557 // We should divide by inDeltaTime, but we should multiply by inDeltaTime in the Euler step below so they're cancelled out
558 float lambda = -mEffectiveMass * inBaumgarte * inC;
559
560 // Directly integrate velocity change for one time step
561 //
562 // Euler velocity integration:
563 // dv = M^-1 P
564 //
565 // Impulse:
566 // P = J^T lambda
567 //
568 // Euler position integration:
569 // x' = x + dv * dt
570 //
571 // Note we don't accumulate velocities for the stabilization. This is using the approach described in 'Modeling and
572 // Solving Constraints' by Erin Catto presented at GDC 2007. On slide 78 it is suggested to split up the Baumgarte
573 // stabilization for positional drift so that it does not actually add to the momentum. We combine an Euler velocity
574 // integrate + a position integrate and then discard the velocity change.
575 if (ioBody1.IsDynamic())
576 {
577 ioBody1.SubPositionStep((lambda * ioBody1.GetMotionProperties()->GetInverseMass()) * inWorldSpaceAxis);
578 ioBody1.SubRotationStep(lambda * Vec3::sLoadFloat3Unsafe(mInvI1_R1PlusUxAxis));
579 }
580 if (ioBody2.IsDynamic())
581 {
582 ioBody2.AddPositionStep((lambda * ioBody2.GetMotionProperties()->GetInverseMass()) * inWorldSpaceAxis);
583 ioBody2.AddRotationStep(lambda * Vec3::sLoadFloat3Unsafe(mInvI2_R2xAxis));
584 }
585 return true;
586 }
587
588 return false;
589 }
590
599 inline bool SolvePositionConstraintWithMassOverride(Body &ioBody1, float inInvMass1, Body &ioBody2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inC, float inBaumgarte) const
600 {
601 // Only apply position constraint when the constraint is hard, otherwise the velocity bias will fix the constraint
602 if (inC != 0.0f && !mSpringPart.IsActive())
603 {
604 // Calculate lagrange multiplier (lambda) for Baumgarte stabilization:
605 //
606 // lambda = -K^-1 * beta / dt * C
607 //
608 // We should divide by inDeltaTime, but we should multiply by inDeltaTime in the Euler step below so they're cancelled out
609 float lambda = -mEffectiveMass * inBaumgarte * inC;
610
611 // Directly integrate velocity change for one time step
612 //
613 // Euler velocity integration:
614 // dv = M^-1 P
615 //
616 // Impulse:
617 // P = J^T lambda
618 //
619 // Euler position integration:
620 // x' = x + dv * dt
621 //
622 // Note we don't accumulate velocities for the stabilization. This is using the approach described in 'Modeling and
623 // Solving Constraints' by Erin Catto presented at GDC 2007. On slide 78 it is suggested to split up the Baumgarte
624 // stabilization for positional drift so that it does not actually add to the momentum. We combine an Euler velocity
625 // integrate + a position integrate and then discard the velocity change.
626 if (ioBody1.IsDynamic())
627 {
628 ioBody1.SubPositionStep((lambda * inInvMass1) * inWorldSpaceAxis);
629 ioBody1.SubRotationStep(lambda * Vec3::sLoadFloat3Unsafe(mInvI1_R1PlusUxAxis));
630 }
631 if (ioBody2.IsDynamic())
632 {
633 ioBody2.AddPositionStep((lambda * inInvMass2) * inWorldSpaceAxis);
634 ioBody2.AddRotationStep(lambda * Vec3::sLoadFloat3Unsafe(mInvI2_R2xAxis));
635 }
636 return true;
637 }
638
639 return false;
640 }
641
643 inline void SetTotalLambda(float inLambda)
644 {
645 mTotalLambda = inLambda;
646 }
647
649 inline float GetTotalLambda() const
650 {
651 return mTotalLambda;
652 }
653
655 void SaveState(StateRecorder &inStream) const
656 {
657 inStream.Write(mTotalLambda);
658 }
659
662 {
663 inStream.Read(mTotalLambda);
664 }
665
666private:
667 Float3 mR1PlusUxAxis;
668 Float3 mR2xAxis;
669 Float3 mInvI1_R1PlusUxAxis;
670 Float3 mInvI2_R2xAxis;
671 float mEffectiveMass = 0.0f;
672 SpringPart mSpringPart;
673 float mTotalLambda = 0.0f;
674};
675
#define JPH_IF_DEBUG(...)
Definition: Core.h:473
#define JPH_NAMESPACE_END
Definition: Core.h:354
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:348
#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:649
bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
Definition: AxisConstraintPart.h:444
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:323
bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inC, float inBaumgarte) const
Definition: AxisConstraintPart.h:548
bool IsActive() const
Check if constraint is active.
Definition: AxisConstraintPart.h:343
void SetTotalLambda(float inLambda)
Override total lagrange multiplier, can be used to set the initial value for warm starting.
Definition: AxisConstraintPart.h:643
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:428
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:291
void WarmStart(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inWarmStartImpulseRatio)
Definition: AxisConstraintPart.h:362
void Deactivate()
Deactivate this constraint.
Definition: AxisConstraintPart.h:336
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:267
bool SolvePositionConstraintWithMassOverride(Body &ioBody1, float inInvMass1, Body &ioBody2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inC, float inBaumgarte) const
Definition: AxisConstraintPart.h:599
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:388
bool SolveVelocityConstraintWithMassOverride(Body &ioBody1, float inInvMass1, Body &ioBody2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
Definition: AxisConstraintPart.h:497
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:418
void SaveState(StateRecorder &inStream) const
Save state of this constraint part.
Definition: AxisConstraintPart.h:655
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:350
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:312
void CalculateConstraintProperties(const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias=0.0f)
Definition: AxisConstraintPart.h:243
void RestoreState(StateRecorder &inStream)
Restore state of this constraint part.
Definition: AxisConstraintPart.h:661
Definition: Body.h:35
const MotionProperties * GetMotionProperties() const
Access to the motion properties.
Definition: Body.h:228
EMotionType GetMotionType() const
Motion type of this body.
Definition: Body.h:99
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:90
void SubPositionStep(Vec3Arg inLinearVelocityTimesDeltaTime)
Definition: Body.h:263
Mat44 GetInverseInertia() const
Get inverse inertia tensor in world space.
Definition: Body.inl:129
void SubRotationStep(Vec3Arg inAngularVelocityTimesDeltaTime)
Definition: Body.inl:109
const MotionProperties * GetMotionPropertiesUnchecked() const
Access to the motion properties (version that does not check if the object is kinematic or dynamic)
Definition: Body.h:232
void AddPositionStep(Vec3Arg inLinearVelocityTimesDeltaTime)
Update position using an Euler step (used during position integrate & constraint solving)
Definition: Body.h:262
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:307
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:157
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:158
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:160
void AddAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition: MotionProperties.h:159
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:56
float mDamping
Definition: SpringSettings.h:63
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