PhysicsSpringConstraint.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "Base.h"
  2. #include "PhysicsSpringConstraint.h"
  3. #include "Node.h"
  4. #include "PhysicsRigidBody.h"
  5. namespace gameplay
  6. {
  7. PhysicsSpringConstraint::PhysicsSpringConstraint(PhysicsRigidBody* a, PhysicsRigidBody* b)
  8. {
  9. GP_ASSERT(a && a->_body);
  10. GP_ASSERT(b && b->_body);
  11. // Initialize the physics rigid body references since we don't call the PhysicsConstraint constructor that does it properly automatically.
  12. _a = a;
  13. _b = b;
  14. Vector3 origin = centerOfMassMidpoint(a->getNode(), b->getNode());
  15. _constraint = bullet_new<btGeneric6DofSpringConstraint>(*a->_body, *b->_body, getTransformOffset(a->getNode(), origin), getTransformOffset(b->getNode(), origin), true);
  16. }
  17. PhysicsSpringConstraint::PhysicsSpringConstraint(PhysicsRigidBody* a, const Quaternion& rotationOffsetA, const Vector3& translationOffsetA,
  18. PhysicsRigidBody* b, const Quaternion& rotationOffsetB, const Vector3& translationOffsetB)
  19. {
  20. GP_ASSERT(a && a->_body && a->getNode());
  21. GP_ASSERT(b && b->_body && b->getNode());
  22. // Initialize the physics rigid body references since we don't call the PhysicsConstraint constructor that does it properly automatically.
  23. _a = a;
  24. _b = b;
  25. // Take scale into account for the translation offsets.
  26. Vector3 sA;
  27. a->getNode()->getWorldMatrix().getScale(&sA);
  28. Vector3 tA(translationOffsetA.x * sA.x, translationOffsetA.y * sA.y, translationOffsetA.z * sA.z);
  29. Vector3 sB;
  30. b->getNode()->getWorldMatrix().getScale(&sB);
  31. Vector3 tB(translationOffsetB.x * sB.x, translationOffsetB.y * sB.y, translationOffsetB.z * sB.z);
  32. btTransform frameInA(BQ(rotationOffsetA), BV(tA));
  33. btTransform frameInB(BQ(rotationOffsetB), BV(tB));
  34. _constraint = bullet_new<btGeneric6DofSpringConstraint>(*a->_body, *b->_body, frameInA, frameInB, true);
  35. }
  36. PhysicsSpringConstraint::~PhysicsSpringConstraint()
  37. {
  38. // Used
  39. }
  40. void PhysicsSpringConstraint::setStrength(SpringProperty property, float strength)
  41. {
  42. GP_ASSERT(_constraint);
  43. if (strength < MATH_EPSILON)
  44. ((btGeneric6DofSpringConstraint*)_constraint)->enableSpring(property, false);
  45. else
  46. {
  47. ((btGeneric6DofSpringConstraint*)_constraint)->enableSpring(property, true);
  48. ((btGeneric6DofSpringConstraint*)_constraint)->setStiffness(property, strength);
  49. ((btGeneric6DofSpringConstraint*)_constraint)->setEquilibriumPoint(property);
  50. }
  51. }
  52. void PhysicsSpringConstraint::setDamping(SpringProperty property, float damping)
  53. {
  54. GP_ASSERT(_constraint);
  55. ((btGeneric6DofSpringConstraint*)_constraint)->setDamping(property, damping);
  56. ((btGeneric6DofSpringConstraint*)_constraint)->setEquilibriumPoint(property);
  57. }
  58. }