PulleyConstraint.h 5.9 KB

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