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 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 invi1 = inBody1.IsDynamic()? inBody1.GetMotionProperties()->GetInverseInertiaForRotation(inRotation1) : Mat44::sZero();
136 Mat44 invi2 = inBody2.IsDynamic()? inBody2.GetMotionProperties()->GetInverseInertiaForRotation(inRotation2) : Mat44::sZero();
137 mInvI1_JPT = invi1.Multiply3x3RightTransposed(jp);
138 mInvI2_JPT = invi2.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 mEffectiveMass = jp.Multiply3x3(invi1 + invi2).Multiply3x3RightTransposed(jp).Inversed3x3();
144 mEffectiveMass_JP = mEffectiveMass.Multiply3x3(jp);
145 }
146
148 inline void WarmStart(Body &ioBody1, Body &ioBody2, float inWarmStartImpulseRatio)
149 {
150 mTotalLambda *= inWarmStartImpulseRatio;
151 ApplyVelocityStep(ioBody1, ioBody2, mTotalLambda);
152 }
153
155 inline bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2)
156 {
157 // Calculate lagrange multiplier:
158 //
159 // lambda = -K^-1 (J v + b)
160 Vec3 lambda = mEffectiveMass_JP.Multiply3x3(ioBody1.GetAngularVelocity() - ioBody2.GetAngularVelocity());
161 mTotalLambda += lambda;
162 return ApplyVelocityStep(ioBody1, ioBody2, lambda);
163 }
164
166 inline bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, QuatArg inInvInitialOrientation, float inBaumgarte) const
167 {
168 // Calculate constraint equation
169 Vec3 c = (ioBody1.GetRotation().Conjugated() * ioBody2.GetRotation() * inInvInitialOrientation).GetXYZ();
170 if (c != Vec3::sZero())
171 {
172 // Calculate lagrange multiplier (lambda) for Baumgarte stabilization:
173 //
174 // lambda = -K^-1 * beta / dt * C
175 //
176 // We should divide by inDeltaTime, but we should multiply by inDeltaTime in the Euler step below so they're cancelled out
177 Vec3 lambda = -inBaumgarte * mEffectiveMass * c;
178
179 // Directly integrate velocity change for one time step
180 //
181 // Euler velocity integration:
182 // dv = M^-1 P
183 //
184 // Impulse:
185 // P = J^T lambda
186 //
187 // Euler position integration:
188 // x' = x + dv * dt
189 //
190 // Note we don't accumulate velocities for the stabilization. This is using the approach described in 'Modeling and
191 // Solving Constraints' by Erin Catto presented at GDC 2007. On slide 78 it is suggested to split up the Baumgarte
192 // stabilization for positional drift so that it does not actually add to the momentum. We combine an Euler velocity
193 // integrate + a position integrate and then discard the velocity change.
194 if (ioBody1.IsDynamic())
195 ioBody1.SubRotationStep(mInvI1_JPT.Multiply3x3(lambda));
196 if (ioBody2.IsDynamic())
197 ioBody2.AddRotationStep(mInvI2_JPT.Multiply3x3(lambda));
198 return true;
199 }
200
201 return false;
202 }
203
206 {
207 return mTotalLambda;
208 }
209
211 void SaveState(StateRecorder &inStream) const
212 {
213 inStream.Write(mTotalLambda);
214 }
215
218 {
219 inStream.Read(mTotalLambda);
220 }
221
222private:
223 Mat44 mInvI1_JPT;
224 Mat44 mInvI2_JPT;
225 Mat44 mEffectiveMass;
226 Mat44 mEffectiveMass_JP;
227 Vec3 mTotalLambda { Vec3::sZero() };
228};
229
#define JPH_NAMESPACE_END
Definition: Core.h:240
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
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
Quat GetRotation() const
World space rotation of the body.
Definition: Body.h:187
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
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:1112
JPH_INLINE Mat44 Multiply3x3RightTransposed(Mat44Arg inM) const
Multiply 3x3 matrix by the transpose of a 3x3 matrix ( )
Definition: Mat44.inl:388
JPH_INLINE Vec3 Multiply3x3(Vec3Arg inV) const
Multiply vector by only 3x3 part of the matrix.
Definition: Mat44.inl:307
static JPH_INLINE Mat44 sQuatLeftMultiply(QuatArg inQ)
Returns matrix ML so that (where p and q are quaternions)
Definition: Mat44.inl:1103
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: 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:168
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:148
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:205
bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, QuatArg inInvInitialOrientation, float inBaumgarte) const
Iteratively update the position constraint. Makes sure C(...) = 0.
Definition: RotationQuatConstraintPart.h:166
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:217
void SaveState(StateRecorder &inStream) const
Save state of this constraint part.
Definition: RotationQuatConstraintPart.h:211
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:155
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
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition: Vec3.inl:107