FixedConstraint.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt/Jolt.h>
  4. #include <Jolt/Physics/Constraints/FixedConstraint.h>
  5. #include <Jolt/Physics/Body/Body.h>
  6. #include <Jolt/ObjectStream/TypeDeclarations.h>
  7. #ifdef JPH_DEBUG_RENDERER
  8. #include <Jolt/Renderer/DebugRenderer.h>
  9. #endif // JPH_DEBUG_RENDERER
  10. JPH_NAMESPACE_BEGIN
  11. JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(FixedConstraintSettings)
  12. {
  13. JPH_ADD_BASE_CLASS(FixedConstraintSettings, TwoBodyConstraintSettings)
  14. }
  15. TwoBodyConstraint *FixedConstraintSettings::Create(Body &inBody1, Body &inBody2) const
  16. {
  17. return new FixedConstraint(inBody1, inBody2, *this);
  18. }
  19. FixedConstraint::FixedConstraint(Body &inBody1, Body &inBody2, const FixedConstraintSettings &inSettings) :
  20. TwoBodyConstraint(inBody1, inBody2, inSettings)
  21. {
  22. // Determine anchor point: If any of the bodies can never be dynamic use the other body as anchor point
  23. Vec3 anchor;
  24. if (!mBody1->CanBeKinematicOrDynamic())
  25. anchor = mBody2->GetCenterOfMassPosition();
  26. else if (!mBody2->CanBeKinematicOrDynamic())
  27. anchor = mBody1->GetCenterOfMassPosition();
  28. else
  29. {
  30. // Otherwise use weighted anchor point towards the lightest body
  31. float inv_m1 = mBody1->GetMotionPropertiesUnchecked()->GetInverseMassUnchecked();
  32. float inv_m2 = mBody2->GetMotionPropertiesUnchecked()->GetInverseMassUnchecked();
  33. anchor = (inv_m1 * mBody1->GetCenterOfMassPosition() + inv_m2 * mBody2->GetCenterOfMassPosition()) / (inv_m1 + inv_m2);
  34. }
  35. // Store local positions
  36. mLocalSpacePosition1 = inBody1.GetInverseCenterOfMassTransform() * anchor;
  37. mLocalSpacePosition2 = inBody2.GetInverseCenterOfMassTransform() * anchor;
  38. // Inverse of initial rotation from body 1 to body 2 in body 1 space
  39. mInvInitialOrientation = RotationEulerConstraintPart::sGetInvInitialOrientation(inBody1, inBody2);
  40. }
  41. void FixedConstraint::SetupVelocityConstraint(float inDeltaTime)
  42. {
  43. // Calculate constraint values that don't change when the bodies don't change position
  44. Mat44 rotation1 = Mat44::sRotation(mBody1->GetRotation());
  45. Mat44 rotation2 = Mat44::sRotation(mBody2->GetRotation());
  46. mRotationConstraintPart.CalculateConstraintProperties(*mBody1, rotation1, *mBody2, rotation2);
  47. mPointConstraintPart.CalculateConstraintProperties(*mBody1, rotation1, mLocalSpacePosition1, *mBody2, rotation2, mLocalSpacePosition2);
  48. }
  49. void FixedConstraint::WarmStartVelocityConstraint(float inWarmStartImpulseRatio)
  50. {
  51. // Warm starting: Apply previous frame impulse
  52. mRotationConstraintPart.WarmStart(*mBody1, *mBody2, inWarmStartImpulseRatio);
  53. mPointConstraintPart.WarmStart(*mBody1, *mBody2, inWarmStartImpulseRatio);
  54. }
  55. bool FixedConstraint::SolveVelocityConstraint(float inDeltaTime)
  56. {
  57. // Solve rotation constraint
  58. bool rot = mRotationConstraintPart.SolveVelocityConstraint(*mBody1, *mBody2);
  59. // Solve position constraint
  60. bool pos = mPointConstraintPart.SolveVelocityConstraint(*mBody1, *mBody2);
  61. return rot || pos;
  62. }
  63. bool FixedConstraint::SolvePositionConstraint(float inDeltaTime, float inBaumgarte)
  64. {
  65. // Solve rotation constraint
  66. mRotationConstraintPart.CalculateConstraintProperties(*mBody1, Mat44::sRotation(mBody1->GetRotation()), *mBody2, Mat44::sRotation(mBody2->GetRotation()));
  67. bool rot = mRotationConstraintPart.SolvePositionConstraint(*mBody1, *mBody2, mInvInitialOrientation, inBaumgarte);
  68. // Solve position constraint
  69. mPointConstraintPart.CalculateConstraintProperties(*mBody1, Mat44::sRotation(mBody1->GetRotation()), mLocalSpacePosition1, *mBody2, Mat44::sRotation(mBody2->GetRotation()), mLocalSpacePosition2);
  70. bool pos = mPointConstraintPart.SolvePositionConstraint(*mBody1, *mBody2, inBaumgarte);
  71. return rot || pos;
  72. }
  73. #ifdef JPH_DEBUG_RENDERER
  74. void FixedConstraint::DrawConstraint(DebugRenderer *inRenderer) const
  75. {
  76. Vec3 com1 = mBody1->GetCenterOfMassPosition();
  77. Vec3 com2 = mBody2->GetCenterOfMassPosition();
  78. // Draw constraint
  79. inRenderer->DrawLine(com1, com2, Color::sGreen);
  80. }
  81. #endif // JPH_DEBUG_RENDERER
  82. void FixedConstraint::SaveState(StateRecorder &inStream) const
  83. {
  84. TwoBodyConstraint::SaveState(inStream);
  85. mRotationConstraintPart.SaveState(inStream);
  86. mPointConstraintPart.SaveState(inStream);
  87. }
  88. void FixedConstraint::RestoreState(StateRecorder &inStream)
  89. {
  90. TwoBodyConstraint::RestoreState(inStream);
  91. mRotationConstraintPart.RestoreState(inStream);
  92. mPointConstraintPart.RestoreState(inStream);
  93. }
  94. JPH_NAMESPACE_END