PhysicsJoint.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (C) 2009-present, 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. PhysicsJoint::PhysicsJoint(JointType type)
  10. : PhysicsObject(kClassType)
  11. , m_type(type)
  12. {
  13. }
  14. void PhysicsJoint::registerToWorld()
  15. {
  16. PhysicsWorld::getSingleton().getBtWorld().addConstraint(getJoint());
  17. }
  18. void PhysicsJoint::unregisterFromWorld()
  19. {
  20. PhysicsWorld::getSingleton().getBtWorld().removeConstraint(getJoint());
  21. }
  22. PhysicsPoint2PointJoint::PhysicsPoint2PointJoint(PhysicsBodyPtr bodyA, const Vec3& relPos)
  23. : PhysicsJoint(JointType::kP2P)
  24. {
  25. m_bodyA = std::move(bodyA);
  26. m_p2p.construct(*m_bodyA->getBtBody(), toBt(relPos));
  27. getJoint()->setUserConstraintPtr(static_cast<PhysicsObject*>(this));
  28. }
  29. PhysicsPoint2PointJoint::PhysicsPoint2PointJoint(PhysicsBodyPtr bodyA, const Vec3& relPosA, PhysicsBodyPtr bodyB, const Vec3& relPosB)
  30. : PhysicsJoint(JointType::kP2P)
  31. {
  32. ANKI_ASSERT(bodyA != bodyB);
  33. m_bodyA = std::move(bodyA);
  34. m_bodyB = std::move(bodyB);
  35. m_p2p.construct(*m_bodyA->getBtBody(), *m_bodyB->getBtBody(), toBt(relPosA), toBt(relPosB));
  36. getJoint()->setUserConstraintPtr(static_cast<PhysicsObject*>(this));
  37. }
  38. PhysicsPoint2PointJoint::~PhysicsPoint2PointJoint()
  39. {
  40. m_p2p.destroy();
  41. }
  42. PhysicsHingeJoint::PhysicsHingeJoint(PhysicsBodyPtr bodyA, const Vec3& relPos, const Vec3& axis)
  43. : PhysicsJoint(JointType::kHinge)
  44. {
  45. m_bodyA = std::move(bodyA);
  46. m_hinge.construct(*m_bodyA->getBtBody(), toBt(relPos), toBt(axis));
  47. getJoint()->setUserConstraintPtr(static_cast<PhysicsObject*>(this));
  48. }
  49. PhysicsHingeJoint::PhysicsHingeJoint(PhysicsBodyPtr bodyA, const Vec3& relPosA, const Vec3& axisA, PhysicsBodyPtr bodyB, const Vec3& relPosB,
  50. const Vec3& axisB)
  51. : PhysicsJoint(JointType::kHinge)
  52. {
  53. m_bodyA = std::move(bodyA);
  54. m_bodyB = std::move(bodyB);
  55. m_hinge.construct(*m_bodyA->getBtBody(), *m_bodyB->getBtBody(), toBt(relPosA), toBt(relPosB), toBt(axisA), toBt(axisB));
  56. getJoint()->setUserConstraintPtr(static_cast<PhysicsObject*>(this));
  57. }
  58. PhysicsHingeJoint::~PhysicsHingeJoint()
  59. {
  60. m_hinge.destroy();
  61. }
  62. } // end namespace anki