SliderConstraint.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. /// If mFrequency > 0 the constraint limits will be soft and mFrequency specifies the oscillation frequency in Hz and mDamping the damping ratio (0 = no damping, 1 = critical damping).
  39. /// If mFrequency <= 0, mDamping is ignored and the limits will be hard.
  40. float mFrequency = 0.0f;
  41. float mDamping = 0.0f;
  42. /// Maximum amount of friction force to apply (N) when not driven by a motor.
  43. float mMaxFrictionForce = 0.0f;
  44. /// In case the constraint is powered, this determines the motor settings around the sliding axis
  45. MotorSettings mMotorSettings;
  46. protected:
  47. // See: ConstraintSettings::RestoreBinaryState
  48. virtual void RestoreBinaryState(StreamIn &inStream) override;
  49. };
  50. /// A slider constraint allows movement in only 1 axis (and no rotation). Also known as a prismatic constraint.
  51. class SliderConstraint final : public TwoBodyConstraint
  52. {
  53. public:
  54. /// Construct slider constraint
  55. SliderConstraint(Body &inBody1, Body &inBody2, const SliderConstraintSettings &inSettings);
  56. // Generic interface of a constraint
  57. virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Slider; }
  58. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  59. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  60. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  61. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  62. #ifdef JPH_DEBUG_RENDERER
  63. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  64. virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;
  65. #endif // JPH_DEBUG_RENDERER
  66. virtual void SaveState(StateRecorder &inStream) const override;
  67. virtual void RestoreState(StateRecorder &inStream) override;
  68. virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
  69. // See: TwoBodyConstraint
  70. virtual Mat44 GetConstraintToBody1Matrix() const override;
  71. virtual Mat44 GetConstraintToBody2Matrix() const override;
  72. /// Friction control
  73. void SetMaxFrictionForce(float inFrictionForce) { mMaxFrictionForce = inFrictionForce; }
  74. float GetMaxFrictionForce() const { return mMaxFrictionForce; }
  75. /// Motor settings
  76. MotorSettings & GetMotorSettings() { return mMotorSettings; }
  77. const MotorSettings & GetMotorSettings() const { return mMotorSettings; }
  78. // Motor controls
  79. void SetMotorState(EMotorState inState) { JPH_ASSERT(inState == EMotorState::Off || mMotorSettings.IsValid()); mMotorState = inState; }
  80. EMotorState GetMotorState() const { return mMotorState; }
  81. void SetTargetVelocity(float inVelocity) { mTargetVelocity = inVelocity; }
  82. float GetTargetVelocity() const { return mTargetVelocity; }
  83. void SetTargetPosition(float inPosition) { mTargetPosition = mHasLimits? Clamp(inPosition, mLimitsMin, mLimitsMax) : inPosition; }
  84. float GetTargetPosition() const { return mTargetPosition; }
  85. /// Update the limits of the slider constraint (see SliderConstraintSettings)
  86. void SetLimits(float inLimitsMin, float inLimitsMax);
  87. float GetLimitsMin() const { return mLimitsMin; }
  88. float GetLimitsMax() const { return mLimitsMax; }
  89. bool HasLimits() const { return mHasLimits; }
  90. /// Update the spring frequency for the limits constraint
  91. void SetFrequency(float inFrequency) { JPH_ASSERT(inFrequency >= 0.0f); mFrequency = inFrequency; }
  92. float GetFrequency() const { return mFrequency; }
  93. /// Update the spring damping for the limits constraint
  94. void SetDamping(float inDamping) { JPH_ASSERT(inDamping >= 0.0f); mDamping = inDamping; }
  95. float GetDamping() const { return mDamping; }
  96. ///@name Get Lagrange multiplier from last physics update (relates to how much force/torque was applied to satisfy the constraint)
  97. inline Vector<2> GetTotalLambdaPosition() const { return mPositionConstraintPart.GetTotalLambda(); }
  98. inline float GetTotalLambdaPositionLimits() const { return mPositionLimitsConstraintPart.GetTotalLambda(); }
  99. inline Vec3 GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }
  100. inline float GetTotalLambdaMotor() const { return mMotorConstraintPart.GetTotalLambda(); }
  101. private:
  102. // Internal helper function to calculate the values below
  103. void CalculateR1R2U(Mat44Arg inRotation1, Mat44Arg inRotation2);
  104. void CalculateSlidingAxisAndPosition(Mat44Arg inRotation1);
  105. void CalculatePositionConstraintProperties(Mat44Arg inRotation1, Mat44Arg inRotation2);
  106. void CalculatePositionLimitsConstraintProperties(float inDeltaTime);
  107. void CalculateMotorConstraintProperties(float inDeltaTime);
  108. // CONFIGURATION PROPERTIES FOLLOW
  109. // Local space constraint positions
  110. Vec3 mLocalSpacePosition1;
  111. Vec3 mLocalSpacePosition2;
  112. // Local space sliding direction
  113. Vec3 mLocalSpaceSliderAxis1;
  114. // Local space normals to the sliding direction (in body 1 space)
  115. Vec3 mLocalSpaceNormal1;
  116. Vec3 mLocalSpaceNormal2;
  117. // Inverse of initial rotation from body 1 to body 2 in body 1 space
  118. Quat mInvInitialOrientation;
  119. // Slider limits
  120. bool mHasLimits;
  121. float mLimitsMin;
  122. float mLimitsMax;
  123. // Soft slider limits
  124. float mFrequency;
  125. float mDamping;
  126. // Friction
  127. float mMaxFrictionForce;
  128. // Motor controls
  129. MotorSettings mMotorSettings;
  130. EMotorState mMotorState = EMotorState::Off;
  131. float mTargetVelocity = 0.0f;
  132. float mTargetPosition = 0.0f;
  133. // RUN TIME PROPERTIES FOLLOW
  134. // Positions where the point constraint acts on (middle point between center of masses)
  135. Vec3 mR1;
  136. Vec3 mR2;
  137. // X2 + R2 - X1 - R1
  138. Vec3 mU;
  139. // World space sliding direction
  140. Vec3 mWorldSpaceSliderAxis;
  141. // Normals to the slider axis
  142. Vec3 mN1;
  143. Vec3 mN2;
  144. // Distance along the slide axis
  145. float mD = 0.0f;
  146. // The constraint parts
  147. DualAxisConstraintPart mPositionConstraintPart;
  148. RotationEulerConstraintPart mRotationConstraintPart;
  149. AxisConstraintPart mPositionLimitsConstraintPart;
  150. AxisConstraintPart mMotorConstraintPart;
  151. };
  152. JPH_NAMESPACE_END