DistanceConstraint.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Physics/Constraints/TwoBodyConstraint.h>
  5. #include <Jolt/Physics/Constraints/ConstraintPart/AxisConstraintPart.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// Distance constraint settings, used to create a distance constraint
  8. class DistanceConstraintSettings final : public TwoBodyConstraintSettings
  9. {
  10. public:
  11. JPH_DECLARE_SERIALIZABLE_VIRTUAL(DistanceConstraintSettings)
  12. // See: ConstraintSettings::SaveBinaryState
  13. virtual void SaveBinaryState(StreamOut &inStream) const override;
  14. /// Create an an instance of this constraint
  15. virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
  16. /// This determines in which space the constraint is setup, all properties below should be in the specified space
  17. EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
  18. /// Body 1 constraint reference frame (space determined by mSpace).
  19. /// Constraint will keep mPoint1 (a point on body 1) and mPoint2 (a point on body 2) at the same distance.
  20. /// Note that this constraint can be used as a cheap PointConstraint by setting mPoint1 = mPoint2 (but this removes only 1 degree of freedom instead of 3).
  21. Vec3 mPoint1 = Vec3::sZero();
  22. /// Body 2 constraint reference frame (space determined by mSpace)
  23. Vec3 mPoint2 = Vec3::sZero();
  24. /// Ability to override the distance range at which the two points are kept apart. If the value is negative, it will be replaced by the distance between mPoint1 and mPoint2 (works only if mSpace is world space).
  25. float mMinDistance = -1.0f;
  26. float mMaxDistance = -1.0f;
  27. /// If mFrequency > 0 the constraint will be soft and mFrequency specifies the oscillation frequency in Hz and mDamping the damping ratio (0 = no damping, 1 = critical damping).
  28. /// If mFrequency <= 0, mDamping is ignored and the distance constraint will have hard limits (as hard as the time step / the number of velocity / position solver steps allows).
  29. /// Note that if you set mDamping = 0, you will not get an infinite oscillation. Because we integrate physics using an explicit Euler scheme, there is always energy loss.
  30. /// This is done to keep the simulation from exploding, because with a damping of 0 and even the slightest rounding error, the oscillation could become bigger and bigger until the simluation explodes.
  31. float mFrequency = 0.0f;
  32. float mDamping = 0.0f;
  33. protected:
  34. // See: ConstraintSettings::RestoreBinaryState
  35. virtual void RestoreBinaryState(StreamIn &inStream) override;
  36. };
  37. /// This constraint is a stiff spring that holds 2 points at a fixed distance from each other
  38. class DistanceConstraint final : public TwoBodyConstraint
  39. {
  40. public:
  41. /// Construct distance constraint
  42. DistanceConstraint(Body &inBody1, Body &inBody2, const DistanceConstraintSettings &inSettings);
  43. // Generic interface of a constraint
  44. virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Distance; }
  45. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  46. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  47. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  48. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  49. #ifdef JPH_DEBUG_RENDERER
  50. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  51. #endif // JPH_DEBUG_RENDERER
  52. virtual void SaveState(StateRecorder &inStream) const override;
  53. virtual void RestoreState(StateRecorder &inStream) override;
  54. virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
  55. // See: TwoBodyConstraint
  56. virtual Mat44 GetConstraintToBody1Matrix() const override { return Mat44::sTranslation(mLocalSpacePosition1); }
  57. 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.
  58. /// Update the minimum and maximum distance for the constraint
  59. void SetDistance(float inMinDistance, float inMaxDistance) { JPH_ASSERT(inMinDistance <= inMaxDistance); mMinDistance = inMinDistance; mMaxDistance = inMaxDistance; }
  60. float GetMinDistance() const { return mMinDistance; }
  61. float GetMaxDistance() const { return mMaxDistance; }
  62. /// Update the spring frequency for the constraint
  63. void SetFrequency(float inFrequency) { JPH_ASSERT(inFrequency >= 0.0f); mFrequency = inFrequency; }
  64. float GetFrequency() const { return mFrequency; }
  65. /// Update the spring damping for the constraint
  66. void SetDamping(float inDamping) { JPH_ASSERT(inDamping >= 0.0f); mDamping = inDamping; }
  67. float GetDamping() const { return mDamping; }
  68. ///@name Get Lagrange multiplier from last physics update (relates to how much force/torque was applied to satisfy the constraint)
  69. inline float GetTotalLambdaPosition() const { return mAxisConstraint.GetTotalLambda(); }
  70. private:
  71. // Internal helper function to calculate the values below
  72. void CalculateConstraintProperties(float inDeltaTime);
  73. // CONFIGURATION PROPERTIES FOLLOW
  74. // Local space constraint positions
  75. Vec3 mLocalSpacePosition1;
  76. Vec3 mLocalSpacePosition2;
  77. // Min/max distance that must be kept between the world space points
  78. float mMinDistance;
  79. float mMaxDistance;
  80. // Soft constraint properties (see DistanceConstraintSettings)
  81. float mFrequency;
  82. float mDamping;
  83. // RUN TIME PROPERTIES FOLLOW
  84. // World space positions and normal
  85. Vec3 mWorldSpacePosition1;
  86. Vec3 mWorldSpacePosition2;
  87. Vec3 mWorldSpaceNormal;
  88. // Depending on if the distance < min or distance > max we can apply forces to prevent further violations
  89. float mMinLambda;
  90. float mMaxLambda;
  91. // The constraint part
  92. AxisConstraintPart mAxisConstraint;
  93. };
  94. JPH_NAMESPACE_END