PhysicsGhostObject.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "Base.h"
  2. #include "PhysicsGhostObject.h"
  3. #include "Node.h"
  4. #include "Game.h"
  5. namespace gameplay
  6. {
  7. PhysicsGhostObject::PhysicsGhostObject(Node* node, const PhysicsCollisionShape::Definition& shape)
  8. : PhysicsCollisionObject(node), _ghostObject(NULL)
  9. {
  10. Vector3 centerOfMassOffset;
  11. PhysicsController* physicsController = Game::getInstance()->getPhysicsController();
  12. // Create and set the collision shape for the ghost object.
  13. _collisionShape = physicsController->createShape(node, shape, &centerOfMassOffset);
  14. // Create the ghost object.
  15. _ghostObject = bullet_new<btPairCachingGhostObject>();
  16. _ghostObject->setCollisionShape(_collisionShape->getShape());
  17. _ghostObject->setCollisionFlags(_ghostObject->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE);
  18. // Initialize a physics motion state object for syncing the transform.
  19. _motionState = new PhysicsMotionState(_node, &centerOfMassOffset);
  20. _motionState->getWorldTransform(_ghostObject->getWorldTransform());
  21. // Add the ghost object to the physics world.
  22. physicsController->addCollisionObject(this);
  23. _node->addListener(this);
  24. }
  25. PhysicsGhostObject::~PhysicsGhostObject()
  26. {
  27. _node->removeListener(this);
  28. Game::getInstance()->getPhysicsController()->removeCollisionObject(this);
  29. SAFE_DELETE(_ghostObject);
  30. }
  31. PhysicsGhostObject* PhysicsGhostObject::create(Node* node, Properties* properties)
  32. {
  33. // Check if the properties is valid and has a valid namespace.
  34. GP_ASSERT(properties);
  35. if (!properties || !(strcmp(properties->getNamespace(), "ghostObject") == 0))
  36. {
  37. GP_WARN("Failed to load ghost object from properties object: must be non-null object and have namespace equal to \'ghost\'.");
  38. return NULL;
  39. }
  40. // Load the physics collision shape definition.
  41. PhysicsCollisionShape::Definition* shape = PhysicsCollisionShape::Definition::create(node, properties);
  42. if (shape == NULL)
  43. {
  44. GP_WARN("Failed to create collision shape during ghost object creation.");
  45. return NULL;
  46. }
  47. // Create the ghost object.
  48. PhysicsGhostObject* ghost = new PhysicsGhostObject(node, *shape);
  49. SAFE_DELETE(shape);
  50. return ghost;
  51. }
  52. PhysicsCollisionObject::Type PhysicsGhostObject::getType() const
  53. {
  54. return GHOST_OBJECT;
  55. }
  56. btCollisionObject* PhysicsGhostObject::getCollisionObject() const
  57. {
  58. return _ghostObject;
  59. }
  60. void PhysicsGhostObject::transformChanged(Transform* transform, long cookie)
  61. {
  62. // Update the motion state with the transform from the node.
  63. _motionState->updateTransformFromNode();
  64. // Update the transform on the ghost object.
  65. _motionState->getWorldTransform(_ghostObject->getWorldTransform());
  66. }
  67. }