HingeConstraint.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/PointConstraintPart.h>
  7. #include <Physics/Constraints/ConstraintPart/HingeRotationConstraintPart.h>
  8. #include <Physics/Constraints/ConstraintPart/AngleConstraintPart.h>
  9. namespace JPH {
  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. /// Construct hinge constraint
  47. HingeConstraint(Body &inBody1, Body &inBody2, const HingeConstraintSettings &inSettings);
  48. // Generic interface of a constraint
  49. virtual EConstraintType GetType() const override { return EConstraintType::Hinge; }
  50. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  51. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  52. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  53. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  54. #ifdef JPH_DEBUG_RENDERER
  55. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  56. virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;
  57. #endif // JPH_DEBUG_RENDERER
  58. virtual void SaveState(StateRecorder &inStream) const override;
  59. virtual void RestoreState(StateRecorder &inStream) override;
  60. // See: TwoBodyConstraint
  61. virtual Mat44 GetConstraintToBody1Matrix() const override;
  62. virtual Mat44 GetConstraintToBody2Matrix() const override;
  63. // Friction control
  64. void SetMaxFrictionTorque(float inFrictionTorque) { mMaxFrictionTorque = inFrictionTorque; }
  65. float GetMaxFrictionTorque() const { return mMaxFrictionTorque; }
  66. // Motor settings
  67. MotorSettings & GetMotorSettings() { return mMotorSettings; }
  68. const MotorSettings & GetMotorSettings() const { return mMotorSettings; }
  69. // Motor controls
  70. void SetMotorState(EMotorState inState) { JPH_ASSERT(inState == EMotorState::Off || mMotorSettings.IsValid()); mMotorState = inState; }
  71. EMotorState GetMotorState() const { return mMotorState; }
  72. void SetTargetAngularVelocity(float inAngularVelocity) { mTargetAngularVelocity = inAngularVelocity; } ///< rad/s
  73. float GetTargetAngularVelocity() const { return mTargetAngularVelocity; }
  74. void SetTargetAngle(float inAngle) { mTargetAngle = mHasLimits? Clamp(inAngle, mLimitsMin, mLimitsMax) : inAngle; } ///< rad
  75. float GetTargetAngle() const { return mTargetAngle; }
  76. /// Update the rotation limits of the hinge, value in radians (see HingeConstraintSettings)
  77. void SetLimits(float inLimitsMin, float inLimitsMax);
  78. float GetLimitsMin() const { return mLimitsMin; }
  79. float GetLimitsMax() const { return mLimitsMax; }
  80. bool HasLimits() const { return mHasLimits; }
  81. ///@name Get Lagrange multiplier from last physics update (relates to how much force/torque was applied to satisfy the constraint)
  82. inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }
  83. inline Vector<2> GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }
  84. inline float GetTotalLambdaRotationLimits() const { return mRotationLimitsConstraintPart.GetTotalLambda(); }
  85. inline float GetTotalLambdaMotor() const { return mMotorConstraintPart.GetTotalLambda(); }
  86. private:
  87. // Internal helper function to calculate the values below
  88. void CalculateA1AndTheta();
  89. void CalculateRotationLimitsConstraintProperties(float inDeltaTime);
  90. void CalculateMotorConstraintProperties(float inDeltaTime);
  91. inline float GetSmallestAngleToLimit() const;
  92. // CONFIGURATION PROPERTIES FOLLOW
  93. // Local space constraint positions
  94. Vec3 mLocalSpacePosition1;
  95. Vec3 mLocalSpacePosition2;
  96. // Local space hinge directions
  97. Vec3 mLocalSpaceHingeAxis1;
  98. Vec3 mLocalSpaceHingeAxis2;
  99. // Local space normal direction (direction relative to which to draw constraint limits)
  100. Vec3 mLocalSpaceNormalAxis1;
  101. Vec3 mLocalSpaceNormalAxis2;
  102. // Inverse of initial relative orientation between bodies (which defines hinge angle = 0)
  103. Quat mInvInitialOrientation;
  104. // Hinge limits
  105. bool mHasLimits;
  106. float mLimitsMin;
  107. float mLimitsMax;
  108. // Friction
  109. float mMaxFrictionTorque;
  110. // Motor controls
  111. MotorSettings mMotorSettings;
  112. EMotorState mMotorState = EMotorState::Off;
  113. float mTargetAngularVelocity = 0.0f;
  114. float mTargetAngle = 0.0f;
  115. // RUN TIME PROPERTIES FOLLOW
  116. // Current rotation around the hinge axis
  117. float mTheta = 0.0f;
  118. // World space hinge axis for body 1
  119. Vec3 mA1;
  120. // The constraint parts
  121. PointConstraintPart mPointConstraintPart;
  122. HingeRotationConstraintPart mRotationConstraintPart;
  123. AngleConstraintPart mRotationLimitsConstraintPart;
  124. AngleConstraintPart mMotorConstraintPart;
  125. };
  126. } // JPH