TwoBodyConstraint.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/Constraint.h>
  6. #include <Jolt/Physics/Body/Body.h>
  7. JPH_NAMESPACE_BEGIN
  8. class TwoBodyConstraint;
  9. /// Base class for settings for all constraints that involve 2 bodies
  10. class TwoBodyConstraintSettings : public ConstraintSettings
  11. {
  12. public:
  13. JPH_DECLARE_SERIALIZABLE_ABSTRACT(TwoBodyConstraintSettings)
  14. /// Create an an instance of this constraint
  15. /// You can use Body::sFixedToWorld for inBody1 if you want to attach inBody2 to the world
  16. virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const = 0;
  17. };
  18. /// Base class for all constraints that involve 2 bodies. Body1 is usually considered the parent, Body2 the child.
  19. class TwoBodyConstraint : public Constraint
  20. {
  21. public:
  22. JPH_OVERRIDE_NEW_DELETE
  23. /// Constructor
  24. TwoBodyConstraint(Body &inBody1, Body &inBody2, const TwoBodyConstraintSettings &inSettings) : Constraint(inSettings), mBody1(&inBody1), mBody2(&inBody2) { }
  25. /// Get the type of a constraint
  26. virtual EConstraintType GetType() const override { return EConstraintType::TwoBodyConstraint; }
  27. /// Solver interface
  28. virtual bool IsActive() const override { return Constraint::IsActive() && (mBody1->IsActive() || mBody2->IsActive()) && (mBody2->IsDynamic() || mBody1->IsDynamic()); }
  29. #ifdef JPH_DEBUG_RENDERER
  30. virtual void DrawConstraintReferenceFrame(DebugRenderer *inRenderer) const override;
  31. #endif // JPH_DEBUG_RENDERER
  32. /// Access to the connected bodies
  33. Body * GetBody1() const { return mBody1; }
  34. Body * GetBody2() const { return mBody2; }
  35. /// 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.
  36. virtual Mat44 GetConstraintToBody1Matrix() const = 0;
  37. /// 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.
  38. virtual Mat44 GetConstraintToBody2Matrix() const = 0;
  39. /// Link bodies that are connected by this constraint in the island builder
  40. virtual void BuildIslands(uint32 inConstraintIndex, IslandBuilder &ioBuilder, BodyManager &inBodyManager) override;
  41. /// Link bodies that are connected by this constraint in the same split. Returns the split index.
  42. virtual uint BuildIslandSplits(LargeIslandSplitter &ioSplitter) const override;
  43. protected:
  44. /// The two bodies involved
  45. Body * mBody1;
  46. Body * mBody2;
  47. };
  48. JPH_NAMESPACE_END