ConeConstraint.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/ConstraintPart/PointConstraintPart.h>
  6. #include <Jolt/Physics/Constraints/ConstraintPart/AngleConstraintPart.h>
  7. JPH_NAMESPACE_BEGIN
  8. /// Cone constraint settings, used to create a cone constraint
  9. class ConeConstraintSettings final : public TwoBodyConstraintSettings
  10. {
  11. public:
  12. JPH_DECLARE_SERIALIZABLE_VIRTUAL(ConeConstraintSettings)
  13. // See: ConstraintSettings::SaveBinaryState
  14. virtual void SaveBinaryState(StreamOut &inStream) const override;
  15. /// Create an an instance of this constraint
  16. virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
  17. /// This determines in which space the constraint is setup, all properties below should be in the specified space
  18. EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
  19. /// Body 1 constraint reference frame (space determined by mSpace)
  20. Vec3 mPoint1 = Vec3::sZero();
  21. Vec3 mTwistAxis1 = Vec3::sAxisX();
  22. /// Body 2 constraint reference frame (space determined by mSpace)
  23. Vec3 mPoint2 = Vec3::sZero();
  24. Vec3 mTwistAxis2 = Vec3::sAxisX();
  25. /// Half of maximum angle between twist axis of body 1 and 2
  26. float mHalfConeAngle = 0.0f;
  27. protected:
  28. // See: ConstraintSettings::RestoreBinaryState
  29. virtual void RestoreBinaryState(StreamIn &inStream) override;
  30. };
  31. /// A cone constraint constraints 2 bodies to a single point and limits the swing between the twist axis within a cone:
  32. ///
  33. /// t1 . t2 <= cos(theta)
  34. ///
  35. /// Where:
  36. ///
  37. /// t1 = twist axis of body 1.
  38. /// t2 = twist axis of body 2.
  39. /// theta = half cone angle (angle from the principal axis of the cone to the edge).
  40. ///
  41. /// Calculating the Jacobian:
  42. ///
  43. /// Constraint equation:
  44. ///
  45. /// C = t1 . t2 - cos(theta)
  46. ///
  47. /// Derivative:
  48. ///
  49. /// d/dt C = d/dt (t1 . t2) = (d/dt t1) . t2 + t1 . (d/dt t2) = (w1 x t1) . t2 + t1 . (w2 x t2) = (t1 x t2) . w1 + (t2 x t1) . w2
  50. ///
  51. /// d/dt C = J v = [0, -t2 x t1, 0, t2 x t1] [v1, w1, v2, w2]
  52. ///
  53. /// Where J is the Jacobian.
  54. ///
  55. /// Note that this is the exact same equation as used in AngleConstraintPart if we use t2 x t1 as the world space axis
  56. class ConeConstraint final : public TwoBodyConstraint
  57. {
  58. public:
  59. JPH_OVERRIDE_NEW_DELETE
  60. /// Construct cone constraint
  61. ConeConstraint(Body &inBody1, Body &inBody2, const ConeConstraintSettings &inSettings);
  62. // Generic interface of a constraint
  63. virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Cone; }
  64. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  65. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  66. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  67. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  68. #ifdef JPH_DEBUG_RENDERER
  69. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  70. virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;
  71. #endif // JPH_DEBUG_RENDERER
  72. virtual void SaveState(StateRecorder &inStream) const override;
  73. virtual void RestoreState(StateRecorder &inStream) override;
  74. virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
  75. // See: TwoBodyConstraint
  76. virtual Mat44 GetConstraintToBody1Matrix() const override;
  77. virtual Mat44 GetConstraintToBody2Matrix() const override;
  78. /// Update maximum angle between body 1 and 2 (see ConeConstraintSettings)
  79. void SetHalfConeAngle(float inHalfConeAngle) { JPH_ASSERT(inHalfConeAngle >= 0.0f && inHalfConeAngle <= JPH_PI); mCosHalfConeAngle = Cos(inHalfConeAngle); }
  80. float GetCosHalfConeAngle() const { return mCosHalfConeAngle; }
  81. ///@name Get Lagrange multiplier from last physics update (relates to how much force/torque was applied to satisfy the constraint)
  82. inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }
  83. inline float GetTotalLambdaRotation() const { return mAngleConstraintPart.GetTotalLambda(); }
  84. private:
  85. // Internal helper function to calculate the values below
  86. void CalculateRotationConstraintProperties(float inDeltaTime, Mat44Arg inRotation1, Mat44Arg inRotation2);
  87. // CONFIGURATION PROPERTIES FOLLOW
  88. // Local space constraint positions
  89. Vec3 mLocalSpacePosition1;
  90. Vec3 mLocalSpacePosition2;
  91. // Local space constraint axis
  92. Vec3 mLocalSpaceTwistAxis1;
  93. Vec3 mLocalSpaceTwistAxis2;
  94. // Angular limits
  95. float mCosHalfConeAngle;
  96. // RUN TIME PROPERTIES FOLLOW
  97. // Axis and angle of rotation between the two bodies
  98. Vec3 mWorldSpaceRotationAxis;
  99. float mCosTheta;
  100. // The constraint parts
  101. PointConstraintPart mPointConstraintPart;
  102. AngleConstraintPart mAngleConstraintPart;
  103. };
  104. JPH_NAMESPACE_END