SliderConstraint.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Physics/Constraints/TwoBodyConstraint.h>
  6. #include <Jolt/Physics/Constraints/MotorSettings.h>
  7. #include <Jolt/Physics/Constraints/ConstraintPart/DualAxisConstraintPart.h>
  8. #include <Jolt/Physics/Constraints/ConstraintPart/RotationEulerConstraintPart.h>
  9. #include <Jolt/Physics/Constraints/ConstraintPart/AxisConstraintPart.h>
  10. JPH_NAMESPACE_BEGIN
  11. /// Slider constraint settings, used to create a slider constraint
  12. class SliderConstraintSettings final : public TwoBodyConstraintSettings
  13. {
  14. public:
  15. JPH_DECLARE_SERIALIZABLE_VIRTUAL(SliderConstraintSettings)
  16. // See: ConstraintSettings::SaveBinaryState
  17. virtual void SaveBinaryState(StreamOut &inStream) const override;
  18. /// Create an an instance of this constraint.
  19. /// 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.
  20. virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
  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. /// When mSpace is WorldSpace mPoint1 and mPoint2 can be automatically calculated based on the positions of the bodies when the constraint is created (the current relative position/orientation is chosen as the '0' position). Set this to false if you want to supply the attachment points yourself.
  26. bool mAutoDetectPoint = false;
  27. /// Body 1 constraint reference frame (space determined by mSpace).
  28. /// Slider axis is the axis along which movement is possible (direction), normal axis is a perpendicular vector to define the frame.
  29. RVec3 mPoint1 = RVec3::sZero();
  30. Vec3 mSliderAxis1 = Vec3::sAxisX();
  31. Vec3 mNormalAxis1 = Vec3::sAxisY();
  32. /// Body 2 constraint reference frame (space determined by mSpace)
  33. RVec3 mPoint2 = RVec3::sZero();
  34. Vec3 mSliderAxis2 = Vec3::sAxisX();
  35. Vec3 mNormalAxis2 = Vec3::sAxisY();
  36. /// 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]
  37. float mLimitsMin = -FLT_MAX;
  38. float mLimitsMax = FLT_MAX;
  39. /// 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).
  40. /// If mFrequency <= 0, mDamping is ignored and the limits will be hard.
  41. float mFrequency = 0.0f;
  42. float mDamping = 0.0f;
  43. /// Maximum amount of friction force to apply (N) when not driven by a motor.
  44. float mMaxFrictionForce = 0.0f;
  45. /// In case the constraint is powered, this determines the motor settings around the sliding axis
  46. MotorSettings mMotorSettings;
  47. protected:
  48. // See: ConstraintSettings::RestoreBinaryState
  49. virtual void RestoreBinaryState(StreamIn &inStream) override;
  50. };
  51. /// A slider constraint allows movement in only 1 axis (and no rotation). Also known as a prismatic constraint.
  52. class SliderConstraint final : public TwoBodyConstraint
  53. {
  54. public:
  55. JPH_OVERRIDE_NEW_DELETE
  56. /// Construct slider constraint
  57. SliderConstraint(Body &inBody1, Body &inBody2, const SliderConstraintSettings &inSettings);
  58. // Generic interface of a constraint
  59. virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Slider; }
  60. virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;
  61. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  62. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  63. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  64. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  65. #ifdef JPH_DEBUG_RENDERER
  66. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  67. virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;
  68. #endif // JPH_DEBUG_RENDERER
  69. virtual void SaveState(StateRecorder &inStream) const override;
  70. virtual void RestoreState(StateRecorder &inStream) override;
  71. virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
  72. // See: TwoBodyConstraint
  73. virtual Mat44 GetConstraintToBody1Matrix() const override;
  74. virtual Mat44 GetConstraintToBody2Matrix() const override;
  75. /// Get the current distance from the rest position
  76. float GetCurrentPosition() const;
  77. /// Friction control
  78. void SetMaxFrictionForce(float inFrictionForce) { mMaxFrictionForce = inFrictionForce; }
  79. float GetMaxFrictionForce() const { return mMaxFrictionForce; }
  80. /// Motor settings
  81. MotorSettings & GetMotorSettings() { return mMotorSettings; }
  82. const MotorSettings & GetMotorSettings() const { return mMotorSettings; }
  83. // Motor controls
  84. void SetMotorState(EMotorState inState) { JPH_ASSERT(inState == EMotorState::Off || mMotorSettings.IsValid()); mMotorState = inState; }
  85. EMotorState GetMotorState() const { return mMotorState; }
  86. void SetTargetVelocity(float inVelocity) { mTargetVelocity = inVelocity; }
  87. float GetTargetVelocity() const { return mTargetVelocity; }
  88. void SetTargetPosition(float inPosition) { mTargetPosition = mHasLimits? Clamp(inPosition, mLimitsMin, mLimitsMax) : inPosition; }
  89. float GetTargetPosition() const { return mTargetPosition; }
  90. /// Update the limits of the slider constraint (see SliderConstraintSettings)
  91. void SetLimits(float inLimitsMin, float inLimitsMax);
  92. float GetLimitsMin() const { return mLimitsMin; }
  93. float GetLimitsMax() const { return mLimitsMax; }
  94. bool HasLimits() const { return mHasLimits; }
  95. /// Update the spring frequency for the limits constraint
  96. void SetFrequency(float inFrequency) { JPH_ASSERT(inFrequency >= 0.0f); mFrequency = inFrequency; }
  97. float GetFrequency() const { return mFrequency; }
  98. /// Update the spring damping for the limits constraint
  99. void SetDamping(float inDamping) { JPH_ASSERT(inDamping >= 0.0f); mDamping = inDamping; }
  100. float GetDamping() const { return mDamping; }
  101. ///@name Get Lagrange multiplier from last physics update (relates to how much force/torque was applied to satisfy the constraint)
  102. inline Vector<2> GetTotalLambdaPosition() const { return mPositionConstraintPart.GetTotalLambda(); }
  103. inline float GetTotalLambdaPositionLimits() const { return mPositionLimitsConstraintPart.GetTotalLambda(); }
  104. inline Vec3 GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }
  105. inline float GetTotalLambdaMotor() const { return mMotorConstraintPart.GetTotalLambda(); }
  106. private:
  107. // Internal helper function to calculate the values below
  108. void CalculateR1R2U(Mat44Arg inRotation1, Mat44Arg inRotation2);
  109. void CalculateSlidingAxisAndPosition(Mat44Arg inRotation1);
  110. void CalculatePositionConstraintProperties(Mat44Arg inRotation1, Mat44Arg inRotation2);
  111. void CalculatePositionLimitsConstraintProperties(float inDeltaTime);
  112. void CalculateMotorConstraintProperties(float inDeltaTime);
  113. // CONFIGURATION PROPERTIES FOLLOW
  114. // Local space constraint positions
  115. Vec3 mLocalSpacePosition1;
  116. Vec3 mLocalSpacePosition2;
  117. // Local space sliding direction
  118. Vec3 mLocalSpaceSliderAxis1;
  119. // Local space normals to the sliding direction (in body 1 space)
  120. Vec3 mLocalSpaceNormal1;
  121. Vec3 mLocalSpaceNormal2;
  122. // Inverse of initial rotation from body 1 to body 2 in body 1 space
  123. Quat mInvInitialOrientation;
  124. // Slider limits
  125. bool mHasLimits;
  126. float mLimitsMin;
  127. float mLimitsMax;
  128. // Soft slider limits
  129. float mFrequency;
  130. float mDamping;
  131. // Friction
  132. float mMaxFrictionForce;
  133. // Motor controls
  134. MotorSettings mMotorSettings;
  135. EMotorState mMotorState = EMotorState::Off;
  136. float mTargetVelocity = 0.0f;
  137. float mTargetPosition = 0.0f;
  138. // RUN TIME PROPERTIES FOLLOW
  139. // Positions where the point constraint acts on (middle point between center of masses)
  140. Vec3 mR1;
  141. Vec3 mR2;
  142. // X2 + R2 - X1 - R1
  143. Vec3 mU;
  144. // World space sliding direction
  145. Vec3 mWorldSpaceSliderAxis;
  146. // Normals to the slider axis
  147. Vec3 mN1;
  148. Vec3 mN2;
  149. // Distance along the slide axis
  150. float mD = 0.0f;
  151. // The constraint parts
  152. DualAxisConstraintPart mPositionConstraintPart;
  153. RotationEulerConstraintPart mRotationConstraintPart;
  154. AxisConstraintPart mPositionLimitsConstraintPart;
  155. AxisConstraintPart mMotorConstraintPart;
  156. };
  157. JPH_NAMESPACE_END