| 1 | // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics) |
| 2 | // SPDX-FileCopyrightText: 2021 Jorrit Rouwe |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | #include <Jolt/Jolt.h> |
| 6 | |
| 7 | #include <Jolt/Physics/Constraints/HingeConstraint.h> |
| 8 | #include <Jolt/Physics/Constraints/ConstraintPart/RotationEulerConstraintPart.h> |
| 9 | #include <Jolt/Physics/Body/Body.h> |
| 10 | #include <Jolt/ObjectStream/TypeDeclarations.h> |
| 11 | #include <Jolt/Core/StreamIn.h> |
| 12 | #include <Jolt/Core/StreamOut.h> |
| 13 | #ifdef JPH_DEBUG_RENDERER |
| 14 | #include <Jolt/Renderer/DebugRenderer.h> |
| 15 | #endif // JPH_DEBUG_RENDERER |
| 16 | |
| 17 | JPH_NAMESPACE_BEGIN |
| 18 | |
| 19 | JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(HingeConstraintSettings) |
| 20 | { |
| 21 | JPH_ADD_BASE_CLASS(HingeConstraintSettings, TwoBodyConstraintSettings) |
| 22 | |
| 23 | JPH_ADD_ENUM_ATTRIBUTE(HingeConstraintSettings, mSpace) |
| 24 | JPH_ADD_ATTRIBUTE(HingeConstraintSettings, mPoint1) |
| 25 | JPH_ADD_ATTRIBUTE(HingeConstraintSettings, mHingeAxis1) |
| 26 | JPH_ADD_ATTRIBUTE(HingeConstraintSettings, mNormalAxis1) |
| 27 | JPH_ADD_ATTRIBUTE(HingeConstraintSettings, mPoint2) |
| 28 | JPH_ADD_ATTRIBUTE(HingeConstraintSettings, mHingeAxis2) |
| 29 | JPH_ADD_ATTRIBUTE(HingeConstraintSettings, mNormalAxis2) |
| 30 | JPH_ADD_ATTRIBUTE(HingeConstraintSettings, mLimitsMin) |
| 31 | JPH_ADD_ATTRIBUTE(HingeConstraintSettings, mLimitsMax) |
| 32 | JPH_ADD_ATTRIBUTE(HingeConstraintSettings, mLimitsSpringSettings) |
| 33 | JPH_ADD_ATTRIBUTE(HingeConstraintSettings, mMaxFrictionTorque) |
| 34 | JPH_ADD_ATTRIBUTE(HingeConstraintSettings, mMotorSettings) |
| 35 | } |
| 36 | |
| 37 | void HingeConstraintSettings::SaveBinaryState(StreamOut &inStream) const |
| 38 | { |
| 39 | ConstraintSettings::SaveBinaryState(inStream); |
| 40 | |
| 41 | inStream.Write(mSpace); |
| 42 | inStream.Write(mPoint1); |
| 43 | inStream.Write(mHingeAxis1); |
| 44 | inStream.Write(mNormalAxis1); |
| 45 | inStream.Write(mPoint2); |
| 46 | inStream.Write(mHingeAxis2); |
| 47 | inStream.Write(mNormalAxis2); |
| 48 | inStream.Write(mLimitsMin); |
| 49 | inStream.Write(mLimitsMax); |
| 50 | inStream.Write(mMaxFrictionTorque); |
| 51 | mLimitsSpringSettings.SaveBinaryState(inStream); |
| 52 | mMotorSettings.SaveBinaryState(inStream); |
| 53 | } |
| 54 | |
| 55 | void HingeConstraintSettings::RestoreBinaryState(StreamIn &inStream) |
| 56 | { |
| 57 | ConstraintSettings::RestoreBinaryState(inStream); |
| 58 | |
| 59 | inStream.Read(mSpace); |
| 60 | inStream.Read(mPoint1); |
| 61 | inStream.Read(mHingeAxis1); |
| 62 | inStream.Read(mNormalAxis1); |
| 63 | inStream.Read(mPoint2); |
| 64 | inStream.Read(mHingeAxis2); |
| 65 | inStream.Read(mNormalAxis2); |
| 66 | inStream.Read(mLimitsMin); |
| 67 | inStream.Read(mLimitsMax); |
| 68 | inStream.Read(mMaxFrictionTorque); |
| 69 | mLimitsSpringSettings.RestoreBinaryState(inStream); |
| 70 | mMotorSettings.RestoreBinaryState(inStream);} |
| 71 | |
| 72 | TwoBodyConstraint *HingeConstraintSettings::Create(Body &inBody1, Body &inBody2) const |
| 73 | { |
| 74 | return new HingeConstraint(inBody1, inBody2, *this); |
| 75 | } |
| 76 | |
| 77 | HingeConstraint::HingeConstraint(Body &inBody1, Body &inBody2, const HingeConstraintSettings &inSettings) : |
| 78 | TwoBodyConstraint(inBody1, inBody2, inSettings), |
| 79 | mMaxFrictionTorque(inSettings.mMaxFrictionTorque), |
| 80 | mMotorSettings(inSettings.mMotorSettings) |
| 81 | { |
| 82 | // Store limits |
| 83 | JPH_ASSERT(inSettings.mLimitsMin != inSettings.mLimitsMax || inSettings.mLimitsSpringSettings.mFrequency > 0.0f, "Better use a fixed constraint in this case"); |
| 84 | SetLimits(inSettings.mLimitsMin, inSettings.mLimitsMax); |
| 85 | |
| 86 | // Store inverse of initial rotation from body 1 to body 2 in body 1 space |
| 87 | mInvInitialOrientation = RotationEulerConstraintPart::sGetInvInitialOrientationXZ(inSettings.mNormalAxis1, inSettings.mHingeAxis1, inSettings.mNormalAxis2, inSettings.mHingeAxis2); |
| 88 | |
| 89 | if (inSettings.mSpace == EConstraintSpace::WorldSpace) |
| 90 | { |
| 91 | // If all properties were specified in world space, take them to local space now |
| 92 | RMat44 inv_transform1 = inBody1.GetInverseCenterOfMassTransform(); |
| 93 | mLocalSpacePosition1 = Vec3(inv_transform1 * inSettings.mPoint1); |
| 94 | mLocalSpaceHingeAxis1 = inv_transform1.Multiply3x3(inSettings.mHingeAxis1).Normalized(); |
| 95 | mLocalSpaceNormalAxis1 = inv_transform1.Multiply3x3(inSettings.mNormalAxis1).Normalized(); |
| 96 | |
| 97 | RMat44 inv_transform2 = inBody2.GetInverseCenterOfMassTransform(); |
| 98 | mLocalSpacePosition2 = Vec3(inv_transform2 * inSettings.mPoint2); |
| 99 | mLocalSpaceHingeAxis2 = inv_transform2.Multiply3x3(inSettings.mHingeAxis2).Normalized(); |
| 100 | mLocalSpaceNormalAxis2 = inv_transform2.Multiply3x3(inSettings.mNormalAxis2).Normalized(); |
| 101 | |
| 102 | // Constraints were specified in world space, so we should have replaced c1 with q10^-1 c1 and c2 with q20^-1 c2 |
| 103 | // => r0^-1 = (q20^-1 c2) (q10^-1 c1)^1 = q20^-1 (c2 c1^-1) q10 |
| 104 | mInvInitialOrientation = inBody2.GetRotation().Conjugated() * mInvInitialOrientation * inBody1.GetRotation(); |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | mLocalSpacePosition1 = Vec3(inSettings.mPoint1); |
| 109 | mLocalSpaceHingeAxis1 = inSettings.mHingeAxis1; |
| 110 | mLocalSpaceNormalAxis1 = inSettings.mNormalAxis1; |
| 111 | |
| 112 | mLocalSpacePosition2 = Vec3(inSettings.mPoint2); |
| 113 | mLocalSpaceHingeAxis2 = inSettings.mHingeAxis2; |
| 114 | mLocalSpaceNormalAxis2 = inSettings.mNormalAxis2; |
| 115 | } |
| 116 | |
| 117 | // Store spring settings |
| 118 | SetLimitsSpringSettings(inSettings.mLimitsSpringSettings); |
| 119 | } |
| 120 | |
| 121 | void HingeConstraint::NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) |
| 122 | { |
| 123 | if (mBody1->GetID() == inBodyID) |
| 124 | mLocalSpacePosition1 -= inDeltaCOM; |
| 125 | else if (mBody2->GetID() == inBodyID) |
| 126 | mLocalSpacePosition2 -= inDeltaCOM; |
| 127 | } |
| 128 | |
| 129 | float HingeConstraint::GetCurrentAngle() const |
| 130 | { |
| 131 | // See: CalculateA1AndTheta |
| 132 | Quat rotation1 = mBody1->GetRotation(); |
| 133 | Quat diff = mBody2->GetRotation() * mInvInitialOrientation * rotation1.Conjugated(); |
| 134 | return diff.GetRotationAngle(rotation1 * mLocalSpaceHingeAxis1); |
| 135 | } |
| 136 | |
| 137 | void HingeConstraint::SetTargetOrientationBS(QuatArg inOrientation) |
| 138 | { |
| 139 | // See: CalculateA1AndTheta |
| 140 | // |
| 141 | // The rotation between body 1 and 2 can be written as: |
| 142 | // |
| 143 | // q2 = q1 rh1 r0 |
| 144 | // |
| 145 | // where rh1 is a rotation around local hinge axis 1, also: |
| 146 | // |
| 147 | // q2 = q1 inOrientation |
| 148 | // |
| 149 | // This means: |
| 150 | // |
| 151 | // rh1 r0 = inOrientation <=> rh1 = inOrientation * r0^-1 |
| 152 | Quat rh1 = inOrientation * mInvInitialOrientation; |
| 153 | SetTargetAngle(rh1.GetRotationAngle(mLocalSpaceHingeAxis1)); |
| 154 | } |
| 155 | |
| 156 | void HingeConstraint::SetLimits(float inLimitsMin, float inLimitsMax) |
| 157 | { |
| 158 | JPH_ASSERT(inLimitsMin <= 0.0f && inLimitsMin >= -JPH_PI); |
| 159 | JPH_ASSERT(inLimitsMax >= 0.0f && inLimitsMax <= JPH_PI); |
| 160 | mLimitsMin = inLimitsMin; |
| 161 | mLimitsMax = inLimitsMax; |
| 162 | mHasLimits = mLimitsMin > -JPH_PI || mLimitsMax < JPH_PI; |
| 163 | } |
| 164 | |
| 165 | void HingeConstraint::CalculateA1AndTheta() |
| 166 | { |
| 167 | if (mHasLimits || mMotorState != EMotorState::Off || mMaxFrictionTorque > 0.0f) |
| 168 | { |
| 169 | Quat rotation1 = mBody1->GetRotation(); |
| 170 | |
| 171 | // Calculate relative rotation in world space |
| 172 | // |
| 173 | // The rest rotation is: |
| 174 | // |
| 175 | // q2 = q1 r0 |
| 176 | // |
| 177 | // But the actual rotation is |
| 178 | // |
| 179 | // q2 = diff q1 r0 |
| 180 | // <=> diff = q2 r0^-1 q1^-1 |
| 181 | // |
| 182 | // Where: |
| 183 | // q1 = current rotation of body 1 |
| 184 | // q2 = current rotation of body 2 |
| 185 | // diff = relative rotation in world space |
| 186 | Quat diff = mBody2->GetRotation() * mInvInitialOrientation * rotation1.Conjugated(); |
| 187 | |
| 188 | // Calculate hinge axis in world space |
| 189 | mA1 = rotation1 * mLocalSpaceHingeAxis1; |
| 190 | |
| 191 | // Get rotation angle around the hinge axis |
| 192 | mTheta = diff.GetRotationAngle(mA1); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void HingeConstraint::CalculateRotationLimitsConstraintProperties(float inDeltaTime) |
| 197 | { |
| 198 | // Apply constraint if outside of limits |
| 199 | if (mHasLimits && (mTheta <= mLimitsMin || mTheta >= mLimitsMax)) |
| 200 | mRotationLimitsConstraintPart.CalculateConstraintPropertiesWithSettings(inDeltaTime, *mBody1, *mBody2, mA1, 0.0f, GetSmallestAngleToLimit(), mLimitsSpringSettings); |
| 201 | else |
| 202 | mRotationLimitsConstraintPart.Deactivate(); |
| 203 | } |
| 204 | |
| 205 | void HingeConstraint::CalculateMotorConstraintProperties(float inDeltaTime) |
| 206 | { |
| 207 | switch (mMotorState) |
| 208 | { |
| 209 | case EMotorState::Off: |
| 210 | if (mMaxFrictionTorque > 0.0f) |
| 211 | mMotorConstraintPart.CalculateConstraintProperties(*mBody1, *mBody2, mA1); |
| 212 | else |
| 213 | mMotorConstraintPart.Deactivate(); |
| 214 | break; |
| 215 | |
| 216 | case EMotorState::Velocity: |
| 217 | mMotorConstraintPart.CalculateConstraintProperties(*mBody1, *mBody2, mA1, -mTargetAngularVelocity); |
| 218 | break; |
| 219 | |
| 220 | case EMotorState::Position: |
| 221 | if (mMotorSettings.mSpringSettings.HasStiffness()) |
| 222 | mMotorConstraintPart.CalculateConstraintPropertiesWithSettings(inDeltaTime, *mBody1, *mBody2, mA1, 0.0f, CenterAngleAroundZero(mTheta - mTargetAngle), mMotorSettings.mSpringSettings); |
| 223 | else |
| 224 | mMotorConstraintPart.Deactivate(); |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | void HingeConstraint::SetupVelocityConstraint(float inDeltaTime) |
| 230 | { |
| 231 | // Cache constraint values that are valid until the bodies move |
| 232 | Mat44 rotation1 = Mat44::sRotation(mBody1->GetRotation()); |
| 233 | Mat44 rotation2 = Mat44::sRotation(mBody2->GetRotation()); |
| 234 | mPointConstraintPart.CalculateConstraintProperties(*mBody1, rotation1, mLocalSpacePosition1, *mBody2, rotation2, mLocalSpacePosition2); |
| 235 | mRotationConstraintPart.CalculateConstraintProperties(*mBody1, rotation1, rotation1.Multiply3x3(mLocalSpaceHingeAxis1), *mBody2, rotation2, rotation2.Multiply3x3(mLocalSpaceHingeAxis2)); |
| 236 | CalculateA1AndTheta(); |
| 237 | CalculateRotationLimitsConstraintProperties(inDeltaTime); |
| 238 | CalculateMotorConstraintProperties(inDeltaTime); |
| 239 | } |
| 240 | |
| 241 | void HingeConstraint::ResetWarmStart() |
| 242 | { |
| 243 | mMotorConstraintPart.Deactivate(); |
| 244 | mPointConstraintPart.Deactivate(); |
| 245 | mRotationConstraintPart.Deactivate(); |
| 246 | mRotationLimitsConstraintPart.Deactivate(); |
| 247 | } |
| 248 | |
| 249 | void HingeConstraint::WarmStartVelocityConstraint(float inWarmStartImpulseRatio) |
| 250 | { |
| 251 | // Warm starting: Apply previous frame impulse |
| 252 | mMotorConstraintPart.WarmStart(*mBody1, *mBody2, inWarmStartImpulseRatio); |
| 253 | mPointConstraintPart.WarmStart(*mBody1, *mBody2, inWarmStartImpulseRatio); |
| 254 | mRotationConstraintPart.WarmStart(*mBody1, *mBody2, inWarmStartImpulseRatio); |
| 255 | mRotationLimitsConstraintPart.WarmStart(*mBody1, *mBody2, inWarmStartImpulseRatio); |
| 256 | } |
| 257 | |
| 258 | float HingeConstraint::GetSmallestAngleToLimit() const |
| 259 | { |
| 260 | float dist_to_min = CenterAngleAroundZero(mTheta - mLimitsMin); |
| 261 | float dist_to_max = CenterAngleAroundZero(mTheta - mLimitsMax); |
| 262 | return abs(dist_to_min) < abs(dist_to_max)? dist_to_min : dist_to_max; |
| 263 | } |
| 264 | |
| 265 | bool HingeConstraint::IsMinLimitClosest() const |
| 266 | { |
| 267 | float dist_to_min = CenterAngleAroundZero(mTheta - mLimitsMin); |
| 268 | float dist_to_max = CenterAngleAroundZero(mTheta - mLimitsMax); |
| 269 | return abs(dist_to_min) < abs(dist_to_max); |
| 270 | } |
| 271 | |
| 272 | bool HingeConstraint::SolveVelocityConstraint(float inDeltaTime) |
| 273 | { |
| 274 | // Solve motor |
| 275 | bool motor = false; |
| 276 | if (mMotorConstraintPart.IsActive()) |
| 277 | { |
| 278 | switch (mMotorState) |
| 279 | { |
| 280 | case EMotorState::Off: |
| 281 | { |
| 282 | float max_lambda = mMaxFrictionTorque * inDeltaTime; |
| 283 | motor = mMotorConstraintPart.SolveVelocityConstraint(*mBody1, *mBody2, mA1, -max_lambda, max_lambda); |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | case EMotorState::Velocity: |
| 288 | case EMotorState::Position: |
| 289 | motor = mMotorConstraintPart.SolveVelocityConstraint(*mBody1, *mBody2, mA1, inDeltaTime * mMotorSettings.mMinTorqueLimit, inDeltaTime * mMotorSettings.mMaxTorqueLimit); |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // Solve point constraint |
| 295 | bool pos = mPointConstraintPart.SolveVelocityConstraint(*mBody1, *mBody2); |
| 296 | |
| 297 | // Solve rotation constraint |
| 298 | bool rot = mRotationConstraintPart.SolveVelocityConstraint(*mBody1, *mBody2); |
| 299 | |
| 300 | // Solve rotation limits |
| 301 | bool limit = false; |
| 302 | if (mRotationLimitsConstraintPart.IsActive()) |
| 303 | { |
| 304 | float min_lambda, max_lambda; |
| 305 | if (mLimitsMin == mLimitsMax) |
| 306 | { |
| 307 | min_lambda = -FLT_MAX; |
| 308 | max_lambda = FLT_MAX; |
| 309 | } |
| 310 | else if (IsMinLimitClosest()) |
| 311 | { |
| 312 | min_lambda = 0.0f; |
| 313 | max_lambda = FLT_MAX; |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | min_lambda = -FLT_MAX; |
| 318 | max_lambda = 0.0f; |
| 319 | } |
| 320 | limit = mRotationLimitsConstraintPart.SolveVelocityConstraint(*mBody1, *mBody2, mA1, min_lambda, max_lambda); |
| 321 | } |
| 322 | |
| 323 | return motor || pos || rot || limit; |
| 324 | } |
| 325 | |
| 326 | bool HingeConstraint::SolvePositionConstraint(float inDeltaTime, float inBaumgarte) |
| 327 | { |
| 328 | // Motor operates on velocities only, don't call SolvePositionConstraint |
| 329 | |
| 330 | // Solve point constraint |
| 331 | mPointConstraintPart.CalculateConstraintProperties(*mBody1, Mat44::sRotation(mBody1->GetRotation()), mLocalSpacePosition1, *mBody2, Mat44::sRotation(mBody2->GetRotation()), mLocalSpacePosition2); |
| 332 | bool pos = mPointConstraintPart.SolvePositionConstraint(*mBody1, *mBody2, inBaumgarte); |
| 333 | |
| 334 | // Solve rotation constraint |
| 335 | Mat44 rotation1 = Mat44::sRotation(mBody1->GetRotation()); // Note that previous call to GetRotation() is out of date since the rotation has changed |
| 336 | Mat44 rotation2 = Mat44::sRotation(mBody2->GetRotation()); |
| 337 | mRotationConstraintPart.CalculateConstraintProperties(*mBody1, rotation1, rotation1.Multiply3x3(mLocalSpaceHingeAxis1), *mBody2, rotation2, rotation2.Multiply3x3(mLocalSpaceHingeAxis2)); |
| 338 | bool rot = mRotationConstraintPart.SolvePositionConstraint(*mBody1, *mBody2, inBaumgarte); |
| 339 | |
| 340 | // Solve rotation limits |
| 341 | bool limit = false; |
| 342 | if (mHasLimits && mLimitsSpringSettings.mFrequency <= 0.0f) |
| 343 | { |
| 344 | CalculateA1AndTheta(); |
| 345 | CalculateRotationLimitsConstraintProperties(inDeltaTime); |
| 346 | if (mRotationLimitsConstraintPart.IsActive()) |
| 347 | limit = mRotationLimitsConstraintPart.SolvePositionConstraint(*mBody1, *mBody2, GetSmallestAngleToLimit(), inBaumgarte); |
| 348 | } |
| 349 | |
| 350 | return pos || rot || limit; |
| 351 | } |
| 352 | |
| 353 | #ifdef JPH_DEBUG_RENDERER |
| 354 | void HingeConstraint::DrawConstraint(DebugRenderer *inRenderer) const |
| 355 | { |
| 356 | RMat44 transform1 = mBody1->GetCenterOfMassTransform(); |
| 357 | RMat44 transform2 = mBody2->GetCenterOfMassTransform(); |
| 358 | |
| 359 | // Draw constraint |
| 360 | RVec3 constraint_pos1 = transform1 * mLocalSpacePosition1; |
| 361 | inRenderer->DrawMarker(constraint_pos1, Color::sRed, 0.1f); |
| 362 | inRenderer->DrawLine(constraint_pos1, transform1 * (mLocalSpacePosition1 + mDrawConstraintSize * mLocalSpaceHingeAxis1), Color::sRed); |
| 363 | |
| 364 | RVec3 constraint_pos2 = transform2 * mLocalSpacePosition2; |
| 365 | inRenderer->DrawMarker(constraint_pos2, Color::sGreen, 0.1f); |
| 366 | inRenderer->DrawLine(constraint_pos2, transform2 * (mLocalSpacePosition2 + mDrawConstraintSize * mLocalSpaceHingeAxis2), Color::sGreen); |
| 367 | inRenderer->DrawLine(constraint_pos2, transform2 * (mLocalSpacePosition2 + mDrawConstraintSize * mLocalSpaceNormalAxis2), Color::sWhite); |
| 368 | } |
| 369 | |
| 370 | void HingeConstraint::DrawConstraintLimits(DebugRenderer *inRenderer) const |
| 371 | { |
| 372 | if (mHasLimits && mLimitsMax > mLimitsMin) |
| 373 | { |
| 374 | // Get constraint properties in world space |
| 375 | RMat44 transform1 = mBody1->GetCenterOfMassTransform(); |
| 376 | RVec3 position1 = transform1 * mLocalSpacePosition1; |
| 377 | Vec3 hinge_axis1 = transform1.Multiply3x3(mLocalSpaceHingeAxis1); |
| 378 | Vec3 normal_axis1 = transform1.Multiply3x3(mLocalSpaceNormalAxis1); |
| 379 | |
| 380 | inRenderer->DrawPie(position1, mDrawConstraintSize, hinge_axis1, normal_axis1, mLimitsMin, mLimitsMax, Color::sPurple, DebugRenderer::ECastShadow::Off); |
| 381 | } |
| 382 | } |
| 383 | #endif // JPH_DEBUG_RENDERER |
| 384 | |
| 385 | void HingeConstraint::SaveState(StateRecorder &inStream) const |
| 386 | { |
| 387 | TwoBodyConstraint::SaveState(inStream); |
| 388 | |
| 389 | mMotorConstraintPart.SaveState(inStream); |
| 390 | mRotationConstraintPart.SaveState(inStream); |
| 391 | mPointConstraintPart.SaveState(inStream); |
| 392 | mRotationLimitsConstraintPart.SaveState(inStream); |
| 393 | |
| 394 | inStream.Write(mMotorState); |
| 395 | inStream.Write(mTargetAngularVelocity); |
| 396 | inStream.Write(mTargetAngle); |
| 397 | } |
| 398 | |
| 399 | void HingeConstraint::RestoreState(StateRecorder &inStream) |
| 400 | { |
| 401 | TwoBodyConstraint::RestoreState(inStream); |
| 402 | |
| 403 | mMotorConstraintPart.RestoreState(inStream); |
| 404 | mRotationConstraintPart.RestoreState(inStream); |
| 405 | mPointConstraintPart.RestoreState(inStream); |
| 406 | mRotationLimitsConstraintPart.RestoreState(inStream); |
| 407 | |
| 408 | inStream.Read(mMotorState); |
| 409 | inStream.Read(mTargetAngularVelocity); |
| 410 | inStream.Read(mTargetAngle); |
| 411 | } |
| 412 | |
| 413 | |
| 414 | Ref<ConstraintSettings> HingeConstraint::GetConstraintSettings() const |
| 415 | { |
| 416 | HingeConstraintSettings *settings = new HingeConstraintSettings; |
| 417 | ToConstraintSettings(*settings); |
| 418 | settings->mSpace = EConstraintSpace::LocalToBodyCOM; |
| 419 | settings->mPoint1 = RVec3(mLocalSpacePosition1); |
| 420 | settings->mHingeAxis1 = mLocalSpaceHingeAxis1; |
| 421 | settings->mNormalAxis1 = mLocalSpaceNormalAxis1; |
| 422 | settings->mPoint2 = RVec3(mLocalSpacePosition2); |
| 423 | settings->mHingeAxis2 = mLocalSpaceHingeAxis2; |
| 424 | settings->mNormalAxis2 = mLocalSpaceNormalAxis2; |
| 425 | settings->mLimitsMin = mLimitsMin; |
| 426 | settings->mLimitsMax = mLimitsMax; |
| 427 | settings->mLimitsSpringSettings = mLimitsSpringSettings; |
| 428 | settings->mMaxFrictionTorque = mMaxFrictionTorque; |
| 429 | settings->mMotorSettings = mMotorSettings; |
| 430 | return settings; |
| 431 | } |
| 432 | |
| 433 | Mat44 HingeConstraint::GetConstraintToBody1Matrix() const |
| 434 | { |
| 435 | return Mat44(Vec4(mLocalSpaceHingeAxis1, 0), Vec4(mLocalSpaceNormalAxis1, 0), Vec4(mLocalSpaceHingeAxis1.Cross(mLocalSpaceNormalAxis1), 0), Vec4(mLocalSpacePosition1, 1)); |
| 436 | } |
| 437 | |
| 438 | Mat44 HingeConstraint::GetConstraintToBody2Matrix() const |
| 439 | { |
| 440 | return Mat44(Vec4(mLocalSpaceHingeAxis2, 0), Vec4(mLocalSpaceNormalAxis2, 0), Vec4(mLocalSpaceHingeAxis2.Cross(mLocalSpaceNormalAxis2), 0), Vec4(mLocalSpacePosition2, 1)); |
| 441 | } |
| 442 | |
| 443 | JPH_NAMESPACE_END |