PointConstraint.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. namespace JPH {
  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. /// Construct point constraint
  32. PointConstraint(Body &inBody1, Body &inBody2, const PointConstraintSettings &inSettings);
  33. // Generic interface of a constraint
  34. virtual EConstraintType GetType() const override { return EConstraintType::Point; }
  35. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  36. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  37. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  38. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  39. #ifdef JPH_DEBUG_RENDERER
  40. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  41. #endif // JPH_DEBUG_RENDERER
  42. virtual void SaveState(StateRecorder &inStream) const override;
  43. virtual void RestoreState(StateRecorder &inStream) override;
  44. // See: TwoBodyConstraint
  45. virtual Mat44 GetConstraintToBody1Matrix() const override { return Mat44::sTranslation(mLocalSpacePosition1); }
  46. 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.
  47. private:
  48. // Internal helper function to calculate the values below
  49. void CalculateConstraintProperties();
  50. // Local space constraint positions
  51. Vec3 mLocalSpacePosition1;
  52. Vec3 mLocalSpacePosition2;
  53. // The constraint part
  54. PointConstraintPart mPointConstraintPart;
  55. };
  56. } // JPH