PhysicsGenericConstraint.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "Base.h"
  2. #include "PhysicsGenericConstraint.h"
  3. #include "Node.h"
  4. #include "PhysicsRigidBody.h"
  5. namespace gameplay
  6. {
  7. PhysicsGenericConstraint::PhysicsGenericConstraint()
  8. : PhysicsConstraint(NULL, NULL), _rotationOffsetA(NULL), _rotationOffsetB(NULL),
  9. _translationOffsetA(NULL), _translationOffsetB(NULL)
  10. {
  11. // Not used.
  12. }
  13. PhysicsGenericConstraint::PhysicsGenericConstraint(PhysicsRigidBody* a, PhysicsRigidBody* b)
  14. : PhysicsConstraint(a, b), _rotationOffsetA(NULL), _rotationOffsetB(NULL),
  15. _translationOffsetA(NULL), _translationOffsetB(NULL)
  16. {
  17. GP_ASSERT(a && a->_body && a->getNode());
  18. if (b)
  19. {
  20. GP_ASSERT(b->_body && b->getNode());
  21. Vector3 origin = centerOfMassMidpoint(a->getNode(), b->getNode());
  22. _constraint = bullet_new<btGeneric6DofConstraint>(*a->_body, *b->_body, getTransformOffset(a->getNode(), origin), getTransformOffset(b->getNode(), origin), true);
  23. }
  24. else
  25. {
  26. _constraint = bullet_new<btGeneric6DofConstraint>(*a->_body, btTransform::getIdentity(), true);
  27. }
  28. }
  29. PhysicsGenericConstraint::PhysicsGenericConstraint(PhysicsRigidBody* a, const Quaternion& rotationOffsetA, const Vector3& translationOffsetA,
  30. PhysicsRigidBody* b, const Quaternion& rotationOffsetB, const Vector3& translationOffsetB)
  31. : PhysicsConstraint(a, b), _rotationOffsetA(NULL), _rotationOffsetB(NULL), _translationOffsetA(NULL), _translationOffsetB(NULL)
  32. {
  33. GP_ASSERT(a && a->_body && a->getNode());
  34. // Take scale into account for the first node's translation offset.
  35. Vector3 sA;
  36. a->getNode()->getWorldMatrix().getScale(&sA);
  37. Vector3 tA(translationOffsetA.x * sA.x, translationOffsetA.y * sA.y, translationOffsetA.z * sA.z);
  38. if (b)
  39. {
  40. GP_ASSERT(b->_body && b->getNode());
  41. // Take scale into account for the second node's translation offset.
  42. Vector3 sB;
  43. b->getNode()->getWorldMatrix().getScale(&sB);
  44. Vector3 tB(translationOffsetB.x * sB.x, translationOffsetB.y * sB.y, translationOffsetB.z * sB.z);
  45. btTransform frameInA(BQ(rotationOffsetA), BV(tA));
  46. btTransform frameInB(BQ(rotationOffsetB), BV(tB));
  47. _constraint = bullet_new<btGeneric6DofConstraint>(*a->_body, *b->_body, frameInA, frameInB, true);
  48. }
  49. else
  50. {
  51. btTransform frameInA(BQ(rotationOffsetA), BV(tA));
  52. _constraint = bullet_new<btGeneric6DofConstraint>(*a->_body, frameInA, true);
  53. }
  54. }
  55. PhysicsGenericConstraint::~PhysicsGenericConstraint()
  56. {
  57. SAFE_DELETE(_rotationOffsetA);
  58. SAFE_DELETE(_rotationOffsetB);
  59. SAFE_DELETE(_translationOffsetA);
  60. SAFE_DELETE(_translationOffsetB);
  61. }
  62. }