PhysicsController.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. #include "Base.h"
  2. #include "PhysicsController.h"
  3. #include "PhysicsRigidBody.h"
  4. #include "PhysicsCharacter.h"
  5. #include "PhysicsMotionState.h"
  6. #include "Game.h"
  7. #include "MeshPart.h"
  8. #include "Package.h"
  9. #include "BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h"
  10. // The initial capacity of the Bullet debug drawer's vertex batch.
  11. #define INITIAL_CAPACITY 280
  12. namespace gameplay
  13. {
  14. const int PhysicsController::DIRTY = 0x01;
  15. const int PhysicsController::COLLISION = 0x02;
  16. const int PhysicsController::REGISTERED = 0x04;
  17. const int PhysicsController::REMOVE = 0x08;
  18. PhysicsController::PhysicsController()
  19. : _collisionConfiguration(NULL), _dispatcher(NULL),
  20. _overlappingPairCache(NULL), _solver(NULL), _world(NULL), _ghostPairCallback(NULL),
  21. _debugDrawer(NULL), _status(PhysicsController::Listener::DEACTIVATED), _listeners(NULL),
  22. _gravity(btScalar(0.0), btScalar(-9.8), btScalar(0.0))
  23. {
  24. // Default gravity is 9.8 along the negative Y axis.
  25. }
  26. PhysicsController::~PhysicsController()
  27. {
  28. SAFE_DELETE(_ghostPairCallback);
  29. SAFE_DELETE(_debugDrawer);
  30. SAFE_DELETE(_listeners);
  31. }
  32. void PhysicsController::addStatusListener(Listener* listener)
  33. {
  34. if (!_listeners)
  35. _listeners = new std::vector<Listener*>();
  36. _listeners->push_back(listener);
  37. }
  38. PhysicsFixedConstraint* PhysicsController::createFixedConstraint(PhysicsRigidBody* a, PhysicsRigidBody* b)
  39. {
  40. checkConstraintRigidBodies(a, b);
  41. PhysicsFixedConstraint* constraint = new PhysicsFixedConstraint(a, b);
  42. addConstraint(a, b, constraint);
  43. return constraint;
  44. }
  45. PhysicsGenericConstraint* PhysicsController::createGenericConstraint(PhysicsRigidBody* a, PhysicsRigidBody* b)
  46. {
  47. checkConstraintRigidBodies(a, b);
  48. PhysicsGenericConstraint* constraint = new PhysicsGenericConstraint(a, b);
  49. addConstraint(a, b, constraint);
  50. return constraint;
  51. }
  52. PhysicsGenericConstraint* PhysicsController::createGenericConstraint(PhysicsRigidBody* a,
  53. const Quaternion& rotationOffsetA, const Vector3& translationOffsetA, PhysicsRigidBody* b,
  54. const Quaternion& rotationOffsetB, const Vector3& translationOffsetB)
  55. {
  56. checkConstraintRigidBodies(a, b);
  57. PhysicsGenericConstraint* constraint = new PhysicsGenericConstraint(a, rotationOffsetA, translationOffsetA, b, rotationOffsetB, translationOffsetB);
  58. addConstraint(a, b, constraint);
  59. return constraint;
  60. }
  61. PhysicsHingeConstraint* PhysicsController::createHingeConstraint(PhysicsRigidBody* a,
  62. const Quaternion& rotationOffsetA, const Vector3& translationOffsetA, PhysicsRigidBody* b,
  63. const Quaternion& rotationOffsetB, const Vector3& translationOffsetB)
  64. {
  65. checkConstraintRigidBodies(a, b);
  66. PhysicsHingeConstraint* constraint = new PhysicsHingeConstraint(a, rotationOffsetA, translationOffsetA, b, rotationOffsetB, translationOffsetB);
  67. addConstraint(a, b, constraint);
  68. return constraint;
  69. }
  70. PhysicsSocketConstraint* PhysicsController::createSocketConstraint(PhysicsRigidBody* a, PhysicsRigidBody* b)
  71. {
  72. checkConstraintRigidBodies(a, b);
  73. PhysicsSocketConstraint* constraint = new PhysicsSocketConstraint(a, b);
  74. addConstraint(a, b, constraint);
  75. return constraint;
  76. }
  77. PhysicsSocketConstraint* PhysicsController::createSocketConstraint(PhysicsRigidBody* a,
  78. const Vector3& translationOffsetA, PhysicsRigidBody* b, const Vector3& translationOffsetB)
  79. {
  80. checkConstraintRigidBodies(a, b);
  81. PhysicsSocketConstraint* constraint = new PhysicsSocketConstraint(a,translationOffsetA, b, translationOffsetB);
  82. addConstraint(a, b, constraint);
  83. return constraint;
  84. }
  85. PhysicsSpringConstraint* PhysicsController::createSpringConstraint(PhysicsRigidBody* a, PhysicsRigidBody* b)
  86. {
  87. checkConstraintRigidBodies(a, b);
  88. PhysicsSpringConstraint* constraint = new PhysicsSpringConstraint(a, b);
  89. addConstraint(a, b, constraint);
  90. return constraint;
  91. }
  92. PhysicsSpringConstraint* PhysicsController::createSpringConstraint(PhysicsRigidBody* a, const Quaternion& rotationOffsetA, const Vector3& translationOffsetA,
  93. PhysicsRigidBody* b, const Quaternion& rotationOffsetB, const Vector3& translationOffsetB)
  94. {
  95. checkConstraintRigidBodies(a, b);
  96. PhysicsSpringConstraint* constraint = new PhysicsSpringConstraint(a, rotationOffsetA, translationOffsetA, b, rotationOffsetB, translationOffsetB);
  97. addConstraint(a, b, constraint);
  98. return constraint;
  99. }
  100. const Vector3& PhysicsController::getGravity() const
  101. {
  102. return _gravity;
  103. }
  104. void PhysicsController::setGravity(const Vector3& gravity)
  105. {
  106. _gravity = gravity;
  107. if (_world)
  108. _world->setGravity(BV(_gravity));
  109. }
  110. void PhysicsController::drawDebug(const Matrix& viewProjection)
  111. {
  112. _debugDrawer->begin(viewProjection);
  113. _world->debugDrawWorld();
  114. _debugDrawer->end();
  115. }
  116. PhysicsCollisionObject* PhysicsController::rayTest(const Ray& ray, float distance, Vector3* hitPoint, float* hitFraction)
  117. {
  118. btCollisionWorld::ClosestRayResultCallback callback(BV(ray.getOrigin()), BV(distance * ray.getDirection()));
  119. _world->rayTest(BV(ray.getOrigin()), BV(distance * ray.getDirection()), callback);
  120. if (callback.hasHit())
  121. {
  122. if (hitPoint)
  123. hitPoint->set(callback.m_hitPointWorld.x(), callback.m_hitPointWorld.y(), callback.m_hitPointWorld.z());
  124. if (hitFraction)
  125. *hitFraction = callback.m_closestHitFraction;
  126. return getCollisionObject(callback.m_collisionObject);
  127. }
  128. return NULL;
  129. }
  130. btScalar PhysicsController::addSingleResult(btManifoldPoint& cp, const btCollisionObject* a, int partIdA, int indexA,
  131. const btCollisionObject* b, int partIdB, int indexB)
  132. {
  133. // Get pointers to the PhysicsCollisionObject objects.
  134. PhysicsCollisionObject* rbA = Game::getInstance()->getPhysicsController()->getCollisionObject(a);
  135. PhysicsCollisionObject* rbB = Game::getInstance()->getPhysicsController()->getCollisionObject(b);
  136. // If the given rigid body pair has collided in the past, then
  137. // we notify the listeners only if the pair was not colliding
  138. // during the previous frame. Otherwise, it's a new pair, so add a
  139. // new entry to the cache with the appropriate listeners and notify them.
  140. PhysicsCollisionObject::CollisionPair pair(rbA, rbB);
  141. CollisionInfo* collisionInfo;
  142. if (_collisionStatus.count(pair) > 0)
  143. {
  144. collisionInfo = &_collisionStatus[pair];
  145. }
  146. else
  147. {
  148. // Add a new collision pair for these objects
  149. collisionInfo = &_collisionStatus[pair];
  150. // Add the appropriate listeners.
  151. PhysicsCollisionObject::CollisionPair p1(pair.objectA, NULL);
  152. if (_collisionStatus.count(p1) > 0)
  153. {
  154. const CollisionInfo& ci = _collisionStatus[p1];
  155. std::vector<PhysicsCollisionObject::CollisionListener*>::const_iterator iter = ci._listeners.begin();
  156. for (; iter != ci._listeners.end(); iter++)
  157. {
  158. collisionInfo->_listeners.push_back(*iter);
  159. }
  160. }
  161. PhysicsCollisionObject::CollisionPair p2(pair.objectB, NULL);
  162. if (_collisionStatus.count(p2) > 0)
  163. {
  164. const CollisionInfo& ci = _collisionStatus[p2];
  165. std::vector<PhysicsCollisionObject::CollisionListener*>::const_iterator iter = ci._listeners.begin();
  166. for (; iter != ci._listeners.end(); iter++)
  167. {
  168. collisionInfo->_listeners.push_back(*iter);
  169. }
  170. }
  171. }
  172. // Fire collision event
  173. if ((collisionInfo->_status & COLLISION) == 0)
  174. {
  175. std::vector<PhysicsCollisionObject::CollisionListener*>::const_iterator iter = collisionInfo->_listeners.begin();
  176. for (; iter != collisionInfo->_listeners.end(); iter++)
  177. {
  178. if ((collisionInfo->_status & REMOVE) == 0)
  179. {
  180. (*iter)->collisionEvent(PhysicsCollisionObject::CollisionListener::COLLIDING, pair, Vector3(cp.getPositionWorldOnA().x(), cp.getPositionWorldOnA().y(), cp.getPositionWorldOnA().z()),
  181. Vector3(cp.getPositionWorldOnB().x(), cp.getPositionWorldOnB().y(), cp.getPositionWorldOnB().z()));
  182. }
  183. }
  184. }
  185. // Update the collision status cache (we remove the dirty bit
  186. // set in the controller's update so that this particular collision pair's
  187. // status is not reset to 'no collision' when the controller's update completes).
  188. collisionInfo->_status &= ~DIRTY;
  189. collisionInfo->_status |= COLLISION;
  190. return 0.0f;
  191. }
  192. void PhysicsController::initialize()
  193. {
  194. _collisionConfiguration = new btDefaultCollisionConfiguration();
  195. _dispatcher = new btCollisionDispatcher(_collisionConfiguration);
  196. _overlappingPairCache = new btDbvtBroadphase();
  197. _solver = new btSequentialImpulseConstraintSolver();
  198. // Create the world.
  199. _world = new btDiscreteDynamicsWorld(_dispatcher, _overlappingPairCache, _solver, _collisionConfiguration);
  200. _world->setGravity(BV(_gravity));
  201. // Register ghost pair callback so bullet detects collisions with ghost objects (used for character collisions).
  202. _ghostPairCallback = bullet_new<btGhostPairCallback>();
  203. _world->getPairCache()->setInternalGhostPairCallback(_ghostPairCallback);
  204. // Set up debug drawing.
  205. _debugDrawer = new DebugDrawer();
  206. _world->setDebugDrawer(_debugDrawer);
  207. }
  208. void PhysicsController::finalize()
  209. {
  210. // Clean up the world and its various components.
  211. SAFE_DELETE(_world);
  212. SAFE_DELETE(_ghostPairCallback);
  213. SAFE_DELETE(_solver);
  214. SAFE_DELETE(_overlappingPairCache);
  215. SAFE_DELETE(_dispatcher);
  216. SAFE_DELETE(_collisionConfiguration);
  217. }
  218. void PhysicsController::pause()
  219. {
  220. // Unused
  221. }
  222. void PhysicsController::resume()
  223. {
  224. // Unused
  225. }
  226. void PhysicsController::update(long elapsedTime)
  227. {
  228. // Update the physics simulation, with a maximum
  229. // of 10 simulation steps being performed in a given frame.
  230. //
  231. // Note that stepSimulation takes elapsed time in seconds
  232. // so we divide by 1000 to convert from milliseconds.
  233. _world->stepSimulation((float)elapsedTime * 0.001, 10);
  234. // If we have status listeners, then check if our status has changed.
  235. if (_listeners)
  236. {
  237. Listener::EventType oldStatus = _status;
  238. if (_status = Listener::DEACTIVATED)
  239. {
  240. for (int i = 0; i < _world->getNumCollisionObjects(); i++)
  241. {
  242. if (_world->getCollisionObjectArray()[i]->isActive())
  243. {
  244. _status = Listener::ACTIVATED;
  245. break;
  246. }
  247. }
  248. }
  249. else
  250. {
  251. bool allInactive = true;
  252. for (int i = 0; i < _world->getNumCollisionObjects(); i++)
  253. {
  254. if (_world->getCollisionObjectArray()[i]->isActive())
  255. {
  256. allInactive = false;
  257. break;
  258. }
  259. }
  260. if (allInactive)
  261. _status = Listener::DEACTIVATED;
  262. }
  263. // If the status has changed, notify our listeners.
  264. if (oldStatus != _status)
  265. {
  266. for (unsigned int k = 0; k < _listeners->size(); k++)
  267. {
  268. (*_listeners)[k]->statusEvent(_status);
  269. }
  270. }
  271. }
  272. // All statuses are set with the DIRTY bit before collision processing occurs.
  273. // During collision processing, if a collision occurs, the status is
  274. // set to COLLISION and the DIRTY bit is cleared. Then, after collision processing
  275. // is finished, if a given status is still dirty, the COLLISION bit is cleared.
  276. //
  277. // If an entry was marked for removal in the last frame, remove it now.
  278. // Dirty the collision status cache entries.
  279. std::map<PhysicsCollisionObject::CollisionPair, CollisionInfo>::iterator iter = _collisionStatus.begin();
  280. for (; iter != _collisionStatus.end();)
  281. {
  282. if ((iter->second._status & REMOVE) != 0)
  283. {
  284. std::map<PhysicsCollisionObject::CollisionPair, CollisionInfo>::iterator eraseIter = iter;
  285. iter++;
  286. _collisionStatus.erase(eraseIter);
  287. }
  288. else
  289. {
  290. iter->second._status |= DIRTY;
  291. iter++;
  292. }
  293. }
  294. // Go through the collision status cache and perform all registered collision tests.
  295. iter = _collisionStatus.begin();
  296. for (; iter != _collisionStatus.end(); iter++)
  297. {
  298. // If this collision pair was one that was registered for listening, then perform the collision test.
  299. // (In the case where we register for all collisions with a rigid body, there will be a lot
  300. // of collision pairs in the status cache that we did not explicitly register for.)
  301. if ((iter->second._status & REGISTERED) != 0 && (iter->second._status & REMOVE) == 0)
  302. {
  303. if (iter->first.objectB)
  304. _world->contactPairTest(iter->first.objectA->getCollisionObject(), iter->first.objectB->getCollisionObject(), *this);
  305. else
  306. _world->contactTest(iter->first.objectA->getCollisionObject(), *this);
  307. }
  308. }
  309. // Update all the collision status cache entries.
  310. iter = _collisionStatus.begin();
  311. for (; iter != _collisionStatus.end(); iter++)
  312. {
  313. if ((iter->second._status & DIRTY) != 0)
  314. {
  315. if ((iter->second._status & COLLISION) != 0 && iter->first.objectB)
  316. {
  317. unsigned int size = iter->second._listeners.size();
  318. for (unsigned int i = 0; i < size; i++)
  319. {
  320. iter->second._listeners[i]->collisionEvent(PhysicsCollisionObject::CollisionListener::NOT_COLLIDING, iter->first);
  321. }
  322. }
  323. iter->second._status &= ~COLLISION;
  324. }
  325. }
  326. }
  327. void PhysicsController::addCollisionListener(PhysicsCollisionObject::CollisionListener* listener, PhysicsCollisionObject* objectA, PhysicsCollisionObject* objectB)
  328. {
  329. PhysicsCollisionObject::CollisionPair pair(objectA, objectB);
  330. // Add the listener and ensure the status includes that this collision pair is registered.
  331. CollisionInfo& info = _collisionStatus[pair];
  332. info._listeners.push_back(listener);
  333. info._status |= PhysicsController::REGISTERED;
  334. }
  335. void PhysicsController::removeCollisionListener(PhysicsCollisionObject::CollisionListener* listener, PhysicsCollisionObject* objectA, PhysicsCollisionObject* objectB)
  336. {
  337. // Mark the collision pair for these objects for removal
  338. PhysicsCollisionObject::CollisionPair pair(objectA, objectB);
  339. if (_collisionStatus.count(pair) > 0)
  340. {
  341. _collisionStatus[pair]._status |= REMOVE;
  342. }
  343. }
  344. void PhysicsController::addCollisionObject(PhysicsCollisionObject* object)
  345. {
  346. // Assign user pointer for the bullet collision object to allow efficient
  347. // lookups of bullet objects -> gameplay objects.
  348. object->getCollisionObject()->setUserPointer(object);
  349. // Add the object to the physics world
  350. switch (object->getType())
  351. {
  352. case PhysicsCollisionObject::RIGID_BODY:
  353. _world->addRigidBody(static_cast<btRigidBody*>(object->getCollisionObject()));//, btBroadphaseProxy::DefaultFilter, btBroadphaseProxy::StaticFilter | btBroadphaseProxy::CharacterFilter | btBroadphaseProxy::AllFilter);
  354. break;
  355. case PhysicsCollisionObject::CHARACTER:
  356. _world->addCollisionObject(object->getCollisionObject());//, btBroadphaseProxy::DefaultFilter, btBroadphaseProxy::StaticFilter | btBroadphaseProxy::CharacterFilter | btBroadphaseProxy::AllFilter);//, btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::CharacterFilter );// | btBroadphaseProxy::CharacterFilter | btBroadphaseProxy::DefaultFilter*/ 0);
  357. break;
  358. case PhysicsCollisionObject::GHOST_OBJECT:
  359. _world->addCollisionObject(object->getCollisionObject());//, btBroadphaseProxy::DefaultFilter, btBroadphaseProxy::StaticFilter | btBroadphaseProxy::CharacterFilter | btBroadphaseProxy::AllFilter);//, btBroadphaseProxy::DefaultFilter, btBroadphaseProxy::DefaultFilter | btBroadphaseProxy::StaticFilter | btBroadphaseProxy::CharacterFilter);
  360. break;
  361. default:
  362. assert(0); // unexpected (new type?)
  363. break;
  364. }
  365. }
  366. void PhysicsController::removeCollisionObject(PhysicsCollisionObject* object)
  367. {
  368. // Remove the collision object from the world
  369. if (object->getCollisionObject())
  370. {
  371. switch (object->getType())
  372. {
  373. case PhysicsCollisionObject::RIGID_BODY:
  374. _world->removeRigidBody(static_cast<btRigidBody*>(object->getCollisionObject()));
  375. break;
  376. case PhysicsCollisionObject::CHARACTER:
  377. case PhysicsCollisionObject::GHOST_OBJECT:
  378. _world->removeCollisionObject(object->getCollisionObject());
  379. break;
  380. default:
  381. assert(0); // unexpected (new type?)
  382. break;
  383. }
  384. }
  385. // Find all references to the object in the collision status cache and mark them for removal.
  386. std::map<PhysicsCollisionObject::CollisionPair, CollisionInfo>::iterator iter = _collisionStatus.begin();
  387. for (; iter != _collisionStatus.end(); iter++)
  388. {
  389. if (iter->first.objectA == object || iter->first.objectB == object)
  390. iter->second._status |= REMOVE;
  391. }
  392. }
  393. PhysicsCollisionObject* PhysicsController::getCollisionObject(const btCollisionObject* collisionObject) const
  394. {
  395. // Gameplay rigid bodies are stored in the userPointer data of bullet collision objects
  396. return reinterpret_cast<PhysicsCollisionObject*>(collisionObject->getUserPointer());
  397. }
  398. void getBoundingBox(Node* node, BoundingBox* out, bool merge = false)
  399. {
  400. if (node->getModel())
  401. {
  402. if (merge)
  403. out->merge(node->getModel()->getMesh()->getBoundingBox());
  404. else
  405. {
  406. out->set(node->getModel()->getMesh()->getBoundingBox());
  407. merge = true;
  408. }
  409. }
  410. Node* child = node->getFirstChild();
  411. while (child)
  412. {
  413. getBoundingBox(child, out, merge);
  414. child = child->getNextSibling();
  415. }
  416. }
  417. void getBoundingSphere(Node* node, BoundingSphere* out, bool merge = false)
  418. {
  419. if (node->getModel())
  420. {
  421. if (merge)
  422. out->merge(node->getModel()->getMesh()->getBoundingSphere());
  423. else
  424. {
  425. out->set(node->getModel()->getMesh()->getBoundingSphere());
  426. merge = true;
  427. }
  428. }
  429. Node* child = node->getFirstChild();
  430. while (child)
  431. {
  432. getBoundingSphere(child, out, merge);
  433. child = child->getNextSibling();
  434. }
  435. }
  436. void computeCenterOfMass(const Vector3& center, const Vector3& scale, Vector3* centerOfMassOffset)
  437. {
  438. // Update center of mass offset
  439. *centerOfMassOffset = center;
  440. centerOfMassOffset->x *= scale.x;
  441. centerOfMassOffset->y *= scale.y;
  442. centerOfMassOffset->z *= scale.z;
  443. centerOfMassOffset->negate();
  444. }
  445. PhysicsCollisionShape* PhysicsController::createShape(Node* node, const PhysicsCollisionShape::Definition& shape, Vector3* centerOfMassOffset)
  446. {
  447. PhysicsCollisionShape* collisionShape = NULL;
  448. // Get the node's world scale (we need to apply this during creation since rigid bodies don't scale dynamically).
  449. Vector3 scale;
  450. node->getWorldMatrix().getScale(&scale);
  451. switch (shape.type)
  452. {
  453. case PhysicsCollisionShape::SHAPE_BOX:
  454. {
  455. if (shape.isExplicit)
  456. {
  457. // Use the passed in box information
  458. collisionShape = createBox(shape.data.boxExtents, Vector3::one());
  459. if (shape.centerAbsolute)
  460. {
  461. computeCenterOfMass(shape.data.boxCenter, scale, centerOfMassOffset);
  462. }
  463. else
  464. {
  465. BoundingBox box;
  466. getBoundingBox(node, &box);
  467. computeCenterOfMass(box.getCenter() + shape.data.boxCenter, scale, centerOfMassOffset);
  468. }
  469. }
  470. else
  471. {
  472. // Automatically compute bounding box from mesh's bounding box
  473. BoundingBox box;
  474. getBoundingBox(node, &box);
  475. collisionShape = createBox(Vector3(std::abs(box.max.x - box.min.x), std::abs(box.max.y - box.min.y), std::abs(box.max.z - box.min.z)), scale);
  476. computeCenterOfMass(box.getCenter(), scale, centerOfMassOffset);
  477. }
  478. }
  479. break;
  480. case PhysicsCollisionShape::SHAPE_SPHERE:
  481. {
  482. if (shape.isExplicit)
  483. {
  484. // Use the passed in sphere information
  485. collisionShape = createSphere(shape.data.sphereRadius, Vector3::one());
  486. if (shape.centerAbsolute)
  487. {
  488. computeCenterOfMass(shape.data.sphereCenter, scale, centerOfMassOffset);
  489. }
  490. else
  491. {
  492. BoundingSphere sphere;
  493. getBoundingSphere(node, &sphere);
  494. computeCenterOfMass(sphere.center + shape.data.sphereCenter, scale, centerOfMassOffset);
  495. }
  496. }
  497. else
  498. {
  499. // Automatically compute bounding sphere from mesh's bounding sphere
  500. BoundingSphere sphere;
  501. getBoundingSphere(node, &sphere);
  502. collisionShape = createSphere(sphere.radius, scale);
  503. computeCenterOfMass(sphere.center, scale, centerOfMassOffset);
  504. }
  505. }
  506. break;
  507. case PhysicsCollisionShape::SHAPE_CAPSULE:
  508. {
  509. if (shape.isExplicit)
  510. {
  511. // Use the passed in capsule information
  512. collisionShape = createCapsule(shape.data.capsuleRadius, shape.data.capsuleHeight, Vector3::one());
  513. if (shape.centerAbsolute)
  514. {
  515. computeCenterOfMass(shape.data.capsuleCenter, scale, centerOfMassOffset);
  516. }
  517. else
  518. {
  519. BoundingBox box;
  520. getBoundingBox(node, &box);
  521. computeCenterOfMass(box.getCenter() + shape.data.capsuleCenter, scale, centerOfMassOffset);
  522. }
  523. }
  524. else
  525. {
  526. // Compute a capsule shape that roughly matches the bounding box of the mesh
  527. BoundingBox box;
  528. getBoundingBox(node, &box);
  529. float radius = std::max((box.max.x - box.min.x) * 0.5f, (box.max.z - box.min.z) * 0.5f);
  530. float height = (box.max.y - box.min.y) - radius * 2.0f;
  531. collisionShape = createCapsule(radius, height, scale);
  532. computeCenterOfMass(box.getCenter(), scale, centerOfMassOffset);
  533. }
  534. }
  535. break;
  536. case PhysicsCollisionShape::SHAPE_HEIGHTFIELD:
  537. {
  538. // Build heightfield rigid body from the passed in shape
  539. collisionShape = createHeightfield(node, shape.data.heightfield, centerOfMassOffset);
  540. }
  541. break;
  542. case PhysicsCollisionShape::SHAPE_MESH:
  543. {
  544. // Build mesh from passed in shape
  545. collisionShape = createMesh(shape.data.mesh, scale);
  546. }
  547. break;
  548. }
  549. return collisionShape;
  550. }
  551. PhysicsCollisionShape* PhysicsController::createBox(const Vector3& extents, const Vector3& scale)
  552. {
  553. btVector3 halfExtents(scale.x * 0.5 * extents.x, scale.y * 0.5 * extents.y, scale.z * 0.5 * extents.z);
  554. PhysicsCollisionShape* shape;
  555. // Return the box shape from the cache if it already exists.
  556. for (unsigned int i = 0; i < _shapes.size(); ++i)
  557. {
  558. shape = _shapes[i];
  559. if (shape->getType() == PhysicsCollisionShape::SHAPE_BOX)
  560. {
  561. btBoxShape* box = static_cast<btBoxShape*>(shape->_shape);
  562. if (box->getHalfExtentsWithMargin() == halfExtents)
  563. {
  564. shape->addRef();
  565. return shape;
  566. }
  567. }
  568. }
  569. // Create the box shape and add it to the cache.
  570. shape = new PhysicsCollisionShape(PhysicsCollisionShape::SHAPE_BOX, bullet_new<btBoxShape>(halfExtents));
  571. _shapes.push_back(shape);
  572. return shape;
  573. }
  574. PhysicsCollisionShape* PhysicsController::createSphere(float radius, const Vector3& scale)
  575. {
  576. // Since sphere shapes depend only on the radius, the best we can do is take
  577. // the largest dimension and apply that as the uniform scale to the rigid body.
  578. float uniformScale = scale.x;
  579. if (uniformScale < scale.y)
  580. uniformScale = scale.y;
  581. if (uniformScale < scale.z)
  582. uniformScale = scale.z;
  583. float scaledRadius = radius * uniformScale;
  584. PhysicsCollisionShape* shape;
  585. // Return the sphere shape from the cache if it already exists.
  586. for (unsigned int i = 0; i < _shapes.size(); ++i)
  587. {
  588. shape = _shapes[i];
  589. if (shape->getType() == PhysicsCollisionShape::SHAPE_SPHERE)
  590. {
  591. btSphereShape* sphere = static_cast<btSphereShape*>(shape->_shape);
  592. if (sphere->getRadius() == scaledRadius)
  593. {
  594. shape->addRef();
  595. return shape;
  596. }
  597. }
  598. }
  599. // Create the sphere shape and add it to the cache.
  600. shape = new PhysicsCollisionShape(PhysicsCollisionShape::SHAPE_SPHERE, bullet_new<btSphereShape>(scaledRadius));
  601. _shapes.push_back(shape);
  602. return shape;
  603. }
  604. PhysicsCollisionShape* PhysicsController::createCapsule(float radius, float height, const Vector3& scale)
  605. {
  606. float girthScale = scale.x;
  607. if (girthScale < scale.z)
  608. girthScale = scale.z;
  609. float scaledRadius = radius * girthScale;
  610. float scaledHeight = height * scale.y;
  611. PhysicsCollisionShape* shape;
  612. // Return the capsule shape from the cache if it already exists.
  613. for (unsigned int i = 0; i < _shapes.size(); i++)
  614. {
  615. shape = _shapes[i];
  616. if (shape->getType() == PhysicsCollisionShape::SHAPE_CAPSULE)
  617. {
  618. btCapsuleShape* capsule = static_cast<btCapsuleShape*>(shape->_shape);
  619. if (capsule->getRadius() == scaledRadius && capsule->getHalfHeight() == 0.5f * scaledHeight)
  620. {
  621. shape->addRef();
  622. return shape;
  623. }
  624. }
  625. }
  626. // Create the capsule shape and add it to the cache.
  627. shape = new PhysicsCollisionShape(PhysicsCollisionShape::SHAPE_CAPSULE, bullet_new<btCapsuleShape>(scaledRadius, scaledHeight));
  628. _shapes.push_back(shape);
  629. return shape;
  630. }
  631. PhysicsCollisionShape* PhysicsController::createHeightfield(Node* node, Image* image, Vector3* centerOfMassOffset)
  632. {
  633. // Get the dimensions of the heightfield.
  634. // If the node has a mesh defined, use the dimensions of the bounding box for the mesh.
  635. // Otherwise simply use the image dimensions (with a max height of 255).
  636. float width, length, minHeight, maxHeight;
  637. if (node->getModel() && node->getModel()->getMesh())
  638. {
  639. const BoundingBox& box = node->getModel()->getMesh()->getBoundingBox();
  640. width = box.max.x - box.min.x;
  641. length = box.max.z - box.min.z;
  642. minHeight = box.min.y;
  643. maxHeight = box.max.y;
  644. }
  645. else
  646. {
  647. width = image->getWidth();
  648. length = image->getHeight();
  649. minHeight = 0.0f;
  650. maxHeight = 255.0f;
  651. }
  652. // Get the size in bytes of a pixel (we ensure that the image's
  653. // pixel format is actually supported before calling this constructor).
  654. unsigned int pixelSize = 0;
  655. switch (image->getFormat())
  656. {
  657. case Image::RGB:
  658. pixelSize = 3;
  659. break;
  660. case Image::RGBA:
  661. pixelSize = 4;
  662. break;
  663. default:
  664. LOG_ERROR("Unsupported pixel format for heightmap image.");
  665. return NULL;
  666. }
  667. // Calculate the heights for each pixel.
  668. float* heights = new float[image->getWidth() * image->getHeight()];
  669. unsigned char* data = image->getData();
  670. for (unsigned int x = 0, w = image->getWidth(); x < w; ++x)
  671. {
  672. for (unsigned int y = 0, h = image->getHeight(); y < h; ++y)
  673. {
  674. heights[x + y * w] = ((((float)data[(x + y * h) * pixelSize + 0]) +
  675. ((float)data[(x + y * h) * pixelSize + 1]) +
  676. ((float)data[(x + y * h) * pixelSize + 2])) / 768.0f) * (maxHeight - minHeight) + minHeight;
  677. }
  678. }
  679. PhysicsCollisionShape::HeightfieldData* heightfieldData = new PhysicsCollisionShape::HeightfieldData();
  680. heightfieldData->heightData = NULL;
  681. heightfieldData->inverseIsDirty = true;
  682. // Generate the heightmap data needed for physics (one height per world unit).
  683. unsigned int sizeWidth = width;
  684. unsigned int sizeHeight = length;
  685. heightfieldData->width = sizeWidth + 1;
  686. heightfieldData->height = sizeHeight + 1;
  687. heightfieldData->heightData = new float[heightfieldData->width * heightfieldData->height];
  688. unsigned int heightIndex = 0;
  689. float widthImageFactor = (float)(image->getWidth() - 1) / sizeWidth;
  690. float heightImageFactor = (float)(image->getHeight() - 1) / sizeHeight;
  691. float x = 0.0f;
  692. float z = 0.0f;
  693. for (unsigned int row = 0, z = 0.0f; row <= sizeHeight; row++, z += 1.0f)
  694. {
  695. for (unsigned int col = 0, x = 0.0f; col <= sizeWidth; col++, x += 1.0f)
  696. {
  697. heightIndex = row * heightfieldData->width + col;
  698. heightfieldData->heightData[heightIndex] = calculateHeight(heights, image->getWidth(), image->getHeight(), x * widthImageFactor, (sizeHeight - z) * heightImageFactor);
  699. }
  700. }
  701. SAFE_DELETE_ARRAY(heights);
  702. // Offset the heightmap's center of mass according to the way that Bullet calculates the origin
  703. // of its heightfield collision shape; see documentation for the btHeightfieldTerrainShape for more info.
  704. Vector3 s;
  705. node->getWorldMatrix().getScale(&s);
  706. centerOfMassOffset->set(0.0f, -(maxHeight - (0.5f * (maxHeight - minHeight))) / s.y, 0.0f);
  707. // Create the bullet terrain shape
  708. btHeightfieldTerrainShape* terrainShape = bullet_new<btHeightfieldTerrainShape>(
  709. heightfieldData->width, heightfieldData->height, heightfieldData->heightData, 1.0f, minHeight, maxHeight, 1, PHY_FLOAT, false);
  710. // Create our collision shape object and store heightfieldData in it
  711. PhysicsCollisionShape* shape = new PhysicsCollisionShape(PhysicsCollisionShape::SHAPE_HEIGHTFIELD, terrainShape);
  712. shape->_shapeData.heightfieldData = heightfieldData;
  713. _shapes.push_back(shape);
  714. return shape;
  715. }
  716. PhysicsCollisionShape* PhysicsController::createMesh(Mesh* mesh, const Vector3& scale)
  717. {
  718. assert(mesh);
  719. // Only support meshes with triangle list primitive types
  720. bool triMesh = true;
  721. if (mesh->getPartCount() > 0)
  722. {
  723. for (unsigned int i = 0; i < mesh->getPartCount(); ++i)
  724. {
  725. if (mesh->getPart(i)->getPrimitiveType() != Mesh::TRIANGLES)
  726. {
  727. triMesh = false;
  728. break;
  729. }
  730. }
  731. }
  732. else
  733. {
  734. triMesh = mesh->getPrimitiveType() == Mesh::TRIANGLES;
  735. }
  736. if (!triMesh)
  737. {
  738. LOG_ERROR("Mesh rigid bodies are currently only supported on meshes with TRIANGLES primitive type.");
  739. return NULL;
  740. }
  741. // The mesh must have a valid URL (i.e. it must have been loaded from a Package)
  742. // in order to fetch mesh data for computing mesh rigid body.
  743. if (strlen(mesh->getUrl()) == 0)
  744. {
  745. LOG_ERROR("Cannot create mesh rigid body for mesh without valid URL.");
  746. return NULL;
  747. }
  748. Package::MeshData* data = Package::readMeshData(mesh->getUrl());
  749. if (data == NULL)
  750. {
  751. return NULL;
  752. }
  753. // Create mesh data to be populated and store in returned collision shape
  754. PhysicsCollisionShape::MeshData* shapeMeshData = new PhysicsCollisionShape::MeshData();
  755. shapeMeshData->vertexData = NULL;
  756. // Copy the scaled vertex position data to the rigid body's local buffer.
  757. Matrix m;
  758. Matrix::createScale(scale, &m);
  759. unsigned int vertexCount = data->vertexCount;
  760. shapeMeshData->vertexData = new float[vertexCount * 3];
  761. Vector3 v;
  762. int vertexStride = data->vertexFormat.getVertexSize();
  763. for (unsigned int i = 0; i < data->vertexCount; i++)
  764. {
  765. v.set(*((float*)&data->vertexData[i * vertexStride + 0 * sizeof(float)]),
  766. *((float*)&data->vertexData[i * vertexStride + 1 * sizeof(float)]),
  767. *((float*)&data->vertexData[i * vertexStride + 2 * sizeof(float)]));
  768. v *= m;
  769. memcpy(&(shapeMeshData->vertexData[i * 3]), &v, sizeof(float) * 3);
  770. }
  771. btTriangleIndexVertexArray* meshInterface = bullet_new<btTriangleIndexVertexArray>();
  772. unsigned int partCount = data->parts.size();
  773. if (partCount > 0)
  774. {
  775. PHY_ScalarType indexType = PHY_UCHAR;
  776. int indexStride = 0;
  777. Package::MeshPartData* meshPart = NULL;
  778. for (unsigned int i = 0; i < partCount; i++)
  779. {
  780. meshPart = data->parts[i];
  781. switch (meshPart->indexFormat)
  782. {
  783. case Mesh::INDEX8:
  784. indexType = PHY_UCHAR;
  785. indexStride = 1;
  786. break;
  787. case Mesh::INDEX16:
  788. indexType = PHY_SHORT;
  789. indexStride = 2;
  790. break;
  791. case Mesh::INDEX32:
  792. indexType = PHY_INTEGER;
  793. indexStride = 4;
  794. break;
  795. }
  796. // Move the index data into the rigid body's local buffer.
  797. // Set it to NULL in the MeshPartData so it is not released when the data is freed.
  798. shapeMeshData->indexData.push_back(meshPart->indexData);
  799. meshPart->indexData = NULL;
  800. // Create a btIndexedMesh object for the current mesh part.
  801. btIndexedMesh indexedMesh;
  802. indexedMesh.m_indexType = indexType;
  803. indexedMesh.m_numTriangles = meshPart->indexCount / 3; // assume TRIANGLES primitive type
  804. indexedMesh.m_numVertices = meshPart->indexCount;
  805. indexedMesh.m_triangleIndexBase = (const unsigned char*)shapeMeshData->indexData[i];
  806. indexedMesh.m_triangleIndexStride = indexStride*3;
  807. indexedMesh.m_vertexBase = (const unsigned char*)shapeMeshData->vertexData;
  808. indexedMesh.m_vertexStride = sizeof(float)*3;
  809. indexedMesh.m_vertexType = PHY_FLOAT;
  810. // Add the indexed mesh data to the mesh interface.
  811. meshInterface->addIndexedMesh(indexedMesh, indexType);
  812. }
  813. }
  814. else
  815. {
  816. // Generate index data for the mesh locally in the rigid body.
  817. unsigned int* indexData = new unsigned int[data->vertexCount];
  818. for (unsigned int i = 0; i < data->vertexCount; i++)
  819. {
  820. indexData[i] = i;
  821. }
  822. shapeMeshData->indexData.push_back((unsigned char*)indexData);
  823. // Create a single btIndexedMesh object for the mesh interface.
  824. btIndexedMesh indexedMesh;
  825. indexedMesh.m_indexType = PHY_INTEGER;
  826. indexedMesh.m_numTriangles = data->vertexCount / 3; // assume TRIANGLES primitive type
  827. indexedMesh.m_numVertices = data->vertexCount;
  828. indexedMesh.m_triangleIndexBase = shapeMeshData->indexData[0];
  829. indexedMesh.m_triangleIndexStride = sizeof(unsigned int);
  830. indexedMesh.m_vertexBase = (const unsigned char*)shapeMeshData->vertexData;
  831. indexedMesh.m_vertexStride = sizeof(float)*3;
  832. indexedMesh.m_vertexType = PHY_FLOAT;
  833. // Set the data in the mesh interface.
  834. meshInterface->addIndexedMesh(indexedMesh, indexedMesh.m_indexType);
  835. }
  836. // Create our collision shape object and store shapeMeshData in it
  837. PhysicsCollisionShape* shape = new PhysicsCollisionShape(PhysicsCollisionShape::SHAPE_MESH, bullet_new<btBvhTriangleMeshShape>(meshInterface, true));
  838. shape->_shapeData.meshData = shapeMeshData;
  839. _shapes.push_back(shape);
  840. // Free the temporary mesh data now that it's stored in physics system
  841. SAFE_DELETE(data);
  842. return shape;
  843. }
  844. void PhysicsController::destroyShape(PhysicsCollisionShape* shape)
  845. {
  846. if (shape)
  847. {
  848. if (shape->getRefCount() == 1)
  849. {
  850. // Remove shape from shape cache
  851. std::vector<PhysicsCollisionShape*>::iterator shapeItr = std::find(_shapes.begin(), _shapes.end(), shape);
  852. if (shapeItr != _shapes.end())
  853. _shapes.erase(shapeItr);
  854. }
  855. // Release the shape
  856. shape->release();
  857. }
  858. }
  859. float PhysicsController::calculateHeight(float* data, unsigned int width, unsigned int height, float x, float y)
  860. {
  861. unsigned int x1 = x;
  862. unsigned int y1 = y;
  863. unsigned int x2 = x1 + 1;
  864. unsigned int y2 = y1 + 1;
  865. float tmp;
  866. float xFactor = modf(x, &tmp);
  867. float yFactor = modf(y, &tmp);
  868. float xFactorI = 1.0f - xFactor;
  869. float yFactorI = 1.0f - yFactor;
  870. if (x2 >= width && y2 >= height)
  871. {
  872. return data[x1 + y1 * width];
  873. }
  874. else if (x2 >= width)
  875. {
  876. return data[x1 + y1 * width] * yFactorI + data[x1 + y2 * width] * yFactor;
  877. }
  878. else if (y2 >= height)
  879. {
  880. return data[x1 + y1 * width] * xFactorI + data[x2 + y1 * width] * xFactor;
  881. }
  882. else
  883. {
  884. return data[x1 + y1 * width] * xFactorI * yFactorI + data[x1 + y2 * width] * xFactorI * yFactor +
  885. data[x2 + y2 * width] * xFactor * yFactor + data[x2 + y1 * width] * xFactor * yFactorI;
  886. }
  887. }
  888. void PhysicsController::addConstraint(PhysicsRigidBody* a, PhysicsRigidBody* b, PhysicsConstraint* constraint)
  889. {
  890. a->addConstraint(constraint);
  891. if (b)
  892. {
  893. b->addConstraint(constraint);
  894. }
  895. _world->addConstraint(constraint->_constraint);
  896. }
  897. bool PhysicsController::checkConstraintRigidBodies(PhysicsRigidBody* a, PhysicsRigidBody* b)
  898. {
  899. if (!a->supportsConstraints())
  900. {
  901. WARN_VARG("Rigid body '%s' does not support constraints; unexpected behavior may occur.", a->_node->getId());
  902. return false;
  903. }
  904. if (b && !b->supportsConstraints())
  905. {
  906. WARN_VARG("Rigid body '%s' does not support constraints; unexpected behavior may occur.", b->_node->getId());
  907. return false;
  908. }
  909. return true;
  910. }
  911. void PhysicsController::removeConstraint(PhysicsConstraint* constraint)
  912. {
  913. // Find the constraint and remove it from the physics world.
  914. for (int i = _world->getNumConstraints() - 1; i >= 0; i--)
  915. {
  916. btTypedConstraint* currentConstraint = _world->getConstraint(i);
  917. if (constraint->_constraint == currentConstraint)
  918. {
  919. _world->removeConstraint(currentConstraint);
  920. break;
  921. }
  922. }
  923. }
  924. PhysicsController::DebugDrawer::DebugDrawer()
  925. : _mode(btIDebugDraw::DBG_DrawAabb | btIDebugDraw::DBG_DrawConstraintLimits | btIDebugDraw::DBG_DrawConstraints |
  926. btIDebugDraw::DBG_DrawContactPoints | btIDebugDraw::DBG_DrawWireframe), _viewProjection(NULL), _meshBatch(NULL)
  927. {
  928. // Vertex shader for drawing colored lines.
  929. const char* vs_str =
  930. {
  931. "uniform mat4 u_viewProjectionMatrix;\n"
  932. "attribute vec4 a_position;\n"
  933. "attribute vec4 a_color;\n"
  934. "varying vec4 v_color;\n"
  935. "void main(void) {\n"
  936. " v_color = a_color;\n"
  937. " gl_Position = u_viewProjectionMatrix * a_position;\n"
  938. "}"
  939. };
  940. // Fragment shader for drawing colored lines.
  941. const char* fs_str =
  942. {
  943. #ifdef OPENGL_ES
  944. "precision highp float;\n"
  945. #endif
  946. "varying vec4 v_color;\n"
  947. "void main(void) {\n"
  948. " gl_FragColor = v_color;\n"
  949. "}"
  950. };
  951. Effect* effect = Effect::createFromSource(vs_str, fs_str);
  952. Material* material = Material::create(effect);
  953. material->getStateBlock()->setDepthTest(true);
  954. VertexFormat::Element elements[] =
  955. {
  956. VertexFormat::Element(VertexFormat::POSITION, 3),
  957. VertexFormat::Element(VertexFormat::COLOR, 4),
  958. };
  959. _meshBatch = MeshBatch::create(VertexFormat(elements, 2), Mesh::LINES, material, false);
  960. SAFE_RELEASE(material);
  961. SAFE_RELEASE(effect);
  962. }
  963. PhysicsController::DebugDrawer::~DebugDrawer()
  964. {
  965. SAFE_DELETE(_meshBatch);
  966. }
  967. void PhysicsController::DebugDrawer::begin(const Matrix& viewProjection)
  968. {
  969. _viewProjection = &viewProjection;
  970. _meshBatch->begin();
  971. }
  972. void PhysicsController::DebugDrawer::end()
  973. {
  974. _meshBatch->end();
  975. _meshBatch->getMaterial()->getParameter("u_viewProjectionMatrix")->setValue(_viewProjection);
  976. _meshBatch->draw();
  977. }
  978. void PhysicsController::DebugDrawer::drawLine(const btVector3& from, const btVector3& to, const btVector3& fromColor, const btVector3& toColor)
  979. {
  980. static DebugDrawer::DebugVertex fromVertex, toVertex;
  981. fromVertex.x = from.getX();
  982. fromVertex.y = from.getY();
  983. fromVertex.z = from.getZ();
  984. fromVertex.r = fromColor.getX();
  985. fromVertex.g = fromColor.getY();
  986. fromVertex.b = fromColor.getZ();
  987. fromVertex.a = 1.0f;
  988. toVertex.x = to.getX();
  989. toVertex.y = to.getY();
  990. toVertex.z = to.getZ();
  991. toVertex.r = toColor.getX();
  992. toVertex.g = toColor.getY();
  993. toVertex.b = toColor.getZ();
  994. toVertex.a = 1.0f;
  995. _meshBatch->add(&fromVertex, 1);
  996. _meshBatch->add(&toVertex, 1);
  997. }
  998. void PhysicsController::DebugDrawer::drawLine(const btVector3& from, const btVector3& to, const btVector3& color)
  999. {
  1000. drawLine(from, to, color, color);
  1001. }
  1002. void PhysicsController::DebugDrawer::drawContactPoint(const btVector3& pointOnB, const btVector3& normalOnB, btScalar distance, int lifeTime, const btVector3& color)
  1003. {
  1004. drawLine(pointOnB, pointOnB + normalOnB, color);
  1005. }
  1006. void PhysicsController::DebugDrawer::reportErrorWarning(const char* warningString)
  1007. {
  1008. WARN(warningString);
  1009. }
  1010. void PhysicsController::DebugDrawer::draw3dText(const btVector3& location, const char* textString)
  1011. {
  1012. WARN("Physics debug drawing: 3D text is not supported.");
  1013. }
  1014. void PhysicsController::DebugDrawer::setDebugMode(int mode)
  1015. {
  1016. _mode = mode;
  1017. }
  1018. int PhysicsController::DebugDrawer::getDebugMode() const
  1019. {
  1020. return _mode;
  1021. }
  1022. }