ConeConstraint.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Physics/Constraints/TwoBodyConstraint.h>
  5. #include <Physics/Constraints/ConstraintPart/PointConstraintPart.h>
  6. #include <Physics/Constraints/ConstraintPart/AngleConstraintPart.h>
  7. namespace JPH {
  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. /// Body 1 constraint reference frame (in world space)
  18. Vec3 mPoint1 = Vec3::sZero();
  19. Vec3 mTwistAxis1 = Vec3::sAxisX();
  20. /// Body 2 constraint reference frame (in world space)
  21. Vec3 mPoint2 = Vec3::sZero();
  22. Vec3 mTwistAxis2 = Vec3::sAxisX();
  23. /// Half of maximum angle between twist axis of body 1 and 2
  24. float mHalfConeAngle = 0.0f;
  25. protected:
  26. // See: ConstraintSettings::RestoreBinaryState
  27. virtual void RestoreBinaryState(StreamIn &inStream) override;
  28. };
  29. /// A cone constraint constraints 2 bodies to a single point and limits the swing between the twist axis within a cone:
  30. ///
  31. /// t1 . t2 <= cos(theta)
  32. ///
  33. /// Where:
  34. ///
  35. /// t1 = twist axis of body 1.
  36. /// t2 = twist axis of body 2.
  37. /// theta = half cone angle (angle from the principal axis of the cone to the edge).
  38. ///
  39. /// Calculating the Jacobian:
  40. ///
  41. /// Constraint equation:
  42. ///
  43. /// C = t1 . t2 - cos(theta)
  44. ///
  45. /// Derivative:
  46. ///
  47. /// 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
  48. ///
  49. /// d/dt C = J v = [0, -t2 x t1, 0, t2 x t1] [v1, w1, v2, w2]
  50. ///
  51. /// Where J is the Jacobian.
  52. ///
  53. /// Note that this is the exact same equation as used in AngleConstraintPart if we use t2 x t1 as the world space axis
  54. class ConeConstraint final : public TwoBodyConstraint
  55. {
  56. public:
  57. /// Construct cone constraint
  58. ConeConstraint(Body &inBody1, Body &inBody2, const ConeConstraintSettings &inSettings);
  59. // Generic interface of a constraint
  60. virtual EConstraintType GetType() const override { return EConstraintType::Cone; }
  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. // See: TwoBodyConstraint
  72. virtual Mat44 GetConstraintToBody1Matrix() const override;
  73. virtual Mat44 GetConstraintToBody2Matrix() const override;
  74. /// Update maximum angle between body 1 and 2 (see ConeConstraintSettings)
  75. void SetHalfConeAngle(float inHalfConeAngle) { JPH_ASSERT(inHalfConeAngle >= 0.0f && inHalfConeAngle <= JPH_PI); mCosHalfConeAngle = cos(inHalfConeAngle); }
  76. float GetCosHalfConeAngle() const { return mCosHalfConeAngle; }
  77. ///@name Get Lagrange multiplier from last physics update (relates to how much force/torque was applied to satisfy the constraint)
  78. inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }
  79. inline float GetTotalLambdaRotation() const { return mAngleConstraintPart.GetTotalLambda(); }
  80. private:
  81. // Internal helper function to calculate the values below
  82. void CalculateRotationConstraintProperties(float inDeltaTime, Mat44Arg inRotation1, Mat44Arg inRotation2);
  83. // CONFIGURATION PROPERTIES FOLLOW
  84. // Local space constraint positions
  85. Vec3 mLocalSpacePosition1;
  86. Vec3 mLocalSpacePosition2;
  87. // Local space constraint axis
  88. Vec3 mLocalSpaceTwistAxis1;
  89. Vec3 mLocalSpaceTwistAxis2;
  90. // Angular limits
  91. float mCosHalfConeAngle;
  92. // RUN TIME PROPERTIES FOLLOW
  93. // Axis and angle of rotation between the two bodies
  94. Vec3 mWorldSpaceRotationAxis;
  95. float mCosTheta;
  96. // The constraint parts
  97. PointConstraintPart mPointConstraintPart;
  98. AngleConstraintPart mAngleConstraintPart;
  99. };
  100. } // JPH