SliderConstraint.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Physics/Constraints/TwoBodyConstraint.h>
  5. #include <Physics/Constraints/MotorSettings.h>
  6. #include <Physics/Constraints/ConstraintPart/DualAxisConstraintPart.h>
  7. #include <Physics/Constraints/ConstraintPart/RotationEulerConstraintPart.h>
  8. #include <Physics/Constraints/ConstraintPart/AxisConstraintPart.h>
  9. namespace JPH {
  10. /// Slider constraint settings, used to create a slider constraint
  11. class SliderConstraintSettings final : public TwoBodyConstraintSettings
  12. {
  13. public:
  14. JPH_DECLARE_SERIALIZABLE_VIRTUAL(SliderConstraintSettings)
  15. // See: ConstraintSettings::SaveBinaryState
  16. virtual void SaveBinaryState(StreamOut &inStream) const override;
  17. /// Create an an instance of this constraint
  18. virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
  19. /// Simple way of setting the anchor points so that the current relative position is chosen as the '0' position
  20. void SetPoint(const Body &inBody1, const Body &inBody2);
  21. /// Simple way of setting the slider and normal axis in world space (assumes the bodies are already oriented correctly when the constraint is created)
  22. void SetSliderAxis(Vec3Arg inSliderAxis);
  23. /// Body 1 constraint reference frame (in world space).
  24. /// Slider axis is the axis along which movement is possible (world space direction), normal axis is a perpendicular vector to define the frame.
  25. Vec3 mPoint1 = Vec3::sZero();
  26. Vec3 mSliderAxis1 = Vec3::sAxisX();
  27. Vec3 mNormalAxis1 = Vec3::sAxisY();
  28. /// Body 2 constraint reference frame (in world space)
  29. Vec3 mPoint2 = Vec3::sZero();
  30. Vec3 mSliderAxis2 = Vec3::sAxisX();
  31. Vec3 mNormalAxis2 = Vec3::sAxisY();
  32. /// When the bodies move so that mPoint1 coincides with mPoint2 the slider position is defined to be 0, movement will be limited between [mLimitsMin, mLimitsMax] where mLimitsMin e [-inf, 0] and mLimitsMax e [0, inf]
  33. float mLimitsMin = -FLT_MAX;
  34. float mLimitsMax = FLT_MAX;
  35. /// Maximum amount of friction force to apply (N) when not driven by a motor.
  36. float mMaxFrictionForce = 0.0f;
  37. /// In case the constraint is powered, this determines the motor settings around the sliding axis
  38. MotorSettings mMotorSettings;
  39. protected:
  40. // See: ConstraintSettings::RestoreBinaryState
  41. virtual void RestoreBinaryState(StreamIn &inStream) override;
  42. };
  43. /// A slider constraint allows movement in only 1 axis (and no rotation). Also known as a prismatic constraint.
  44. class SliderConstraint final : public TwoBodyConstraint
  45. {
  46. public:
  47. /// Construct slider constraint
  48. SliderConstraint(Body &inBody1, Body &inBody2, const SliderConstraintSettings &inSettings);
  49. // Generic interface of a constraint
  50. virtual EConstraintType GetType() const override { return EConstraintType::Slider; }
  51. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  52. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  53. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  54. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  55. #ifdef JPH_DEBUG_RENDERER
  56. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  57. virtual void DrawConstraintLimits(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. // See: TwoBodyConstraint
  62. virtual Mat44 GetConstraintToBody1Matrix() const override;
  63. virtual Mat44 GetConstraintToBody2Matrix() const override;
  64. /// Friction control
  65. void SetMaxFrictionForce(float inFrictionForce) { mMaxFrictionForce = inFrictionForce; }
  66. float GetMaxFrictionForce() const { return mMaxFrictionForce; }
  67. /// Motor settings
  68. MotorSettings & GetMotorSettings() { return mMotorSettings; }
  69. const MotorSettings & GetMotorSettings() const { return mMotorSettings; }
  70. // Motor controls
  71. void SetMotorState(EMotorState inState) { JPH_ASSERT(inState == EMotorState::Off || mMotorSettings.IsValid()); mMotorState = inState; }
  72. EMotorState GetMotorState() const { return mMotorState; }
  73. void SetTargetVelocity(float inVelocity) { mTargetVelocity = inVelocity; }
  74. float GetTargetVelocity() const { return mTargetVelocity; }
  75. void SetTargetPosition(float inPosition) { mTargetPosition = mHasLimits? Clamp(inPosition, mLimitsMin, mLimitsMax) : inPosition; }
  76. float GetTargetPosition() const { return mTargetPosition; }
  77. /// Update the limits of the slider constraint (see SliderConstraintSettings)
  78. void SetLimits(float inLimitsMin, float inLimitsMax);
  79. float GetLimitsMin() const { return mLimitsMin; }
  80. float GetLimitsMax() const { return mLimitsMax; }
  81. bool HasLimits() const { return mHasLimits; }
  82. ///@name Get Lagrange multiplier from last physics update (relates to how much force/torque was applied to satisfy the constraint)
  83. inline Vector<2> GetTotalLambdaPosition() const { return mPositionConstraintPart.GetTotalLambda(); }
  84. inline float GetTotalLambdaPositionLimits() const { return mPositionLimitsConstraintPart.GetTotalLambda(); }
  85. inline Vec3 GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }
  86. inline float GetTotalLambdaMotor() const { return mMotorConstraintPart.GetTotalLambda(); }
  87. private:
  88. // Internal helper function to calculate the values below
  89. void CalculateR1R2U(Mat44Arg inRotation1, Mat44Arg inRotation2);
  90. void CalculateSlidingAxisAndPosition(Mat44Arg inRotation1);
  91. void CalculatePositionConstraintProperties(Mat44Arg inRotation1, Mat44Arg inRotation2);
  92. void CalculatePositionLimitsConstraintProperties(float inDeltaTime, Mat44Arg inRotation1);
  93. void CalculateMotorConstraintProperties(float inDeltaTime);
  94. // CONFIGURATION PROPERTIES FOLLOW
  95. // Local space constraint positions
  96. Vec3 mLocalSpacePosition1;
  97. Vec3 mLocalSpacePosition2;
  98. // Local space sliding direction
  99. Vec3 mLocalSpaceSliderAxis1;
  100. // Local space normals to the sliding direction
  101. Vec3 mLocalSpaceNormal1;
  102. Vec3 mLocalSpaceNormal2;
  103. // Inverse of initial rotation from body 1 to body 2 in body 1 space
  104. Quat mInvInitialOrientation;
  105. // Slider limits
  106. bool mHasLimits;
  107. float mLimitsMin;
  108. float mLimitsMax;
  109. // Friction
  110. float mMaxFrictionForce;
  111. // Motor controls
  112. MotorSettings mMotorSettings;
  113. EMotorState mMotorState = EMotorState::Off;
  114. float mTargetVelocity = 0.0f;
  115. float mTargetPosition = 0.0f;
  116. // RUN TIME PROPERTIES FOLLOW
  117. // Positions where the point constraint acts on (middle point between center of masses)
  118. Vec3 mR1;
  119. Vec3 mR2;
  120. // X2 + R2 - X1 - R1
  121. Vec3 mU;
  122. // World space sliding direction
  123. Vec3 mWorldSpaceSliderAxis;
  124. // Normals to the slider axis
  125. Vec3 mN1;
  126. Vec3 mN2;
  127. // Distance along the slide axis
  128. float mD = 0.0f;
  129. // The constraint parts
  130. DualAxisConstraintPart mPositionConstraintPart;
  131. RotationEulerConstraintPart mRotationConstraintPart;
  132. AxisConstraintPart mPositionLimitsConstraintPart;
  133. AxisConstraintPart mMotorConstraintPart;
  134. };
  135. } // JPH