ConeConstraint.h 4.9 KB

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