SwingTwistConstraint.h 9.1 KB

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