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 Vec3 impulse = inLambda * inWorldSpaceAxis;
57 if (ioBody1.IsDynamic())
58 {
60 mp1->SubLinearVelocityStep(mp1->GetInverseMass() * impulse);
61 mp1->SubAngularVelocityStep(inLambda * Vec3::sLoadFloat3Unsafe(mInvI1_R1PlusUxAxis));
62 }
63 if (ioBody2.IsDynamic())
64 {
66 mp2->AddLinearVelocityStep(mp2->GetInverseMass() * impulse);
67 mp2->AddAngularVelocityStep(inLambda * Vec3::sLoadFloat3Unsafe(mInvI2_R2xAxis));
68 }
69 return true;
70 }
71
72 return false;
73 }
74
76 JPH_INLINE float CalculateInverseEffectiveMass(const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis)
77 {
78 JPH_ASSERT(inWorldSpaceAxis.IsNormalized(1.0e-5f));
79
80 // Calculate inverse effective mass: K = J M^-1 J^T
81 float inv_effective_mass;
82
83 if (!inBody1.IsStatic())
84 {
85 Vec3 r1_plus_u_x_axis = inR1PlusU.Cross(inWorldSpaceAxis);
86 r1_plus_u_x_axis.StoreFloat3(&mR1PlusUxAxis);
87
88 if (inBody1.IsDynamic())
89 {
91 Vec3 invi1_r1_plus_u_x_axis = mp1->MultiplyWorldSpaceInverseInertiaByVector(inBody1.GetRotation(), r1_plus_u_x_axis);
92 invi1_r1_plus_u_x_axis.StoreFloat3(&mInvI1_R1PlusUxAxis);
93
94 inv_effective_mass = mp1->GetInverseMass() + invi1_r1_plus_u_x_axis.Dot(r1_plus_u_x_axis);
95 }
96 else
97 {
98 JPH_IF_DEBUG(Vec3::sNaN().StoreFloat3(&mInvI1_R1PlusUxAxis);)
99
100 inv_effective_mass = 0.0f;
101 }
102 }
103 else
104 {
105 JPH_IF_DEBUG(Vec3::sNaN().StoreFloat3(&mR1PlusUxAxis);)
106 JPH_IF_DEBUG(Vec3::sNaN().StoreFloat3(&mInvI1_R1PlusUxAxis);)
107
108 inv_effective_mass = 0.0f;
109 }
110
111 if (!inBody2.IsStatic())
112 {
113 Vec3 r2_x_axis = inR2.Cross(inWorldSpaceAxis);
114 r2_x_axis.StoreFloat3(&mR2xAxis);
115
116 if (inBody2.IsDynamic())
117 {
118 const MotionProperties *mp2 = inBody2.GetMotionPropertiesUnchecked();
119 Vec3 invi2_r2_x_axis = mp2->MultiplyWorldSpaceInverseInertiaByVector(inBody2.GetRotation(), r2_x_axis);
120 invi2_r2_x_axis.StoreFloat3(&mInvI2_R2xAxis);
121
122 inv_effective_mass += mp2->GetInverseMass() + invi2_r2_x_axis.Dot(r2_x_axis);
123 }
124 else
125 {
126 JPH_IF_DEBUG(Vec3::sNaN().StoreFloat3(&mInvI2_R2xAxis);)
127 }
128 }
129 else
130 {
131 JPH_IF_DEBUG(Vec3::sNaN().StoreFloat3(&mR2xAxis);)
132 JPH_IF_DEBUG(Vec3::sNaN().StoreFloat3(&mInvI2_R2xAxis);)
133 }
134
135 return inv_effective_mass;
136 }
137
138public:
146 inline void CalculateConstraintProperties(const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias = 0.0f)
147 {
148 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
149
150 if (inv_effective_mass < FLT_MIN)
151 Deactivate();
152 else
153 {
154 mEffectiveMass = 1.0f / inv_effective_mass;
155 mSpringPart.CalculateSpringPropertiesWithBias(inBias);
156 }
157 }
158
170 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)
171 {
172 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
173
174 if (inv_effective_mass < FLT_MIN)
175 Deactivate();
176 else if (inFrequency > 0.0f)
177 mSpringPart.CalculateSpringPropertiesWithFrequencyAndDamping(inDeltaTime, inv_effective_mass, inBias, inC, inFrequency, inDamping, mEffectiveMass);
178 else
179 {
180 mEffectiveMass = 1.0f / inv_effective_mass;
181 mSpringPart.CalculateSpringPropertiesWithBias(inBias);
182 }
183 }
184
196 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)
197 {
198 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
199
200 if (inv_effective_mass < FLT_MIN)
201 Deactivate();
202 else if (inStiffness > 0.0f || inDamping > 0.0f)
203 mSpringPart.CalculateSpringPropertiesWithStiffnessAndDamping(inDeltaTime, inv_effective_mass, inBias, inC, inStiffness, inDamping, mEffectiveMass);
204 else
205 {
206 mEffectiveMass = 1.0f / inv_effective_mass;
207 mSpringPart.CalculateSpringPropertiesWithBias(inBias);
208 }
209 }
210
213 inline void CalculateConstraintPropertiesWithSettingsForLimit(float inDeltaTime, const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias, float inC, const SpringSettings &inSpringSettings)
214 {
215 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
216
217 if (inv_effective_mass < FLT_MIN)
218 Deactivate();
219 else if (!inSpringSettings.HasStiffness())
220 {
221 mEffectiveMass = 1.0f / inv_effective_mass;
222 mSpringPart.CalculateSpringPropertiesWithBias(inBias);
223 }
224 else
225 mSpringPart.CalculateSpringPropertiesWithSettings(inDeltaTime, inv_effective_mass, inBias, inC, inSpringSettings, mEffectiveMass);
226 }
227
230 inline void CalculateConstraintPropertiesWithSettingsForMotor(float inDeltaTime, const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias, float inC, const SpringSettings &inSpringSettings)
231 {
232 JPH_ASSERT(inSpringSettings.HasStiffnessOrDamping());
233
234 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
235
236 if (inv_effective_mass < FLT_MIN)
237 Deactivate();
238 else
239 mSpringPart.CalculateSpringPropertiesWithSettings(inDeltaTime, inv_effective_mass, inBias, inC, inSpringSettings, mEffectiveMass);
240 }
241
243 inline void Deactivate()
244 {
245 mEffectiveMass = 0.0f;
246 mTotalLambda = 0.0f;
247 }
248
250 inline bool IsActive() const
251 {
252 return mEffectiveMass != 0.0f;
253 }
254
260 inline void WarmStart(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inWarmStartImpulseRatio)
261 {
262 mTotalLambda *= inWarmStartImpulseRatio;
263
264 ApplyVelocityStep(ioBody1, ioBody2, inWorldSpaceAxis, mTotalLambda);
265 }
266
273 inline bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
274 {
275 // Calculate jacobian multiplied by linear velocity
276 Vec3 acc;
277 if (!ioBody1.IsStatic())
278 {
279 const MotionProperties *mp1 = ioBody1.GetMotionPropertiesUnchecked();
280
281 if (!ioBody2.IsStatic())
282 {
283 const MotionProperties *mp2 = ioBody2.GetMotionPropertiesUnchecked();
284 acc = inWorldSpaceAxis * (mp1->GetLinearVelocity() - mp2->GetLinearVelocity());
285 acc -= Vec3::sLoadFloat3Unsafe(mR2xAxis) * mp2->GetAngularVelocity();
286 }
287 else
288 acc = inWorldSpaceAxis * mp1->GetLinearVelocity();
289
290 acc += Vec3::sLoadFloat3Unsafe(mR1PlusUxAxis) * mp1->GetAngularVelocity();
291 }
292 else
293 {
294 JPH_ASSERT(!ioBody2.IsStatic());
295 const MotionProperties *mp2 = ioBody2.GetMotionPropertiesUnchecked();
296 acc = inWorldSpaceAxis * (-mp2->GetLinearVelocity());
297 acc -= Vec3::sLoadFloat3Unsafe(mR2xAxis) * mp2->GetAngularVelocity();
298 }
299
300 float jv = acc.ReduceSum();
301
302 // Lagrange multiplier is:
303 //
304 // lambda = -K^-1 (J v + b)
305 float lambda = mEffectiveMass * (jv - mSpringPart.GetBias(mTotalLambda));
306 float new_lambda = Clamp(mTotalLambda + lambda, inMinLambda, inMaxLambda); // Clamp impulse
307 lambda = new_lambda - mTotalLambda; // Lambda potentially got clamped, calculate the new impulse to apply
308 mTotalLambda = new_lambda; // Store accumulated impulse
309
310 return ApplyVelocityStep(ioBody1, ioBody2, inWorldSpaceAxis, lambda);
311 }
312
314 float GetTotalLambda() const
315 {
316 return mTotalLambda;
317 }
318
325 inline bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inC, float inBaumgarte) const
326 {
327 // Only apply position constraint when the constraint is hard, otherwise the velocity bias will fix the constraint
328 if (inC != 0.0f && !mSpringPart.IsActive())
329 {
330 // Calculate lagrange multiplier (lambda) for Baumgarte stabilization:
331 //
332 // lambda = -K^-1 * beta / dt * C
333 //
334 // We should divide by inDeltaTime, but we should multiply by inDeltaTime in the Euler step below so they're cancelled out
335 float lambda = -mEffectiveMass * inBaumgarte * inC;
336
337 // Directly integrate velocity change for one time step
338 //
339 // Euler velocity integration:
340 // dv = M^-1 P
341 //
342 // Impulse:
343 // P = J^T lambda
344 //
345 // Euler position integration:
346 // x' = x + dv * dt
347 //
348 // Note we don't accumulate velocities for the stabilization. This is using the approach described in 'Modeling and
349 // Solving Constraints' by Erin Catto presented at GDC 2007. On slide 78 it is suggested to split up the Baumgarte
350 // stabilization for positional drift so that it does not actually add to the momentum. We combine an Euler velocity
351 // integrate + a position integrate and then discard the velocity change.
352 Vec3 impulse = lambda * inWorldSpaceAxis;
353 if (ioBody1.IsDynamic())
354 {
355 ioBody1.SubPositionStep(ioBody1.GetMotionPropertiesUnchecked()->GetInverseMass() * impulse);
356 ioBody1.SubRotationStep(lambda * Vec3::sLoadFloat3Unsafe(mInvI1_R1PlusUxAxis));
357 }
358 if (ioBody2.IsDynamic())
359 {
360 ioBody2.AddPositionStep(ioBody2.GetMotionPropertiesUnchecked()->GetInverseMass() * impulse);
361 ioBody2.AddRotationStep(lambda * Vec3::sLoadFloat3Unsafe(mInvI2_R2xAxis));
362 }
363 return true;
364 }
365
366 return false;
367 }
368
370 void SaveState(StateRecorder &inStream) const
371 {
372 inStream.Write(mTotalLambda);
373 }
374
377 {
378 inStream.Read(mTotalLambda);
379 }
380
381private:
382 Float3 mR1PlusUxAxis;
383 Float3 mR2xAxis;
384 Float3 mInvI1_R1PlusUxAxis;
385 Float3 mInvI2_R2xAxis;
386 float mEffectiveMass = 0.0f;
387 SpringPart mSpringPart;
388 float mTotalLambda = 0.0f;
389};
390
#define JPH_IF_DEBUG(...)
Definition Core.h:633
#define JPH_NAMESPACE_END
Definition Core.h:487
#define JPH_NAMESPACE_BEGIN
Definition Core.h:481
#define JPH_ASSERT(...)
Definition IssueReporting.h:28
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:314
bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
Definition AxisConstraintPart.h:273
bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inC, float inBaumgarte) const
Definition AxisConstraintPart.h:325
bool IsActive() const
Check if constraint is active.
Definition AxisConstraintPart.h:250
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:230
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:170
void WarmStart(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inWarmStartImpulseRatio)
Definition AxisConstraintPart.h:260
void Deactivate()
Deactivate this constraint.
Definition AxisConstraintPart.h:243
void SaveState(StateRecorder &inStream) const
Save state of this constraint part.
Definition AxisConstraintPart.h:370
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:213
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:196
void CalculateConstraintProperties(const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias=0.0f)
Definition AxisConstraintPart.h:146
void RestoreState(StateRecorder &inStream)
Restore state of this constraint part.
Definition AxisConstraintPart.h:376
Definition Body.h:39
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:367
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:276
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:329
void AddPositionStep(Vec3Arg inLinearVelocityTimesDeltaTime)
Update position using an Euler step (used during position integrate & constraint solving)
Definition Body.h:366
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:236
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:56
void SubLinearVelocityStep(Vec3Arg inLinearVelocityChange)
Definition MotionProperties.h:237
float GetInverseMass() const
Get inverse mass (1 / mass). Should only be called on a dynamic object (static or kinematic bodies ha...
Definition MotionProperties.h:105
void SubAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition MotionProperties.h:248
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:247
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:931
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition Vec3.inl:855
JPH_INLINE bool IsNormalized(float inTolerance=1.0e-6f) const
Test if length^2 of this vector is within the range [1 - inTolerance, 1 + inTolerance].
Definition Vec3.inl:1030
JPH_INLINE float ReduceSum() const
Get the sum of X, Y and Z.
Definition Vec3.inl:898
JPH_INLINE void StoreFloat3(Float3 *outV) const
Store 3 floats to memory.
Definition Vec3.inl:1055
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