PhysicsGenericConstraint.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "PhysicsGenericConstraint.h"
  2. #include "Node.h"
  3. #include "PhysicsMotionState.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. if (b)
  18. {
  19. Vector3 origin = centerOfMassMidpoint(a->getNode(), b->getNode());
  20. _constraint = new btGeneric6DofConstraint(*a->_body, *b->_body, getTransformOffset(a->getNode(), origin), getTransformOffset(b->getNode(), origin), true);
  21. }
  22. else
  23. {
  24. _constraint = new btGeneric6DofConstraint(*a->_body, btTransform::getIdentity(), true);
  25. }
  26. }
  27. PhysicsGenericConstraint::PhysicsGenericConstraint(PhysicsRigidBody* a, const Quaternion& rotationOffsetA, const Vector3& translationOffsetA,
  28. PhysicsRigidBody* b, const Quaternion& rotationOffsetB, const Vector3& translationOffsetB)
  29. : PhysicsConstraint(a, b), _rotationOffsetA(NULL), _rotationOffsetB(NULL), _translationOffsetA(NULL), _translationOffsetB(NULL)
  30. {
  31. // Take scale into account for the first node's translation offset.
  32. Vector3 sA;
  33. a->getNode()->getWorldMatrix().getScale(&sA);
  34. Vector3 tA(translationOffsetA.x * sA.x, translationOffsetA.y * sA.y, translationOffsetA.z * sA.z);
  35. if (b)
  36. {
  37. // Take scale into account for the second node's translation offset.
  38. Vector3 sB;
  39. b->getNode()->getWorldMatrix().getScale(&sB);
  40. Vector3 tB(translationOffsetB.x * sB.x, translationOffsetB.y * sB.y, translationOffsetB.z * sB.z);
  41. btTransform frameInA(btQuaternion(rotationOffsetA.x, rotationOffsetA.y, rotationOffsetA.z, rotationOffsetA.w), btVector3(tA.x, tA.y, tA.z));
  42. btTransform frameInB(btQuaternion(rotationOffsetB.x, rotationOffsetB.y, rotationOffsetB.z, rotationOffsetB.w), btVector3(tB.x, tB.y, tB.z));
  43. _constraint = new btGeneric6DofConstraint(*a->_body, *b->_body, frameInA, frameInB, true);
  44. }
  45. else
  46. {
  47. btTransform frameInA(btQuaternion(rotationOffsetA.x, rotationOffsetA.y, rotationOffsetA.z, rotationOffsetA.w), btVector3(tA.x, tA.y, tA.z));
  48. _constraint = new btGeneric6DofConstraint(*a->_body, frameInA, true);
  49. }
  50. }
  51. PhysicsGenericConstraint::~PhysicsGenericConstraint()
  52. {
  53. SAFE_DELETE(_rotationOffsetA);
  54. SAFE_DELETE(_rotationOffsetB);
  55. SAFE_DELETE(_translationOffsetA);
  56. SAFE_DELETE(_translationOffsetB);
  57. }
  58. }