44 template <EMotionType Type1, EMotionType Type2>
75 template <EMotionType Type1, EMotionType Type2>
76 JPH_INLINE
void TemplatedCalculateConstraintProperties(
float inDeltaTime,
const MotionProperties *inMotionProperties1,
Mat44Arg inInvI1,
Vec3Arg inR1PlusU,
const MotionProperties *inMotionProperties2,
Mat44Arg inInvI2,
Vec3Arg inR2,
Vec3Arg inWorldSpaceAxis,
float inBias = 0.0f,
float inC = 0.0f,
float inFrequency = 0.0f,
float inDamping = 0.0f)
81 Vec3 r1_plus_u_x_axis;
84 r1_plus_u_x_axis = inR1PlusU.
Cross(inWorldSpaceAxis);
92 r2_x_axis = inR2.
Cross(inWorldSpaceAxis);
98 float inv_effective_mass;
103 invi1_r1_plus_u_x_axis.
StoreFloat3(&mInvI1_R1PlusUxAxis);
104 inv_effective_mass = inMotionProperties1->
GetInverseMass() + invi1_r1_plus_u_x_axis.
Dot(r1_plus_u_x_axis);
108 (void)r1_plus_u_x_axis;
110 inv_effective_mass = 0.0f;
117 inv_effective_mass += inMotionProperties2->
GetInverseMass() + invi2_r2_x_axis.
Dot(r2_x_axis);
126 mSpringPart.
CalculateSpringProperties(inDeltaTime, inv_effective_mass, inBias, inC, inFrequency, inDamping, mEffectiveMass);
128 JPH_DET_LOG(
"TemplatedCalculateConstraintProperties: dt: " << inDeltaTime <<
" invI1: " << inInvI1 <<
" r1PlusU: " << inR1PlusU <<
" invI2: " << inInvI2 <<
" r2: " << inR2 <<
" bias: " << inBias <<
", c: " << inC <<
", frequency: " << inFrequency <<
", damping: " << inDamping <<
" r1PlusUxAxis: " << mR1PlusUxAxis <<
" r2xAxis: " << mR2xAxis <<
" invI1_R1PlusUxAxis: " << mInvI1_R1PlusUxAxis <<
" invI2_R2xAxis: " << mInvI2_R2xAxis <<
" effectiveMass: " << mEffectiveMass <<
" totalLambda: " << mTotalLambda);
154 TemplatedCalculateConstraintProperties<EMotionType::Dynamic, EMotionType::Dynamic>(inDeltaTime, mp1, invi1, inR1PlusU, inBody2.
GetMotionPropertiesUnchecked(), inBody2.
GetInverseInertia(), inR2, inWorldSpaceAxis, inBias, inC, inFrequency, inDamping);
158 TemplatedCalculateConstraintProperties<EMotionType::Dynamic, EMotionType::Kinematic>(inDeltaTime, mp1, invi1, inR1PlusU,
nullptr,
Mat44() , inR2, inWorldSpaceAxis, inBias, inC, inFrequency, inDamping);
162 TemplatedCalculateConstraintProperties<EMotionType::Dynamic, EMotionType::Static>(inDeltaTime, mp1, invi1, inR1PlusU,
nullptr,
Mat44() , inR2, inWorldSpaceAxis, inBias, inC, inFrequency, inDamping);
174 TemplatedCalculateConstraintProperties<EMotionType::Kinematic, EMotionType::Dynamic>(inDeltaTime,
nullptr,
Mat44() , inR1PlusU, inBody2.
GetMotionPropertiesUnchecked(), inBody2.
GetInverseInertia(), inR2, inWorldSpaceAxis, inBias, inC, inFrequency, inDamping);
179 TemplatedCalculateConstraintProperties<EMotionType::Static, EMotionType::Dynamic>(inDeltaTime,
nullptr,
Mat44() , inR1PlusU, inBody2.
GetMotionPropertiesUnchecked(), inBody2.
GetInverseInertia(), inR2, inWorldSpaceAxis, inBias, inC, inFrequency, inDamping);
191 mEffectiveMass = 0.0f;
198 return mEffectiveMass != 0.0f;
202 template <EMotionType Type1, EMotionType Type2>
205 mTotalLambda *= inWarmStartImpulseRatio;
207 ApplyVelocityStep<Type1, Type2>(ioMotionProperties1, ioMotionProperties2, inWorldSpaceAxis, mTotalLambda);
228 TemplatedWarmStart<EMotionType::Dynamic, EMotionType::Dynamic>(motion_properties1, motion_properties2, inWorldSpaceAxis, inWarmStartImpulseRatio);
230 TemplatedWarmStart<EMotionType::Dynamic, EMotionType::Static>(motion_properties1, motion_properties2, inWorldSpaceAxis, inWarmStartImpulseRatio);
235 TemplatedWarmStart<EMotionType::Static, EMotionType::Dynamic>(motion_properties1, motion_properties2, inWorldSpaceAxis, inWarmStartImpulseRatio);
240 template <EMotionType Type1, EMotionType Type2>
263 float lambda = mEffectiveMass * (jv - mSpringPart.
GetBias(mTotalLambda));
264 float new_lambda =
Clamp(mTotalLambda + lambda, inMinLambda, inMaxLambda);
265 lambda = new_lambda - mTotalLambda;
266 mTotalLambda = new_lambda;
268 return ApplyVelocityStep<Type1, Type2>(ioMotionProperties1, ioMotionProperties2, inWorldSpaceAxis, lambda);
286 switch (motion_type1)
289 switch (motion_type2)
292 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Dynamic>(motion_properties1, motion_properties2, inWorldSpaceAxis, inMinLambda, inMaxLambda);
295 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Kinematic>(motion_properties1, motion_properties2, inWorldSpaceAxis, inMinLambda, inMaxLambda);
298 return TemplatedSolveVelocityConstraint<EMotionType::Dynamic, EMotionType::Static>(motion_properties1, motion_properties2, inWorldSpaceAxis, inMinLambda, inMaxLambda);
308 return TemplatedSolveVelocityConstraint<EMotionType::Kinematic, EMotionType::Dynamic>(motion_properties1, motion_properties2, inWorldSpaceAxis, inMinLambda, inMaxLambda);
312 return TemplatedSolveVelocityConstraint<EMotionType::Static, EMotionType::Dynamic>(motion_properties1, motion_properties2, inWorldSpaceAxis, inMinLambda, inMaxLambda);
331 if (inC != 0.0f && !mSpringPart.
IsActive())
338 float lambda = -mEffectiveMass * inBaumgarte * inC;
374 mTotalLambda = inLambda;
386 inStream.
Write(mTotalLambda);
392 inStream.
Read(mTotalLambda);
398 Float3 mInvI1_R1PlusUxAxis;
400 float mEffectiveMass = 0.0f;
402 float mTotalLambda = 0.0f;
#define JPH_IF_DEBUG(...)
Definition Core.h:354
#define JPH_NAMESPACE_END
Definition Core.h:240
#define JPH_NAMESPACE_BEGIN
Definition Core.h:234
#define JPH_DET_LOG(...)
By default we log nothing.
Definition DeterminismLog.h:155
#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
EMotionType
Motion type of a physics body.
Definition MotionType.h:11
@ Kinematic
Movable using velocities only, does not respond to forces.
@ Dynamic
Responds to forces as a normal physics object.
Definition AxisConstraintPart.h:42
float GetTotalLambda() const
Return lagrange multiplier.
Definition AxisConstraintPart.h:378
bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
Definition AxisConstraintPart.h:277
void CalculateConstraintProperties(float inDeltaTime, const Body &inBody1, Vec3Arg inR1PlusU, const Body &inBody2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias=0.0f, float inC=0.0f, float inFrequency=0.0f, float inDamping=0.0f)
Definition AxisConstraintPart.h:142
bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inC, float inBaumgarte) const
Definition AxisConstraintPart.h:328
bool IsActive() const
Check if constraint is active.
Definition AxisConstraintPart.h:196
void SetTotalLambda(float inLambda)
Override total lagrange multiplier, can be used to set the initial value for warm starting.
Definition AxisConstraintPart.h:372
void WarmStart(Body &ioBody1, Body &ioBody2, Vec3Arg inWorldSpaceAxis, float inWarmStartImpulseRatio)
Definition AxisConstraintPart.h:215
bool TemplatedSolveVelocityConstraint(MotionProperties *ioMotionProperties1, MotionProperties *ioMotionProperties2, Vec3Arg inWorldSpaceAxis, float inMinLambda, float inMaxLambda)
Templated form of SolveVelocityConstraint with the motion types baked in.
Definition AxisConstraintPart.h:241
void Deactivate()
Deactivate this constraint.
Definition AxisConstraintPart.h:189
void TemplatedWarmStart(MotionProperties *ioMotionProperties1, MotionProperties *ioMotionProperties2, Vec3Arg inWorldSpaceAxis, float inWarmStartImpulseRatio)
Templated form of WarmStart with the motion types baked in.
Definition AxisConstraintPart.h:203
JPH_INLINE void TemplatedCalculateConstraintProperties(float inDeltaTime, const MotionProperties *inMotionProperties1, Mat44Arg inInvI1, Vec3Arg inR1PlusU, const MotionProperties *inMotionProperties2, Mat44Arg inInvI2, Vec3Arg inR2, Vec3Arg inWorldSpaceAxis, float inBias=0.0f, float inC=0.0f, float inFrequency=0.0f, float inDamping=0.0f)
Templated form of CalculateConstraintProperties with the motion types baked in.
Definition AxisConstraintPart.h:76
void SaveState(StateRecorder &inStream) const
Save state of this constraint part.
Definition AxisConstraintPart.h:384
void RestoreState(StateRecorder &inStream)
Restore state of this constraint part.
Definition AxisConstraintPart.h:390
EMotionType GetMotionType() const
Motion type of this body.
Definition Body.h:82
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
Mat44 GetInverseInertia() const
Get inverse inertia tensor in world space.
Definition Body.inl:112
void SubRotationStep(Vec3Arg inAngularVelocityTimesDeltaTime)
Definition Body.inl:93
const MotionProperties * GetMotionPropertiesUnchecked() const
Access to the motion properties (version that does not check if the object is kinematic or dynamic)
Definition Body.h:209
void AddPositionStep(Vec3Arg inLinearVelocityTimesDeltaTime)
Update position using an Euler step (used during position integrate & constraint solving)
Definition Body.h:236
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:307
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
Vec3 GetLinearVelocity() const
Get world space linear velocity of the center of mass.
Definition MotionProperties.h:28
Vec3 GetAngularVelocity() const
Get world space angular velocity of the center of mass.
Definition MotionProperties.h:37
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
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
JPH_INLINE float Dot(Vec3Arg inV2) const
Dot product.
Definition Vec3.inl:637
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition Vec3.inl:582
JPH_INLINE bool IsNormalized(float inTolerance=1.0e-6f) const
Test if vector is normalized.
Definition Vec3.inl:737
JPH_INLINE void StoreFloat3(Float3 *outV) const
Store 3 floats to memory.
Definition Vec3.inl:757
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