SliderConstraint.h 7.7 KB

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