45 template <EMotionType Type1, EMotionType Type2>
46 JPH_INLINE
bool ApplyVelocityStep(
MotionProperties *ioMotionProperties1,
float inInvMass1,
MotionProperties *ioMotionProperties2,
float inInvMass2,
Vec3Arg inWorldSpaceAxis,
float inLambda)
const
58 if constexpr (Type1 == EMotionType::Dynamic)
63 if constexpr (Type2 == EMotionType::Dynamic)
75 template <EMotionType Type1, EMotionType Type2>
76 JPH_INLINE
float TemplatedCalculateInverseEffectiveMass(
float inInvMass1,
Mat44Arg inInvI1,
Vec3Arg inR1PlusU,
float inInvMass2,
Mat44Arg inInvI2,
Vec3Arg inR2,
Vec3Arg inWorldSpaceAxis)
81 Vec3 r1_plus_u_x_axis;
82 if constexpr (Type1 != EMotionType::Static)
84 r1_plus_u_x_axis = inR1PlusU.
Cross(inWorldSpaceAxis);
95 if constexpr (Type2 != EMotionType::Static)
97 r2_x_axis = inR2.
Cross(inWorldSpaceAxis);
108 float inv_effective_mass;
110 if constexpr (Type1 == EMotionType::Dynamic)
113 invi1_r1_plus_u_x_axis.
StoreFloat3(&mInvI1_R1PlusUxAxis);
114 inv_effective_mass = inInvMass1 + invi1_r1_plus_u_x_axis.
Dot(r1_plus_u_x_axis);
118 (void)r1_plus_u_x_axis;
120 inv_effective_mass = 0.0f;
123 if constexpr (Type2 == EMotionType::Dynamic)
127 inv_effective_mass += inInvMass2 + invi2_r2_x_axis.
Dot(r2_x_axis);
135 return inv_effective_mass;
139 JPH_INLINE
float CalculateInverseEffectiveMass(
const Body &inBody1,
Vec3Arg inR1PlusU,
const Body &inBody2,
Vec3Arg inR2,
Vec3Arg inWorldSpaceAxis)
144 case EMotionType::Dynamic:
151 case EMotionType::Dynamic:
154 case EMotionType::Kinematic:
155 return TemplatedCalculateInverseEffectiveMass<EMotionType::Dynamic, EMotionType::Kinematic>(inv_m1, inv_i1, inR1PlusU, 0 ,
Mat44() , inR2, inWorldSpaceAxis);
157 case EMotionType::Static:
158 return TemplatedCalculateInverseEffectiveMass<EMotionType::Dynamic, EMotionType::Static>(inv_m1, inv_i1, inR1PlusU, 0 ,
Mat44() , inR2, inWorldSpaceAxis);
166 case EMotionType::Kinematic:
170 case EMotionType::Static:
183 JPH_INLINE
float CalculateInverseEffectiveMassWithMassOverride(
const Body &inBody1,
float inInvMass1,
float inInvInertiaScale1,
Vec3Arg inR1PlusU,
const Body &inBody2,
float inInvMass2,
float inInvInertiaScale2,
Vec3Arg inR2,
Vec3Arg inWorldSpaceAxis)
188 case EMotionType::Dynamic:
193 case EMotionType::Dynamic:
194 return TemplatedCalculateInverseEffectiveMass<EMotionType::Dynamic, EMotionType::Dynamic>(inInvMass1, inv_i1, inR1PlusU, inInvMass2, inInvInertiaScale2 * inBody2.
GetInverseInertia(), inR2, inWorldSpaceAxis);
196 case EMotionType::Kinematic:
197 return TemplatedCalculateInverseEffectiveMass<EMotionType::Dynamic, EMotionType::Kinematic>(inInvMass1, inv_i1, inR1PlusU, 0 ,
Mat44() , inR2, inWorldSpaceAxis);
199 case EMotionType::Static:
200 return TemplatedCalculateInverseEffectiveMass<EMotionType::Dynamic, EMotionType::Static>(inInvMass1, inv_i1, inR1PlusU, 0 ,
Mat44() , inR2, inWorldSpaceAxis);
208 case EMotionType::Kinematic:
210 return TemplatedCalculateInverseEffectiveMass<EMotionType::Kinematic, EMotionType::Dynamic>(0 ,
Mat44() , inR1PlusU, inInvMass2, inInvInertiaScale2 * inBody2.
GetInverseInertia(), inR2, inWorldSpaceAxis);
212 case EMotionType::Static:
214 return TemplatedCalculateInverseEffectiveMass<EMotionType::Static, EMotionType::Dynamic>(0 ,
Mat44() , inR1PlusU, inInvMass2, inInvInertiaScale2 * inBody2.
GetInverseInertia(), inR2, inWorldSpaceAxis);
226 template <EMotionType Type1, EMotionType Type2>
229 float inv_effective_mass = TemplatedCalculateInverseEffectiveMass<Type1, Type2>(inInvMass1, inInvI1, inR1PlusU, inInvMass2, inInvI2, inR2, inWorldSpaceAxis);
231 if (inv_effective_mass == 0.0f)
235 mEffectiveMass = 1.0f / inv_effective_mass;
239 JPH_DET_LOG(
"TemplatedCalculateConstraintProperties: invM1: " << inInvMass1 <<
" invI1: " << inInvI1 <<
" r1PlusU: " << inR1PlusU <<
" invM2: " << inInvMass2 <<
" invI2: " << inInvI2 <<
" r2: " << inR2 <<
" bias: " << inBias <<
" r1PlusUxAxis: " << mR1PlusUxAxis <<
" r2xAxis: " << mR2xAxis <<
" invI1_R1PlusUxAxis: " << mInvI1_R1PlusUxAxis <<
" invI2_R2xAxis: " << mInvI2_R2xAxis <<
" effectiveMass: " << mEffectiveMass <<
" totalLambda: " << mTotalLambda);
251 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
253 if (inv_effective_mass == 0.0f)
257 mEffectiveMass = 1.0f / inv_effective_mass;
275 float inv_effective_mass = CalculateInverseEffectiveMassWithMassOverride(inBody1, inInvMass1, inInvInertiaScale1, inR1PlusU, inBody2, inInvMass2, inInvInertiaScale2, inR2, inWorldSpaceAxis);
277 if (inv_effective_mass == 0.0f)
281 mEffectiveMass = 1.0f / inv_effective_mass;
299 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
301 if (inv_effective_mass == 0.0f)
320 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
322 if (inv_effective_mass == 0.0f)
331 float inv_effective_mass = CalculateInverseEffectiveMass(inBody1, inR1PlusU, inBody2, inR2, inWorldSpaceAxis);
333 if (inv_effective_mass == 0.0f)
335 else if (inSpringSettings.
mMode == ESpringMode::FrequencyAndDamping)
344 mEffectiveMass = 0.0f;
351 return mEffectiveMass != 0.0f;
355 template <EMotionType Type1, EMotionType Type2>
358 mTotalLambda *= inWarmStartImpulseRatio;
360 ApplyVelocityStep<Type1, Type2>(ioMotionProperties1, inInvMass1, ioMotionProperties2, inInvMass2, inWorldSpaceAxis, mTotalLambda);
378 if (motion_type1 == EMotionType::Dynamic)
380 if (motion_type2 == EMotionType::Dynamic)
381 TemplatedWarmStart<EMotionType::Dynamic, EMotionType::Dynamic>(motion_properties1, motion_properties1->
GetInverseMass(), motion_properties2, motion_properties2->
GetInverseMass(), inWorldSpaceAxis, inWarmStartImpulseRatio);
383 TemplatedWarmStart<EMotionType::Dynamic, EMotionType::Static>(motion_properties1, motion_properties1->
GetInverseMass(), motion_properties2, 0.0f , inWorldSpaceAxis, inWarmStartImpulseRatio);
387 JPH_ASSERT(motion_type2 == EMotionType::Dynamic);
388 TemplatedWarmStart<EMotionType::Static, EMotionType::Dynamic>(motion_properties1, 0.0f , motion_properties2, motion_properties2->
GetInverseMass(), inWorldSpaceAxis, inWarmStartImpulseRatio);
393 template <EMotionType Type1, EMotionType Type2>
398 if constexpr (Type1 != EMotionType::Static && Type2 != EMotionType::Static)
400 else if constexpr (Type1 != EMotionType::Static)
402 else if constexpr (Type2 != EMotionType::Static)
408 if constexpr (Type1 != EMotionType::Static)
410 if constexpr (Type2 != EMotionType::Static)
416 float lambda = mEffectiveMass * (jv - mSpringPart.
GetBias(mTotalLambda));
419 return mTotalLambda + lambda;
423 template <EMotionType Type1, EMotionType Type2>
426 float delta_lambda = inTotalLambda - mTotalLambda;
427 mTotalLambda = inTotalLambda;
429 return ApplyVelocityStep<Type1, Type2>(ioMotionProperties1, inInvMass1, ioMotionProperties2, inInvMass2, inWorldSpaceAxis, delta_lambda);
433 template <EMotionType Type1, EMotionType Type2>
436 float total_lambda = TemplatedSolveVelocityConstraintGetTotalLambda<Type1, Type2>(ioMotionProperties1, ioMotionProperties2, inWorldSpaceAxis);
439 total_lambda =
Clamp(total_lambda, inMinLambda, inMaxLambda);
441 return TemplatedSolveVelocityConstraintApplyLambda<Type1, Type2>(ioMotionProperties1, inInvMass1, ioMotionProperties2, inInvMass2, inWorldSpaceAxis, total_lambda);
459 switch (motion_type1)
461 case EMotionType::Dynamic:
462 switch (motion_type2)
464 case EMotionType::Dynamic:
465 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Dynamic>(motion_properties1, motion_properties1->
GetInverseMass(), motion_properties2, motion_properties2->
GetInverseMass(), inWorldSpaceAxis, inMinLambda, inMaxLambda);
467 case EMotionType::Kinematic:
468 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Kinematic>(motion_properties1, motion_properties1->
GetInverseMass(), motion_properties2, 0.0f , inWorldSpaceAxis, inMinLambda, inMaxLambda);
470 case EMotionType::Static:
471 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Static>(motion_properties1, motion_properties1->
GetInverseMass(), motion_properties2, 0.0f , inWorldSpaceAxis, inMinLambda, inMaxLambda);
479 case EMotionType::Kinematic:
480 JPH_ASSERT(motion_type2 == EMotionType::Dynamic);
481 return TemplatedSolveVelocityConstraint<EMotionType::Kinematic, EMotionType::Dynamic>(motion_properties1, 0.0f , motion_properties2, motion_properties2->
GetInverseMass(), inWorldSpaceAxis, inMinLambda, inMaxLambda);
483 case EMotionType::Static:
484 JPH_ASSERT(motion_type2 == EMotionType::Dynamic);
485 return TemplatedSolveVelocityConstraint<EMotionType::Static, EMotionType::Dynamic>(motion_properties1, 0.0f , motion_properties2, motion_properties2->
GetInverseMass(), inWorldSpaceAxis, inMinLambda, inMaxLambda);
512 switch (motion_type1)
514 case EMotionType::Dynamic:
515 switch (motion_type2)
517 case EMotionType::Dynamic:
518 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Dynamic>(motion_properties1, inInvMass1, motion_properties2, inInvMass2, inWorldSpaceAxis, inMinLambda, inMaxLambda);
520 case EMotionType::Kinematic:
521 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Kinematic>(motion_properties1, inInvMass1, motion_properties2, 0.0f , inWorldSpaceAxis, inMinLambda, inMaxLambda);
523 case EMotionType::Static:
524 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Static>(motion_properties1, inInvMass1, motion_properties2, 0.0f , inWorldSpaceAxis, inMinLambda, inMaxLambda);
532 case EMotionType::Kinematic:
533 JPH_ASSERT(motion_type2 == EMotionType::Dynamic);
534 return TemplatedSolveVelocityConstraint<EMotionType::Kinematic, EMotionType::Dynamic>(motion_properties1, 0.0f , motion_properties2, inInvMass2, inWorldSpaceAxis, inMinLambda, inMaxLambda);
536 case EMotionType::Static:
537 JPH_ASSERT(motion_type2 == EMotionType::Dynamic);
538 return TemplatedSolveVelocityConstraint<EMotionType::Static, EMotionType::Dynamic>(motion_properties1, 0.0f , motion_properties2, inInvMass2, inWorldSpaceAxis, inMinLambda, inMaxLambda);
557 if (inC != 0.0f && !mSpringPart.
IsActive())
564 float lambda = -mEffectiveMass * inBaumgarte * inC;
608 if (inC != 0.0f && !mSpringPart.
IsActive())
615 float lambda = -mEffectiveMass * inBaumgarte * inC;
651 mTotalLambda = inLambda;
663 inStream.
Write(mTotalLambda);
669 inStream.
Read(mTotalLambda);
675 Float3 mInvI1_R1PlusUxAxis;
677 float mEffectiveMass = 0.0f;
679 float mTotalLambda = 0.0f;
#define JPH_IF_DEBUG(...)
Definition: Core.h:509
#define JPH_NAMESPACE_END
Definition: Core.h:378
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
#define JPH_DET_LOG(...)
By default we log nothing.
Definition: DeterminismLog.h:155
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
JPH_INLINE constexpr T Clamp(T inV, T inMin, T inMax)
Clamp a value between two values.
Definition: Math.h:45
EMotionType
Motion type of a physics body.
Definition: MotionType.h:11
Definition: AxisConstraintPart.h:43
float GetTotalLambda() const
Return lagrange multiplier.
Definition: AxisConstraintPart.h:655
bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
Definition: AxisConstraintPart.h:450
void CalculateConstraintPropertiesWithSettings(float inDeltaTime, const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias, float inC, const SpringSettings &inSpringSettings)
Selects one of the above functions based on the spring settings.
Definition: AxisConstraintPart.h:329
bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inC, float inBaumgarte) const
Definition: AxisConstraintPart.h:554
bool IsActive() const
Check if constraint is active.
Definition: AxisConstraintPart.h:349
void SetTotalLambda(float inLambda)
Override total lagrange multiplier, can be used to set the initial value for warm starting.
Definition: AxisConstraintPart.h:649
bool TemplatedSolveVelocityConstraint(MotionProperties *ioMotionProperties1, float inInvMass1, MotionProperties *ioMotionProperties2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
Templated form of SolveVelocityConstraint with the motion types baked in.
Definition: AxisConstraintPart.h:434
JPH_INLINE void TemplatedCalculateConstraintProperties(float inInvMass1, Mat44Arg inInvI1, Vec3Arg inR1PlusU, float inInvMass2, Mat44Arg inInvI2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias=0.0f)
Templated form of CalculateConstraintProperties with the motion types baked in.
Definition: AxisConstraintPart.h:227
void CalculateConstraintPropertiesWithFrequencyAndDamping(float inDeltaTime, const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias, float inC, float inFrequency, float inDamping)
Definition: AxisConstraintPart.h:297
void WarmStart(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inWarmStartImpulseRatio)
Definition: AxisConstraintPart.h:368
void Deactivate()
Deactivate this constraint.
Definition: AxisConstraintPart.h:342
void CalculateConstraintPropertiesWithMassOverride(const Body &inBody1, float inInvMass1, float inInvInertiaScale1, Vec3Arg inR1PlusU, const Body &inBody2, float inInvMass2, float inInvInertiaScale2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias=0.0f)
Definition: AxisConstraintPart.h:273
bool SolvePositionConstraintWithMassOverride(Body &ioBody1, float inInvMass1, Body &ioBody2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inC, float inBaumgarte) const
Definition: AxisConstraintPart.h:605
JPH_INLINE float TemplatedSolveVelocityConstraintGetTotalLambda(const MotionProperties *ioMotionProperties1, const MotionProperties *ioMotionProperties2, Vec3Arg inWorldSpaceAxis) const
Templated form of SolveVelocityConstraint with the motion types baked in, part 1: get the total lambd...
Definition: AxisConstraintPart.h:394
bool SolveVelocityConstraintWithMassOverride(Body &ioBody1, float inInvMass1, Body &ioBody2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
Definition: AxisConstraintPart.h:503
JPH_INLINE bool TemplatedSolveVelocityConstraintApplyLambda(MotionProperties *ioMotionProperties1, float inInvMass1, MotionProperties *ioMotionProperties2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inTotalLambda)
Templated form of SolveVelocityConstraint with the motion types baked in, part 2: apply new lambda.
Definition: AxisConstraintPart.h:424
void SaveState(StateRecorder &inStream) const
Save state of this constraint part.
Definition: AxisConstraintPart.h:661
void TemplatedWarmStart(MotionProperties *ioMotionProperties1, float inInvMass1, MotionProperties *ioMotionProperties2, float inInvMass2, Vec3Arg inWorldSpaceAxis, float inWarmStartImpulseRatio)
Templated form of WarmStart with the motion types baked in.
Definition: AxisConstraintPart.h:356
void CalculateConstraintPropertiesWithStiffnessAndDamping(float inDeltaTime, const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias, float inC, float inStiffness, float inDamping)
Definition: AxisConstraintPart.h:318
void CalculateConstraintProperties(const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias=0.0f)
Definition: AxisConstraintPart.h:249
void RestoreState(StateRecorder &inStream)
Restore state of this constraint part.
Definition: AxisConstraintPart.h:667
const MotionProperties * GetMotionProperties() const
Access to the motion properties.
Definition: Body.h:263
EMotionType GetMotionType() const
Get the bodies motion type.
Definition: Body.h:114
bool IsDynamic() const
Check if this body is dynamic, which means that it moves and forces can act on it.
Definition: Body.h:63
void AddRotationStep(Vec3Arg inAngularVelocityTimesDeltaTime)
Update rotation using an Euler step (used during position integrate & constraint solving)
Definition: Body.inl:81
void SubPositionStep(Vec3Arg inLinearVelocityTimesDeltaTime)
Definition: Body.h:298
Mat44 GetInverseInertia() const
Get inverse inertia tensor in world space.
Definition: Body.inl:120
void SubRotationStep(Vec3Arg inAngularVelocityTimesDeltaTime)
Definition: Body.inl:100
const MotionProperties * GetMotionPropertiesUnchecked() const
Access to the motion properties (version that does not check if the object is kinematic or dynamic)
Definition: Body.h:267
void AddPositionStep(Vec3Arg inLinearVelocityTimesDeltaTime)
Update position using an Euler step (used during position integrate & constraint solving)
Definition: Body.h:297
Class that holds 3 floats. Used as a storage class. Convert to Vec3 for calculations.
Definition: Float3.h:13
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition: Mat44.h:13
JPH_INLINE Vec3 Multiply3x3(Vec3Arg inV) const
Multiply vector by only 3x3 part of the matrix.
Definition: Mat44.inl:316
The Body class only keeps track of state for static bodies, the MotionProperties class keeps the addi...
Definition: MotionProperties.h:29
void AddLinearVelocityStep(Vec3Arg inLinearVelocityChange)
Definition: MotionProperties.h:191
Vec3 GetLinearVelocity() const
Get world space linear velocity of the center of mass.
Definition: MotionProperties.h:43
Vec3 GetAngularVelocity() const
Get world space angular velocity of the center of mass.
Definition: MotionProperties.h:52
void SubLinearVelocityStep(Vec3Arg inLinearVelocityChange)
Definition: MotionProperties.h:192
float GetInverseMass() const
Get inverse mass (1 / mass). Should only be called on a dynamic object (static or kinematic bodies ha...
Definition: MotionProperties.h:95
void SubAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition: MotionProperties.h:194
void AddAngularVelocityStep(Vec3Arg inAngularVelocityChange)
Definition: MotionProperties.h:193
Class used in other constraint parts to calculate the required bias factor in the lagrange multiplier...
Definition: SpringPart.h:14
void CalculateSpringPropertiesWithFrequencyAndDamping(float inDeltaTime, float inInvEffectiveMass, float inBias, float inC, float inFrequency, float inDamping, float &outEffectiveMass)
Definition: SpringPart.h:86
void CalculateSpringPropertiesWithStiffnessAndDamping(float inDeltaTime, float inInvEffectiveMass, float inBias, float inC, float inStiffness, float inDamping, float &outEffectiveMass)
Definition: SpringPart.h:116
float GetBias(float inTotalLambda) const
Get total bias b, including supplied bias and bias for spring: lambda = J v + b.
Definition: SpringPart.h:137
void CalculateSpringPropertiesWithBias(float inBias)
Definition: SpringPart.h:71
bool IsActive() const
Returns if this spring is active.
Definition: SpringPart.h:131
Settings for a linear or angular spring.
Definition: SpringSettings.h:23
float mStiffness
Definition: SpringSettings.h:60
float mDamping
Definition: SpringSettings.h:67
ESpringMode mMode
Definition: SpringSettings.h:44
float mFrequency
Definition: SpringSettings.h:51
Definition: StateRecorder.h:105
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
JPH_INLINE float Dot(Vec3Arg inV2) const
Dot product.
Definition: Vec3.inl:649
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition: Vec3.inl:594
JPH_INLINE bool IsNormalized(float inTolerance=1.0e-6f) const
Test if vector is normalized.
Definition: Vec3.inl:749
JPH_INLINE void StoreFloat3(Float3 *outV) const
Store 3 floats to memory.
Definition: Vec3.inl:769
static JPH_INLINE Vec3 sLoadFloat3Unsafe(const Float3 &inV)
Load 3 floats from memory (reads 32 bits extra which it doesn't use)
Definition: Vec3.inl:134
static JPH_INLINE Vec3 sNaN()
Vector with all NaN's.
Definition: Vec3.inl:129