HingeConstraint.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/PointConstraintPart.h>
  7. #include <Jolt/Physics/Constraints/ConstraintPart/HingeRotationConstraintPart.h>
  8. #include <Jolt/Physics/Constraints/ConstraintPart/AngleConstraintPart.h>
  9. JPH_NAMESPACE_BEGIN
  10. /// Hinge constraint settings, used to create a hinge constraint
  11. class HingeConstraintSettings final : public TwoBodyConstraintSettings
  12. {
  13. public:
  14. JPH_DECLARE_SERIALIZABLE_VIRTUAL(HingeConstraintSettings)
  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. /// This determines in which space the constraint is setup, all properties below should be in the specified space
  20. EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
  21. /// Body 1 constraint reference frame (space determined by mSpace).
  22. /// Hinge axis is the axis where rotation is allowed, normal axis defines the 0 angle of the hinge.
  23. Vec3 mPoint1 = Vec3::sZero();
  24. Vec3 mHingeAxis1 = Vec3::sAxisY();
  25. Vec3 mNormalAxis1 = Vec3::sAxisX();
  26. /// Body 2 constraint reference frame (space determined by mSpace)
  27. Vec3 mPoint2 = Vec3::sZero();
  28. Vec3 mHingeAxis2 = Vec3::sAxisY();
  29. Vec3 mNormalAxis2 = Vec3::sAxisX();
  30. /// Bodies are assumed to be placed so that the hinge angle = 0, movement will be limited between [mLimitsMin, mLimitsMax] where mLimitsMin e [-pi, 0] and mLimitsMax e [0, pi].
  31. /// Both angles are in radians.
  32. float mLimitsMin = -JPH_PI;
  33. float mLimitsMax = JPH_PI;
  34. /// Maximum amount of torque (N m) to apply as friction when the constraint is not powered by a motor
  35. float mMaxFrictionTorque = 0.0f;
  36. /// In case the constraint is powered, this determines the motor settings around the hinge axis
  37. MotorSettings mMotorSettings;
  38. protected:
  39. // See: ConstraintSettings::RestoreBinaryState
  40. virtual void RestoreBinaryState(StreamIn &inStream) override;
  41. };
  42. /// A hinge constraint constrains 2 bodies on a single point and allows only a single axis of rotation
  43. class HingeConstraint final : public TwoBodyConstraint
  44. {
  45. public:
  46. JPH_OVERRIDE_NEW_DELETE
  47. /// Construct hinge constraint
  48. HingeConstraint(Body &inBody1, Body &inBody2, const HingeConstraintSettings &inSettings);
  49. // Generic interface of a constraint
  50. virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Hinge; }
  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. virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
  62. // See: TwoBodyConstraint
  63. virtual Mat44 GetConstraintToBody1Matrix() const override;
  64. virtual Mat44 GetConstraintToBody2Matrix() const override;
  65. /// Get the current rotation angle from the rest position
  66. float GetCurrentAngle() const;
  67. // Friction control
  68. void SetMaxFrictionTorque(float inFrictionTorque) { mMaxFrictionTorque = inFrictionTorque; }
  69. float GetMaxFrictionTorque() const { return mMaxFrictionTorque; }
  70. // Motor settings
  71. MotorSettings & GetMotorSettings() { return mMotorSettings; }
  72. const MotorSettings & GetMotorSettings() const { return mMotorSettings; }
  73. // Motor controls
  74. void SetMotorState(EMotorState inState) { JPH_ASSERT(inState == EMotorState::Off || mMotorSettings.IsValid()); mMotorState = inState; }
  75. EMotorState GetMotorState() const { return mMotorState; }
  76. void SetTargetAngularVelocity(float inAngularVelocity) { mTargetAngularVelocity = inAngularVelocity; } ///< rad/s
  77. float GetTargetAngularVelocity() const { return mTargetAngularVelocity; }
  78. void SetTargetAngle(float inAngle) { mTargetAngle = mHasLimits? Clamp(inAngle, mLimitsMin, mLimitsMax) : inAngle; } ///< rad
  79. float GetTargetAngle() const { return mTargetAngle; }
  80. /// Update the rotation limits of the hinge, value in radians (see HingeConstraintSettings)
  81. void SetLimits(float inLimitsMin, float inLimitsMax);
  82. float GetLimitsMin() const { return mLimitsMin; }
  83. float GetLimitsMax() const { return mLimitsMax; }
  84. bool HasLimits() const { return mHasLimits; }
  85. ///@name Get Lagrange multiplier from last physics update (relates to how much force/torque was applied to satisfy the constraint)
  86. inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }
  87. inline Vector<2> GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }
  88. inline float GetTotalLambdaRotationLimits() const { return mRotationLimitsConstraintPart.GetTotalLambda(); }
  89. inline float GetTotalLambdaMotor() const { return mMotorConstraintPart.GetTotalLambda(); }
  90. private:
  91. // Internal helper function to calculate the values below
  92. void CalculateA1AndTheta();
  93. void CalculateRotationLimitsConstraintProperties(float inDeltaTime);
  94. void CalculateMotorConstraintProperties(float inDeltaTime);
  95. inline float GetSmallestAngleToLimit() const;
  96. // CONFIGURATION PROPERTIES FOLLOW
  97. // Local space constraint positions
  98. Vec3 mLocalSpacePosition1;
  99. Vec3 mLocalSpacePosition2;
  100. // Local space hinge directions
  101. Vec3 mLocalSpaceHingeAxis1;
  102. Vec3 mLocalSpaceHingeAxis2;
  103. // Local space normal direction (direction relative to which to draw constraint limits)
  104. Vec3 mLocalSpaceNormalAxis1;
  105. Vec3 mLocalSpaceNormalAxis2;
  106. // Inverse of initial relative orientation between bodies (which defines hinge angle = 0)
  107. Quat mInvInitialOrientation;
  108. // Hinge limits
  109. bool mHasLimits;
  110. float mLimitsMin;
  111. float mLimitsMax;
  112. // Friction
  113. float mMaxFrictionTorque;
  114. // Motor controls
  115. MotorSettings mMotorSettings;
  116. EMotorState mMotorState = EMotorState::Off;
  117. float mTargetAngularVelocity = 0.0f;
  118. float mTargetAngle = 0.0f;
  119. // RUN TIME PROPERTIES FOLLOW
  120. // Current rotation around the hinge axis
  121. float mTheta = 0.0f;
  122. // World space hinge axis for body 1
  123. Vec3 mA1;
  124. // The constraint parts
  125. PointConstraintPart mPointConstraintPart;
  126. HingeRotationConstraintPart mRotationConstraintPart;
  127. AngleConstraintPart mRotationLimitsConstraintPart;
  128. AngleConstraintPart mMotorConstraintPart;
  129. };
  130. JPH_NAMESPACE_END