HingeConstraint.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 JPH_EXPORT HingeConstraintSettings final : public TwoBodyConstraintSettings
  13. {
  14. JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, HingeConstraintSettings)
  15. public:
  16. // See: ConstraintSettings::SaveBinaryState
  17. virtual void SaveBinaryState(StreamOut &inStream) const override;
  18. /// Create 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.
  24. /// When the normal axis of both bodies align in world space, the hinge angle is defined to be 0.
  25. /// mHingeAxis1 and mNormalAxis1 should be perpendicular. mHingeAxis2 and mNormalAxis2 should also be perpendicular.
  26. /// If you configure the joint in world space and create both bodies with a relative rotation you want to be defined as zero,
  27. /// you can simply set mHingeAxis1 = mHingeAxis2 and mNormalAxis1 = mNormalAxis2.
  28. RVec3 mPoint1 = RVec3::sZero();
  29. Vec3 mHingeAxis1 = Vec3::sAxisY();
  30. Vec3 mNormalAxis1 = Vec3::sAxisX();
  31. /// Body 2 constraint reference frame (space determined by mSpace)
  32. RVec3 mPoint2 = RVec3::sZero();
  33. Vec3 mHingeAxis2 = Vec3::sAxisY();
  34. Vec3 mNormalAxis2 = Vec3::sAxisX();
  35. /// Rotation around the hinge axis will be limited between [mLimitsMin, mLimitsMax] where mLimitsMin e [-pi, 0] and mLimitsMax e [0, pi].
  36. /// Both angles are in radians.
  37. float mLimitsMin = -JPH_PI;
  38. float mLimitsMax = JPH_PI;
  39. /// When enabled, this makes the limits soft. When the constraint exceeds the limits, a spring force will pull it back.
  40. SpringSettings mLimitsSpringSettings;
  41. /// Maximum amount of torque (N m) to apply as friction when the constraint is not powered by a motor
  42. float mMaxFrictionTorque = 0.0f;
  43. /// In case the constraint is powered, this determines the motor settings around the hinge axis
  44. MotorSettings mMotorSettings;
  45. protected:
  46. // See: ConstraintSettings::RestoreBinaryState
  47. virtual void RestoreBinaryState(StreamIn &inStream) override;
  48. };
  49. /// A hinge constraint constrains 2 bodies on a single point and allows only a single axis of rotation
  50. class JPH_EXPORT HingeConstraint final : public TwoBodyConstraint
  51. {
  52. public:
  53. JPH_OVERRIDE_NEW_DELETE
  54. /// Construct hinge constraint
  55. HingeConstraint(Body &inBody1, Body &inBody2, const HingeConstraintSettings &inSettings);
  56. // Generic interface of a constraint
  57. virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Hinge; }
  58. virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;
  59. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  60. virtual void ResetWarmStart() override;
  61. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  62. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  63. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  64. #ifdef JPH_DEBUG_RENDERER
  65. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  66. virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;
  67. #endif // JPH_DEBUG_RENDERER
  68. virtual void SaveState(StateRecorder &inStream) const override;
  69. virtual void RestoreState(StateRecorder &inStream) override;
  70. virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
  71. // See: TwoBodyConstraint
  72. virtual Mat44 GetConstraintToBody1Matrix() const override;
  73. virtual Mat44 GetConstraintToBody2Matrix() const override;
  74. /// Get the attachment point for body 1 relative to body 1 COM (transform by Body::GetCenterOfMassTransform to take to world space)
  75. inline Vec3 GetLocalSpacePoint1() const { return mLocalSpacePosition1; }
  76. /// Get the attachment point for body 2 relative to body 2 COM (transform by Body::GetCenterOfMassTransform to take to world space)
  77. inline Vec3 GetLocalSpacePoint2() const { return mLocalSpacePosition2; }
  78. // Local space hinge directions (transform direction by Body::GetCenterOfMassTransform to take to world space)
  79. Vec3 GetLocalSpaceHingeAxis1() const { return mLocalSpaceHingeAxis1; }
  80. Vec3 GetLocalSpaceHingeAxis2() const { return mLocalSpaceHingeAxis2; }
  81. // Local space normal directions (transform direction by Body::GetCenterOfMassTransform to take to world space)
  82. Vec3 GetLocalSpaceNormalAxis1() const { return mLocalSpaceNormalAxis1; }
  83. Vec3 GetLocalSpaceNormalAxis2() const { return mLocalSpaceNormalAxis2; }
  84. /// Get the current rotation angle from the rest position
  85. float GetCurrentAngle() const;
  86. // Friction control
  87. void SetMaxFrictionTorque(float inFrictionTorque) { mMaxFrictionTorque = inFrictionTorque; }
  88. float GetMaxFrictionTorque() const { return mMaxFrictionTorque; }
  89. // Motor settings
  90. MotorSettings & GetMotorSettings() { return mMotorSettings; }
  91. const MotorSettings & GetMotorSettings() const { return mMotorSettings; }
  92. // Motor controls
  93. void SetMotorState(EMotorState inState) { JPH_ASSERT(inState == EMotorState::Off || mMotorSettings.IsValid()); mMotorState = inState; }
  94. EMotorState GetMotorState() const { return mMotorState; }
  95. void SetTargetAngularVelocity(float inAngularVelocity) { mTargetAngularVelocity = inAngularVelocity; } ///< rad/s
  96. float GetTargetAngularVelocity() const { return mTargetAngularVelocity; }
  97. void SetTargetAngle(float inAngle) { mTargetAngle = mHasLimits? Clamp(inAngle, mLimitsMin, mLimitsMax) : inAngle; } ///< rad
  98. float GetTargetAngle() const { return mTargetAngle; }
  99. /// Set the target orientation in body space (R2 = R1 * inOrientation, where R1 and R2 are the world space rotations for body 1 and 2).
  100. /// Calculates the local space target angle and calls SetTargetAngle. Motor state must be EMotorState::Position for this to have any effect.
  101. /// May set the wrong angle if inOrientation contains large rotations around other axis than the hinge axis.
  102. void SetTargetOrientationBS(QuatArg inOrientation);
  103. /// Update the rotation limits of the hinge, value in radians (see HingeConstraintSettings)
  104. void SetLimits(float inLimitsMin, float inLimitsMax);
  105. float GetLimitsMin() const { return mLimitsMin; }
  106. float GetLimitsMax() const { return mLimitsMax; }
  107. bool HasLimits() const { return mHasLimits; }
  108. /// Update the limits spring settings
  109. const SpringSettings & GetLimitsSpringSettings() const { return mLimitsSpringSettings; }
  110. SpringSettings & GetLimitsSpringSettings() { return mLimitsSpringSettings; }
  111. void SetLimitsSpringSettings(const SpringSettings &inLimitsSpringSettings) { mLimitsSpringSettings = inLimitsSpringSettings; }
  112. ///@name Get Lagrange multiplier from last physics update (the linear/angular impulse applied to satisfy the constraint)
  113. inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }
  114. inline Vector<2> GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }
  115. inline float GetTotalLambdaRotationLimits() const { return mRotationLimitsConstraintPart.GetTotalLambda(); }
  116. inline float GetTotalLambdaMotor() const { return mMotorConstraintPart.GetTotalLambda(); }
  117. private:
  118. // Internal helper function to calculate the values below
  119. void CalculateA1AndTheta();
  120. void CalculateRotationLimitsConstraintProperties(float inDeltaTime);
  121. void CalculateMotorConstraintProperties(float inDeltaTime);
  122. inline float GetSmallestAngleToLimit() const;
  123. inline bool IsMinLimitClosest() const;
  124. // CONFIGURATION PROPERTIES FOLLOW
  125. // Local space constraint positions
  126. Vec3 mLocalSpacePosition1;
  127. Vec3 mLocalSpacePosition2;
  128. // Local space hinge directions
  129. Vec3 mLocalSpaceHingeAxis1;
  130. Vec3 mLocalSpaceHingeAxis2;
  131. // Local space normal direction (direction relative to which to draw constraint limits)
  132. Vec3 mLocalSpaceNormalAxis1;
  133. Vec3 mLocalSpaceNormalAxis2;
  134. // Inverse of initial relative orientation between bodies (which defines hinge angle = 0)
  135. Quat mInvInitialOrientation;
  136. // Hinge limits
  137. bool mHasLimits;
  138. float mLimitsMin;
  139. float mLimitsMax;
  140. // Soft constraint limits
  141. SpringSettings mLimitsSpringSettings;
  142. // Friction
  143. float mMaxFrictionTorque;
  144. // Motor controls
  145. MotorSettings mMotorSettings;
  146. EMotorState mMotorState = EMotorState::Off;
  147. float mTargetAngularVelocity = 0.0f;
  148. float mTargetAngle = 0.0f;
  149. // RUN TIME PROPERTIES FOLLOW
  150. // Current rotation around the hinge axis
  151. float mTheta = 0.0f;
  152. // World space hinge axis for body 1
  153. Vec3 mA1;
  154. // The constraint parts
  155. PointConstraintPart mPointConstraintPart;
  156. HingeRotationConstraintPart mRotationConstraintPart;
  157. AngleConstraintPart mRotationLimitsConstraintPart;
  158. AngleConstraintPart mMotorConstraintPart;
  159. };
  160. JPH_NAMESPACE_END