PhysicsGenericConstraint.cpp 2.7 KB

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