Debian Codegraph @

thirdparty/jolt_physics/Jolt/Physics/Constraints/ConstraintPart/RackAndPinionConstraintPart.h

raw symbols SCIP
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 /// Constraint that constrains a rotation to a translation
13 ///
14 /// Constraint equation:
15 ///
16 /// C = Theta(t) - r d(t)
17 ///
18 /// Derivative:
19 ///
20 /// d/dt C = 0
21 /// <=> w1 . a - r v2 . b = 0
22 ///
23 /// Jacobian:
24 ///
25 /// \f[J = \begin{bmatrix}0 & a^T & -r b^T & 0\end{bmatrix}\f]
26 ///
27 /// Used terms (here and below, everything in world space):\n
28 /// a = axis around which body 1 rotates (normalized).\n
29 /// b = axis along which body 2 slides (normalized).\n
30 /// Theta(t) = rotation around a of body 1.\n
31 /// d(t) = distance body 2 slides.\n
32 /// r = ratio between rotation and translation.\n
33 /// v = [v1, w1, v2, w2].\n
34 /// v1, v2 = linear velocity of body 1 and 2.\n
35 /// w1, w2 = angular velocity of body 1 and 2.\n
36 /// M = mass matrix, a diagonal matrix of the mass and inertia with diagonal [m1, I1, m2, I2].\n
37 /// \f$K^{-1} = \left( J M^{-1} J^T \right)^{-1}\f$ = effective mass.\n
38 /// \f$\beta\f$ = baumgarte constant.
39 class RackAndPinionConstraintPart
40 {
41 /// Internal helper function to update velocities of bodies after Lagrange multiplier is calculated
42 JPH_INLINE bool ApplyVelocityStep(Body &ioBody1, Body &ioBody2, float inLambda) const
43 {
44 // Apply impulse if delta is not zero
45 if (inLambda != 0.0f)
46 {
47 // Calculate velocity change due to constraint
48 //
49 // Impulse:
50 // P = J^T lambda
51 //
52 // Euler velocity integration:
53 // v' = v + M^-1 P
54 ioBody1.GetMotionProperties()->AddAngularVelocityStep(inLambda * mInvI1_A);
55 ioBody2.GetMotionProperties()->SubLinearVelocityStep(inLambda * mRatio_InvM2_B);
56 return true;
57 }
58
59 return false;
60 }
61
62 public:
63 /// Calculate properties used during the functions below
64 /// @param inBody1 The first body that this constraint is attached to
65 /// @param inBody2 The second body that this constraint is attached to
66 /// @param inWorldSpaceHingeAxis The axis around which body 1 rotates
67 /// @param inWorldSpaceSliderAxis The axis along which body 2 slides
68 /// @param inRatio The ratio between rotation and translation
69 inline void CalculateConstraintProperties(const Body &inBody1, Vec3Arg inWorldSpaceHingeAxis, const Body &inBody2, Vec3Arg inWorldSpaceSliderAxis, float inRatio)
70 {
71 JPH_ASSERT(inWorldSpaceHingeAxis.IsNormalized(1.0e-4f));
72 JPH_ASSERT(inWorldSpaceSliderAxis.IsNormalized(1.0e-4f));
73
74 // Calculate: I1^-1 a
75 mInvI1_A = inBody1.GetMotionProperties()->MultiplyWorldSpaceInverseInertiaByVector(inBody1.GetRotation(), inWorldSpaceHingeAxis);
76
77 // Calculate: r/m2 b
78 float inv_m2 = inBody2.GetMotionProperties()->GetInverseMass();
79 mRatio_InvM2_B = inRatio * inv_m2 * inWorldSpaceSliderAxis;
80
81 // K^-1 = 1 / (J M^-1 J^T) = 1 / (a^T I1^-1 a + 1/m2 * r^2 * b . b)
82 float inv_effective_mass = (inWorldSpaceHingeAxis.Dot(mInvI1_A) + inv_m2 * Square(inRatio));
83 if (inv_effective_mass == 0.0f)
84 Deactivate();
85 else
86 mEffectiveMass = 1.0f / inv_effective_mass;
87 }
88
89 /// Deactivate this constraint
90 inline void Deactivate()
91 {
92 mEffectiveMass = 0.0f;
93 mTotalLambda = 0.0f;
94 }
95
96 /// Check if constraint is active
97 inline bool IsActive() const
98 {
99 return mEffectiveMass != 0.0f;
100 }
101
102 /// Must be called from the WarmStartVelocityConstraint call to apply the previous frame's impulses
103 /// @param ioBody1 The first body that this constraint is attached to
104 /// @param ioBody2 The second body that this constraint is attached to
105 /// @param inWarmStartImpulseRatio Ratio of new step to old time step (dt_new / dt_old) for scaling the lagrange multiplier of the previous frame
106 inline void WarmStart(Body &ioBody1, Body &ioBody2, float inWarmStartImpulseRatio)
107 {
108 mTotalLambda *= inWarmStartImpulseRatio;
109 ApplyVelocityStep(ioBody1, ioBody2, mTotalLambda);
110 }
111
112 /// Iteratively update the velocity constraint. Makes sure d/dt C(...) = 0, where C is the constraint equation.
113 /// @param ioBody1 The first body that this constraint is attached to
114 /// @param ioBody2 The second body that this constraint is attached to
115 /// @param inWorldSpaceHingeAxis The axis around which body 1 rotates
116 /// @param inWorldSpaceSliderAxis The axis along which body 2 slides
117 /// @param inRatio The ratio between rotation and translation
118 inline bool SolveVelocityConstraint(Body &ioBody1, Vec3Arg inWorldSpaceHingeAxis, Body &ioBody2, Vec3Arg inWorldSpaceSliderAxis, float inRatio)
119 {
120 // Lagrange multiplier is:
121 //
122 // lambda = -K^-1 (J v + b)
123 float lambda = mEffectiveMass * (inRatio * inWorldSpaceSliderAxis.Dot(ioBody2.GetLinearVelocity()) - inWorldSpaceHingeAxis.Dot(ioBody1.GetAngularVelocity()));
124 mTotalLambda += lambda; // Store accumulated impulse
125
126 return ApplyVelocityStep(ioBody1, ioBody2, lambda);
127 }
128
129 /// Return lagrange multiplier
130 float GetTotalLambda() const
131 {
132 return mTotalLambda;
133 }
134
135 /// Iteratively update the position constraint. Makes sure C(...) == 0.
136 /// @param ioBody1 The first body that this constraint is attached to
137 /// @param ioBody2 The second body that this constraint is attached to
138 /// @param inC Value of the constraint equation (C)
139 /// @param inBaumgarte Baumgarte constant (fraction of the error to correct)
140 inline bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, float inC, float inBaumgarte) const
141 {
142 // Only apply position constraint when the constraint is hard, otherwise the velocity bias will fix the constraint
143 if (inC != 0.0f)
144 {
145 // Calculate lagrange multiplier (lambda) for Baumgarte stabilization:
146 //
147 // lambda = -K^-1 * beta / dt * C
148 //
149 // We should divide by inDeltaTime, but we should multiply by inDeltaTime in the Euler step below so they're cancelled out
150 float lambda = -mEffectiveMass * inBaumgarte * inC;
151
152 // Directly integrate velocity change for one time step
153 //
154 // Euler velocity integration:
155 // dv = M^-1 P
156 //
157 // Impulse:
158 // P = J^T lambda
159 //
160 // Euler position integration:
161 // x' = x + dv * dt
162 //
163 // Note we don't accumulate velocities for the stabilization. This is using the approach described in 'Modeling and
164 // Solving Constraints' by Erin Catto presented at GDC 2007. On slide 78 it is suggested to split up the Baumgarte
165 // stabilization for positional drift so that it does not actually add to the momentum. We combine an Euler velocity
166 // integrate + a position integrate and then discard the velocity change.
167 if (ioBody1.IsDynamic())
168 ioBody1.AddRotationStep(lambda * mInvI1_A);
169 if (ioBody2.IsDynamic())
170 ioBody2.SubPositionStep(lambda * mRatio_InvM2_B);
171 return true;
172 }
173
174 return false;
175 }
176
177 /// Save state of this constraint part
178 void SaveState(StateRecorder &inStream) const
179 {
180 inStream.Write(mTotalLambda);
181 }
182
183 /// Restore state of this constraint part
184 void RestoreState(StateRecorder &inStream)
185 {
186 inStream.Read(mTotalLambda);
187 }
188
189 private:
190 Vec3 mInvI1_A;
191 Vec3 mRatio_InvM2_B;
192 float mEffectiveMass = 0.0f;
193 float mTotalLambda = 0.0f;
194 };
195
196 JPH_NAMESPACE_END