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(&mR2xAxis);)
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
176 mSpringPart.CalculateSpringPropertiesWithFrequencyAndDamping(inDeltaTime, inv_effective_mass, inBias, inC, inFrequency, inDamping, mEffectiveMass);
177 }
178
190 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)
191 {
192 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
193
194 if (inv_effective_mass == 0.0f)
195 Deactivate();
196 else
197 mSpringPart.CalculateSpringPropertiesWithStiffnessAndDamping(inDeltaTime, inv_effective_mass, inBias, inC, inStiffness, inDamping, mEffectiveMass);
198 }
199
201 inline void CalculateConstraintPropertiesWithSettings(float inDeltaTime, const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias, float inC, const SpringSettings &inSpringSettings)
202 {
203 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
204
205 if (inv_effective_mass == 0.0f)
206 Deactivate();
207 else if (inSpringSettings.mMode == ESpringMode::FrequencyAndDamping)
208 mSpringPart.CalculateSpringPropertiesWithFrequencyAndDamping(inDeltaTime, inv_effective_mass, inBias, inC, inSpringSettings.mFrequency, inSpringSettings.mDamping, mEffectiveMass);
209 else
210 mSpringPart.CalculateSpringPropertiesWithStiffnessAndDamping(inDeltaTime, inv_effective_mass, inBias, inC, inSpringSettings.mStiffness, inSpringSettings.mDamping, mEffectiveMass);
211 }
212
214 inline void Deactivate()
215 {
216 mEffectiveMass = 0.0f;
217 mTotalLambda = 0.0f;
218 }
219
221 inline bool IsActive() const
222 {
223 return mEffectiveMass != 0.0f;
224 }
225
231 inline void WarmStart(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inWarmStartImpulseRatio)
232 {
233 mTotalLambda *= inWarmStartImpulseRatio;
234
235 ApplyVelocityStep(ioBody1, ioBody2, inWorldSpaceAxis, mTotalLambda);
236 }
237
244 inline bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
245 {
246 const MotionProperties *mp1 = ioBody1.GetMotionPropertiesUnchecked();
247 const MotionProperties *mp2 = ioBody2.GetMotionPropertiesUnchecked();
248
249 // Calculate jacobian multiplied by linear velocity
250 float jv;
251 if (!ioBody1.IsStatic())
252 {
253 if (!ioBody2.IsStatic())
254 jv = inWorldSpaceAxis.Dot(mp1->GetLinearVelocity() - mp2->GetLinearVelocity());
255 else
256 jv = inWorldSpaceAxis.Dot(mp1->GetLinearVelocity());
257 }
258 else
259 {
260 JPH_ASSERT(!ioBody2.IsStatic());
261 jv = inWorldSpaceAxis.Dot(-mp2->GetLinearVelocity());
262 }
263
264 // Calculate jacobian multiplied by angular velocity
265 if (!ioBody1.IsStatic())
266 jv += Vec3::sLoadFloat3Unsafe(mR1PlusUxAxis).Dot(mp1->GetAngularVelocity());
267 if (!ioBody2.IsStatic())
268 jv -= Vec3::sLoadFloat3Unsafe(mR2xAxis).Dot(mp2->GetAngularVelocity());
269
270 // Lagrange multiplier is:
271 //
272 // lambda = -K^-1 (J v + b)
273 float lambda = mEffectiveMass * (jv - mSpringPart.GetBias(mTotalLambda));
274 float new_lambda = Clamp(mTotalLambda + lambda, inMinLambda, inMaxLambda); // Clamp impulse
275 lambda = new_lambda - mTotalLambda; // Lambda potentially got clamped, calculate the new impulse to apply
276 mTotalLambda = new_lambda; // Store accumulated impulse
277
278 return ApplyVelocityStep(ioBody1, ioBody2, inWorldSpaceAxis, lambda);
279 }
280
282 float GetTotalLambda() const
283 {
284 return mTotalLambda;
285 }
286
293 inline bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inC, float inBaumgarte) const
294 {
295 // Only apply position constraint when the constraint is hard, otherwise the velocity bias will fix the constraint
296 if (inC != 0.0f && !mSpringPart.IsActive())
297 {
298 // Calculate lagrange multiplier (lambda) for Baumgarte stabilization:
299 //
300 // lambda = -K^-1 * beta / dt * C
301 //
302 // We should divide by inDeltaTime, but we should multiply by inDeltaTime in the Euler step below so they're cancelled out
303 float lambda = -mEffectiveMass * inBaumgarte * inC;
304
305 // Directly integrate velocity change for one time step
306 //
307 // Euler velocity integration:
308 // dv = M^-1 P
309 //
310 // Impulse:
311 // P = J^T lambda
312 //
313 // Euler position integration:
314 // x' = x + dv * dt
315 //
316 // Note we don't accumulate velocities for the stabilization. This is using the approach described in 'Modeling and
317 // Solving Constraints' by Erin Catto presented at GDC 2007. On slide 78 it is suggested to split up the Baumgarte
318 // stabilization for positional drift so that it does not actually add to the momentum. We combine an Euler velocity
319 // integrate + a position integrate and then discard the velocity change.
320 if (ioBody1.IsDynamic())
321 {
322 ioBody1.SubPositionStep((lambda * ioBody1.GetMotionProperties()->GetInverseMass()) * inWorldSpaceAxis);
323 ioBody1.SubRotationStep(lambda * Vec3::sLoadFloat3Unsafe(mInvI1_R1PlusUxAxis));
324 }
325 if (ioBody2.IsDynamic())
326 {
327 ioBody2.AddPositionStep((lambda * ioBody2.GetMotionProperties()->GetInverseMass()) * inWorldSpaceAxis);
328 ioBody2.AddRotationStep(lambda * Vec3::sLoadFloat3Unsafe(mInvI2_R2xAxis));
329 }
330 return true;
331 }
332
333 return false;
334 }
335
337 void SaveState(StateRecorder &inStream) const
338 {
339 inStream.Write(mTotalLambda);
340 }
341
344 {
345 inStream.Read(mTotalLambda);
346 }
347
348private:
349 Float3 mR1PlusUxAxis;
350 Float3 mR2xAxis;
351 Float3 mInvI1_R1PlusUxAxis;
352 Float3 mInvI2_R2xAxis;
353 float mEffectiveMass = 0.0f;
354 SpringPart mSpringPart;
355 float mTotalLambda = 0.0f;
356};
357
#define JPH_IF_DEBUG(...)
Definition Core.h:569
#define JPH_NAMESPACE_END
Definition Core.h:428
#define JPH_NAMESPACE_BEGIN
Definition Core.h:422
#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:48
@ FrequencyAndDamping
Frequency and damping are specified.
Definition AxisConstraintPart.h:42
float GetTotalLambda() const
Return lagrange multiplier.
Definition AxisConstraintPart.h:282
bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
Definition AxisConstraintPart.h:244
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:201
bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inC, float inBaumgarte) const
Definition AxisConstraintPart.h:293
bool IsActive() const
Check if constraint is active.
Definition AxisConstraintPart.h:221
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:231
void Deactivate()
Deactivate this constraint.
Definition AxisConstraintPart.h:214
void SaveState(StateRecorder &inStream) const
Save state of this constraint part.
Definition AxisConstraintPart.h:337
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:190
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:343
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:343
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:342
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:217
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:218
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:221
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:220
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: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:943
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition Vec3.inl:841
JPH_INLINE bool IsNormalized(float inTolerance=1.0e-6f) const
Test if vector is normalized.
Definition Vec3.inl:1128
JPH_INLINE void StoreFloat3(Float3 *outV) const
Store 3 floats to memory.
Definition Vec3.inl:1153
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