Skip to content
0.2.0-dev Download ZIP
283 lines ·9.5 KB Raw
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
7#include <Jolt/Physics/Body/Body.h>
8#include <Jolt/Physics/StateRecorder.h>
9
10JPH_NAMESPACE_BEGIN
11
12/// Constrains rotation around all axis so that only translation is allowed
13///
14/// Based on: "Constraints Derivation for Rigid Body Simulation in 3D" - Daniel Chappuis, section 2.5.1
15///
16/// Constraint equation (eq 129):
17///
18/// \f[C = \begin{bmatrix}\Delta\theta_x, \Delta\theta_y, \Delta\theta_z\end{bmatrix}\f]
19///
20/// Jacobian (eq 131):
21///
22/// \f[J = \begin{bmatrix}0 & -E & 0 & E\end{bmatrix}\f]
23///
24/// Used terms (here and below, everything in world space):\n
25/// delta_theta_* = difference in rotation between initial rotation of bodies 1 and 2.\n
26/// x1, x2 = center of mass for the bodies.\n
27/// v = [v1, w1, v2, w2].\n
28/// v1, v2 = linear velocity of body 1 and 2.\n
29/// w1, w2 = angular velocity of body 1 and 2.\n
30/// M = mass matrix, a diagonal matrix of the mass and inertia with diagonal [m1, I1, m2, I2].\n
31/// \f$K^{-1} = \left( J M^{-1} J^T \right)^{-1}\f$ = effective mass.\n
32/// b = velocity bias.\n
33/// \f$\beta\f$ = baumgarte constant.\n
34/// E = identity matrix.\n
35class RotationEulerConstraintPart
36{
37private:
38 /// Internal helper function to update velocities of bodies after Lagrange multiplier is calculated
39 JPH_INLINE bool ApplyVelocityStep(Body &ioBody1, Body &ioBody2, Vec3Arg inLambda) const
40 {
41 // Apply impulse if delta is not zero
42 if (inLambda != Vec3::sZero())
43 {
44 // Calculate velocity change due to constraint
45 //
46 // Impulse:
47 // P = J^T lambda
48 //
49 // Euler velocity integration:
50 // v' = v + M^-1 P
51 if (ioBody1.IsDynamic())
52 ioBody1.GetMotionProperties()->SubAngularVelocityStep(mInvI1.Multiply3x3(inLambda));
53 if (ioBody2.IsDynamic())
54 ioBody2.GetMotionProperties()->AddAngularVelocityStep(mInvI2.Multiply3x3(inLambda));
55 return true;
56 }
57
58 return false;
59 }
60
61public:
62 /// Return inverse of initial rotation from body 1 to body 2 in body 1 space
63 static Quat sGetInvInitialOrientation(const Body &inBody1, const Body &inBody2)
64 {
65 // q20 = q10 r0
66 // <=> r0 = q10^-1 q20
67 // <=> r0^-1 = q20^-1 q10
68 //
69 // where:
70 //
71 // q20 = initial orientation of body 2
72 // q10 = initial orientation of body 1
73 // r0 = initial rotation from body 1 to body 2
74 return inBody2.GetRotation().Conjugated() * inBody1.GetRotation();
75 }
76
77 /// @brief Return inverse of initial rotation from body 1 to body 2 in body 1 space
78 /// @param inAxisX1 Reference axis X for body 1
79 /// @param inAxisY1 Reference axis Y for body 1
80 /// @param inAxisX2 Reference axis X for body 2
81 /// @param inAxisY2 Reference axis Y for body 2
82 static Quat sGetInvInitialOrientationXY(Vec3Arg inAxisX1, Vec3Arg inAxisY1, Vec3Arg inAxisX2, Vec3Arg inAxisY2)
83 {
84 // Store inverse of initial rotation from body 1 to body 2 in body 1 space:
85 //
86 // q20 = q10 r0
87 // <=> r0 = q10^-1 q20
88 // <=> r0^-1 = q20^-1 q10
89 //
90 // where:
91 //
92 // q10, q20 = world space initial orientation of body 1 and 2
93 // r0 = initial rotation from body 1 to body 2 in local space of body 1
94 //
95 // We can also write this in terms of the constraint matrices:
96 //
97 // q20 c2 = q10 c1
98 // <=> q20 = q10 c1 c2^-1
99 // => r0 = c1 c2^-1
100 // <=> r0^-1 = c2 c1^-1
101 //
102 // where:
103 //
104 // c1, c2 = matrix that takes us from body 1 and 2 COM to constraint space 1 and 2
105 if (inAxisX1 == inAxisX2 && inAxisY1 == inAxisY2)
106 {
107 // Axis are the same -> identity transform
108 return Quat::sIdentity();
109 }
110 else
111 {
112 Mat44 constraint1(Vec4(inAxisX1, 0), Vec4(inAxisY1, 0), Vec4(inAxisX1.Cross(inAxisY1), 0), Vec4(0, 0, 0, 1));
113 Mat44 constraint2(Vec4(inAxisX2, 0), Vec4(inAxisY2, 0), Vec4(inAxisX2.Cross(inAxisY2), 0), Vec4(0, 0, 0, 1));
114 return constraint2.GetQuaternion() * constraint1.GetQuaternion().Conjugated();
115 }
116 }
117
118 /// @brief Return inverse of initial rotation from body 1 to body 2 in body 1 space
119 /// @param inAxisX1 Reference axis X for body 1
120 /// @param inAxisZ1 Reference axis Z for body 1
121 /// @param inAxisX2 Reference axis X for body 2
122 /// @param inAxisZ2 Reference axis Z for body 2
123 static Quat sGetInvInitialOrientationXZ(Vec3Arg inAxisX1, Vec3Arg inAxisZ1, Vec3Arg inAxisX2, Vec3Arg inAxisZ2)
124 {
125 // See comment at sGetInvInitialOrientationXY
126 if (inAxisX1 == inAxisX2 && inAxisZ1 == inAxisZ2)
127 {
128 return Quat::sIdentity();
129 }
130 else
131 {
132 Mat44 constraint1(Vec4(inAxisX1, 0), Vec4(inAxisZ1.Cross(inAxisX1), 0), Vec4(inAxisZ1, 0), Vec4(0, 0, 0, 1));
133 Mat44 constraint2(Vec4(inAxisX2, 0), Vec4(inAxisZ2.Cross(inAxisX2), 0), Vec4(inAxisZ2, 0), Vec4(0, 0, 0, 1));
134 return constraint2.GetQuaternion() * constraint1.GetQuaternion().Conjugated();
135 }
136 }
137
138 /// Calculate properties used during the functions below
139 inline void CalculateConstraintProperties(const Body &inBody1, Mat44Arg inRotation1, const Body &inBody2, Mat44Arg inRotation2)
140 {
141 // Calculate properties used during constraint solving
142 mInvI1 = inBody1.IsDynamic()? inBody1.GetMotionProperties()->GetInverseInertiaForRotation(inRotation1) : Mat44::sZero();
143 mInvI2 = inBody2.IsDynamic()? inBody2.GetMotionProperties()->GetInverseInertiaForRotation(inRotation2) : Mat44::sZero();
144
145 // Calculate effective mass: K^-1 = (J M^-1 J^T)^-1
146 Mat44 inertia_sum = mInvI1 + mInvI2;
147 if (!mEffectiveMass.SetInversed3x3(inertia_sum))
148 {
149 // If a column is zero, the axis is locked and we set the column to identity.
150 // This does not matter because any impulse will always be multiplied with mInvI1 or mInvI2 which will result in zero for the locked coordinate.
151 Vec4 zero = Vec4::sZero();
152 if (inertia_sum.GetColumn4(0) == zero)
153 inertia_sum.SetColumn4(0, Vec4(1, 0, 0, 0));
154 if (inertia_sum.GetColumn4(1) == zero)
155 inertia_sum.SetColumn4(1, Vec4(0, 1, 0, 0));
156 if (inertia_sum.GetColumn4(2) == zero)
157 inertia_sum.SetColumn4(2, Vec4(0, 0, 1, 0));
158 if (!mEffectiveMass.SetInversed3x3(inertia_sum))
159 Deactivate();
160 }
161 }
162
163 /// Deactivate this constraint
164 inline void Deactivate()
165 {
166 mEffectiveMass = Mat44::sZero();
167 mTotalLambda = Vec3::sZero();
168 }
169
170 /// Check if constraint is active
171 inline bool IsActive() const
172 {
173 return mEffectiveMass(3, 3) != 0.0f;
174 }
175
176 /// Must be called from the WarmStartVelocityConstraint call to apply the previous frame's impulses
177 inline void WarmStart(Body &ioBody1, Body &ioBody2, float inWarmStartImpulseRatio)
178 {
179 mTotalLambda *= inWarmStartImpulseRatio;
180 ApplyVelocityStep(ioBody1, ioBody2, mTotalLambda);
181 }
182
183 /// Iteratively update the velocity constraint. Makes sure d/dt C(...) = 0, where C is the constraint equation.
184 inline bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2)
185 {
186 // Calculate lagrange multiplier:
187 //
188 // lambda = -K^-1 (J v + b)
189 Vec3 lambda = mEffectiveMass.Multiply3x3(ioBody1.GetAngularVelocity() - ioBody2.GetAngularVelocity());
190 mTotalLambda += lambda;
191 return ApplyVelocityStep(ioBody1, ioBody2, lambda);
192 }
193
194 /// Iteratively update the position constraint. Makes sure C(...) = 0.
195 inline bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, QuatArg inInvInitialOrientation, float inBaumgarte) const
196 {
197 // Calculate difference in rotation
198 //
199 // The rotation should be:
200 //
201 // q2 = q1 r0
202 //
203 // But because of drift the actual rotation is
204 //
205 // q2 = diff q1 r0
206 // <=> diff = q2 r0^-1 q1^-1
207 //
208 // Where:
209 // q1 = current rotation of body 1
210 // q2 = current rotation of body 2
211 // diff = error that needs to be reduced to zero
212 Quat diff = ioBody2.GetRotation() * inInvInitialOrientation * ioBody1.GetRotation().Conjugated();
213
214 // A quaternion can be seen as:
215 //
216 // q = [sin(theta / 2) * v, cos(theta/2)]
217 //
218 // Where:
219 // v = rotation vector
220 // theta = rotation angle
221 //
222 // If we assume theta is small (error is small) then sin(x) = x so an approximation of the error angles is:
223 Vec3 error = 2.0f * diff.EnsureWPositive().GetXYZ();
224 if (error != Vec3::sZero())
225 {
226 // Calculate lagrange multiplier (lambda) for Baumgarte stabilization:
227 //
228 // lambda = -K^-1 * beta / dt * C
229 //
230 // We should divide by inDeltaTime, but we should multiply by inDeltaTime in the Euler step below so they're cancelled out
231 Vec3 lambda = -inBaumgarte * mEffectiveMass * error;
232
233 // Directly integrate velocity change for one time step
234 //
235 // Euler velocity integration:
236 // dv = M^-1 P
237 //
238 // Impulse:
239 // P = J^T lambda
240 //
241 // Euler position integration:
242 // x' = x + dv * dt
243 //
244 // Note we don't accumulate velocities for the stabilization. This is using the approach described in 'Modeling and
245 // Solving Constraints' by Erin Catto presented at GDC 2007. On slide 78 it is suggested to split up the Baumgarte
246 // stabilization for positional drift so that it does not actually add to the momentum. We combine an Euler velocity
247 // integrate + a position integrate and then discard the velocity change.
248 if (ioBody1.IsDynamic())
249 ioBody1.SubRotationStep(mInvI1.Multiply3x3(lambda));
250 if (ioBody2.IsDynamic())
251 ioBody2.AddRotationStep(mInvI2.Multiply3x3(lambda));
252 return true;
253 }
254
255 return false;
256 }
257
258 /// Return lagrange multiplier
259 Vec3 GetTotalLambda() const
260 {
261 return mTotalLambda;
262 }
263
264 /// Save state of this constraint part
265 void SaveState(StateRecorder &inStream) const
266 {
267 inStream.Write(mTotalLambda);
268 }
269
270 /// Restore state of this constraint part
271 void RestoreState(StateRecorder &inStream)
272 {
273 inStream.Read(mTotalLambda);
274 }
275
276private:
277 Mat44 mInvI1;
278 Mat44 mInvI2;
279 Mat44 mEffectiveMass;
280 Vec3 mTotalLambda { Vec3::sZero() };
281};
282
283JPH_NAMESPACE_END