SliderConstraint.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 in world space 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. /// This determines in which space the constraint is setup, all properties below should be in the specified space
  24. EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
  25. /// Body 1 constraint reference frame (space determined by mSpace).
  26. /// Slider axis is the axis along which movement is possible (direction), normal axis is a perpendicular vector to define the frame.
  27. Vec3 mPoint1 = Vec3::sZero();
  28. Vec3 mSliderAxis1 = Vec3::sAxisX();
  29. Vec3 mNormalAxis1 = Vec3::sAxisY();
  30. /// Body 2 constraint reference frame (space determined by mSpace)
  31. Vec3 mPoint2 = Vec3::sZero();
  32. Vec3 mSliderAxis2 = Vec3::sAxisX();
  33. Vec3 mNormalAxis2 = Vec3::sAxisY();
  34. /// 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]
  35. float mLimitsMin = -FLT_MAX;
  36. float mLimitsMax = FLT_MAX;
  37. /// Maximum amount of friction force to apply (N) when not driven by a motor.
  38. float mMaxFrictionForce = 0.0f;
  39. /// In case the constraint is powered, this determines the motor settings around the sliding axis
  40. MotorSettings mMotorSettings;
  41. protected:
  42. // See: ConstraintSettings::RestoreBinaryState
  43. virtual void RestoreBinaryState(StreamIn &inStream) override;
  44. };
  45. /// A slider constraint allows movement in only 1 axis (and no rotation). Also known as a prismatic constraint.
  46. class SliderConstraint final : public TwoBodyConstraint
  47. {
  48. public:
  49. /// Construct slider constraint
  50. SliderConstraint(Body &inBody1, Body &inBody2, const SliderConstraintSettings &inSettings);
  51. // Generic interface of a constraint
  52. virtual EConstraintType GetType() const override { return EConstraintType::Slider; }
  53. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  54. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  55. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  56. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  57. #ifdef JPH_DEBUG_RENDERER
  58. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  59. virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;
  60. #endif // JPH_DEBUG_RENDERER
  61. virtual void SaveState(StateRecorder &inStream) const override;
  62. virtual void RestoreState(StateRecorder &inStream) override;
  63. // See: TwoBodyConstraint
  64. virtual Mat44 GetConstraintToBody1Matrix() const override;
  65. virtual Mat44 GetConstraintToBody2Matrix() const override;
  66. /// Friction control
  67. void SetMaxFrictionForce(float inFrictionForce) { mMaxFrictionForce = inFrictionForce; }
  68. float GetMaxFrictionForce() const { return mMaxFrictionForce; }
  69. /// Motor settings
  70. MotorSettings & GetMotorSettings() { return mMotorSettings; }
  71. const MotorSettings & GetMotorSettings() const { return mMotorSettings; }
  72. // Motor controls
  73. void SetMotorState(EMotorState inState) { JPH_ASSERT(inState == EMotorState::Off || mMotorSettings.IsValid()); mMotorState = inState; }
  74. EMotorState GetMotorState() const { return mMotorState; }
  75. void SetTargetVelocity(float inVelocity) { mTargetVelocity = inVelocity; }
  76. float GetTargetVelocity() const { return mTargetVelocity; }
  77. void SetTargetPosition(float inPosition) { mTargetPosition = mHasLimits? Clamp(inPosition, mLimitsMin, mLimitsMax) : inPosition; }
  78. float GetTargetPosition() const { return mTargetPosition; }
  79. /// Update the limits of the slider constraint (see SliderConstraintSettings)
  80. void SetLimits(float inLimitsMin, float inLimitsMax);
  81. float GetLimitsMin() const { return mLimitsMin; }
  82. float GetLimitsMax() const { return mLimitsMax; }
  83. bool HasLimits() const { return mHasLimits; }
  84. ///@name Get Lagrange multiplier from last physics update (relates to how much force/torque was applied to satisfy the constraint)
  85. inline Vector<2> GetTotalLambdaPosition() const { return mPositionConstraintPart.GetTotalLambda(); }
  86. inline float GetTotalLambdaPositionLimits() const { return mPositionLimitsConstraintPart.GetTotalLambda(); }
  87. inline Vec3 GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }
  88. inline float GetTotalLambdaMotor() const { return mMotorConstraintPart.GetTotalLambda(); }
  89. private:
  90. // Internal helper function to calculate the values below
  91. void CalculateR1R2U(Mat44Arg inRotation1, Mat44Arg inRotation2);
  92. void CalculateSlidingAxisAndPosition(Mat44Arg inRotation1);
  93. void CalculatePositionConstraintProperties(Mat44Arg inRotation1, Mat44Arg inRotation2);
  94. void CalculatePositionLimitsConstraintProperties(float inDeltaTime, Mat44Arg inRotation1);
  95. void CalculateMotorConstraintProperties(float inDeltaTime);
  96. // CONFIGURATION PROPERTIES FOLLOW
  97. // Local space constraint positions
  98. Vec3 mLocalSpacePosition1;
  99. Vec3 mLocalSpacePosition2;
  100. // Local space sliding direction
  101. Vec3 mLocalSpaceSliderAxis1;
  102. // Local space normals to the sliding direction (in body 1 space)
  103. Vec3 mLocalSpaceNormal1;
  104. Vec3 mLocalSpaceNormal2;
  105. // Inverse of initial rotation from body 1 to body 2 in body 1 space
  106. Quat mInvInitialOrientation;
  107. // Slider limits
  108. bool mHasLimits;
  109. float mLimitsMin;
  110. float mLimitsMax;
  111. // Friction
  112. float mMaxFrictionForce;
  113. // Motor controls
  114. MotorSettings mMotorSettings;
  115. EMotorState mMotorState = EMotorState::Off;
  116. float mTargetVelocity = 0.0f;
  117. float mTargetPosition = 0.0f;
  118. // RUN TIME PROPERTIES FOLLOW
  119. // Positions where the point constraint acts on (middle point between center of masses)
  120. Vec3 mR1;
  121. Vec3 mR2;
  122. // X2 + R2 - X1 - R1
  123. Vec3 mU;
  124. // World space sliding direction
  125. Vec3 mWorldSpaceSliderAxis;
  126. // Normals to the slider axis
  127. Vec3 mN1;
  128. Vec3 mN2;
  129. // Distance along the slide axis
  130. float mD = 0.0f;
  131. // The constraint parts
  132. DualAxisConstraintPart mPositionConstraintPart;
  133. RotationEulerConstraintPart mRotationConstraintPart;
  134. AxisConstraintPart mPositionLimitsConstraintPart;
  135. AxisConstraintPart mMotorConstraintPart;
  136. };
  137. } // JPH