PhysicsJoint.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (C) 2009-2020, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <anki/physics/PhysicsJoint.h>
  6. #include <anki/physics/PhysicsBody.h>
  7. #include <anki/physics/PhysicsWorld.h>
  8. namespace anki
  9. {
  10. PhysicsJoint::PhysicsJoint(PhysicsWorld* world, JointType type)
  11. : PhysicsObject(CLASS_TYPE, world)
  12. , m_type(type)
  13. {
  14. }
  15. void PhysicsJoint::addToWorld()
  16. {
  17. getJoint()->setUserConstraintPtr(static_cast<PhysicsObject*>(this));
  18. auto lock = getWorld().lockBtWorld();
  19. getWorld().getBtWorld()->addConstraint(getJoint());
  20. }
  21. void PhysicsJoint::removeFromWorld()
  22. {
  23. auto lock = getWorld().lockBtWorld();
  24. getWorld().getBtWorld()->removeConstraint(getJoint());
  25. }
  26. PhysicsPoint2PointJoint::PhysicsPoint2PointJoint(PhysicsWorld* world, PhysicsBodyPtr bodyA, const Vec3& relPos)
  27. : PhysicsJoint(world, JointType::P2P)
  28. {
  29. m_bodyA = bodyA;
  30. m_p2p.init(*m_bodyA->getBtBody(), toBt(relPos));
  31. addToWorld();
  32. }
  33. PhysicsPoint2PointJoint::PhysicsPoint2PointJoint(PhysicsWorld* world, PhysicsBodyPtr bodyA, const Vec3& relPosA,
  34. PhysicsBodyPtr bodyB, const Vec3& relPosB)
  35. : PhysicsJoint(world, JointType::P2P)
  36. {
  37. ANKI_ASSERT(bodyA != bodyB);
  38. m_bodyA = bodyA;
  39. m_bodyB = bodyB;
  40. m_p2p.init(*m_bodyA->getBtBody(), *m_bodyB->getBtBody(), toBt(relPosA), toBt(relPosB));
  41. addToWorld();
  42. }
  43. PhysicsPoint2PointJoint::~PhysicsPoint2PointJoint()
  44. {
  45. removeFromWorld();
  46. m_p2p.destroy();
  47. }
  48. PhysicsHingeJoint::PhysicsHingeJoint(PhysicsWorld* world, PhysicsBodyPtr bodyA, const Vec3& relPos, const Vec3& axis)
  49. : PhysicsJoint(world, JointType::HINGE)
  50. {
  51. m_bodyA = bodyA;
  52. m_hinge.init(*m_bodyA->getBtBody(), toBt(relPos), toBt(axis));
  53. addToWorld();
  54. }
  55. PhysicsHingeJoint::~PhysicsHingeJoint()
  56. {
  57. removeFromWorld();
  58. m_hinge.destroy();
  59. }
  60. } // end namespace anki