DistanceConstraint.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Physics/Constraints/TwoBodyConstraint.h>
  5. #include <Physics/Constraints/ConstraintPart/AxisConstraintPart.h>
  6. namespace JPH {
  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. /// Note that due to the way the damping is implemented, it is impossible to get an undamped oscillation. See comment in AxisConstraintPart::CalculateConstraintProperties.
  29. float mFrequency = 0.0f;
  30. float mDamping = 0.0f;
  31. protected:
  32. // See: ConstraintSettings::RestoreBinaryState
  33. virtual void RestoreBinaryState(StreamIn &inStream) override;
  34. };
  35. /// This constraint is a stiff spring that holds 2 points at a fixed distance from each other
  36. class DistanceConstraint final : public TwoBodyConstraint
  37. {
  38. public:
  39. /// Construct distance constraint
  40. DistanceConstraint(Body &inBody1, Body &inBody2, const DistanceConstraintSettings &inSettings);
  41. // Generic interface of a constraint
  42. virtual EConstraintType GetType() const override { return EConstraintType::Distance; }
  43. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  44. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  45. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  46. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  47. #ifdef JPH_DEBUG_RENDERER
  48. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  49. #endif // JPH_DEBUG_RENDERER
  50. virtual void SaveState(StateRecorder &inStream) const override;
  51. virtual void RestoreState(StateRecorder &inStream) override;
  52. // See: TwoBodyConstraint
  53. virtual Mat44 GetConstraintToBody1Matrix() const override { return Mat44::sTranslation(mLocalSpacePosition1); }
  54. 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.
  55. /// Update the minimum and maximum distance for the constraint
  56. void SetDistance(float inMinDistance, float inMaxDistance) { JPH_ASSERT(inMinDistance <= inMaxDistance); mMinDistance = inMinDistance; mMaxDistance = inMaxDistance; }
  57. float GetMinDistance() const { return mMinDistance; }
  58. float GetMaxDistance() const { return mMaxDistance; }
  59. /// Update the spring frequency for the constraint
  60. void SetFrequency(float inFrequency) { JPH_ASSERT(inFrequency >= 0.0f); mFrequency = inFrequency; }
  61. float GetFrequency() const { return mFrequency; }
  62. /// Update the spring damping for the constraint
  63. void SetDamping(float inDamping) { JPH_ASSERT(inDamping >= 0.0f); mDamping = inDamping; }
  64. float GetDamping() const { return mDamping; }
  65. ///@name Get Lagrange multiplier from last physics update (relates to how much force/torque was applied to satisfy the constraint)
  66. inline float GetTotalLambdaPosition() const { return mAxisConstraint.GetTotalLambda(); }
  67. private:
  68. // Internal helper function to calculate the values below
  69. void CalculateConstraintProperties(float inDeltaTime);
  70. // CONFIGURATION PROPERTIES FOLLOW
  71. // Local space constraint positions
  72. Vec3 mLocalSpacePosition1;
  73. Vec3 mLocalSpacePosition2;
  74. // Min/max distance that must be kept between the world space points
  75. float mMinDistance;
  76. float mMaxDistance;
  77. // Soft constraint properties (see DistanceConstraintSettings)
  78. float mFrequency;
  79. float mDamping;
  80. // RUN TIME PROPERTIES FOLLOW
  81. // World space positions and normal
  82. Vec3 mWorldSpacePosition1;
  83. Vec3 mWorldSpacePosition2;
  84. Vec3 mWorldSpaceNormal;
  85. // Depending on if the distance < min or distance > max we can apply forces to prevent further violations
  86. float mMinLambda;
  87. float mMaxLambda;
  88. // The constraint part
  89. AxisConstraintPart mAxisConstraint;
  90. };
  91. } // JPH