Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
PointConstraintPart.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
41{
42 JPH_INLINE bool ApplyVelocityStep(Body &ioBody1, Body &ioBody2, Vec3Arg inLambda) const
43 {
44 // Apply impulse if delta is not zero
45 if (inLambda != Vec3::sZero())
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 if (ioBody1.IsDynamic())
55 {
57 mp1->SubLinearVelocityStep(mp1->GetInverseMass() * inLambda);
58 mp1->SubAngularVelocityStep(mInvI1_R1X * inLambda);
59 }
60 if (ioBody2.IsDynamic())
61 {
63 mp2->AddLinearVelocityStep(mp2->GetInverseMass() * inLambda);
64 mp2->AddAngularVelocityStep(mInvI2_R2X * inLambda);
65 }
66 return true;
67 }
68
69 return false;
70 }
71
72public:
80 inline void CalculateConstraintProperties(const Body &inBody1, Mat44Arg inRotation1, Vec3Arg inR1, const Body &inBody2, Mat44Arg inRotation2, Vec3Arg inR2)
81 {
82 // Positions where the point constraint acts on (middle point between center of masses) in world space
83 mR1 = inRotation1.Multiply3x3(inR1);
84 mR2 = inRotation2.Multiply3x3(inR2);
85
86 // Calculate effective mass: K^-1 = (J M^-1 J^T)^-1
87 // Using: I^-1 = R * Ibody^-1 * R^T
88 float summed_inv_mass;
89 Mat44 inv_effective_mass;
90 if (inBody1.IsDynamic())
91 {
92 const MotionProperties *mp1 = inBody1.GetMotionProperties();
93 Mat44 invi1 = mp1->GetInverseInertiaForRotation(inRotation1);
94 summed_inv_mass = mp1->GetInverseMass();
95
96 Mat44 r1x = Mat44::sCrossProduct(mR1);
97 mInvI1_R1X = invi1.Multiply3x3(r1x);
98 inv_effective_mass = r1x.Multiply3x3(invi1).Multiply3x3RightTransposed(r1x);
99 }
100 else
101 {
102 JPH_IF_DEBUG(mInvI1_R1X = Mat44::sNaN();)
103
104 summed_inv_mass = 0.0f;
105 inv_effective_mass = Mat44::sZero();
106 }
107
108 if (inBody2.IsDynamic())
109 {
110 const MotionProperties *mp2 = inBody2.GetMotionProperties();
111 Mat44 invi2 = mp2->GetInverseInertiaForRotation(inRotation2);
112 summed_inv_mass += mp2->GetInverseMass();
113
114 Mat44 r2x = Mat44::sCrossProduct(mR2);
115 mInvI2_R2X = invi2.Multiply3x3(r2x);
116 inv_effective_mass += r2x.Multiply3x3(invi2).Multiply3x3RightTransposed(r2x);
117 }
118 else
119 {
120 JPH_IF_DEBUG(mInvI2_R2X = Mat44::sNaN();)
121 }
122
123 inv_effective_mass += Mat44::sScale(summed_inv_mass);
124 mEffectiveMass = inv_effective_mass.Inversed3x3();
125 }
126
131 inline void WarmStart(Body &ioBody1, Body &ioBody2, float inWarmStartImpulseRatio)
132 {
133 mTotalLambda *= inWarmStartImpulseRatio;
134 ApplyVelocityStep(ioBody1, ioBody2, mTotalLambda);
135 }
136
140 inline bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2)
141 {
142 // Calculate lagrange multiplier:
143 //
144 // lambda = -K^-1 (J v + b)
145 Vec3 lambda = mEffectiveMass * (ioBody1.GetLinearVelocity() - mR1.Cross(ioBody1.GetAngularVelocity()) - ioBody2.GetLinearVelocity() + mR2.Cross(ioBody2.GetAngularVelocity()));
146 mTotalLambda += lambda; // Store accumulated lambda
147 return ApplyVelocityStep(ioBody1, ioBody2, lambda);
148 }
149
154 inline bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, float inBaumgarte) const
155 {
156 Vec3 separation = (Vec3(ioBody2.GetCenterOfMassPosition() - ioBody1.GetCenterOfMassPosition()) + mR2 - mR1);
157 if (separation != Vec3::sZero())
158 {
159 // Calculate lagrange multiplier (lambda) for Baumgarte stabilization:
160 //
161 // lambda = -K^-1 * beta / dt * C
162 //
163 // We should divide by inDeltaTime, but we should multiply by inDeltaTime in the Euler step below so they're cancelled out
164 Vec3 lambda = mEffectiveMass * -inBaumgarte * separation;
165
166 // Directly integrate velocity change for one time step
167 //
168 // Euler velocity integration:
169 // dv = M^-1 P
170 //
171 // Impulse:
172 // P = J^T lambda
173 //
174 // Euler position integration:
175 // x' = x + dv * dt
176 //
177 // Note we don't accumulate velocities for the stabilization. This is using the approach described in 'Modeling and
178 // Solving Constraints' by Erin Catto presented at GDC 2007. On slide 78 it is suggested to split up the Baumgarte
179 // stabilization for positional drift so that it does not actually add to the momentum. We combine an Euler velocity
180 // integrate + a position integrate and then discard the velocity change.
181 if (ioBody1.IsDynamic())
182 {
183 ioBody1.SubPositionStep(ioBody1.GetMotionProperties()->GetInverseMass() * lambda);
184 ioBody1.SubRotationStep(mInvI1_R1X * lambda);
185 }
186 if (ioBody2.IsDynamic())
187 {
188 ioBody2.AddPositionStep(ioBody2.GetMotionProperties()->GetInverseMass() * lambda);
189 ioBody2.AddRotationStep(mInvI2_R2X * lambda);
190 }
191
192 return true;
193 }
194
195 return false;
196 }
197
200 {
201 return mTotalLambda;
202 }
203
205 void SaveState(StateRecorder &inStream) const
206 {
207 inStream.Write(mTotalLambda);
208 }
209
212 {
213 inStream.Read(mTotalLambda);
214 }
215
216private:
217 Vec3 mR1;
218 Vec3 mR2;
219 Mat44 mInvI1_R1X;
220 Mat44 mInvI2_R2X;
221 Mat44 mEffectiveMass;
222 Vec3 mTotalLambda { Vec3::sZero() };
223};
224
#define JPH_IF_DEBUG(...)
Definition: Core.h:354
#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
RVec3 GetCenterOfMassPosition() const
Gets the world space position of this body's center of mass.
Definition: Body.h:193
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
void SubPositionStep(Vec3Arg inLinearVelocityTimesDeltaTime)
Definition: Body.h:237
Vec3 GetLinearVelocity() const
Get world space linear velocity of the center of mass (unit: m/s)
Definition: Body.h:109
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
void AddPositionStep(Vec3Arg inLinearVelocityTimesDeltaTime)
Update position using an Euler step (used during position integrate & constraint solving)
Definition: Body.h:236
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 sCrossProduct(Vec3Arg inV)
Get matrix that represents a cross product .
Definition: Mat44.inl:179
static JPH_INLINE Mat44 sNaN()
Matrix filled with NaN's.
Definition: Mat44.inl:40
static JPH_INLINE Mat44 sScale(float inScale)
Get matrix that scales uniformly.
Definition: Mat44.inl:163
JPH_INLINE Vec3 Multiply3x3(Vec3Arg inV) const
Multiply vector by only 3x3 part of the matrix.
Definition: Mat44.inl:307
JPH_INLINE Mat44 Inversed3x3() const
Inverse 3x3 matrix.
Definition: Mat44.inl:883
The Body class only keeps track of state for static bodies, the MotionProperties class keeps the addi...
Definition: MotionProperties.h:20
void AddLinearVelocityStep(Vec3Arg inLinearVelocityChange)
Definition: MotionProperties.h:126
void SubLinearVelocityStep(Vec3Arg inLinearVelocityChange)
Definition: MotionProperties.h:127
float GetInverseMass() const
Get inverse mass (1 / mass). Should only be called on a dynamic object (static or kinematic bodies ha...
Definition: MotionProperties.h:80
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: PointConstraintPart.h:41
void RestoreState(StateRecorder &inStream)
Restore state of this constraint part.
Definition: PointConstraintPart.h:211
bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, float inBaumgarte) const
Definition: PointConstraintPart.h:154
bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2)
Definition: PointConstraintPart.h:140
Vec3 GetTotalLambda() const
Return lagrange multiplier.
Definition: PointConstraintPart.h:199
void WarmStart(Body &ioBody1, Body &ioBody2, float inWarmStartImpulseRatio)
Definition: PointConstraintPart.h:131
void CalculateConstraintProperties(const Body &inBody1, Mat44Arg inRotation1, Vec3Arg inR1, const Body &inBody2, Mat44Arg inRotation2, Vec3Arg inR2)
Definition: PointConstraintPart.h:80
void SaveState(StateRecorder &inStream) const
Save state of this constraint part.
Definition: PointConstraintPart.h:205
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
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition: Vec3.inl:582
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition: Vec3.inl:107