PhysicsJoint.h 2.4 KB

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