HingeConstraint.h 7.2 KB

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