PhysicsController.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  1. #include "Base.h"
  2. #include "PhysicsController.h"
  3. #include "PhysicsRigidBody.h"
  4. #include "PhysicsCharacter.h"
  5. #include "Game.h"
  6. #include "MeshPart.h"
  7. #include "Bundle.h"
  8. #include "Terrain.h"
  9. #ifdef GP_USE_MEM_LEAK_DETECTION
  10. #undef new
  11. #endif
  12. #include "BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h"
  13. #include "BulletCollision/CollisionShapes/btShapeHull.h"
  14. #ifdef GP_USE_MEM_LEAK_DETECTION
  15. #define new DEBUG_NEW
  16. #endif
  17. // The initial capacity of the Bullet debug drawer's vertex batch.
  18. #define INITIAL_CAPACITY 280
  19. namespace gameplay
  20. {
  21. const int PhysicsController::DIRTY = 0x01;
  22. const int PhysicsController::COLLISION = 0x02;
  23. const int PhysicsController::REGISTERED = 0x04;
  24. const int PhysicsController::REMOVE = 0x08;
  25. PhysicsController::PhysicsController()
  26. : _isUpdating(false), _collisionConfiguration(NULL), _dispatcher(NULL),
  27. _overlappingPairCache(NULL), _solver(NULL), _world(NULL), _ghostPairCallback(NULL),
  28. _debugDrawer(NULL), _status(PhysicsController::Listener::DEACTIVATED), _listeners(NULL),
  29. _gravity(btScalar(0.0), btScalar(-9.8), btScalar(0.0)), _collisionCallback(NULL)
  30. {
  31. GP_REGISTER_SCRIPT_EVENTS();
  32. // Default gravity is 9.8 along the negative Y axis.
  33. _collisionCallback = new CollisionCallback(this);
  34. }
  35. PhysicsController::~PhysicsController()
  36. {
  37. SAFE_DELETE(_collisionCallback);
  38. SAFE_DELETE(_ghostPairCallback);
  39. SAFE_DELETE(_debugDrawer);
  40. SAFE_DELETE(_listeners);
  41. }
  42. void PhysicsController::addStatusListener(Listener* listener)
  43. {
  44. GP_ASSERT(listener);
  45. if (!_listeners)
  46. _listeners = new std::vector<Listener*>();
  47. _listeners->push_back(listener);
  48. }
  49. void PhysicsController::removeStatusListener(Listener* listener)
  50. {
  51. GP_ASSERT(listener);
  52. if (!_listeners)
  53. return;
  54. for (std::vector<Listener*>::iterator iter = _listeners->begin(); iter != _listeners->end(); iter++)
  55. {
  56. if (*iter == listener)
  57. {
  58. _listeners->erase(iter);
  59. return;
  60. }
  61. }
  62. }
  63. PhysicsFixedConstraint* PhysicsController::createFixedConstraint(PhysicsRigidBody* a, PhysicsRigidBody* b)
  64. {
  65. checkConstraintRigidBodies(a, b);
  66. PhysicsFixedConstraint* constraint = new PhysicsFixedConstraint(a, b);
  67. addConstraint(a, b, constraint);
  68. return constraint;
  69. }
  70. PhysicsGenericConstraint* PhysicsController::createGenericConstraint(PhysicsRigidBody* a, PhysicsRigidBody* b)
  71. {
  72. checkConstraintRigidBodies(a, b);
  73. PhysicsGenericConstraint* constraint = new PhysicsGenericConstraint(a, b);
  74. addConstraint(a, b, constraint);
  75. return constraint;
  76. }
  77. PhysicsGenericConstraint* PhysicsController::createGenericConstraint(PhysicsRigidBody* a,
  78. const Quaternion& rotationOffsetA, const Vector3& translationOffsetA, PhysicsRigidBody* b,
  79. const Quaternion& rotationOffsetB, const Vector3& translationOffsetB)
  80. {
  81. checkConstraintRigidBodies(a, b);
  82. PhysicsGenericConstraint* constraint = new PhysicsGenericConstraint(a, rotationOffsetA, translationOffsetA, b, rotationOffsetB, translationOffsetB);
  83. addConstraint(a, b, constraint);
  84. return constraint;
  85. }
  86. PhysicsHingeConstraint* PhysicsController::createHingeConstraint(PhysicsRigidBody* a,
  87. const Quaternion& rotationOffsetA, const Vector3& translationOffsetA, PhysicsRigidBody* b,
  88. const Quaternion& rotationOffsetB, const Vector3& translationOffsetB)
  89. {
  90. checkConstraintRigidBodies(a, b);
  91. PhysicsHingeConstraint* constraint = new PhysicsHingeConstraint(a, rotationOffsetA, translationOffsetA, b, rotationOffsetB, translationOffsetB);
  92. addConstraint(a, b, constraint);
  93. return constraint;
  94. }
  95. PhysicsSocketConstraint* PhysicsController::createSocketConstraint(PhysicsRigidBody* a, PhysicsRigidBody* b)
  96. {
  97. checkConstraintRigidBodies(a, b);
  98. PhysicsSocketConstraint* constraint = new PhysicsSocketConstraint(a, b);
  99. addConstraint(a, b, constraint);
  100. return constraint;
  101. }
  102. PhysicsSocketConstraint* PhysicsController::createSocketConstraint(PhysicsRigidBody* a,
  103. const Vector3& translationOffsetA, PhysicsRigidBody* b, const Vector3& translationOffsetB)
  104. {
  105. checkConstraintRigidBodies(a, b);
  106. PhysicsSocketConstraint* constraint = new PhysicsSocketConstraint(a,translationOffsetA, b, translationOffsetB);
  107. addConstraint(a, b, constraint);
  108. return constraint;
  109. }
  110. PhysicsSpringConstraint* PhysicsController::createSpringConstraint(PhysicsRigidBody* a, PhysicsRigidBody* b)
  111. {
  112. checkConstraintRigidBodies(a, b);
  113. PhysicsSpringConstraint* constraint = new PhysicsSpringConstraint(a, b);
  114. addConstraint(a, b, constraint);
  115. return constraint;
  116. }
  117. PhysicsSpringConstraint* PhysicsController::createSpringConstraint(PhysicsRigidBody* a, const Quaternion& rotationOffsetA, const Vector3& translationOffsetA,
  118. PhysicsRigidBody* b, const Quaternion& rotationOffsetB, const Vector3& translationOffsetB)
  119. {
  120. checkConstraintRigidBodies(a, b);
  121. PhysicsSpringConstraint* constraint = new PhysicsSpringConstraint(a, rotationOffsetA, translationOffsetA, b, rotationOffsetB, translationOffsetB);
  122. addConstraint(a, b, constraint);
  123. return constraint;
  124. }
  125. const Vector3& PhysicsController::getGravity() const
  126. {
  127. return _gravity;
  128. }
  129. void PhysicsController::setGravity(const Vector3& gravity)
  130. {
  131. _gravity = gravity;
  132. if (_world)
  133. _world->setGravity(BV(_gravity));
  134. }
  135. void PhysicsController::drawDebug(const Matrix& viewProjection)
  136. {
  137. GP_ASSERT(_debugDrawer);
  138. GP_ASSERT(_world);
  139. _debugDrawer->begin(viewProjection);
  140. _world->debugDrawWorld();
  141. _debugDrawer->end();
  142. }
  143. bool PhysicsController::rayTest(const Ray& ray, float distance, PhysicsController::HitResult* result, PhysicsController::HitFilter* filter)
  144. {
  145. class RayTestCallback : public btCollisionWorld::ClosestRayResultCallback
  146. {
  147. private:
  148. HitFilter* filter;
  149. HitResult hitResult;
  150. public:
  151. RayTestCallback(const btVector3& rayFromWorld, const btVector3& rayToWorld, PhysicsController::HitFilter* filter)
  152. : btCollisionWorld::ClosestRayResultCallback(rayFromWorld, rayToWorld), filter(filter)
  153. {
  154. }
  155. virtual bool needsCollision(btBroadphaseProxy* proxy0) const
  156. {
  157. if (!btCollisionWorld::ClosestRayResultCallback::needsCollision(proxy0))
  158. return false;
  159. btCollisionObject* co = reinterpret_cast<btCollisionObject*>(proxy0->m_clientObject);
  160. PhysicsCollisionObject* object = reinterpret_cast<PhysicsCollisionObject*>(co->getUserPointer());
  161. if (object == NULL)
  162. return false;
  163. return filter ? !filter->filter(object) : true;
  164. }
  165. btScalar addSingleResult(btCollisionWorld::LocalRayResult& rayResult, bool normalInWorldSpace)
  166. {
  167. GP_ASSERT(rayResult.m_collisionObject);
  168. PhysicsCollisionObject* object = reinterpret_cast<PhysicsCollisionObject*>(rayResult.m_collisionObject->getUserPointer());
  169. if (object == NULL)
  170. return 1.0f; // ignore
  171. float result = btCollisionWorld::ClosestRayResultCallback::addSingleResult(rayResult, normalInWorldSpace);
  172. hitResult.object = object;
  173. hitResult.point.set(m_hitPointWorld.x(), m_hitPointWorld.y(), m_hitPointWorld.z());
  174. hitResult.fraction = m_closestHitFraction;
  175. hitResult.normal.set(m_hitNormalWorld.x(), m_hitNormalWorld.y(), m_hitNormalWorld.z());
  176. if (filter && !filter->hit(hitResult))
  177. return 1.0f; // process next collision
  178. return result; // continue normally
  179. }
  180. };
  181. GP_ASSERT(_world);
  182. btVector3 rayFromWorld(BV(ray.getOrigin()));
  183. btVector3 rayToWorld(rayFromWorld + BV(ray.getDirection() * distance));
  184. RayTestCallback callback(rayFromWorld, rayToWorld, filter);
  185. _world->rayTest(rayFromWorld, rayToWorld, callback);
  186. if (callback.hasHit())
  187. {
  188. if (result)
  189. {
  190. result->object = getCollisionObject(callback.m_collisionObject);
  191. result->point.set(callback.m_hitPointWorld.x(), callback.m_hitPointWorld.y(), callback.m_hitPointWorld.z());
  192. result->fraction = callback.m_closestHitFraction;
  193. result->normal.set(callback.m_hitNormalWorld.x(), callback.m_hitNormalWorld.y(), callback.m_hitNormalWorld.z());
  194. }
  195. return true;
  196. }
  197. return false;
  198. }
  199. bool PhysicsController::sweepTest(PhysicsCollisionObject* object, const Vector3& endPosition, PhysicsController::HitResult* result, PhysicsController::HitFilter* filter)
  200. {
  201. class SweepTestCallback : public btCollisionWorld::ClosestConvexResultCallback
  202. {
  203. private:
  204. PhysicsCollisionObject* me;
  205. PhysicsController::HitFilter* filter;
  206. PhysicsController::HitResult hitResult;
  207. public:
  208. SweepTestCallback(PhysicsCollisionObject* me, PhysicsController::HitFilter* filter)
  209. : btCollisionWorld::ClosestConvexResultCallback(btVector3(0.0, 0.0, 0.0), btVector3(0.0, 0.0, 0.0)), me(me), filter(filter)
  210. {
  211. }
  212. virtual bool needsCollision(btBroadphaseProxy* proxy0) const
  213. {
  214. if (!btCollisionWorld::ClosestConvexResultCallback::needsCollision(proxy0))
  215. return false;
  216. btCollisionObject* co = reinterpret_cast<btCollisionObject*>(proxy0->m_clientObject);
  217. PhysicsCollisionObject* object = reinterpret_cast<PhysicsCollisionObject*>(co->getUserPointer());
  218. if (object == NULL || object == me)
  219. return false;
  220. return filter ? !filter->filter(object) : true;
  221. }
  222. btScalar addSingleResult(btCollisionWorld::LocalConvexResult& convexResult, bool normalInWorldSpace)
  223. {
  224. GP_ASSERT(convexResult.m_hitCollisionObject);
  225. PhysicsCollisionObject* object = reinterpret_cast<PhysicsCollisionObject*>(convexResult.m_hitCollisionObject->getUserPointer());
  226. if (object == NULL)
  227. return 1.0f;
  228. float result = ClosestConvexResultCallback::addSingleResult(convexResult, normalInWorldSpace);
  229. hitResult.object = object;
  230. hitResult.point.set(m_hitPointWorld.x(), m_hitPointWorld.y(), m_hitPointWorld.z());
  231. hitResult.fraction = m_closestHitFraction;
  232. hitResult.normal.set(m_hitNormalWorld.x(), m_hitNormalWorld.y(), m_hitNormalWorld.z());
  233. if (filter && !filter->hit(hitResult))
  234. return 1.0f;
  235. return result;
  236. }
  237. };
  238. GP_ASSERT(object && object->getCollisionShape());
  239. PhysicsCollisionShape* shape = object->getCollisionShape();
  240. PhysicsCollisionShape::Type type = shape->getType();
  241. if (type != PhysicsCollisionShape::SHAPE_BOX && type != PhysicsCollisionShape::SHAPE_SPHERE && type != PhysicsCollisionShape::SHAPE_CAPSULE)
  242. return false; // unsupported type
  243. // Define the start transform.
  244. btTransform start;
  245. start.setIdentity();
  246. if (object->getNode())
  247. {
  248. Vector3 translation;
  249. Quaternion rotation;
  250. const Matrix& m = object->getNode()->getWorldMatrix();
  251. m.getTranslation(&translation);
  252. m.getRotation(&rotation);
  253. start.setIdentity();
  254. start.setOrigin(BV(translation));
  255. start.setRotation(BQ(rotation));
  256. }
  257. // Define the end transform.
  258. btTransform end(start);
  259. end.setOrigin(BV(endPosition));
  260. // Perform bullet convex sweep test.
  261. SweepTestCallback callback(object, filter);
  262. // If the object is represented by a ghost object, use the ghost object's convex sweep test
  263. // since it is much faster than the world's version.
  264. // NOTE: Unfortunately the ghost object sweep test does not seem reliable here currently, so using world's version instead.
  265. /*switch (object->getType())
  266. {
  267. case PhysicsCollisionObject::GHOST_OBJECT:
  268. case PhysicsCollisionObject::CHARACTER:
  269. static_cast<PhysicsGhostObject*>(object)->_ghostObject->convexSweepTest(static_cast<btConvexShape*>(shape->getShape()), start, end, callback, _world->getDispatchInfo().m_allowedCcdPenetration);
  270. break;
  271. default:
  272. _world->convexSweepTest(static_cast<btConvexShape*>(shape->getShape()), start, end, callback, _world->getDispatchInfo().m_allowedCcdPenetration);
  273. break;
  274. }*/
  275. GP_ASSERT(_world);
  276. _world->convexSweepTest(static_cast<btConvexShape*>(shape->getShape()), start, end, callback, _world->getDispatchInfo().m_allowedCcdPenetration);
  277. // Check for hits and store results.
  278. if (callback.hasHit())
  279. {
  280. if (result)
  281. {
  282. result->object = getCollisionObject(callback.m_hitCollisionObject);
  283. result->point.set(callback.m_hitPointWorld.x(), callback.m_hitPointWorld.y(), callback.m_hitPointWorld.z());
  284. result->fraction = callback.m_closestHitFraction;
  285. result->normal.set(callback.m_hitNormalWorld.x(), callback.m_hitNormalWorld.y(), callback.m_hitNormalWorld.z());
  286. }
  287. return true;
  288. }
  289. return false;
  290. }
  291. btScalar PhysicsController::CollisionCallback::addSingleResult(btManifoldPoint& cp, const btCollisionObjectWrapper* a, int partIdA, int indexA,
  292. const btCollisionObjectWrapper* b, int partIdB, int indexB)
  293. {
  294. GP_ASSERT(_pc);
  295. // Get pointers to the PhysicsCollisionObject objects.
  296. PhysicsCollisionObject* objectA = _pc->getCollisionObject(a->m_collisionObject);
  297. PhysicsCollisionObject* objectB = _pc->getCollisionObject(b->m_collisionObject);
  298. // If the given collision object pair has collided in the past, then
  299. // we notify the listeners only if the pair was not colliding
  300. // during the previous frame. Otherwise, it's a new pair, so add a
  301. // new entry to the cache with the appropriate listeners and notify them.
  302. PhysicsCollisionObject::CollisionPair pair(objectA, objectB);
  303. CollisionInfo* collisionInfo;
  304. if (_pc->_collisionStatus.count(pair) > 0)
  305. {
  306. collisionInfo = &_pc->_collisionStatus[pair];
  307. }
  308. else
  309. {
  310. // Add a new collision pair for these objects.
  311. collisionInfo = &_pc->_collisionStatus[pair];
  312. // Add the appropriate listeners.
  313. PhysicsCollisionObject::CollisionPair p1(pair.objectA, NULL);
  314. if (_pc->_collisionStatus.count(p1) > 0)
  315. {
  316. const CollisionInfo& ci = _pc->_collisionStatus[p1];
  317. std::vector<PhysicsCollisionObject::CollisionListener*>::const_iterator iter = ci._listeners.begin();
  318. for (; iter != ci._listeners.end(); iter++)
  319. {
  320. GP_ASSERT(*iter);
  321. collisionInfo->_listeners.push_back(*iter);
  322. }
  323. }
  324. PhysicsCollisionObject::CollisionPair p2(pair.objectB, NULL);
  325. if (_pc->_collisionStatus.count(p2) > 0)
  326. {
  327. const CollisionInfo& ci = _pc->_collisionStatus[p2];
  328. std::vector<PhysicsCollisionObject::CollisionListener*>::const_iterator iter = ci._listeners.begin();
  329. for (; iter != ci._listeners.end(); iter++)
  330. {
  331. GP_ASSERT(*iter);
  332. collisionInfo->_listeners.push_back(*iter);
  333. }
  334. }
  335. }
  336. // Fire collision event.
  337. if ((collisionInfo->_status & COLLISION) == 0)
  338. {
  339. std::vector<PhysicsCollisionObject::CollisionListener*>::const_iterator iter = collisionInfo->_listeners.begin();
  340. for (; iter != collisionInfo->_listeners.end(); iter++)
  341. {
  342. GP_ASSERT(*iter);
  343. if ((collisionInfo->_status & REMOVE) == 0)
  344. {
  345. (*iter)->collisionEvent(PhysicsCollisionObject::CollisionListener::COLLIDING, pair, Vector3(cp.getPositionWorldOnA().x(), cp.getPositionWorldOnA().y(), cp.getPositionWorldOnA().z()),
  346. Vector3(cp.getPositionWorldOnB().x(), cp.getPositionWorldOnB().y(), cp.getPositionWorldOnB().z()));
  347. }
  348. }
  349. }
  350. // Update the collision status cache (we remove the dirty bit
  351. // set in the controller's update so that this particular collision pair's
  352. // status is not reset to 'no collision' when the controller's update completes).
  353. collisionInfo->_status &= ~DIRTY;
  354. collisionInfo->_status |= COLLISION;
  355. return 0.0f;
  356. }
  357. void PhysicsController::initialize()
  358. {
  359. _collisionConfiguration = bullet_new<btDefaultCollisionConfiguration>();
  360. _dispatcher = bullet_new<btCollisionDispatcher>(_collisionConfiguration);
  361. _overlappingPairCache = bullet_new<btDbvtBroadphase>();
  362. _solver = bullet_new<btSequentialImpulseConstraintSolver>();
  363. // Create the world.
  364. _world = bullet_new<btDiscreteDynamicsWorld>(_dispatcher, _overlappingPairCache, _solver, _collisionConfiguration);
  365. _world->setGravity(BV(_gravity));
  366. // Register ghost pair callback so bullet detects collisions with ghost objects (used for character collisions).
  367. GP_ASSERT(_world->getPairCache());
  368. _ghostPairCallback = bullet_new<btGhostPairCallback>();
  369. _world->getPairCache()->setInternalGhostPairCallback(_ghostPairCallback);
  370. _world->getDispatchInfo().m_allowedCcdPenetration = 0.0001f;
  371. // Set up debug drawing.
  372. _debugDrawer = new DebugDrawer();
  373. _world->setDebugDrawer(_debugDrawer);
  374. }
  375. void PhysicsController::finalize()
  376. {
  377. // Clean up the world and its various components.
  378. SAFE_DELETE(_world);
  379. SAFE_DELETE(_ghostPairCallback);
  380. SAFE_DELETE(_solver);
  381. SAFE_DELETE(_overlappingPairCache);
  382. SAFE_DELETE(_dispatcher);
  383. SAFE_DELETE(_collisionConfiguration);
  384. }
  385. void PhysicsController::pause()
  386. {
  387. // Unused
  388. }
  389. void PhysicsController::resume()
  390. {
  391. // Unused
  392. }
  393. void PhysicsController::update(float elapsedTime)
  394. {
  395. GP_ASSERT(_world);
  396. _isUpdating = true;
  397. // Update the physics simulation, with a maximum
  398. // of 10 simulation steps being performed in a given frame.
  399. //
  400. // Note that stepSimulation takes elapsed time in seconds
  401. // so we divide by 1000 to convert from milliseconds.
  402. _world->stepSimulation(elapsedTime * 0.001f, 10);
  403. // If we have status listeners, then check if our status has changed.
  404. if (_listeners || hasScriptListener(GP_GET_SCRIPT_EVENT(PhysicsController, statusEvent)))
  405. {
  406. Listener::EventType oldStatus = _status;
  407. if (_status == Listener::DEACTIVATED)
  408. {
  409. for (int i = 0; i < _world->getNumCollisionObjects(); i++)
  410. {
  411. GP_ASSERT(_world->getCollisionObjectArray()[i]);
  412. if (_world->getCollisionObjectArray()[i]->isActive())
  413. {
  414. _status = Listener::ACTIVATED;
  415. break;
  416. }
  417. }
  418. }
  419. else
  420. {
  421. bool allInactive = true;
  422. for (int i = 0; i < _world->getNumCollisionObjects(); i++)
  423. {
  424. GP_ASSERT(_world->getCollisionObjectArray()[i]);
  425. if (_world->getCollisionObjectArray()[i]->isActive())
  426. {
  427. allInactive = false;
  428. break;
  429. }
  430. }
  431. if (allInactive)
  432. _status = Listener::DEACTIVATED;
  433. }
  434. // If the status has changed, notify our listeners.
  435. if (oldStatus != _status)
  436. {
  437. if (_listeners)
  438. {
  439. for (unsigned int k = 0; k < _listeners->size(); k++)
  440. {
  441. GP_ASSERT((*_listeners)[k]);
  442. (*_listeners)[k]->statusEvent(_status);
  443. }
  444. }
  445. fireScriptEvent<void>(GP_GET_SCRIPT_EVENT(PhysicsController, statusEvent), _status);
  446. }
  447. }
  448. // All statuses are set with the DIRTY bit before collision processing occurs.
  449. // During collision processing, if a collision occurs, the status is
  450. // set to COLLISION and the DIRTY bit is cleared. Then, after collision processing
  451. // is finished, if a given status is still dirty, the COLLISION bit is cleared.
  452. //
  453. // If an entry was marked for removal in the last frame, fire NOT_COLLIDING if appropriate and remove it now.
  454. // Dirty the collision status cache entries.
  455. std::map<PhysicsCollisionObject::CollisionPair, CollisionInfo>::iterator iter = _collisionStatus.begin();
  456. for (; iter != _collisionStatus.end();)
  457. {
  458. if ((iter->second._status & REMOVE) != 0)
  459. {
  460. if ((iter->second._status & COLLISION) != 0 && iter->first.objectB)
  461. {
  462. size_t size = iter->second._listeners.size();
  463. for (size_t i = 0; i < size; i++)
  464. {
  465. PhysicsCollisionObject::CollisionPair cp(iter->first.objectA, NULL);
  466. iter->second._listeners[i]->collisionEvent(PhysicsCollisionObject::CollisionListener::NOT_COLLIDING, cp);
  467. }
  468. }
  469. std::map<PhysicsCollisionObject::CollisionPair, CollisionInfo>::iterator eraseIter = iter;
  470. iter++;
  471. _collisionStatus.erase(eraseIter);
  472. }
  473. else
  474. {
  475. iter->second._status |= DIRTY;
  476. iter++;
  477. }
  478. }
  479. // Go through the collision status cache and perform all registered collision tests.
  480. iter = _collisionStatus.begin();
  481. for (; iter != _collisionStatus.end(); iter++)
  482. {
  483. // If this collision pair was one that was registered for listening, then perform the collision test.
  484. // (In the case where we register for all collisions with a rigid body, there will be a lot
  485. // of collision pairs in the status cache that we did not explicitly register for.)
  486. if ((iter->second._status & REGISTERED) != 0 && (iter->second._status & REMOVE) == 0)
  487. {
  488. if (iter->first.objectB)
  489. _world->contactPairTest(iter->first.objectA->getCollisionObject(), iter->first.objectB->getCollisionObject(), *_collisionCallback);
  490. else
  491. _world->contactTest(iter->first.objectA->getCollisionObject(), *_collisionCallback);
  492. }
  493. }
  494. // Update all the collision status cache entries.
  495. iter = _collisionStatus.begin();
  496. for ( ; iter != _collisionStatus.end(); iter++)
  497. {
  498. if ((iter->second._status & DIRTY) != 0)
  499. {
  500. if ((iter->second._status & COLLISION) != 0 && iter->first.objectB)
  501. {
  502. size_t size = iter->second._listeners.size();
  503. for (size_t i = 0; i < size; i++)
  504. {
  505. iter->second._listeners[i]->collisionEvent(PhysicsCollisionObject::CollisionListener::NOT_COLLIDING, iter->first);
  506. }
  507. }
  508. iter->second._status &= ~COLLISION;
  509. }
  510. }
  511. _isUpdating = false;
  512. }
  513. void PhysicsController::addCollisionListener(PhysicsCollisionObject::CollisionListener* listener, PhysicsCollisionObject* objectA, PhysicsCollisionObject* objectB)
  514. {
  515. GP_ASSERT(listener);
  516. // One of the collision objects in the pair must be non-null.
  517. GP_ASSERT(objectA || objectB);
  518. PhysicsCollisionObject::CollisionPair pair(objectA, objectB);
  519. // Add the listener and ensure the status includes that this collision pair is registered.
  520. CollisionInfo& info = _collisionStatus[pair];
  521. info._listeners.push_back(listener);
  522. info._status |= PhysicsController::REGISTERED;
  523. }
  524. void PhysicsController::removeCollisionListener(PhysicsCollisionObject::CollisionListener* listener, PhysicsCollisionObject* objectA, PhysicsCollisionObject* objectB)
  525. {
  526. // One of the collision objects in the pair must be non-null.
  527. GP_ASSERT(objectA || objectB);
  528. PhysicsCollisionObject::CollisionPair pair(objectA, objectB);
  529. // Mark the collision pair for these objects for removal.
  530. if (_collisionStatus.count(pair) > 0)
  531. {
  532. _collisionStatus[pair]._status |= REMOVE;
  533. }
  534. }
  535. void PhysicsController::addCollisionObject(PhysicsCollisionObject* object)
  536. {
  537. GP_ASSERT(object && object->getCollisionObject());
  538. GP_ASSERT(_world);
  539. // Assign user pointer for the bullet collision object to allow efficient
  540. // lookups of bullet objects -> gameplay objects.
  541. object->getCollisionObject()->setUserPointer(object);
  542. short group = (short)object->_group;
  543. short mask = (short)object->_mask;
  544. // Add the object to the physics world.
  545. switch (object->getType())
  546. {
  547. case PhysicsCollisionObject::RIGID_BODY:
  548. _world->addRigidBody(static_cast<btRigidBody*>(object->getCollisionObject()), group, mask);
  549. break;
  550. case PhysicsCollisionObject::CHARACTER:
  551. _world->addCollisionObject(object->getCollisionObject(), group, mask);
  552. break;
  553. case PhysicsCollisionObject::GHOST_OBJECT:
  554. _world->addCollisionObject(object->getCollisionObject(), group, mask);
  555. break;
  556. default:
  557. GP_ERROR("Unsupported collision object type (%d).", object->getType());
  558. break;
  559. }
  560. }
  561. void PhysicsController::removeCollisionObject(PhysicsCollisionObject* object, bool removeListeners)
  562. {
  563. GP_ASSERT(object);
  564. GP_ASSERT(_world);
  565. GP_ASSERT(!_isUpdating);
  566. // Remove the collision object from the world.
  567. if (object->getCollisionObject())
  568. {
  569. switch (object->getType())
  570. {
  571. case PhysicsCollisionObject::RIGID_BODY:
  572. _world->removeRigidBody(static_cast<btRigidBody*>(object->getCollisionObject()));
  573. break;
  574. case PhysicsCollisionObject::CHARACTER:
  575. case PhysicsCollisionObject::GHOST_OBJECT:
  576. _world->removeCollisionObject(object->getCollisionObject());
  577. break;
  578. default:
  579. GP_ERROR("Unsupported collision object type (%d).", object->getType());
  580. break;
  581. }
  582. }
  583. // Find all references to the object in the collision status cache and mark them for removal.
  584. if (removeListeners)
  585. {
  586. std::map<PhysicsCollisionObject::CollisionPair, CollisionInfo>::iterator iter = _collisionStatus.begin();
  587. for (; iter != _collisionStatus.end(); iter++)
  588. {
  589. if (iter->first.objectA == object || iter->first.objectB == object)
  590. iter->second._status |= REMOVE;
  591. }
  592. }
  593. }
  594. PhysicsCollisionObject* PhysicsController::getCollisionObject(const btCollisionObject* collisionObject) const
  595. {
  596. // Gameplay collision objects are stored in the userPointer data of Bullet collision objects.
  597. GP_ASSERT(collisionObject);
  598. return reinterpret_cast<PhysicsCollisionObject*>(collisionObject->getUserPointer());
  599. }
  600. static void getBoundingBox(Node* node, BoundingBox* out, bool merge = false)
  601. {
  602. GP_ASSERT(node);
  603. GP_ASSERT(out);
  604. if (node->getModel())
  605. {
  606. GP_ASSERT(node->getModel()->getMesh());
  607. if (merge)
  608. out->merge(node->getModel()->getMesh()->getBoundingBox());
  609. else
  610. {
  611. out->set(node->getModel()->getMesh()->getBoundingBox());
  612. merge = true;
  613. }
  614. }
  615. Node* child = node->getFirstChild();
  616. while (child)
  617. {
  618. getBoundingBox(child, out, merge);
  619. child = child->getNextSibling();
  620. }
  621. }
  622. static void getBoundingSphere(Node* node, BoundingSphere* out, bool merge = false)
  623. {
  624. GP_ASSERT(node);
  625. GP_ASSERT(out);
  626. if (node->getModel())
  627. {
  628. GP_ASSERT(node->getModel()->getMesh());
  629. if (merge)
  630. out->merge(node->getModel()->getMesh()->getBoundingSphere());
  631. else
  632. {
  633. out->set(node->getModel()->getMesh()->getBoundingSphere());
  634. merge = true;
  635. }
  636. }
  637. Node* child = node->getFirstChild();
  638. while (child)
  639. {
  640. getBoundingSphere(child, out, merge);
  641. child = child->getNextSibling();
  642. }
  643. }
  644. static void computeCenterOfMass(const Vector3& center, const Vector3& scale, Vector3* centerOfMassOffset)
  645. {
  646. GP_ASSERT(centerOfMassOffset);
  647. // Update center of mass offset.
  648. *centerOfMassOffset = center;
  649. centerOfMassOffset->x *= scale.x;
  650. centerOfMassOffset->y *= scale.y;
  651. centerOfMassOffset->z *= scale.z;
  652. centerOfMassOffset->negate();
  653. }
  654. PhysicsCollisionShape* PhysicsController::createShape(Node* node, const PhysicsCollisionShape::Definition& shape, Vector3* centerOfMassOffset, bool dynamic)
  655. {
  656. GP_ASSERT(node);
  657. PhysicsCollisionShape* collisionShape = NULL;
  658. // Get the node's world scale (we need to apply this during creation since rigid bodies don't scale dynamically).
  659. Vector3 scale;
  660. node->getWorldMatrix().getScale(&scale);
  661. switch (shape.type)
  662. {
  663. case PhysicsCollisionShape::SHAPE_BOX:
  664. {
  665. if (shape.isExplicit)
  666. {
  667. // Use the passed in box information.
  668. collisionShape = createBox(Vector3(shape.data.box.extents), Vector3::one());
  669. if (shape.centerAbsolute)
  670. {
  671. computeCenterOfMass(Vector3(shape.data.box.center), Vector3::one(), centerOfMassOffset);
  672. }
  673. else
  674. {
  675. BoundingBox box;
  676. getBoundingBox(node, &box);
  677. computeCenterOfMass(box.getCenter() + Vector3(shape.data.box.center), scale, centerOfMassOffset);
  678. }
  679. }
  680. else
  681. {
  682. // Automatically compute bounding box from mesh's bounding box.
  683. BoundingBox box;
  684. getBoundingBox(node, &box);
  685. collisionShape = createBox(Vector3(std::fabs(box.max.x - box.min.x), std::fabs(box.max.y - box.min.y), std::fabs(box.max.z - box.min.z)), scale);
  686. computeCenterOfMass(box.getCenter(), scale, centerOfMassOffset);
  687. }
  688. }
  689. break;
  690. case PhysicsCollisionShape::SHAPE_SPHERE:
  691. {
  692. if (shape.isExplicit)
  693. {
  694. // Use the passed in sphere information.
  695. collisionShape = createSphere(shape.data.sphere.radius, Vector3::one());
  696. if (shape.centerAbsolute)
  697. {
  698. computeCenterOfMass(Vector3(shape.data.sphere.center), Vector3::one(), centerOfMassOffset);
  699. }
  700. else
  701. {
  702. BoundingSphere sphere;
  703. getBoundingSphere(node, &sphere);
  704. computeCenterOfMass(sphere.center + Vector3(shape.data.sphere.center), scale, centerOfMassOffset);
  705. }
  706. }
  707. else
  708. {
  709. // Automatically compute bounding sphere from mesh's bounding sphere.
  710. BoundingSphere sphere;
  711. getBoundingSphere(node, &sphere);
  712. collisionShape = createSphere(sphere.radius, scale);
  713. computeCenterOfMass(sphere.center, scale, centerOfMassOffset);
  714. }
  715. }
  716. break;
  717. case PhysicsCollisionShape::SHAPE_CAPSULE:
  718. {
  719. if (shape.isExplicit)
  720. {
  721. // Use the passed in capsule information.
  722. collisionShape = createCapsule(shape.data.capsule.radius, shape.data.capsule.height, Vector3::one());
  723. if (shape.centerAbsolute)
  724. {
  725. computeCenterOfMass(Vector3(shape.data.capsule.center), Vector3::one(), centerOfMassOffset);
  726. }
  727. else
  728. {
  729. BoundingBox box;
  730. getBoundingBox(node, &box);
  731. computeCenterOfMass(box.getCenter() + Vector3(shape.data.capsule.center), scale, centerOfMassOffset);
  732. }
  733. }
  734. else
  735. {
  736. // Compute a capsule shape that roughly matches the bounding box of the mesh.
  737. BoundingBox box;
  738. getBoundingBox(node, &box);
  739. float radius = std::max((box.max.x - box.min.x) * 0.5f, (box.max.z - box.min.z) * 0.5f);
  740. float height = box.max.y - box.min.y;
  741. collisionShape = createCapsule(radius, height, scale);
  742. computeCenterOfMass(box.getCenter(), scale, centerOfMassOffset);
  743. }
  744. }
  745. break;
  746. case PhysicsCollisionShape::SHAPE_HEIGHTFIELD:
  747. {
  748. if (shape.isExplicit)
  749. {
  750. // Build heightfield rigid body from the passed in shape.
  751. collisionShape = createHeightfield(node, shape.data.heightfield, centerOfMassOffset);
  752. }
  753. else
  754. {
  755. // Build the heightfield from an attached terrain's height array
  756. if (node->getTerrain() == NULL)
  757. GP_ERROR("Empty heightfield collision shapes can only be used on nodes that have an attached Terrain.");
  758. else
  759. collisionShape = createHeightfield(node, node->getTerrain()->_heightfield, centerOfMassOffset);
  760. }
  761. }
  762. break;
  763. case PhysicsCollisionShape::SHAPE_MESH:
  764. {
  765. // Build mesh from passed in shape.
  766. collisionShape = createMesh(shape.data.mesh, scale, dynamic);
  767. }
  768. break;
  769. default:
  770. GP_ERROR("Unsupported collision shape type (%d).", shape.type);
  771. break;
  772. }
  773. return collisionShape;
  774. }
  775. PhysicsCollisionShape* PhysicsController::createBox(const Vector3& extents, const Vector3& scale)
  776. {
  777. btVector3 halfExtents(scale.x * 0.5 * extents.x, scale.y * 0.5 * extents.y, scale.z * 0.5 * extents.z);
  778. PhysicsCollisionShape* shape;
  779. // Return the box shape from the cache if it already exists.
  780. for (unsigned int i = 0; i < _shapes.size(); ++i)
  781. {
  782. shape = _shapes[i];
  783. GP_ASSERT(shape);
  784. if (shape->getType() == PhysicsCollisionShape::SHAPE_BOX)
  785. {
  786. btBoxShape* box = static_cast<btBoxShape*>(shape->_shape);
  787. if (box && box->getHalfExtentsWithMargin() == halfExtents)
  788. {
  789. shape->addRef();
  790. return shape;
  791. }
  792. }
  793. }
  794. // Create the box shape and add it to the cache.
  795. shape = new PhysicsCollisionShape(PhysicsCollisionShape::SHAPE_BOX, bullet_new<btBoxShape>(halfExtents));
  796. _shapes.push_back(shape);
  797. return shape;
  798. }
  799. PhysicsCollisionShape* PhysicsController::createSphere(float radius, const Vector3& scale)
  800. {
  801. // Since sphere shapes depend only on the radius, the best we can do is take
  802. // the largest dimension and apply that as the uniform scale to the rigid body.
  803. float uniformScale = scale.x;
  804. if (uniformScale < scale.y)
  805. uniformScale = scale.y;
  806. if (uniformScale < scale.z)
  807. uniformScale = scale.z;
  808. float scaledRadius = radius * uniformScale;
  809. PhysicsCollisionShape* shape;
  810. // Return the sphere shape from the cache if it already exists.
  811. for (unsigned int i = 0; i < _shapes.size(); ++i)
  812. {
  813. shape = _shapes[i];
  814. GP_ASSERT(shape);
  815. if (shape->getType() == PhysicsCollisionShape::SHAPE_SPHERE)
  816. {
  817. btSphereShape* sphere = static_cast<btSphereShape*>(shape->_shape);
  818. if (sphere && sphere->getRadius() == scaledRadius)
  819. {
  820. shape->addRef();
  821. return shape;
  822. }
  823. }
  824. }
  825. // Create the sphere shape and add it to the cache.
  826. shape = new PhysicsCollisionShape(PhysicsCollisionShape::SHAPE_SPHERE, bullet_new<btSphereShape>(scaledRadius));
  827. _shapes.push_back(shape);
  828. return shape;
  829. }
  830. PhysicsCollisionShape* PhysicsController::createCapsule(float radius, float height, const Vector3& scale)
  831. {
  832. float girthScale = scale.x;
  833. if (girthScale < scale.z)
  834. girthScale = scale.z;
  835. float scaledRadius = radius * girthScale;
  836. float scaledHeight = height * scale.y - radius * 2;
  837. PhysicsCollisionShape* shape;
  838. // Return the capsule shape from the cache if it already exists.
  839. for (unsigned int i = 0; i < _shapes.size(); i++)
  840. {
  841. shape = _shapes[i];
  842. GP_ASSERT(shape);
  843. if (shape->getType() == PhysicsCollisionShape::SHAPE_CAPSULE)
  844. {
  845. btCapsuleShape* capsule = static_cast<btCapsuleShape*>(shape->_shape);
  846. if (capsule && capsule->getRadius() == scaledRadius && capsule->getHalfHeight() == 0.5f * scaledHeight)
  847. {
  848. shape->addRef();
  849. return shape;
  850. }
  851. }
  852. }
  853. // Create the capsule shape and add it to the cache.
  854. shape = new PhysicsCollisionShape(PhysicsCollisionShape::SHAPE_CAPSULE, bullet_new<btCapsuleShape>(scaledRadius, scaledHeight));
  855. _shapes.push_back(shape);
  856. return shape;
  857. }
  858. PhysicsCollisionShape* PhysicsController::createHeightfield(Node* node, HeightField* heightfield, Vector3* centerOfMassOffset)
  859. {
  860. GP_ASSERT(node);
  861. GP_ASSERT(heightfield);
  862. GP_ASSERT(centerOfMassOffset);
  863. // Inspect the height array for the min and max values
  864. float* heights = heightfield->getArray();
  865. float minHeight = FLT_MAX, maxHeight = -FLT_MAX;
  866. for (unsigned int i = 0, count = heightfield->getColumnCount()*heightfield->getRowCount(); i < count; ++i)
  867. {
  868. float h = heights[i];
  869. if (h < minHeight)
  870. minHeight = h;
  871. if (h > maxHeight)
  872. maxHeight = h;
  873. }
  874. // Compute initial heightfield scale by pulling the current world scale out of the node
  875. Vector3 scale;
  876. node->getWorldMatrix().getScale(&scale);
  877. // If the node has a terrain, apply the terrain's local scale to the world scale
  878. if (node->getTerrain())
  879. {
  880. const Vector3& tScale = node->getTerrain()->_localScale;
  881. scale.set(scale.x * tScale.x, scale.y * tScale.y, scale.z * tScale.z);
  882. }
  883. // Compute initial center of mass offset necessary to move the height from its position in bullet
  884. // physics (always centered around origin) to its intended location.
  885. centerOfMassOffset->set(0, -(minHeight + (maxHeight-minHeight)*0.5f) * scale.y, 0);
  886. // Create our heightfield data to be stored in the collision shape
  887. PhysicsCollisionShape::HeightfieldData* heightfieldData = new PhysicsCollisionShape::HeightfieldData();
  888. heightfieldData->heightfield = heightfield;
  889. heightfieldData->heightfield->addRef();
  890. heightfieldData->inverseIsDirty = true;
  891. heightfieldData->minHeight = minHeight;
  892. heightfieldData->maxHeight = maxHeight;
  893. // Create the bullet terrain shape
  894. btHeightfieldTerrainShape* terrainShape = bullet_new<btHeightfieldTerrainShape>(
  895. heightfield->getColumnCount(), heightfield->getRowCount(), heightfield->getArray(), 1.0f, minHeight, maxHeight, 1, PHY_FLOAT, false);
  896. // Set initial bullet local scaling for the heightfield
  897. terrainShape->setLocalScaling(BV(scale));
  898. // Create our collision shape object and store heightfieldData in it.
  899. PhysicsCollisionShape* shape = new PhysicsCollisionShape(PhysicsCollisionShape::SHAPE_HEIGHTFIELD, terrainShape);
  900. shape->_shapeData.heightfieldData = heightfieldData;
  901. _shapes.push_back(shape);
  902. return shape;
  903. }
  904. PhysicsCollisionShape* PhysicsController::createMesh(Mesh* mesh, const Vector3& scale, bool dynamic)
  905. {
  906. GP_ASSERT(mesh);
  907. // The mesh must have a valid URL (i.e. it must have been loaded from a Bundle)
  908. // in order to fetch mesh data for computing mesh rigid body.
  909. if (strlen(mesh->getUrl()) == 0)
  910. {
  911. GP_ERROR("Cannot create mesh rigid body for mesh without valid URL.");
  912. return NULL;
  913. }
  914. if (!dynamic)
  915. {
  916. // Static meshes use btBvhTriangleMeshShape and therefore only support triangle mesh shapes.
  917. // Dynamic meshes are approximated with a btConvexHullShape (convex wrapper on cloud of vertices)
  918. // and therefore can support any primitive type.
  919. bool triMesh = true;
  920. if (mesh->getPartCount() > 0)
  921. {
  922. for (unsigned int i = 0; i < mesh->getPartCount(); ++i)
  923. {
  924. if (mesh->getPart(i)->getPrimitiveType() != Mesh::TRIANGLES)
  925. {
  926. triMesh = false;
  927. break;
  928. }
  929. }
  930. }
  931. else
  932. {
  933. triMesh = mesh->getPrimitiveType() == Mesh::TRIANGLES;
  934. }
  935. if (!triMesh)
  936. {
  937. GP_ERROR("Mesh rigid bodies are currently only supported on meshes with TRIANGLES primitive type.");
  938. return NULL;
  939. }
  940. }
  941. // Read mesh data from URL
  942. Bundle::MeshData* data = Bundle::readMeshData(mesh->getUrl());
  943. if (data == NULL)
  944. {
  945. GP_ERROR("Failed to load mesh data from url '%s'.", mesh->getUrl());
  946. return NULL;
  947. }
  948. // Create mesh data to be populated and store in returned collision shape.
  949. PhysicsCollisionShape::MeshData* shapeMeshData = new PhysicsCollisionShape::MeshData();
  950. shapeMeshData->vertexData = NULL;
  951. // Copy the scaled vertex position data to the rigid body's local buffer.
  952. Matrix m;
  953. Matrix::createScale(scale, &m);
  954. unsigned int vertexCount = data->vertexCount;
  955. shapeMeshData->vertexData = new float[vertexCount * 3];
  956. Vector3 v;
  957. int vertexStride = data->vertexFormat.getVertexSize();
  958. for (unsigned int i = 0; i < data->vertexCount; i++)
  959. {
  960. v.set(*((float*)&data->vertexData[i * vertexStride + 0 * sizeof(float)]),
  961. *((float*)&data->vertexData[i * vertexStride + 1 * sizeof(float)]),
  962. *((float*)&data->vertexData[i * vertexStride + 2 * sizeof(float)]));
  963. v *= m;
  964. memcpy(&(shapeMeshData->vertexData[i * 3]), &v, sizeof(float) * 3);
  965. }
  966. btCollisionShape* collisionShape = NULL;
  967. btTriangleIndexVertexArray* meshInterface = NULL;
  968. if (dynamic)
  969. {
  970. // For dynamic meshes, use a btConvexHullShape approximation
  971. btConvexHullShape* originalConvexShape = bullet_new<btConvexHullShape>(shapeMeshData->vertexData, data->vertexCount, sizeof(float)*3);
  972. // Create a hull approximation for better performance
  973. btShapeHull* hull = bullet_new<btShapeHull>(originalConvexShape);
  974. hull->buildHull(originalConvexShape->getMargin());
  975. collisionShape = bullet_new<btConvexHullShape>((btScalar*)hull->getVertexPointer(), hull->numVertices());
  976. SAFE_DELETE(hull);
  977. SAFE_DELETE(originalConvexShape);
  978. }
  979. else
  980. {
  981. // For static meshes, use btBvhTriangleMeshShape
  982. meshInterface = bullet_new<btTriangleIndexVertexArray>();
  983. size_t partCount = data->parts.size();
  984. if (partCount > 0)
  985. {
  986. PHY_ScalarType indexType = PHY_UCHAR;
  987. int indexStride = 0;
  988. Bundle::MeshPartData* meshPart = NULL;
  989. for (size_t i = 0; i < partCount; i++)
  990. {
  991. meshPart = data->parts[i];
  992. GP_ASSERT(meshPart);
  993. switch (meshPart->indexFormat)
  994. {
  995. case Mesh::INDEX8:
  996. indexType = PHY_UCHAR;
  997. indexStride = 1;
  998. break;
  999. case Mesh::INDEX16:
  1000. indexType = PHY_SHORT;
  1001. indexStride = 2;
  1002. break;
  1003. case Mesh::INDEX32:
  1004. indexType = PHY_INTEGER;
  1005. indexStride = 4;
  1006. break;
  1007. default:
  1008. GP_ERROR("Unsupported index format (%d).", meshPart->indexFormat);
  1009. SAFE_DELETE(meshInterface);
  1010. SAFE_DELETE_ARRAY(shapeMeshData->vertexData);
  1011. SAFE_DELETE(shapeMeshData);
  1012. SAFE_DELETE(data);
  1013. return NULL;
  1014. }
  1015. // Move the index data into the rigid body's local buffer.
  1016. // Set it to NULL in the MeshPartData so it is not released when the data is freed.
  1017. shapeMeshData->indexData.push_back(meshPart->indexData);
  1018. meshPart->indexData = NULL;
  1019. // Create a btIndexedMesh object for the current mesh part.
  1020. btIndexedMesh indexedMesh;
  1021. indexedMesh.m_indexType = indexType;
  1022. indexedMesh.m_numTriangles = meshPart->indexCount / 3; // assume TRIANGLES primitive type
  1023. indexedMesh.m_numVertices = meshPart->indexCount;
  1024. indexedMesh.m_triangleIndexBase = (const unsigned char*)shapeMeshData->indexData[i];
  1025. indexedMesh.m_triangleIndexStride = indexStride*3;
  1026. indexedMesh.m_vertexBase = (const unsigned char*)shapeMeshData->vertexData;
  1027. indexedMesh.m_vertexStride = sizeof(float)*3;
  1028. indexedMesh.m_vertexType = PHY_FLOAT;
  1029. // Add the indexed mesh data to the mesh interface.
  1030. meshInterface->addIndexedMesh(indexedMesh, indexType);
  1031. }
  1032. }
  1033. else
  1034. {
  1035. // Generate index data for the mesh locally in the rigid body.
  1036. unsigned int* indexData = new unsigned int[data->vertexCount];
  1037. for (unsigned int i = 0; i < data->vertexCount; i++)
  1038. {
  1039. indexData[i] = i;
  1040. }
  1041. shapeMeshData->indexData.push_back((unsigned char*)indexData);
  1042. // Create a single btIndexedMesh object for the mesh interface.
  1043. btIndexedMesh indexedMesh;
  1044. indexedMesh.m_indexType = PHY_INTEGER;
  1045. indexedMesh.m_numTriangles = data->vertexCount / 3; // assume TRIANGLES primitive type
  1046. indexedMesh.m_numVertices = data->vertexCount;
  1047. indexedMesh.m_triangleIndexBase = shapeMeshData->indexData[0];
  1048. indexedMesh.m_triangleIndexStride = sizeof(unsigned int);
  1049. indexedMesh.m_vertexBase = (const unsigned char*)shapeMeshData->vertexData;
  1050. indexedMesh.m_vertexStride = sizeof(float)*3;
  1051. indexedMesh.m_vertexType = PHY_FLOAT;
  1052. // Set the data in the mesh interface.
  1053. meshInterface->addIndexedMesh(indexedMesh, indexedMesh.m_indexType);
  1054. }
  1055. // Create our collision shape object and store shapeMeshData in it.
  1056. collisionShape = bullet_new<btBvhTriangleMeshShape>(meshInterface, true);
  1057. }
  1058. // Create our collision shape object and store shapeMeshData in it.
  1059. PhysicsCollisionShape* shape = new PhysicsCollisionShape(PhysicsCollisionShape::SHAPE_MESH, collisionShape, meshInterface);
  1060. shape->_shapeData.meshData = shapeMeshData;
  1061. _shapes.push_back(shape);
  1062. // Free the temporary mesh data now that it's stored in physics system.
  1063. SAFE_DELETE(data);
  1064. return shape;
  1065. }
  1066. void PhysicsController::destroyShape(PhysicsCollisionShape* shape)
  1067. {
  1068. if (shape)
  1069. {
  1070. if (shape->getRefCount() == 1)
  1071. {
  1072. // Remove shape from shape cache.
  1073. std::vector<PhysicsCollisionShape*>::iterator shapeItr = std::find(_shapes.begin(), _shapes.end(), shape);
  1074. if (shapeItr != _shapes.end())
  1075. _shapes.erase(shapeItr);
  1076. }
  1077. // Release the shape.
  1078. shape->release();
  1079. }
  1080. }
  1081. void PhysicsController::addConstraint(PhysicsRigidBody* a, PhysicsRigidBody* b, PhysicsConstraint* constraint)
  1082. {
  1083. GP_ASSERT(a);
  1084. GP_ASSERT(constraint);
  1085. GP_ASSERT(_world);
  1086. a->addConstraint(constraint);
  1087. if (b)
  1088. {
  1089. b->addConstraint(constraint);
  1090. }
  1091. _world->addConstraint(constraint->_constraint);
  1092. }
  1093. bool PhysicsController::checkConstraintRigidBodies(PhysicsRigidBody* a, PhysicsRigidBody* b)
  1094. {
  1095. GP_ASSERT(a);
  1096. if (!a->supportsConstraints())
  1097. {
  1098. GP_ASSERT(a->_node);
  1099. GP_ERROR("Rigid body '%s' does not support constraints; unexpected behavior may occur.", a->_node->getId());
  1100. return false;
  1101. }
  1102. if (b && !b->supportsConstraints())
  1103. {
  1104. GP_ASSERT(b->_node);
  1105. GP_ERROR("Rigid body '%s' does not support constraints; unexpected behavior may occur.", b->_node->getId());
  1106. return false;
  1107. }
  1108. return true;
  1109. }
  1110. void PhysicsController::removeConstraint(PhysicsConstraint* constraint)
  1111. {
  1112. GP_ASSERT(constraint);
  1113. GP_ASSERT(_world);
  1114. // Find the constraint and remove it from the physics world.
  1115. for (int i = _world->getNumConstraints() - 1; i >= 0; i--)
  1116. {
  1117. btTypedConstraint* currentConstraint = _world->getConstraint(i);
  1118. if (constraint->_constraint == currentConstraint)
  1119. {
  1120. _world->removeConstraint(currentConstraint);
  1121. break;
  1122. }
  1123. }
  1124. }
  1125. PhysicsController::DebugDrawer::DebugDrawer()
  1126. : _mode(btIDebugDraw::DBG_DrawAabb | btIDebugDraw::DBG_DrawConstraintLimits | btIDebugDraw::DBG_DrawConstraints |
  1127. btIDebugDraw::DBG_DrawContactPoints | btIDebugDraw::DBG_DrawWireframe), _meshBatch(NULL), _lineCount(0)
  1128. {
  1129. // Vertex shader for drawing colored lines.
  1130. const char* vs_str =
  1131. {
  1132. "uniform mat4 u_viewProjectionMatrix;\n"
  1133. "attribute vec4 a_position;\n"
  1134. "attribute vec4 a_color;\n"
  1135. "varying vec4 v_color;\n"
  1136. "void main(void) {\n"
  1137. " v_color = a_color;\n"
  1138. " gl_Position = u_viewProjectionMatrix * a_position;\n"
  1139. "}"
  1140. };
  1141. // Fragment shader for drawing colored lines.
  1142. const char* fs_str =
  1143. {
  1144. #ifdef OPENGL_ES
  1145. "precision highp float;\n"
  1146. #endif
  1147. "varying vec4 v_color;\n"
  1148. "void main(void) {\n"
  1149. " gl_FragColor = v_color;\n"
  1150. "}"
  1151. };
  1152. Effect* effect = Effect::createFromSource(vs_str, fs_str);
  1153. Material* material = Material::create(effect);
  1154. GP_ASSERT(material && material->getStateBlock());
  1155. material->getStateBlock()->setDepthTest(true);
  1156. material->getStateBlock()->setDepthFunction(RenderState::DEPTH_LEQUAL);
  1157. VertexFormat::Element elements[] =
  1158. {
  1159. VertexFormat::Element(VertexFormat::POSITION, 3),
  1160. VertexFormat::Element(VertexFormat::COLOR, 4),
  1161. };
  1162. _meshBatch = MeshBatch::create(VertexFormat(elements, 2), Mesh::LINES, material, false, 4096, 4096);
  1163. SAFE_RELEASE(material);
  1164. SAFE_RELEASE(effect);
  1165. }
  1166. PhysicsController::DebugDrawer::~DebugDrawer()
  1167. {
  1168. SAFE_DELETE(_meshBatch);
  1169. }
  1170. void PhysicsController::DebugDrawer::begin(const Matrix& viewProjection)
  1171. {
  1172. GP_ASSERT(_meshBatch);
  1173. _meshBatch->start();
  1174. _meshBatch->getMaterial()->getParameter("u_viewProjectionMatrix")->setValue(viewProjection);
  1175. }
  1176. void PhysicsController::DebugDrawer::end()
  1177. {
  1178. GP_ASSERT(_meshBatch && _meshBatch->getMaterial());
  1179. _meshBatch->finish();
  1180. _meshBatch->draw();
  1181. _lineCount = 0;
  1182. }
  1183. void PhysicsController::DebugDrawer::drawLine(const btVector3& from, const btVector3& to, const btVector3& fromColor, const btVector3& toColor)
  1184. {
  1185. GP_ASSERT(_meshBatch);
  1186. static DebugDrawer::DebugVertex vertices[2];
  1187. vertices[0].x = from.getX();
  1188. vertices[0].y = from.getY();
  1189. vertices[0].z = from.getZ();
  1190. vertices[0].r = fromColor.getX();
  1191. vertices[0].g = fromColor.getY();
  1192. vertices[0].b = fromColor.getZ();
  1193. vertices[0].a = 1.0f;
  1194. vertices[1].x = to.getX();
  1195. vertices[1].y = to.getY();
  1196. vertices[1].z = to.getZ();
  1197. vertices[1].r = toColor.getX();
  1198. vertices[1].g = toColor.getY();
  1199. vertices[1].b = toColor.getZ();
  1200. vertices[1].a = 1.0f;
  1201. _meshBatch->add(vertices, 2);
  1202. ++_lineCount;
  1203. if (_lineCount >= 4096)
  1204. {
  1205. // Flush the batch when it gets full (don't want to to grow infinitely)
  1206. end();
  1207. _meshBatch->start();
  1208. }
  1209. }
  1210. void PhysicsController::DebugDrawer::drawLine(const btVector3& from, const btVector3& to, const btVector3& color)
  1211. {
  1212. drawLine(from, to, color, color);
  1213. }
  1214. void PhysicsController::DebugDrawer::drawContactPoint(const btVector3& pointOnB, const btVector3& normalOnB, btScalar distance, int lifeTime, const btVector3& color)
  1215. {
  1216. drawLine(pointOnB, pointOnB + normalOnB, color);
  1217. }
  1218. void PhysicsController::DebugDrawer::reportErrorWarning(const char* warningString)
  1219. {
  1220. GP_WARN(warningString);
  1221. }
  1222. void PhysicsController::DebugDrawer::draw3dText(const btVector3& location, const char* textString)
  1223. {
  1224. GP_WARN("Physics debug drawing: 3D text is not supported.");
  1225. }
  1226. void PhysicsController::DebugDrawer::setDebugMode(int mode)
  1227. {
  1228. _mode = mode;
  1229. }
  1230. int PhysicsController::DebugDrawer::getDebugMode() const
  1231. {
  1232. return _mode;
  1233. }
  1234. PhysicsController::Listener::~Listener()
  1235. {
  1236. GP_ASSERT(Game::getInstance()->getPhysicsController());
  1237. Game::getInstance()->getPhysicsController()->removeStatusListener(this);
  1238. }
  1239. PhysicsController::HitFilter::HitFilter()
  1240. {
  1241. }
  1242. PhysicsController::HitFilter::~HitFilter()
  1243. {
  1244. }
  1245. bool PhysicsController::HitFilter::filter(PhysicsCollisionObject* object)
  1246. {
  1247. return false;
  1248. }
  1249. bool PhysicsController::HitFilter::hit(const PhysicsController::HitResult& result)
  1250. {
  1251. return true;
  1252. }
  1253. }