HingeConstraint.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. /// Body 1 constraint reference frame (in world space).
  20. /// Hinge axis is the axis where rotation is allowed, normal axis defines the 0 angle of the hinge.
  21. Vec3 mPoint1 = Vec3::sZero();
  22. Vec3 mHingeAxis1 = Vec3::sAxisY();
  23. Vec3 mNormalAxis1 = Vec3::sAxisX();
  24. /// Body 2 constraint reference frame (in world space)
  25. Vec3 mPoint2 = Vec3::sZero();
  26. Vec3 mHingeAxis2 = Vec3::sAxisY();
  27. Vec3 mNormalAxis2 = Vec3::sAxisX();
  28. /// 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].
  29. /// Both angles are in radians.
  30. float mLimitsMin = -JPH_PI;
  31. float mLimitsMax = JPH_PI;
  32. /// Maximum amount of torque (N m) to apply as friction when the constraint is not powered by a motor
  33. float mMaxFrictionTorque = 0.0f;
  34. /// In case the constraint is powered, this determines the motor settings around the hinge axis
  35. MotorSettings mMotorSettings;
  36. protected:
  37. // See: ConstraintSettings::RestoreBinaryState
  38. virtual void RestoreBinaryState(StreamIn &inStream) override;
  39. };
  40. /// A hinge constraint constrains 2 bodies on a single point and allows only a single axis of rotation
  41. class HingeConstraint final : public TwoBodyConstraint
  42. {
  43. public:
  44. /// Construct hinge constraint
  45. HingeConstraint(Body &inBody1, Body &inBody2, const HingeConstraintSettings &inSettings);
  46. // Generic interface of a constraint
  47. virtual EConstraintType GetType() const override { return EConstraintType::Hinge; }
  48. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  49. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  50. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  51. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  52. #ifdef JPH_DEBUG_RENDERER
  53. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  54. virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;
  55. #endif // JPH_DEBUG_RENDERER
  56. virtual void SaveState(StateRecorder &inStream) const override;
  57. virtual void RestoreState(StateRecorder &inStream) override;
  58. // See: TwoBodyConstraint
  59. virtual Mat44 GetConstraintToBody1Matrix() const override;
  60. virtual Mat44 GetConstraintToBody2Matrix() const override;
  61. // Friction control
  62. void SetMaxFrictionTorque(float inFrictionTorque) { mMaxFrictionTorque = inFrictionTorque; }
  63. float GetMaxFrictionTorque() const { return mMaxFrictionTorque; }
  64. // Motor settings
  65. MotorSettings & GetMotorSettings() { return mMotorSettings; }
  66. const MotorSettings & GetMotorSettings() const { return mMotorSettings; }
  67. // Motor controls
  68. void SetMotorState(EMotorState inState) { JPH_ASSERT(inState == EMotorState::Off || mMotorSettings.IsValid()); mMotorState = inState; }
  69. EMotorState GetMotorState() const { return mMotorState; }
  70. void SetTargetAngularVelocity(float inAngularVelocity) { mTargetAngularVelocity = inAngularVelocity; } ///< rad/s
  71. float GetTargetAngularVelocity() const { return mTargetAngularVelocity; }
  72. void SetTargetAngle(float inAngle) { mTargetAngle = mHasLimits? Clamp(inAngle, mLimitsMin, mLimitsMax) : inAngle; } ///< rad
  73. float GetTargetAngle() const { return mTargetAngle; }
  74. /// Update the rotation limits of the hinge, value in radians (see HingeConstraintSettings)
  75. void SetLimits(float inLimitsMin, float inLimitsMax);
  76. float GetLimitsMin() const { return mLimitsMin; }
  77. float GetLimitsMax() const { return mLimitsMax; }
  78. bool HasLimits() const { return mHasLimits; }
  79. ///@name Get Lagrange multiplier from last physics update (relates to how much force/torque was applied to satisfy the constraint)
  80. inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }
  81. inline Vector<2> GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }
  82. inline float GetTotalLambdaRotationLimits() const { return mRotationLimitsConstraintPart.GetTotalLambda(); }
  83. inline float GetTotalLambdaMotor() const { return mMotorConstraintPart.GetTotalLambda(); }
  84. private:
  85. // Internal helper function to calculate the values below
  86. void CalculateA1AndTheta();
  87. void CalculateRotationLimitsConstraintProperties(float inDeltaTime);
  88. void CalculateMotorConstraintProperties(float inDeltaTime);
  89. inline float GetSmallestAngleToLimit() const;
  90. // CONFIGURATION PROPERTIES FOLLOW
  91. // Local space constraint positions
  92. Vec3 mLocalSpacePosition1;
  93. Vec3 mLocalSpacePosition2;
  94. // Local space hinge directions
  95. Vec3 mLocalSpaceHingeAxis1;
  96. Vec3 mLocalSpaceHingeAxis2;
  97. // Local space normal direction (direction relative to which to draw constraint limits)
  98. Vec3 mLocalSpaceNormalAxis1;
  99. Vec3 mLocalSpaceNormalAxis2;
  100. // Inverse of initial relative orientation between bodies (which defines hinge angle = 0)
  101. Quat mInvInitialOrientation;
  102. // Hinge limits
  103. bool mHasLimits;
  104. float mLimitsMin;
  105. float mLimitsMax;
  106. // Friction
  107. float mMaxFrictionTorque;
  108. // Motor controls
  109. MotorSettings mMotorSettings;
  110. EMotorState mMotorState = EMotorState::Off;
  111. float mTargetAngularVelocity = 0.0f;
  112. float mTargetAngle = 0.0f;
  113. // RUN TIME PROPERTIES FOLLOW
  114. // Current rotation around the hinge axis
  115. float mTheta = 0.0f;
  116. // World space hinge axis for body 1
  117. Vec3 mA1;
  118. // The constraint parts
  119. PointConstraintPart mPointConstraintPart;
  120. HingeRotationConstraintPart mRotationConstraintPart;
  121. AngleConstraintPart mRotationLimitsConstraintPart;
  122. AngleConstraintPart mMotorConstraintPart;
  123. };
  124. } // JPH