PointConstraint.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. JPH_NAMESPACE_BEGIN
  8. /// Point constraint settings, used to create a point constraint
  9. class JPH_EXPORT PointConstraintSettings final : public TwoBodyConstraintSettings
  10. {
  11. public:
  12. JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, PointConstraintSettings)
  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 position (space determined by mSpace).
  20. RVec3 mPoint1 = RVec3::sZero();
  21. /// Body 2 constraint position (space determined by mSpace).
  22. /// Note: Normally you would set mPoint1 = mPoint2 if the bodies are already placed how you want to constrain them (if mSpace = world space).
  23. RVec3 mPoint2 = RVec3::sZero();
  24. protected:
  25. // See: ConstraintSettings::RestoreBinaryState
  26. virtual void RestoreBinaryState(StreamIn &inStream) override;
  27. };
  28. /// A point constraint constrains 2 bodies on a single point (removing 3 degrees of freedom)
  29. class JPH_EXPORT PointConstraint final : public TwoBodyConstraint
  30. {
  31. public:
  32. JPH_OVERRIDE_NEW_DELETE
  33. /// Construct point constraint
  34. PointConstraint(Body &inBody1, Body &inBody2, const PointConstraintSettings &inSettings);
  35. // Generic interface of a constraint
  36. virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Point; }
  37. virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;
  38. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  39. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  40. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  41. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  42. #ifdef JPH_DEBUG_RENDERER
  43. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  44. #endif // JPH_DEBUG_RENDERER
  45. virtual void SaveState(StateRecorder &inStream) const override;
  46. virtual void RestoreState(StateRecorder &inStream) override;
  47. virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
  48. /// Update the attachment point for body 1
  49. void SetPoint1(EConstraintSpace inSpace, RVec3Arg inPoint1);
  50. /// Update the attachment point for body 2
  51. void SetPoint2(EConstraintSpace inSpace, RVec3Arg inPoint2);
  52. /// Get the attachment point for body 1 relative to body 1 COM
  53. inline Vec3 GetLocalSpacePoint1() const { return mLocalSpacePosition1; }
  54. /// Get the attachment point for body 2 relative to body 2 COM
  55. inline Vec3 GetLocalSpacePoint2() const { return mLocalSpacePosition2; }
  56. // See: TwoBodyConstraint
  57. virtual Mat44 GetConstraintToBody1Matrix() const override { return Mat44::sTranslation(mLocalSpacePosition1); }
  58. virtual Mat44 GetConstraintToBody2Matrix() const override { return Mat44::sTranslation(mLocalSpacePosition2); } // Note: Incorrect rotation as we don't track the original rotation difference, should not matter though as the constraint is not limiting rotation.
  59. ///@name Get Lagrange multiplier from last physics update (the linear impulse applied to satisfy the constraint)
  60. inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }
  61. private:
  62. // Internal helper function to calculate the values below
  63. void CalculateConstraintProperties();
  64. // Local space constraint positions
  65. Vec3 mLocalSpacePosition1;
  66. Vec3 mLocalSpacePosition2;
  67. // The constraint part
  68. PointConstraintPart mPointConstraintPart;
  69. };
  70. JPH_NAMESPACE_END