Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
RotationQuatConstraintPart.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
11
87{
88private:
90 JPH_INLINE bool ApplyVelocityStep(Body &ioBody1, Body &ioBody2, Vec3Arg inLambda) const
91 {
92 // Apply impulse if delta is not zero
93 if (inLambda != Vec3::sZero())
94 {
95 // Calculate velocity change due to constraint
96 //
97 // Impulse:
98 // P = J^T lambda
99 //
100 // Euler velocity integration:
101 // v' = v + M^-1 P
102 if (ioBody1.IsDynamic())
103 ioBody1.GetMotionProperties()->SubAngularVelocityStep(mInvI1_JPT.Multiply3x3(inLambda));
104 if (ioBody2.IsDynamic())
105 ioBody2.GetMotionProperties()->AddAngularVelocityStep(mInvI2_JPT.Multiply3x3(inLambda));
106 return true;
107 }
108
109 return false;
110 }
111
112public:
114 static Quat sGetInvInitialOrientation(const Body &inBody1, const Body &inBody2)
115 {
116 // q20 = q10 r0
117 // <=> r0 = q10^-1 q20
118 // <=> r0^-1 = q20^-1 q10
119 //
120 // where:
121 //
122 // q20 = initial orientation of body 2
123 // q10 = initial orientation of body 1
124 // r0 = initial rotation from body 1 to body 2
125 return inBody2.GetRotation().Conjugated() * inBody1.GetRotation();
126 }
127
129 inline void CalculateConstraintProperties(const Body &inBody1, Mat44Arg inRotation1, const Body &inBody2, Mat44Arg inRotation2, QuatArg inInvInitialOrientation)
130 {
131 // Calculate: JP = 1/2 A ML(q1^*) MR(q2 r0^*) A^T
132 Mat44 jp = (Mat44::sQuatLeftMultiply(0.5f * inBody1.GetRotation().Conjugated()) * Mat44::sQuatRightMultiply(inBody2.GetRotation() * inInvInitialOrientation)).GetRotationSafe();
133
134 // Calculate properties used during constraint solving
135 Mat44 inv_i1 = inBody1.IsDynamic()? inBody1.GetMotionProperties()->GetInverseInertiaForRotation(inRotation1) : Mat44::sZero();
136 Mat44 inv_i2 = inBody2.IsDynamic()? inBody2.GetMotionProperties()->GetInverseInertiaForRotation(inRotation2) : Mat44::sZero();
137 mInvI1_JPT = inv_i1.Multiply3x3RightTransposed(jp);
138 mInvI2_JPT = inv_i2.Multiply3x3RightTransposed(jp);
139
140 // Calculate effective mass: K^-1 = (J M^-1 J^T)^-1
141 // = (JP * I1^-1 * JP^T + JP * I2^-1 * JP^T)^-1
142 // = (JP * (I1^-1 + I2^-1) * JP^T)^-1
143 if (!mEffectiveMass.SetInversed3x3(jp.Multiply3x3(inv_i1 + inv_i2).Multiply3x3RightTransposed(jp)))
144 Deactivate();
145 else
146 mEffectiveMass_JP = mEffectiveMass.Multiply3x3(jp);
147 }
148
150 inline void Deactivate()
151 {
152 mEffectiveMass = Mat44::sZero();
153 mEffectiveMass_JP = Mat44::sZero();
154 mTotalLambda = Vec3::sZero();
155 }
156
158 inline bool IsActive() const
159 {
160 return mEffectiveMass(3, 3) != 0.0f;
161 }
162
164 inline void WarmStart(Body &ioBody1, Body &ioBody2, float inWarmStartImpulseRatio)
165 {
166 mTotalLambda *= inWarmStartImpulseRatio;
167 ApplyVelocityStep(ioBody1, ioBody2, mTotalLambda);
168 }
169
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_JP.Multiply3x3(ioBody1.GetAngularVelocity() - ioBody2.GetAngularVelocity());
177 mTotalLambda += lambda;
178 return ApplyVelocityStep(ioBody1, ioBody2, lambda);
179 }
180
182 inline bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, QuatArg inInvInitialOrientation, float inBaumgarte) const
183 {
184 // Calculate constraint equation
185 Vec3 c = (ioBody1.GetRotation().Conjugated() * ioBody2.GetRotation() * inInvInitialOrientation).GetXYZ();
186 if (c != Vec3::sZero())
187 {
188 // Calculate lagrange multiplier (lambda) for Baumgarte stabilization:
189 //
190 // lambda = -K^-1 * beta / dt * C
191 //
192 // We should divide by inDeltaTime, but we should multiply by inDeltaTime in the Euler step below so they're cancelled out
193 Vec3 lambda = -inBaumgarte * mEffectiveMass * c;
194
195 // Directly integrate velocity change for one time step
196 //
197 // Euler velocity integration:
198 // dv = M^-1 P
199 //
200 // Impulse:
201 // P = J^T lambda
202 //
203 // Euler position integration:
204 // x' = x + dv * dt
205 //
206 // Note we don't accumulate velocities for the stabilization. This is using the approach described in 'Modeling and
207 // Solving Constraints' by Erin Catto presented at GDC 2007. On slide 78 it is suggested to split up the Baumgarte
208 // stabilization for positional drift so that it does not actually add to the momentum. We combine an Euler velocity
209 // integrate + a position integrate and then discard the velocity change.
210 if (ioBody1.IsDynamic())
211 ioBody1.SubRotationStep(mInvI1_JPT.Multiply3x3(lambda));
212 if (ioBody2.IsDynamic())
213 ioBody2.AddRotationStep(mInvI2_JPT.Multiply3x3(lambda));
214 return true;
215 }
216
217 return false;
218 }
219
222 {
223 return mTotalLambda;
224 }
225
227 void SaveState(StateRecorder &inStream) const
228 {
229 inStream.Write(mTotalLambda);
230 }
231
234 {
235 inStream.Read(mTotalLambda);
236 }
237
238private:
239 Mat44 mInvI1_JPT;
240 Mat44 mInvI2_JPT;
241 Mat44 mEffectiveMass;
242 Mat44 mEffectiveMass_JP;
243 Vec3 mTotalLambda { Vec3::sZero() };
244};
245
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
Definition: Body.h:35
const MotionProperties * GetMotionProperties() const
Access to the motion properties.
Definition: Body.h:255
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 (using during position integrate & constraint solving)
Definition: Body.inl:81
Quat GetRotation() const
World space rotation of the body.
Definition: Body.h:237
void SubRotationStep(Vec3Arg inAngularVelocityTimesDeltaTime)
Definition: Body.inl:100
Vec3 GetAngularVelocity() const
Get world space angular velocity of the center of mass (unit: rad/s)
Definition: Body.h:159
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition: Mat44.h:13
static JPH_INLINE Mat44 sZero()
Zero matrix.
Definition: Mat44.inl:30
static JPH_INLINE Mat44 sQuatRightMultiply(QuatArg inQ)
Returns matrix MR so that (where p and q are quaternions)
Definition: Mat44.inl:847
JPH_INLINE Mat44 Multiply3x3RightTransposed(Mat44Arg inM) const
Multiply 3x3 matrix by the transpose of a 3x3 matrix ( )
Definition: Mat44.inl:397
JPH_INLINE bool SetInversed3x3(Mat44Arg inM)
*this = inM.Inversed3x3(), returns false if the matrix is singular in which case *this is unchanged
Definition: Mat44.inl:767
JPH_INLINE Vec3 Multiply3x3(Vec3Arg inV) const
Multiply vector by only 3x3 part of the matrix.
Definition: Mat44.inl:316
static JPH_INLINE Mat44 sQuatLeftMultiply(QuatArg inQ)
Returns matrix ML so that (where p and q are quaternions)
Definition: Mat44.inl:838
void SubAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition: MotionProperties.h:194
Mat44 GetInverseInertiaForRotation(Mat44Arg inRotation) const
Get inverse inertia matrix ( ) for a given object rotation (translation will be ignored)....
Definition: MotionProperties.inl:59
void AddAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition: MotionProperties.h:193
Definition: Quat.h:33
JPH_INLINE Quat Conjugated() const
The conjugate [w, -x, -y, -z] is the same as the inverse for unit quaternions.
Definition: Quat.h:178
Definition: RotationQuatConstraintPart.h:87
void WarmStart(Body &ioBody1, Body &ioBody2, float inWarmStartImpulseRatio)
Must be called from the WarmStartVelocityConstraint call to apply the previous frame's impulses.
Definition: RotationQuatConstraintPart.h:164
void CalculateConstraintProperties(const Body &inBody1, Mat44Arg inRotation1, const Body &inBody2, Mat44Arg inRotation2, QuatArg inInvInitialOrientation)
Calculate properties used during the functions below.
Definition: RotationQuatConstraintPart.h:129
Vec3 GetTotalLambda() const
Return lagrange multiplier.
Definition: RotationQuatConstraintPart.h:221
bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, QuatArg inInvInitialOrientation, float inBaumgarte) const
Iteratively update the position constraint. Makes sure C(...) = 0.
Definition: RotationQuatConstraintPart.h:182
void Deactivate()
Deactivate this constraint.
Definition: RotationQuatConstraintPart.h:150
static Quat sGetInvInitialOrientation(const Body &inBody1, const Body &inBody2)
Return inverse of initial rotation from body 1 to body 2 in body 1 space.
Definition: RotationQuatConstraintPart.h:114
void RestoreState(StateRecorder &inStream)
Restore state of this constraint part.
Definition: RotationQuatConstraintPart.h:233
void SaveState(StateRecorder &inStream) const
Save state of this constraint part.
Definition: RotationQuatConstraintPart.h:227
bool IsActive() const
Check if constraint is active.
Definition: RotationQuatConstraintPart.h:158
bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2)
Iteratively update the velocity constraint. Makes sure d/dt C(...) = 0, where C is the constraint equ...
Definition: RotationQuatConstraintPart.h:171
Definition: StateRecorder.h:48
void Read(T &outT)
Read a primitive (e.g. float, int, etc.) from the binary stream.
Definition: StreamIn.h:29
void Write(const T &inT)
Write a primitive (e.g. float, int, etc.) to the binary stream.
Definition: StreamOut.h:26
Definition: Vec3.h:16
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition: Vec3.inl:107