SwingTwistConstraint.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/AngleConstraintPart.h>
  8. #include <Physics/Constraints/ConstraintPart/RotationEulerConstraintPart.h>
  9. #include <Physics/Constraints/ConstraintPart/SwingTwistConstraintPart.h>
  10. namespace JPH {
  11. /// Swing twist constraint settings, used to create a swing twist constraint
  12. /// All values in this structure are copied to the swing twist constraint and the settings object is no longer needed afterwards.
  13. ///
  14. /// This image describes the limit settings:
  15. /// @image html Docs/SwingTwistConstraint.png
  16. class SwingTwistConstraintSettings final : public TwoBodyConstraintSettings
  17. {
  18. public:
  19. JPH_DECLARE_SERIALIZABLE_VIRTUAL(SwingTwistConstraintSettings)
  20. // See: ConstraintSettings::SaveBinaryState
  21. virtual void SaveBinaryState(StreamOut &inStream) const override;
  22. /// Create an an instance of this constraint
  23. virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
  24. /// This determines in which space the constraint is setup, all properties below should be in the specified space
  25. EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
  26. ///@name Body 1 constraint reference frame (space determined by mSpace)
  27. Vec3 mPosition1 = Vec3::sZero();
  28. Vec3 mTwistAxis1 = Vec3::sAxisX();
  29. Vec3 mPlaneAxis1 = Vec3::sAxisY();
  30. ///@name Body 2 constraint reference frame (space determined by mSpace)
  31. Vec3 mPosition2 = Vec3::sZero();
  32. Vec3 mTwistAxis2 = Vec3::sAxisX();
  33. Vec3 mPlaneAxis2 = Vec3::sAxisY();
  34. ///@name Swing rotation limits
  35. float mNormalHalfConeAngle = 0.0f; ///< See image. Angle in radians.
  36. float mPlaneHalfConeAngle = 0.0f; ///< See image. Angle in radians.
  37. ///@name Twist rotation limits
  38. float mTwistMinAngle = 0.0f; ///< See image. Angle in radians. Rotation will be limited between [mLimitsMin, mLimitsMax] where mLimitsMin \f$\in [-\pi, 0]\f$ and mLimitsMax \f$\in [0, \pi]\f$
  39. float mTwistMaxAngle = 0.0f; ///< See image. Angle in radians.
  40. ///@name Friction
  41. float mMaxFrictionTorque = 0.0f; ///< Maximum amount of torque (N m) to apply as friction when the constraint is not powered by a motor
  42. ///@name In case the constraint is powered, this determines the motor settings around the swing and twist axis
  43. MotorSettings mSwingMotorSettings;
  44. MotorSettings mTwistMotorSettings;
  45. protected:
  46. // See: ConstraintSettings::RestoreBinaryState
  47. virtual void RestoreBinaryState(StreamIn &inStream) override;
  48. };
  49. /// A swing twist constraint is a specialized constraint for humanoid ragdolls that allows limited rotation only
  50. ///
  51. /// @see SwingTwistConstraintSettings for a description of the limits
  52. class SwingTwistConstraint final : public TwoBodyConstraint
  53. {
  54. public:
  55. /// Construct swing twist constraint
  56. SwingTwistConstraint(Body &inBody1, Body &inBody2, const SwingTwistConstraintSettings &inSettings);
  57. ///@name Generic interface of a constraint
  58. virtual EConstraintType GetType() const override { return EConstraintType::SwingTwist; }
  59. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  60. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  61. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  62. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  63. #ifdef JPH_DEBUG_RENDERER
  64. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  65. virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;
  66. #endif // JPH_DEBUG_RENDERER
  67. virtual void SaveState(StateRecorder &inStream) const override;
  68. virtual void RestoreState(StateRecorder &inStream) override;
  69. // See: TwoBodyConstraint
  70. virtual Mat44 GetConstraintToBody1Matrix() const override { return Mat44::sRotationTranslation(mConstraintToBody1, mLocalSpacePosition1); }
  71. virtual Mat44 GetConstraintToBody2Matrix() const override { return Mat44::sRotationTranslation(mConstraintToBody2, mLocalSpacePosition2); }
  72. ///@name Constraint reference frame
  73. inline Vec3 GetLocalSpacePosition1() const { return mLocalSpacePosition1; }
  74. inline Vec3 GetLocalSpacePosition2() const { return mLocalSpacePosition2; }
  75. inline Quat GetConstraintToBody1() const { return mConstraintToBody1; }
  76. inline Quat GetConstraintToBody2() const { return mConstraintToBody2; }
  77. ///@name Constraint limits
  78. inline float GetNormalHalfConeAngle() const { return mNormalHalfConeAngle; }
  79. inline void SetNormalHalfConeAngle(float inAngle) { mNormalHalfConeAngle = inAngle; UpdateLimits(); }
  80. inline float GetPlaneHalfConeAngle() const { return mPlaneHalfConeAngle; }
  81. inline void SetPlaneHalfConeAngle(float inAngle) { mPlaneHalfConeAngle = inAngle; UpdateLimits(); }
  82. inline float GetTwistMinAngle() const { return mTwistMinAngle; }
  83. inline void SetTwistMinAngle(float inAngle) { mTwistMinAngle = inAngle; UpdateLimits(); }
  84. inline float GetTwistMaxAngle() const { return mTwistMaxAngle; }
  85. inline void SetTwistMaxAngle(float inAngle) { mTwistMaxAngle = inAngle; UpdateLimits(); }
  86. ///@name Motor settings
  87. const MotorSettings & GetSwingMotorSettings() const { return mSwingMotorSettings; }
  88. MotorSettings & GetSwingMotorSettings() { return mSwingMotorSettings; }
  89. const MotorSettings & GetTwistMotorSettings() const { return mTwistMotorSettings; }
  90. MotorSettings & GetTwistMotorSettings() { return mTwistMotorSettings; }
  91. ///@name Friction control
  92. void SetMaxFrictionTorque(float inFrictionTorque) { mMaxFrictionTorque = inFrictionTorque; }
  93. float GetMaxFrictionTorque() const { return mMaxFrictionTorque; }
  94. ///@name Motor controls
  95. /// Controls if the motors are on or off
  96. void SetSwingMotorState(EMotorState inState);
  97. EMotorState GetSwingMotorState() const { return mSwingMotorState; }
  98. void SetTwistMotorState(EMotorState inState);
  99. EMotorState GetTwistMotorState() const { return mTwistMotorState; }
  100. /// Set the target angular velocity of body 2 in constraint space of body 2
  101. void SetTargetAngularVelocityCS(Vec3Arg inAngularVelocity) { mTargetAngularVelocity = inAngularVelocity; }
  102. Vec3 GetTargetAngularVelocityCS() const { return mTargetAngularVelocity; }
  103. /// Set the target orientation in constraint space (drives constraint to: GetRotationInConstraintSpace() == inOrientation)
  104. void SetTargetOrientationCS(QuatArg inOrientation);
  105. Quat GetTargetOrientationCS() const { return mTargetOrientation; }
  106. /// Set the target orientation in body space (R2 = R1 * inOrientation, where R1 and R2 are the world space rotations for body 1 and 2).
  107. /// Solve: R2 * ConstraintToBody2 = R1 * ConstraintToBody1 * q (see SwingTwistConstraint::GetSwingTwist) and R2 = R1 * inOrientation for q.
  108. void SetTargetOrientationBS(QuatArg inOrientation) { SetTargetOrientationCS(mConstraintToBody1.Conjugated() * inOrientation * mConstraintToBody2); }
  109. /// Get current rotation of constraint in constraint space.
  110. /// Solve: R2 * ConstraintToBody2 = R1 * ConstraintToBody1 * q for q.
  111. inline Quat GetRotationInConstraintSpace() const;
  112. ///@name Get Lagrange multiplier from last physics update (relates to how much force/torque was applied to satisfy the constraint)
  113. inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }
  114. inline float GetTotalLambdaTwist() const { return mSwingTwistConstraintPart.GetTotalTwistLambda(); }
  115. inline float GetTotalLambdaSwingY() const { return mSwingTwistConstraintPart.GetTotalSwingYLambda(); }
  116. inline float GetTotalLambdaSwingZ() const { return mSwingTwistConstraintPart.GetTotalSwingZLambda(); }
  117. inline Vec3 GetTotalLambdaMotor() const { return Vec3(mMotorConstraintPart[0].GetTotalLambda(), mMotorConstraintPart[1].GetTotalLambda(), mMotorConstraintPart[2].GetTotalLambda()); }
  118. private:
  119. // Update the limits in the swing twist constraint part
  120. void UpdateLimits();
  121. // CONFIGURATION PROPERTIES FOLLOW
  122. // Local space constraint positions
  123. Vec3 mLocalSpacePosition1;
  124. Vec3 mLocalSpacePosition2;
  125. // Transforms from constraint space to body space
  126. Quat mConstraintToBody1;
  127. Quat mConstraintToBody2;
  128. // Limits
  129. float mNormalHalfConeAngle;
  130. float mPlaneHalfConeAngle;
  131. float mTwistMinAngle;
  132. float mTwistMaxAngle;
  133. // Friction
  134. float mMaxFrictionTorque;
  135. // Motor controls
  136. MotorSettings mSwingMotorSettings;
  137. MotorSettings mTwistMotorSettings;
  138. EMotorState mSwingMotorState = EMotorState::Off;
  139. EMotorState mTwistMotorState = EMotorState::Off;
  140. Vec3 mTargetAngularVelocity = Vec3::sZero();
  141. Quat mTargetOrientation = Quat::sIdentity();
  142. // RUN TIME PROPERTIES FOLLOW
  143. // Rotation axis for motor constraint parts
  144. Vec3 mWorldSpaceMotorAxis[3];
  145. // The constraint parts
  146. PointConstraintPart mPointConstraintPart;
  147. SwingTwistConstraintPart mSwingTwistConstraintPart;
  148. AngleConstraintPart mMotorConstraintPart[3];
  149. };
  150. } // JPH