PhysicsJoint.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Physics/PhysicsObject.h>
  7. #include <AnKi/Util/ClassWrapper.h>
  8. namespace anki
  9. {
  10. /// @addtogroup physics
  11. /// @{
  12. /// Joint base class. Joints connect two (or a single one) rigid bodies together.
  13. class PhysicsJoint : public PhysicsObject
  14. {
  15. ANKI_PHYSICS_OBJECT(PhysicsObjectType::JOINT)
  16. public:
  17. /// Set the breaking impulse.
  18. void setBreakingImpulseThreshold(F32 impulse)
  19. {
  20. getJoint()->setBreakingImpulseThreshold(impulse);
  21. }
  22. /// Return true if the joint broke.
  23. Bool isBroken() const
  24. {
  25. return !getJoint()->isEnabled();
  26. }
  27. /// Break the joint.
  28. void brake()
  29. {
  30. getJoint()->setEnabled(false);
  31. }
  32. protected:
  33. union
  34. {
  35. ClassWrapper<btPoint2PointConstraint> m_p2p;
  36. ClassWrapper<btHingeConstraint> m_hinge;
  37. };
  38. PhysicsBodyPtr m_bodyA;
  39. PhysicsBodyPtr m_bodyB;
  40. enum class JointType : U8
  41. {
  42. P2P,
  43. HINGE,
  44. };
  45. JointType m_type;
  46. PhysicsJoint(PhysicsWorld* world, JointType type);
  47. void registerToWorld() override;
  48. void unregisterFromWorld() override;
  49. const btTypedConstraint* getJoint() const
  50. {
  51. return getJointInternal();
  52. }
  53. btTypedConstraint* getJoint()
  54. {
  55. return const_cast<btTypedConstraint*>(getJointInternal());
  56. }
  57. const btTypedConstraint* getJointInternal() const
  58. {
  59. switch(m_type)
  60. {
  61. case JointType::P2P:
  62. return m_p2p.get();
  63. case JointType::HINGE:
  64. return m_hinge.get();
  65. default:
  66. ANKI_ASSERT(0);
  67. return nullptr;
  68. }
  69. }
  70. };
  71. /// Point to point joint.
  72. class PhysicsPoint2PointJoint : public PhysicsJoint
  73. {
  74. ANKI_PHYSICS_OBJECT(PhysicsObjectType::JOINT)
  75. private:
  76. PhysicsPoint2PointJoint(PhysicsWorld* world, PhysicsBodyPtr bodyA, const Vec3& relPos);
  77. PhysicsPoint2PointJoint(PhysicsWorld* world, PhysicsBodyPtr bodyA, const Vec3& relPosA, PhysicsBodyPtr bodyB,
  78. const Vec3& relPosB);
  79. ~PhysicsPoint2PointJoint();
  80. };
  81. /// Hinge joint.
  82. class PhysicsHingeJoint : public PhysicsJoint
  83. {
  84. ANKI_PHYSICS_OBJECT(PhysicsObjectType::JOINT)
  85. private:
  86. PhysicsHingeJoint(PhysicsWorld* world, PhysicsBodyPtr bodyA, const Vec3& relPos, const Vec3& axis);
  87. PhysicsHingeJoint(PhysicsWorld* world, PhysicsBodyPtr bodyA, const Vec3& relPosA, const Vec3& axisA,
  88. PhysicsBodyPtr bodyB, const Vec3& relPosB, const Vec3& axisB);
  89. ~PhysicsHingeJoint();
  90. };
  91. /// @}
  92. } // end namespace anki