Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
DualAxisConstraintPart.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
9#include <Jolt/Math/Vector.h>
10#include <Jolt/Math/Matrix.h>
11
13
46{
47public:
48 using Vec2 = Vector<2>;
50
51private:
53 JPH_INLINE bool ApplyVelocityStep(Body &ioBody1, Body &ioBody2, Vec3Arg inN1, Vec3Arg inN2, const Vec2 &inLambda) const
54 {
55 // Apply impulse if delta is not zero
56 if (!inLambda.IsZero())
57 {
58 // Calculate velocity change due to constraint
59 //
60 // Impulse:
61 // P = J^T lambda
62 //
63 // Euler velocity integration:
64 // v' = v + M^-1 P
65 Vec3 impulse = inN1 * inLambda[0] + inN2 * inLambda[1];
66 if (ioBody1.IsDynamic())
67 {
69 mp1->SubLinearVelocityStep(mp1->GetInverseMass() * impulse);
70 mp1->SubAngularVelocityStep(mInvI1_R1PlusUxN1 * inLambda[0] + mInvI1_R1PlusUxN2 * inLambda[1]);
71 }
72 if (ioBody2.IsDynamic())
73 {
75 mp2->AddLinearVelocityStep(mp2->GetInverseMass() * impulse);
76 mp2->AddAngularVelocityStep(mInvI2_R2xN1 * inLambda[0] + mInvI2_R2xN2 * inLambda[1]);
77 }
78 return true;
79 }
80
81 return false;
82 }
83
85 inline void CalculateLagrangeMultiplier(const Body &inBody1, const Body &inBody2, Vec3Arg inN1, Vec3Arg inN2, Vec2 &outLambda) const
86 {
87 // Calculate lagrange multiplier:
88 //
89 // lambda = -K^-1 (J v + b)
90 Vec3 delta_lin = inBody1.GetLinearVelocity() - inBody2.GetLinearVelocity();
91 Vec2 jv;
92 jv[0] = inN1.Dot(delta_lin) + mR1PlusUxN1.Dot(inBody1.GetAngularVelocity()) - mR2xN1.Dot(inBody2.GetAngularVelocity());
93 jv[1] = inN2.Dot(delta_lin) + mR1PlusUxN2.Dot(inBody1.GetAngularVelocity()) - mR2xN2.Dot(inBody2.GetAngularVelocity());
94 outLambda = mEffectiveMass * jv;
95 }
96
97public:
100 inline void CalculateConstraintProperties(const Body &inBody1, Mat44Arg inRotation1, Vec3Arg inR1PlusU, const Body &inBody2, Mat44Arg inRotation2, Vec3Arg inR2, Vec3Arg inN1, Vec3Arg inN2)
101 {
102 JPH_ASSERT(inN1.IsNormalized(1.0e-5f));
103 JPH_ASSERT(inN2.IsNormalized(1.0e-5f));
104
105 // Calculate properties used during constraint solving
106 mR1PlusUxN1 = inR1PlusU.Cross(inN1);
107 mR1PlusUxN2 = inR1PlusU.Cross(inN2);
108 mR2xN1 = inR2.Cross(inN1);
109 mR2xN2 = inR2.Cross(inN2);
110
111 // Calculate effective mass: K^-1 = (J M^-1 J^T)^-1, eq 59
112 Mat22 inv_effective_mass;
113 if (inBody1.IsDynamic())
114 {
115 const MotionProperties *mp1 = inBody1.GetMotionProperties();
116 Mat44 inv_i1 = mp1->GetInverseInertiaForRotation(inRotation1);
117 mInvI1_R1PlusUxN1 = inv_i1.Multiply3x3(mR1PlusUxN1);
118 mInvI1_R1PlusUxN2 = inv_i1.Multiply3x3(mR1PlusUxN2);
119
120 inv_effective_mass(0, 0) = mp1->GetInverseMass() + mR1PlusUxN1.Dot(mInvI1_R1PlusUxN1);
121 inv_effective_mass(0, 1) = mR1PlusUxN1.Dot(mInvI1_R1PlusUxN2);
122 inv_effective_mass(1, 0) = mR1PlusUxN2.Dot(mInvI1_R1PlusUxN1);
123 inv_effective_mass(1, 1) = mp1->GetInverseMass() + mR1PlusUxN2.Dot(mInvI1_R1PlusUxN2);
124 }
125 else
126 {
127 JPH_IF_DEBUG(mInvI1_R1PlusUxN1 = Vec3::sNaN();)
128 JPH_IF_DEBUG(mInvI1_R1PlusUxN2 = Vec3::sNaN();)
129
130 inv_effective_mass = Mat22::sZero();
131 }
132
133 if (inBody2.IsDynamic())
134 {
135 const MotionProperties *mp2 = inBody2.GetMotionProperties();
136 Mat44 inv_i2 = mp2->GetInverseInertiaForRotation(inRotation2);
137 mInvI2_R2xN1 = inv_i2.Multiply3x3(mR2xN1);
138 mInvI2_R2xN2 = inv_i2.Multiply3x3(mR2xN2);
139
140 inv_effective_mass(0, 0) += mp2->GetInverseMass() + mR2xN1.Dot(mInvI2_R2xN1);
141 inv_effective_mass(0, 1) += mR2xN1.Dot(mInvI2_R2xN2);
142 inv_effective_mass(1, 0) += mR2xN2.Dot(mInvI2_R2xN1);
143 inv_effective_mass(1, 1) += mp2->GetInverseMass() + mR2xN2.Dot(mInvI2_R2xN2);
144 }
145 else
146 {
147 JPH_IF_DEBUG(mInvI2_R2xN1 = Vec3::sNaN();)
148 JPH_IF_DEBUG(mInvI2_R2xN2 = Vec3::sNaN();)
149 }
150
151 if (!mEffectiveMass.SetInversed(inv_effective_mass))
152 {
153 JPH_ASSERT(false, "Determinant is zero!");
154 Deactivate();
155 }
156 }
157
159 inline void Deactivate()
160 {
161 mEffectiveMass.SetZero();
162 mTotalLambda.SetZero();
163 }
164
166 inline bool IsActive() const
167 {
168 return !mEffectiveMass.IsZero();
169 }
170
173 inline void WarmStart(Body &ioBody1, Body &ioBody2, Vec3Arg inN1, Vec3Arg inN2, float inWarmStartImpulseRatio)
174 {
175 mTotalLambda *= inWarmStartImpulseRatio;
176 ApplyVelocityStep(ioBody1, ioBody2, inN1, inN2, mTotalLambda);
177 }
178
181 inline bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inN1, Vec3Arg inN2)
182 {
183 Vec2 lambda;
184 CalculateLagrangeMultiplier(ioBody1, ioBody2, inN1, inN2, lambda);
185
186 // Store accumulated lambda
187 mTotalLambda += lambda;
188
189 return ApplyVelocityStep(ioBody1, ioBody2, inN1, inN2, lambda);
190 }
191
194 inline bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inU, Vec3Arg inN1, Vec3Arg inN2, float inBaumgarte) const
195 {
196 Vec2 c;
197 c[0] = inU.Dot(inN1);
198 c[1] = inU.Dot(inN2);
199 if (!c.IsZero())
200 {
201 // Calculate lagrange multiplier (lambda) for Baumgarte stabilization:
202 //
203 // lambda = -K^-1 * beta / dt * C
204 //
205 // We should divide by inDeltaTime, but we should multiply by inDeltaTime in the Euler step below so they're cancelled out
206 Vec2 lambda = -inBaumgarte * (mEffectiveMass * c);
207
208 // Directly integrate velocity change for one time step
209 //
210 // Euler velocity integration:
211 // dv = M^-1 P
212 //
213 // Impulse:
214 // P = J^T lambda
215 //
216 // Euler position integration:
217 // x' = x + dv * dt
218 //
219 // Note we don't accumulate velocities for the stabilization. This is using the approach described in 'Modeling and
220 // Solving Constraints' by Erin Catto presented at GDC 2007. On slide 78 it is suggested to split up the Baumgarte
221 // stabilization for positional drift so that it does not actually add to the momentum. We combine an Euler velocity
222 // integrate + a position integrate and then discard the velocity change.
223 Vec3 impulse = inN1 * lambda[0] + inN2 * lambda[1];
224 if (ioBody1.IsDynamic())
225 {
226 ioBody1.SubPositionStep(ioBody1.GetMotionProperties()->GetInverseMass() * impulse);
227 ioBody1.SubRotationStep(mInvI1_R1PlusUxN1 * lambda[0] + mInvI1_R1PlusUxN2 * lambda[1]);
228 }
229 if (ioBody2.IsDynamic())
230 {
231 ioBody2.AddPositionStep(ioBody2.GetMotionProperties()->GetInverseMass() * impulse);
232 ioBody2.AddRotationStep(mInvI2_R2xN1 * lambda[0] + mInvI2_R2xN2 * lambda[1]);
233 }
234 return true;
235 }
236
237 return false;
238 }
239
241 inline void SetTotalLambda(const Vec2 &inLambda)
242 {
243 mTotalLambda = inLambda;
244 }
245
247 inline const Vec2 & GetTotalLambda() const
248 {
249 return mTotalLambda;
250 }
251
253 void SaveState(StateRecorder &inStream) const
254 {
255 inStream.Write(mTotalLambda);
256 }
257
260 {
261 inStream.Read(mTotalLambda);
262 }
263
264private:
265 Vec3 mR1PlusUxN1;
266 Vec3 mR1PlusUxN2;
267 Vec3 mR2xN1;
268 Vec3 mR2xN2;
269 Vec3 mInvI1_R1PlusUxN1;
270 Vec3 mInvI1_R1PlusUxN2;
271 Vec3 mInvI2_R2xN1;
272 Vec3 mInvI2_R2xN2;
273 Mat22 mEffectiveMass;
274 Vec2 mTotalLambda { Vec2::sZero() };
275};
276
#define JPH_IF_DEBUG(...)
Definition: Core.h:354
#define JPH_NAMESPACE_END
Definition: Core.h:240
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
Definition: Body.h:33
const MotionProperties * GetMotionProperties() const
Access to the motion properties.
Definition: Body.h:205
bool IsDynamic() const
Check if this body is dynamic, which means that it moves and forces can act on it.
Definition: Body.h:56
void AddRotationStep(Vec3Arg inAngularVelocityTimesDeltaTime)
Update rotation using an Euler step (using during position integrate & constraint solving)
Definition: Body.inl:75
void SubPositionStep(Vec3Arg inLinearVelocityTimesDeltaTime)
Definition: Body.h:237
Vec3 GetLinearVelocity() const
Get world space linear velocity of the center of mass (unit: m/s)
Definition: Body.h:109
void SubRotationStep(Vec3Arg inAngularVelocityTimesDeltaTime)
Definition: Body.inl:93
Vec3 GetAngularVelocity() const
Get world space angular velocity of the center of mass (unit: rad/s)
Definition: Body.h:118
void AddPositionStep(Vec3Arg inLinearVelocityTimesDeltaTime)
Update position using an Euler step (used during position integrate & constraint solving)
Definition: Body.h:236
Definition: DualAxisConstraintPart.h:46
bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inN1, Vec3Arg inN2)
Definition: DualAxisConstraintPart.h:181
void RestoreState(StateRecorder &inStream)
Restore state of this constraint part.
Definition: DualAxisConstraintPart.h:259
Matrix< 2, 2 > Mat22
Definition: DualAxisConstraintPart.h:49
void WarmStart(Body &ioBody1, Body &ioBody2, Vec3Arg inN1, Vec3Arg inN2, float inWarmStartImpulseRatio)
Definition: DualAxisConstraintPart.h:173
bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inU, Vec3Arg inN1, Vec3Arg inN2, float inBaumgarte) const
Definition: DualAxisConstraintPart.h:194
Vector< 2 > Vec2
Definition: DualAxisConstraintPart.h:48
void SaveState(StateRecorder &inStream) const
Save state of this constraint part.
Definition: DualAxisConstraintPart.h:253
const Vec2 & GetTotalLambda() const
Return lagrange multiplier.
Definition: DualAxisConstraintPart.h:247
void SetTotalLambda(const Vec2 &inLambda)
Override total lagrange multiplier, can be used to set the initial value for warm starting.
Definition: DualAxisConstraintPart.h:241
bool IsActive() const
Check if constraint is active.
Definition: DualAxisConstraintPart.h:166
void Deactivate()
Deactivate this constraint.
Definition: DualAxisConstraintPart.h:159
void CalculateConstraintProperties(const Body &inBody1, Mat44Arg inRotation1, Vec3Arg inR1PlusU, const Body &inBody2, Mat44Arg inRotation2, Vec3Arg inR2, Vec3Arg inN1, Vec3Arg inN2)
Definition: DualAxisConstraintPart.h:100
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
static Matrix sZero()
Definition: Matrix.h:32
bool SetInversed(const Matrix &inM)
Inverse matrix.
Definition: Matrix.h:200
void SetZero()
Zero matrix.
Definition: Matrix.h:26
bool IsZero() const
Check if this matrix consists of all zeros.
Definition: Matrix.h:35
The Body class only keeps track of state for static bodies, the MotionProperties class keeps the addi...
Definition: MotionProperties.h:20
void AddLinearVelocityStep(Vec3Arg inLinearVelocityChange)
Definition: MotionProperties.h:126
void SubLinearVelocityStep(Vec3Arg inLinearVelocityChange)
Definition: MotionProperties.h:127
float GetInverseMass() const
Get inverse mass (1 / mass). Should only be called on a dynamic object (static or kinematic bodies ha...
Definition: MotionProperties.h:80
void SubAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition: MotionProperties.h:129
Mat44 GetInverseInertiaForRotation(Mat44Arg inRotation) const
Get inverse inertia matrix ( ) for a given object rotation (translation will be ignored)....
Definition: MotionProperties.inl:81
void AddAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition: MotionProperties.h:128
Definition: StateRecorder.h:15
void Read(T &outT)
Read a primitive (e.g. float, int, etc.) from the binary stream.
Definition: StreamIn.h:27
void Write(const T &inT)
Write a primitive (e.g. float, int, etc.) to the binary stream.
Definition: StreamOut.h:24
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
static JPH_INLINE Vec3 sNaN()
Vector with all NaN's.
Definition: Vec3.inl:129
void SetZero()
Vector with all zeros.
Definition: Vector.h:22
bool IsZero() const
Test if vector consists of all zeros.
Definition: Vector.h:69
static Vector sZero()
Definition: Vector.h:28