PhysicsGenericConstraint.cpp 2.8 KB

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