PulleyConstraint.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-FileCopyrightText: 2022 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Physics/Constraints/TwoBodyConstraint.h>
  5. #include <Jolt/Physics/Constraints/ConstraintPart/IndependentAxisConstraintPart.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// Pulley constraint settings, used to create a pulley constraint.
  8. /// A pulley connects two bodies via two fixed world points to each other similar to a distance constraint.
  9. /// We define Length1 = |BodyPoint1 - FixedPoint1| where Body1 is a point on body 1 in world space and FixedPoint1 a fixed point in world space
  10. /// Length2 = |BodyPoint2 - FixedPoint2|
  11. /// The constraint keeps the two line segments constrained so that
  12. /// MinDistance <= Length1 + Ratio * Length2 <= MaxDistance
  13. class PulleyConstraintSettings final : public TwoBodyConstraintSettings
  14. {
  15. public:
  16. JPH_DECLARE_SERIALIZABLE_VIRTUAL(PulleyConstraintSettings)
  17. // See: ConstraintSettings::SaveBinaryState
  18. virtual void SaveBinaryState(StreamOut &inStream) const override;
  19. /// Create an an instance of this constraint
  20. virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
  21. /// This determines in which space the constraint is setup, specified properties below should be in the specified space
  22. EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
  23. /// Body 1 constraint attachment point (space determined by mSpace).
  24. Vec3 mBodyPoint1 = Vec3::sZero();
  25. /// Fixed world point to which body 1 is connected (always world space)
  26. Vec3 mFixedPoint1 = Vec3::sZero();
  27. /// Body 2 constraint attachment point (space determined by mSpace)
  28. Vec3 mBodyPoint2 = Vec3::sZero();
  29. /// Fixed world point to which body 2 is connected (always world space)
  30. Vec3 mFixedPoint2 = Vec3::sZero();
  31. /// Ratio between the two line segments (see formula above), can be used to create a block and tackle
  32. float mRatio = 1.0f;
  33. /// The minimum length of the line segments (see formula above), use -1 to calculate the length based on the positions of the objects when the constraint is created.
  34. float mMinLength = 0.0f;
  35. /// The maximum length of the line segments (see formula above), use -1 to calculate the length based on the positions of the objects when the constraint is created.
  36. float mMaxLength = -1.0f;
  37. protected:
  38. // See: ConstraintSettings::RestoreBinaryState
  39. virtual void RestoreBinaryState(StreamIn &inStream) override;
  40. };
  41. /// A pulley constraint.
  42. class PulleyConstraint final : public TwoBodyConstraint
  43. {
  44. public:
  45. JPH_OVERRIDE_NEW_DELETE
  46. /// Construct distance constraint
  47. PulleyConstraint(Body &inBody1, Body &inBody2, const PulleyConstraintSettings &inSettings);
  48. // Generic interface of a constraint
  49. virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Pulley; }
  50. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  51. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  52. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  53. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  54. #ifdef JPH_DEBUG_RENDERER
  55. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  56. #endif // JPH_DEBUG_RENDERER
  57. virtual void SaveState(StateRecorder &inStream) const override;
  58. virtual void RestoreState(StateRecorder &inStream) override;
  59. virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
  60. // See: TwoBodyConstraint
  61. virtual Mat44 GetConstraintToBody1Matrix() const override { return Mat44::sTranslation(mLocalSpacePosition1); }
  62. virtual Mat44 GetConstraintToBody2Matrix() const override { return Mat44::sTranslation(mLocalSpacePosition2); } // Note: Incorrect rotation as we don't track the original rotation difference, should not matter though as the constraint is not limiting rotation.
  63. /// Update the minimum and maximum length for the constraint
  64. void SetLength(float inMinLength, float inMaxLength) { JPH_ASSERT(inMinLength >= 0.0f && inMinLength <= inMaxLength); mMinLength = inMinLength; mMaxLength = inMaxLength; }
  65. float GetMinLength() const { return mMinLength; }
  66. float GetMaxLength() const { return mMaxLength; }
  67. /// Get the current length of both segments (multiplied by the ratio for segment 2)
  68. float GetCurrentLength() const { return (mWorldSpacePosition1 - mFixedPosition1).Length() + mRatio * (mWorldSpacePosition2 - mFixedPosition2).Length(); }
  69. ///@name Get Lagrange multiplier from last physics update (relates to how much force/torque was applied to satisfy the constraint)
  70. inline float GetTotalLambdaPosition() const { return mIndependentAxisConstraintPart.GetTotalLambda(); }
  71. private:
  72. // Calculates world positions and normals and returns current length
  73. float CalculatePositionsNormalsAndLength();
  74. // Internal helper function to calculate the values below
  75. void CalculateConstraintProperties();
  76. // CONFIGURATION PROPERTIES FOLLOW
  77. // Local space constraint positions on the bodies
  78. Vec3 mLocalSpacePosition1;
  79. Vec3 mLocalSpacePosition2;
  80. // World space fixed positions
  81. Vec3 mFixedPosition1;
  82. Vec3 mFixedPosition2;
  83. /// Ratio between the two line segments
  84. float mRatio;
  85. // The minimum/maximum length of the line segments
  86. float mMinLength;
  87. float mMaxLength;
  88. // RUN TIME PROPERTIES FOLLOW
  89. // World space positions and normal
  90. Vec3 mWorldSpacePosition1;
  91. Vec3 mWorldSpacePosition2;
  92. Vec3 mWorldSpaceNormal1;
  93. Vec3 mWorldSpaceNormal2;
  94. // Depending on if the length < min or length > max we can apply forces to prevent further violations
  95. float mMinLambda;
  96. float mMaxLambda;
  97. // The constraint part
  98. IndependentAxisConstraintPart mIndependentAxisConstraintPart;
  99. };
  100. JPH_NAMESPACE_END