SwingTwistConstraint.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/AngleConstraintPart.h>
  9. #include <Jolt/Physics/Constraints/ConstraintPart/SwingTwistConstraintPart.h>
  10. JPH_NAMESPACE_BEGIN
  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 JPH_EXPORT SwingTwistConstraintSettings final : public TwoBodyConstraintSettings
  17. {
  18. public:
  19. JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, 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. RVec3 mPosition1 = RVec3::sZero();
  28. Vec3 mTwistAxis1 = Vec3::sAxisX();
  29. Vec3 mPlaneAxis1 = Vec3::sAxisY();
  30. ///@name Body 2 constraint reference frame (space determined by mSpace)
  31. RVec3 mPosition2 = RVec3::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 JPH_EXPORT SwingTwistConstraint final : public TwoBodyConstraint
  53. {
  54. public:
  55. JPH_OVERRIDE_NEW_DELETE
  56. /// Construct swing twist constraint
  57. SwingTwistConstraint(Body &inBody1, Body &inBody2, const SwingTwistConstraintSettings &inSettings);
  58. ///@name Generic interface of a constraint
  59. virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::SwingTwist; }
  60. virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;
  61. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  62. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  63. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  64. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  65. #ifdef JPH_DEBUG_RENDERER
  66. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  67. virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;
  68. #endif // JPH_DEBUG_RENDERER
  69. virtual void SaveState(StateRecorder &inStream) const override;
  70. virtual void RestoreState(StateRecorder &inStream) override;
  71. virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
  72. // See: TwoBodyConstraint
  73. virtual Mat44 GetConstraintToBody1Matrix() const override { return Mat44::sRotationTranslation(mConstraintToBody1, mLocalSpacePosition1); }
  74. virtual Mat44 GetConstraintToBody2Matrix() const override { return Mat44::sRotationTranslation(mConstraintToBody2, mLocalSpacePosition2); }
  75. ///@name Constraint reference frame
  76. inline Vec3 GetLocalSpacePosition1() const { return mLocalSpacePosition1; }
  77. inline Vec3 GetLocalSpacePosition2() const { return mLocalSpacePosition2; }
  78. inline Quat GetConstraintToBody1() const { return mConstraintToBody1; }
  79. inline Quat GetConstraintToBody2() const { return mConstraintToBody2; }
  80. ///@name Constraint limits
  81. inline float GetNormalHalfConeAngle() const { return mNormalHalfConeAngle; }
  82. inline void SetNormalHalfConeAngle(float inAngle) { mNormalHalfConeAngle = inAngle; UpdateLimits(); }
  83. inline float GetPlaneHalfConeAngle() const { return mPlaneHalfConeAngle; }
  84. inline void SetPlaneHalfConeAngle(float inAngle) { mPlaneHalfConeAngle = inAngle; UpdateLimits(); }
  85. inline float GetTwistMinAngle() const { return mTwistMinAngle; }
  86. inline void SetTwistMinAngle(float inAngle) { mTwistMinAngle = inAngle; UpdateLimits(); }
  87. inline float GetTwistMaxAngle() const { return mTwistMaxAngle; }
  88. inline void SetTwistMaxAngle(float inAngle) { mTwistMaxAngle = inAngle; UpdateLimits(); }
  89. ///@name Motor settings
  90. const MotorSettings & GetSwingMotorSettings() const { return mSwingMotorSettings; }
  91. MotorSettings & GetSwingMotorSettings() { return mSwingMotorSettings; }
  92. const MotorSettings & GetTwistMotorSettings() const { return mTwistMotorSettings; }
  93. MotorSettings & GetTwistMotorSettings() { return mTwistMotorSettings; }
  94. ///@name Friction control
  95. void SetMaxFrictionTorque(float inFrictionTorque) { mMaxFrictionTorque = inFrictionTorque; }
  96. float GetMaxFrictionTorque() const { return mMaxFrictionTorque; }
  97. ///@name Motor controls
  98. /// Controls if the motors are on or off
  99. void SetSwingMotorState(EMotorState inState);
  100. EMotorState GetSwingMotorState() const { return mSwingMotorState; }
  101. void SetTwistMotorState(EMotorState inState);
  102. EMotorState GetTwistMotorState() const { return mTwistMotorState; }
  103. /// Set the target angular velocity of body 2 in constraint space of body 2
  104. void SetTargetAngularVelocityCS(Vec3Arg inAngularVelocity) { mTargetAngularVelocity = inAngularVelocity; }
  105. Vec3 GetTargetAngularVelocityCS() const { return mTargetAngularVelocity; }
  106. /// Set the target orientation in constraint space (drives constraint to: GetRotationInConstraintSpace() == inOrientation)
  107. void SetTargetOrientationCS(QuatArg inOrientation);
  108. Quat GetTargetOrientationCS() const { return mTargetOrientation; }
  109. /// Set the target orientation in body space (R2 = R1 * inOrientation, where R1 and R2 are the world space rotations for body 1 and 2).
  110. /// Solve: R2 * ConstraintToBody2 = R1 * ConstraintToBody1 * q (see SwingTwistConstraint::GetSwingTwist) and R2 = R1 * inOrientation for q.
  111. void SetTargetOrientationBS(QuatArg inOrientation) { SetTargetOrientationCS(mConstraintToBody1.Conjugated() * inOrientation * mConstraintToBody2); }
  112. /// Get current rotation of constraint in constraint space.
  113. /// Solve: R2 * ConstraintToBody2 = R1 * ConstraintToBody1 * q for q.
  114. Quat GetRotationInConstraintSpace() const;
  115. ///@name Get Lagrange multiplier from last physics update (the linear/angular impulse applied to satisfy the constraint)
  116. inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }
  117. inline float GetTotalLambdaTwist() const { return mSwingTwistConstraintPart.GetTotalTwistLambda(); }
  118. inline float GetTotalLambdaSwingY() const { return mSwingTwistConstraintPart.GetTotalSwingYLambda(); }
  119. inline float GetTotalLambdaSwingZ() const { return mSwingTwistConstraintPart.GetTotalSwingZLambda(); }
  120. inline Vec3 GetTotalLambdaMotor() const { return Vec3(mMotorConstraintPart[0].GetTotalLambda(), mMotorConstraintPart[1].GetTotalLambda(), mMotorConstraintPart[2].GetTotalLambda()); }
  121. private:
  122. // Update the limits in the swing twist constraint part
  123. void UpdateLimits();
  124. // CONFIGURATION PROPERTIES FOLLOW
  125. // Local space constraint positions
  126. Vec3 mLocalSpacePosition1;
  127. Vec3 mLocalSpacePosition2;
  128. // Transforms from constraint space to body space
  129. Quat mConstraintToBody1;
  130. Quat mConstraintToBody2;
  131. // Limits
  132. float mNormalHalfConeAngle;
  133. float mPlaneHalfConeAngle;
  134. float mTwistMinAngle;
  135. float mTwistMaxAngle;
  136. // Friction
  137. float mMaxFrictionTorque;
  138. // Motor controls
  139. MotorSettings mSwingMotorSettings;
  140. MotorSettings mTwistMotorSettings;
  141. EMotorState mSwingMotorState = EMotorState::Off;
  142. EMotorState mTwistMotorState = EMotorState::Off;
  143. Vec3 mTargetAngularVelocity = Vec3::sZero();
  144. Quat mTargetOrientation = Quat::sIdentity();
  145. // RUN TIME PROPERTIES FOLLOW
  146. // Rotation axis for motor constraint parts
  147. Vec3 mWorldSpaceMotorAxis[3];
  148. // The constraint parts
  149. PointConstraintPart mPointConstraintPart;
  150. SwingTwistConstraintPart mSwingTwistConstraintPart;
  151. AngleConstraintPart mMotorConstraintPart[3];
  152. };
  153. JPH_NAMESPACE_END