Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
AngleConstraintPart.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
10
12
36{
38 JPH_INLINE bool ApplyVelocityStep(Body &ioBody1, Body &ioBody2, float inLambda) const
39 {
40 // Apply impulse if delta is not zero
41 if (inLambda != 0.0f)
42 {
43 // Calculate velocity change due to constraint
44 //
45 // Impulse:
46 // P = J^T lambda
47 //
48 // Euler velocity integration:
49 // v' = v + M^-1 P
50 if (ioBody1.IsDynamic())
51 ioBody1.GetMotionProperties()->SubAngularVelocityStep(inLambda * mInvI1_Axis);
52 if (ioBody2.IsDynamic())
53 ioBody2.GetMotionProperties()->AddAngularVelocityStep(inLambda * mInvI2_Axis);
54 return true;
55 }
56
57 return false;
58 }
59
60public:
71 inline void CalculateConstraintProperties(float inDeltaTime, const Body &inBody1, const Body &inBody2, Vec3Arg inWorldSpaceAxis, float inBias = 0.0f, float inC = 0.0f, float inFrequency = 0.0f, float inDamping = 0.0f)
72 {
73 JPH_ASSERT(inWorldSpaceAxis.IsNormalized(1.0e-4f));
74
75 // Calculate properties used below
76 mInvI1_Axis = inBody1.IsDynamic()? inBody1.GetMotionProperties()->MultiplyWorldSpaceInverseInertiaByVector(inBody1.GetRotation(), inWorldSpaceAxis) : Vec3::sZero();
77 mInvI2_Axis = inBody2.IsDynamic()? inBody2.GetMotionProperties()->MultiplyWorldSpaceInverseInertiaByVector(inBody2.GetRotation(), inWorldSpaceAxis) : Vec3::sZero();
78
79 // Calculate inverse effective mass: K = J M^-1 J^T
80 float inv_effective_mass = inWorldSpaceAxis.Dot(mInvI1_Axis + mInvI2_Axis);
81
82 // Calculate effective mass and spring properties
83 mSpringPart.CalculateSpringProperties(inDeltaTime, inv_effective_mass, inBias, inC, inFrequency, inDamping, mEffectiveMass);
84 }
85
87 inline void Deactivate()
88 {
89 mEffectiveMass = 0.0f;
90 mTotalLambda = 0.0f;
91 }
92
94 inline bool IsActive() const
95 {
96 return mEffectiveMass != 0.0f;
97 }
98
103 inline void WarmStart(Body &ioBody1, Body &ioBody2, float inWarmStartImpulseRatio)
104 {
105 mTotalLambda *= inWarmStartImpulseRatio;
106 ApplyVelocityStep(ioBody1, ioBody2, mTotalLambda);
107 }
108
115 inline bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
116 {
117 // Lagrange multiplier is:
118 //
119 // lambda = -K^-1 (J v + b)
120 float lambda = mEffectiveMass * (inWorldSpaceAxis.Dot(ioBody1.GetAngularVelocity() - ioBody2.GetAngularVelocity()) - mSpringPart.GetBias(mTotalLambda));
121 float new_lambda = Clamp(mTotalLambda + lambda, inMinLambda, inMaxLambda); // Clamp impulse
122 lambda = new_lambda - mTotalLambda; // Lambda potentially got clamped, calculate the new impulse to apply
123 mTotalLambda = new_lambda; // Store accumulated impulse
124
125 return ApplyVelocityStep(ioBody1, ioBody2, lambda);
126 }
127
129 float GetTotalLambda() const
130 {
131 return mTotalLambda;
132 }
133
139 inline bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, float inC, float inBaumgarte) const
140 {
141 // Only apply position constraint when the constraint is hard, otherwise the velocity bias will fix the constraint
142 if (inC != 0.0f && !mSpringPart.IsActive())
143 {
144 // Calculate lagrange multiplier (lambda) for Baumgarte stabilization:
145 //
146 // lambda = -K^-1 * beta / dt * C
147 //
148 // We should divide by inDeltaTime, but we should multiply by inDeltaTime in the Euler step below so they're cancelled out
149 float lambda = -mEffectiveMass * inBaumgarte * inC;
150
151 // Directly integrate velocity change for one time step
152 //
153 // Euler velocity integration:
154 // dv = M^-1 P
155 //
156 // Impulse:
157 // P = J^T lambda
158 //
159 // Euler position integration:
160 // x' = x + dv * dt
161 //
162 // Note we don't accumulate velocities for the stabilization. This is using the approach described in 'Modeling and
163 // Solving Constraints' by Erin Catto presented at GDC 2007. On slide 78 it is suggested to split up the Baumgarte
164 // stabilization for positional drift so that it does not actually add to the momentum. We combine an Euler velocity
165 // integrate + a position integrate and then discard the velocity change.
166 if (ioBody1.IsDynamic())
167 ioBody1.SubRotationStep(lambda * mInvI1_Axis);
168 if (ioBody2.IsDynamic())
169 ioBody2.AddRotationStep(lambda * mInvI2_Axis);
170 return true;
171 }
172
173 return false;
174 }
175
177 void SaveState(StateRecorder &inStream) const
178 {
179 inStream.Write(mTotalLambda);
180 }
181
184 {
185 inStream.Read(mTotalLambda);
186 }
187
188private:
189 Vec3 mInvI1_Axis;
190 Vec3 mInvI2_Axis;
191 float mEffectiveMass = 0.0f;
192 SpringPart mSpringPart;
193 float mTotalLambda = 0.0f;
194};
195
#define JPH_NAMESPACE_END
Definition: Core.h:240
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
constexpr T Clamp(T inV, T inMin, T inMax)
Clamp a value between two values.
Definition: Math.h:45
Definition: AngleConstraintPart.h:36
bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, float inC, float inBaumgarte) const
Definition: AngleConstraintPart.h:139
void Deactivate()
Deactivate this constraint.
Definition: AngleConstraintPart.h:87
bool IsActive() const
Check if constraint is active.
Definition: AngleConstraintPart.h:94
void CalculateConstraintProperties(float inDeltaTime, const Body &inBody1, const Body &inBody2, Vec3Arg inWorldSpaceAxis, float inBias=0.0f, float inC=0.0f, float inFrequency=0.0f, float inDamping=0.0f)
Definition: AngleConstraintPart.h:71
void RestoreState(StateRecorder &inStream)
Restore state of this constraint part.
Definition: AngleConstraintPart.h:183
void WarmStart(Body &ioBody1, Body &ioBody2, float inWarmStartImpulseRatio)
Definition: AngleConstraintPart.h:103
bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
Definition: AngleConstraintPart.h:115
void SaveState(StateRecorder &inStream) const
Save state of this constraint part.
Definition: AngleConstraintPart.h:177
float GetTotalLambda() const
Return lagrange multiplier.
Definition: AngleConstraintPart.h:129
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
void SubAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition: MotionProperties.h:129
JPH_INLINE Vec3 MultiplyWorldSpaceInverseInertiaByVector(QuatArg inBodyRotation, Vec3Arg inV) const
Multiply a vector with the inverse world space inertia tensor ( ). Zero if object is static or kinema...
Definition: MotionProperties.inl:90
void AddAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition: MotionProperties.h:128
Class used in other constraint parts to calculate the required bias factor in the lagrange multiplier...
Definition: SpringPart.h:14
void CalculateSpringProperties(float inDeltaTime, float inInvEffectiveMass, float inBias, float inC, float inFrequency, float inDamping, float &outEffectiveMass)
Definition: SpringPart.h:25
float GetBias(float inTotalLambda) const
Get total bias b, including supplied bias and bias for spring: lambda = J v + b.
Definition: SpringPart.h:98
bool IsActive() const
Returns if this spring is active.
Definition: SpringPart.h:92
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 float Dot(Vec3Arg inV2) const
Dot product.
Definition: Vec3.inl:637
JPH_INLINE bool IsNormalized(float inTolerance=1.0e-6f) const
Test if vector is normalized.
Definition: Vec3.inl:737
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition: Vec3.inl:107