PointConstraint.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. JPH_NAMESPACE_BEGIN
  7. /// Point constraint settings, used to create a point constraint
  8. class PointConstraintSettings final : public TwoBodyConstraintSettings
  9. {
  10. public:
  11. JPH_DECLARE_SERIALIZABLE_VIRTUAL(PointConstraintSettings)
  12. // See: ConstraintSettings::SaveBinaryState
  13. virtual void SaveBinaryState(StreamOut &inStream) const override;
  14. /// Create an an instance of this constraint
  15. virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
  16. /// This determines in which space the constraint is setup, all properties below should be in the specified space
  17. EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
  18. /// Body 1 constraint position (space determined by mSpace).
  19. Vec3 mPoint1 = Vec3::sZero();
  20. /// Body 2 constraint position (space determined by mSpace).
  21. /// Note: Normally you would set mPoint1 = mPoint2 if the bodies are already placed how you want to constrain them (if mSpace = world space).
  22. Vec3 mPoint2 = Vec3::sZero();
  23. protected:
  24. // See: ConstraintSettings::RestoreBinaryState
  25. virtual void RestoreBinaryState(StreamIn &inStream) override;
  26. };
  27. /// A point constraint constrains 2 bodies on a single point (removing 3 degrees of freedom)
  28. class PointConstraint final : public TwoBodyConstraint
  29. {
  30. public:
  31. JPH_OVERRIDE_NEW_DELETE
  32. /// Construct point constraint
  33. PointConstraint(Body &inBody1, Body &inBody2, const PointConstraintSettings &inSettings);
  34. // Generic interface of a constraint
  35. virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Point; }
  36. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  37. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  38. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  39. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  40. #ifdef JPH_DEBUG_RENDERER
  41. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  42. #endif // JPH_DEBUG_RENDERER
  43. virtual void SaveState(StateRecorder &inStream) const override;
  44. virtual void RestoreState(StateRecorder &inStream) override;
  45. virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
  46. /// Update the attachment point for body 1
  47. void SetPoint1(EConstraintSpace inSpace, Vec3Arg inPoint1);
  48. /// Update the attachment point for body 2
  49. void SetPoint2(EConstraintSpace inSpace, Vec3Arg inPoint2);
  50. /// Get the attachment point for body 1 relative to body 1 COM
  51. inline Vec3 GetLocalSpacePoint1() const { return mLocalSpacePosition1; }
  52. /// Get the attachment point for body 2 relative to body 2 COM
  53. inline Vec3 GetLocalSpacePoint2() const { return mLocalSpacePosition2; }
  54. // See: TwoBodyConstraint
  55. virtual Mat44 GetConstraintToBody1Matrix() const override { return Mat44::sTranslation(mLocalSpacePosition1); }
  56. 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.
  57. ///@name Get Lagrange multiplier from last physics update (relates to how much force/torque was applied to satisfy the constraint)
  58. inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }
  59. private:
  60. // Internal helper function to calculate the values below
  61. void CalculateConstraintProperties();
  62. // Local space constraint positions
  63. Vec3 mLocalSpacePosition1;
  64. Vec3 mLocalSpacePosition2;
  65. // The constraint part
  66. PointConstraintPart mPointConstraintPart;
  67. };
  68. JPH_NAMESPACE_END