TwoBodyConstraint.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Physics/Constraints/Constraint.h>
  5. #include <Jolt/Physics/Body/Body.h>
  6. JPH_NAMESPACE_BEGIN
  7. class TwoBodyConstraint;
  8. /// Base class for settings for all constraints that involve 2 bodies
  9. class TwoBodyConstraintSettings : public ConstraintSettings
  10. {
  11. public:
  12. JPH_DECLARE_SERIALIZABLE_ABSTRACT(TwoBodyConstraintSettings)
  13. /// Create an an instance of this constraint
  14. /// You can use Body::sFixedToWorld for inBody1 if you want to attach inBody2 to the world
  15. virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const = 0;
  16. };
  17. /// Base class for all constraints that involve 2 bodies. Body1 is usually considered the parent, Body2 the child.
  18. class TwoBodyConstraint : public Constraint
  19. {
  20. public:
  21. JPH_OVERRIDE_NEW_DELETE
  22. /// Constructor
  23. TwoBodyConstraint(Body &inBody1, Body &inBody2, const TwoBodyConstraintSettings &inSettings) : Constraint(inSettings), mBody1(&inBody1), mBody2(&inBody2) { }
  24. /// Get the type of a constraint
  25. virtual EConstraintType GetType() const override { return EConstraintType::TwoBodyConstraint; }
  26. /// Solver interface
  27. virtual bool IsActive() const override { return Constraint::IsActive() && (mBody1->IsActive() || mBody2->IsActive()) && (mBody2->IsDynamic() || mBody1->IsDynamic()); }
  28. #ifdef JPH_DEBUG_RENDERER
  29. virtual void DrawConstraintReferenceFrame(DebugRenderer *inRenderer) const override;
  30. #endif // JPH_DEBUG_RENDERER
  31. /// Access to the connected bodies
  32. Body * GetBody1() const { return mBody1; }
  33. Body * GetBody2() const { return mBody2; }
  34. /// Calculates the transform that transforms from constraint space to body 1 space. The first column of the matrix is the primary constraint axis (e.g. the hinge axis / slider direction), second column the secondary etc.
  35. virtual Mat44 GetConstraintToBody1Matrix() const = 0;
  36. /// Calculates the transform that transforms from constraint space to body 2 space. The first column of the matrix is the primary constraint axis (e.g. the hinge axis / slider direction), second column the secondary etc.
  37. virtual Mat44 GetConstraintToBody2Matrix() const = 0;
  38. /// Link bodies that are connected by this constraint in the island builder
  39. virtual void BuildIslands(uint32 inConstraintIndex, IslandBuilder &ioBuilder, BodyManager &inBodyManager) override;
  40. protected:
  41. /// The two bodies involved
  42. Body * mBody1;
  43. Body * mBody2;
  44. };
  45. JPH_NAMESPACE_END