Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Constraints/ConstraintPart/RotationEulerConstraintPart.h
9913 views
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
10
JPH_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
35
class RotationEulerConstraintPart
36
{
37
private:
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
61
public:
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
if (!mEffectiveMass.SetInversed3x3(mInvI1 + mInvI2))
147
Deactivate();
148
}
149
150
/// Deactivate this constraint
151
inline void Deactivate()
152
{
153
mEffectiveMass = Mat44::sZero();
154
mTotalLambda = Vec3::sZero();
155
}
156
157
/// Check if constraint is active
158
inline bool IsActive() const
159
{
160
return mEffectiveMass(3, 3) != 0.0f;
161
}
162
163
/// Must be called from the WarmStartVelocityConstraint call to apply the previous frame's impulses
164
inline void WarmStart(Body &ioBody1, Body &ioBody2, float inWarmStartImpulseRatio)
165
{
166
mTotalLambda *= inWarmStartImpulseRatio;
167
ApplyVelocityStep(ioBody1, ioBody2, mTotalLambda);
168
}
169
170
/// Iteratively update the velocity constraint. Makes sure d/dt C(...) = 0, where C is the constraint equation.
171
inline bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2)
172
{
173
// Calculate lagrange multiplier:
174
//
175
// lambda = -K^-1 (J v + b)
176
Vec3 lambda = mEffectiveMass.Multiply3x3(ioBody1.GetAngularVelocity() - ioBody2.GetAngularVelocity());
177
mTotalLambda += lambda;
178
return ApplyVelocityStep(ioBody1, ioBody2, lambda);
179
}
180
181
/// Iteratively update the position constraint. Makes sure C(...) = 0.
182
inline bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, QuatArg inInvInitialOrientation, float inBaumgarte) const
183
{
184
// Calculate difference in rotation
185
//
186
// The rotation should be:
187
//
188
// q2 = q1 r0
189
//
190
// But because of drift the actual rotation is
191
//
192
// q2 = diff q1 r0
193
// <=> diff = q2 r0^-1 q1^-1
194
//
195
// Where:
196
// q1 = current rotation of body 1
197
// q2 = current rotation of body 2
198
// diff = error that needs to be reduced to zero
199
Quat diff = ioBody2.GetRotation() * inInvInitialOrientation * ioBody1.GetRotation().Conjugated();
200
201
// A quaternion can be seen as:
202
//
203
// q = [sin(theta / 2) * v, cos(theta/2)]
204
//
205
// Where:
206
// v = rotation vector
207
// theta = rotation angle
208
//
209
// If we assume theta is small (error is small) then sin(x) = x so an approximation of the error angles is:
210
Vec3 error = 2.0f * diff.EnsureWPositive().GetXYZ();
211
if (error != Vec3::sZero())
212
{
213
// Calculate lagrange multiplier (lambda) for Baumgarte stabilization:
214
//
215
// lambda = -K^-1 * beta / dt * C
216
//
217
// We should divide by inDeltaTime, but we should multiply by inDeltaTime in the Euler step below so they're cancelled out
218
Vec3 lambda = -inBaumgarte * mEffectiveMass * error;
219
220
// Directly integrate velocity change for one time step
221
//
222
// Euler velocity integration:
223
// dv = M^-1 P
224
//
225
// Impulse:
226
// P = J^T lambda
227
//
228
// Euler position integration:
229
// x' = x + dv * dt
230
//
231
// Note we don't accumulate velocities for the stabilization. This is using the approach described in 'Modeling and
232
// Solving Constraints' by Erin Catto presented at GDC 2007. On slide 78 it is suggested to split up the Baumgarte
233
// stabilization for positional drift so that it does not actually add to the momentum. We combine an Euler velocity
234
// integrate + a position integrate and then discard the velocity change.
235
if (ioBody1.IsDynamic())
236
ioBody1.SubRotationStep(mInvI1.Multiply3x3(lambda));
237
if (ioBody2.IsDynamic())
238
ioBody2.AddRotationStep(mInvI2.Multiply3x3(lambda));
239
return true;
240
}
241
242
return false;
243
}
244
245
/// Return lagrange multiplier
246
Vec3 GetTotalLambda() const
247
{
248
return mTotalLambda;
249
}
250
251
/// Save state of this constraint part
252
void SaveState(StateRecorder &inStream) const
253
{
254
inStream.Write(mTotalLambda);
255
}
256
257
/// Restore state of this constraint part
258
void RestoreState(StateRecorder &inStream)
259
{
260
inStream.Read(mTotalLambda);
261
}
262
263
private:
264
Mat44 mInvI1;
265
Mat44 mInvI2;
266
Mat44 mEffectiveMass;
267
Vec3 mTotalLambda { Vec3::sZero() };
268
};
269
270
JPH_NAMESPACE_END
271
272