PhysicsGhostObject.cpp 3.0 KB

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