PhysicsGenericConstraint.cpp 2.5 KB

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