| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Physics/Body/Body.h> |
| 8 |
#include <Jolt/Physics/StateRecorder.h> |
| 9 |
#include <Jolt/Math/Vector.h> |
| 10 |
#include <Jolt/Math/Matrix.h> |
| 11 |
|
| 12 |
JPH_NAMESPACE_BEGIN |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class HingeRotationConstraintPart |
| 44 |
{ |
| 45 |
public: |
| 46 |
using Vec2 = Vector<2>; |
| 47 |
using Mat22 = Matrix<2, 2>; |
| 48 |
|
| 49 |
private: |
| 50 |
|
| 51 |
JPH_INLINE bool ApplyVelocityStep(Body &ioBody1, Body &ioBody2, const Vec2 &inLambda) const |
| 52 |
{ |
| 53 |
|
| 54 |
if (!inLambda.IsZero()) |
| 55 |
{ |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
Vec3 impulse = mB2xA1 * inLambda[0] + mC2xA1 * inLambda[1]; |
| 64 |
if (ioBody1.IsDynamic()) |
| 65 |
ioBody1.GetMotionProperties()->SubAngularVelocityStep(mInvI1.Multiply3x3(impulse)); |
| 66 |
if (ioBody2.IsDynamic()) |
| 67 |
ioBody2.GetMotionProperties()->AddAngularVelocityStep(mInvI2.Multiply3x3(impulse)); |
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
public: |
| 75 |
|
| 76 |
inline void CalculateConstraintProperties(const Body &inBody1, Mat44Arg inRotation1, Vec3Arg inWorldSpaceHingeAxis1, const Body &inBody2, Mat44Arg inRotation2, Vec3Arg inWorldSpaceHingeAxis2) |
| 77 |
{ |
| 78 |
JPH_ASSERT(inWorldSpaceHingeAxis1.IsNormalized(1.0e-5f)); |
| 79 |
JPH_ASSERT(inWorldSpaceHingeAxis2.IsNormalized(1.0e-5f)); |
| 80 |
|
| 81 |
|
| 82 |
mA1 = inWorldSpaceHingeAxis1; |
| 83 |
Vec3 a2 = inWorldSpaceHingeAxis2; |
| 84 |
float dot = mA1.Dot(a2); |
| 85 |
if (dot <= 1.0e-3f) |
| 86 |
{ |
| 87 |
|
| 88 |
Vec3 perp = a2 - dot * mA1; |
| 89 |
if (perp.LengthSq() < 1.0e-6f) |
| 90 |
{ |
| 91 |
|
| 92 |
perp = mA1.GetNormalizedPerpendicular(); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
a2 = (0.99f * perp.Normalized() + 0.01f * mA1).Normalized(); |
| 97 |
} |
| 98 |
mB2 = a2.GetNormalizedPerpendicular(); |
| 99 |
mC2 = a2.Cross(mB2); |
| 100 |
|
| 101 |
|
| 102 |
mInvI1 = inBody1.IsDynamic()? inBody1.GetMotionProperties()->GetInverseInertiaForRotation(inRotation1) : Mat44::sZero(); |
| 103 |
mInvI2 = inBody2.IsDynamic()? inBody2.GetMotionProperties()->GetInverseInertiaForRotation(inRotation2) : Mat44::sZero(); |
| 104 |
mB2xA1 = mB2.Cross(mA1); |
| 105 |
mC2xA1 = mC2.Cross(mA1); |
| 106 |
|
| 107 |
|
| 108 |
Mat44 summed_inv_inertia = mInvI1 + mInvI2; |
| 109 |
Mat22 inv_effective_mass; |
| 110 |
inv_effective_mass(0, 0) = mB2xA1.Dot(summed_inv_inertia.Multiply3x3(mB2xA1)); |
| 111 |
inv_effective_mass(0, 1) = mB2xA1.Dot(summed_inv_inertia.Multiply3x3(mC2xA1)); |
| 112 |
inv_effective_mass(1, 0) = mC2xA1.Dot(summed_inv_inertia.Multiply3x3(mB2xA1)); |
| 113 |
inv_effective_mass(1, 1) = mC2xA1.Dot(summed_inv_inertia.Multiply3x3(mC2xA1)); |
| 114 |
if (!mEffectiveMass.SetInversed(inv_effective_mass)) |
| 115 |
Deactivate(); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
inline void Deactivate() |
| 120 |
{ |
| 121 |
mEffectiveMass.SetZero(); |
| 122 |
mTotalLambda.SetZero(); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
inline void WarmStart(Body &ioBody1, Body &ioBody2, float inWarmStartImpulseRatio) |
| 127 |
{ |
| 128 |
mTotalLambda *= inWarmStartImpulseRatio; |
| 129 |
ApplyVelocityStep(ioBody1, ioBody2, mTotalLambda); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
inline bool SolveVelocityConstraint(Body &ioBody1, Body &ioBody2) |
| 134 |
{ |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
Vec3 delta_ang = ioBody1.GetAngularVelocity() - ioBody2.GetAngularVelocity(); |
| 139 |
Vec2 jv; |
| 140 |
jv[0] = mB2xA1.Dot(delta_ang); |
| 141 |
jv[1] = mC2xA1.Dot(delta_ang); |
| 142 |
Vec2 lambda = mEffectiveMass * jv; |
| 143 |
|
| 144 |
|
| 145 |
mTotalLambda += lambda; |
| 146 |
|
| 147 |
return ApplyVelocityStep(ioBody1, ioBody2, lambda); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
inline bool SolvePositionConstraint(Body &ioBody1, Body &ioBody2, float inBaumgarte) const |
| 152 |
{ |
| 153 |
|
| 154 |
Vec2 c; |
| 155 |
c[0] = mA1.Dot(mB2); |
| 156 |
c[1] = mA1.Dot(mC2); |
| 157 |
if (!c.IsZero()) |
| 158 |
{ |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
Vec2 lambda = -inBaumgarte * (mEffectiveMass * c); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
Vec3 impulse = mB2xA1 * lambda[0] + mC2xA1 * lambda[1]; |
| 182 |
if (ioBody1.IsDynamic()) |
| 183 |
ioBody1.SubRotationStep(mInvI1.Multiply3x3(impulse)); |
| 184 |
if (ioBody2.IsDynamic()) |
| 185 |
ioBody2.AddRotationStep(mInvI2.Multiply3x3(impulse)); |
| 186 |
return true; |
| 187 |
} |
| 188 |
|
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
const Vec2 & GetTotalLambda() const |
| 194 |
{ |
| 195 |
return mTotalLambda; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
void SaveState(StateRecorder &inStream) const |
| 200 |
{ |
| 201 |
inStream.Write(mTotalLambda); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
void RestoreState(StateRecorder &inStream) |
| 206 |
{ |
| 207 |
inStream.Read(mTotalLambda); |
| 208 |
} |
| 209 |
|
| 210 |
private: |
| 211 |
Vec3 mA1; |
| 212 |
Vec3 mB2; |
| 213 |
Vec3 mC2; |
| 214 |
Mat44 mInvI1; |
| 215 |
Mat44 mInvI2; |
| 216 |
Vec3 mB2xA1; |
| 217 |
Vec3 mC2xA1; |
| 218 |
Mat22 mEffectiveMass; |
| 219 |
Vec2 mTotalLambda { Vec2::sZero() }; |
| 220 |
}; |
| 221 |
|
| 222 |
JPH_NAMESPACE_END |