TwoBodyConstraint.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. /// Constructor
  22. TwoBodyConstraint(Body &inBody1, Body &inBody2, const TwoBodyConstraintSettings &inSettings) : Constraint(inSettings), mBody1(&inBody1), mBody2(&inBody2) { }
  23. /// Get the type of a constraint
  24. virtual EConstraintType GetType() const override { return EConstraintType::TwoBodyConstraint; }
  25. /// Solver interface
  26. virtual bool IsActive() const override { return Constraint::IsActive() && (mBody1->IsActive() || mBody2->IsActive()) && (mBody2->IsDynamic() || mBody1->IsDynamic()); }
  27. #ifdef JPH_DEBUG_RENDERER
  28. virtual void DrawConstraintReferenceFrame(DebugRenderer *inRenderer) const override;
  29. #endif // JPH_DEBUG_RENDERER
  30. /// Access to the connected bodies
  31. Body * GetBody1() const { return mBody1; }
  32. Body * GetBody2() const { return mBody2; }
  33. /// 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.
  34. virtual Mat44 GetConstraintToBody1Matrix() const = 0;
  35. /// 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.
  36. virtual Mat44 GetConstraintToBody2Matrix() const = 0;
  37. /// Link bodies that are connected by this constraint in the island builder
  38. virtual void BuildIslands(uint32 inConstraintIndex, IslandBuilder &ioBuilder, BodyManager &inBodyManager) override;
  39. protected:
  40. /// The two bodies involved
  41. Body * mBody1;
  42. Body * mBody2;
  43. };
  44. JPH_NAMESPACE_END