SwingTwistConstraint.h 9.3 KB

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