PolyPhysicsScreen.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyPhysicsScreen.h"
  20. #include "PolyPhysicsScreenEntity.h"
  21. #include "PolyCoreServices.h"
  22. #include "PolyCore.h"
  23. using namespace Polycode;
  24. PhysicsScene2DEvent::PhysicsScene2DEvent() : Event() {
  25. }
  26. PhysicsScene2DEvent::~PhysicsScene2DEvent() {
  27. }
  28. Entity *PhysicsScene2DEvent::getFirstEntity() {
  29. return entity1;
  30. }
  31. Entity *PhysicsScene2DEvent::getSecondEntity() {
  32. return entity2;
  33. }
  34. void PhysicsScene2D::PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
  35. {
  36. if(((PhysicsScene2DEntity*)contact->GetFixtureA()->GetBody()->GetUserData())->collisionOnly ||
  37. ((PhysicsScene2DEntity*)contact->GetFixtureB()->GetBody()->GetUserData())->collisionOnly) {
  38. contact->SetEnabled(false);
  39. }
  40. }
  41. void PhysicsScene2D::BeginContact (b2Contact *contact) {
  42. // if(!contact->GetFixtureA()->IsSensor() && !contact->GetFixtureB()->IsSensor()) {
  43. // return;
  44. // }
  45. PhysicsScene2DEvent *newEvent = new PhysicsScene2DEvent();
  46. newEvent->entity1 = ((PhysicsScene2DEntity*)contact->GetFixtureA()->GetBody()->GetUserData())->getEntity();
  47. newEvent->entity2 = ((PhysicsScene2DEntity*)contact->GetFixtureB()->GetBody()->GetUserData())->getEntity();
  48. if(((PhysicsScene2DEntity*)contact->GetFixtureA()->GetBody()->GetUserData())->collisionOnly ||
  49. ((PhysicsScene2DEntity*)contact->GetFixtureB()->GetBody()->GetUserData())->collisionOnly) {
  50. contact->SetEnabled(false);
  51. }
  52. b2Manifold *manifold = contact->GetManifold();
  53. b2Vec2 nor = manifold->localNormal;
  54. b2Vec2 point = manifold->localPoint;
  55. b2WorldManifold w_manifold;
  56. contact->GetWorldManifold(&w_manifold);
  57. b2Vec2 w_nor = w_manifold.normal;
  58. newEvent->localCollisionNormal.x = nor.x;
  59. newEvent->localCollisionNormal.y = nor.y;
  60. newEvent->worldCollisionNormal.x = w_nor.x;
  61. newEvent->worldCollisionNormal.y = w_nor.y;
  62. newEvent->localCollisionPoint.x = point.x * worldScale;
  63. newEvent->localCollisionPoint.y = point.y * worldScale;
  64. newEvent->worldCollisionPoint.x = w_manifold.points[0].x * worldScale;
  65. newEvent->worldCollisionPoint.y = w_manifold.points[0].y * worldScale;
  66. newEvent->impactStrength = 0;
  67. newEvent->frictionStrength = 0;
  68. newEvent->contact = contact;
  69. newEvent->setEventCode(PhysicsScene2DEvent::EVENT_NEW_SHAPE_COLLISION);
  70. eventsToDispatch.push_back(newEvent);
  71. contacts.push_back(contact);
  72. }
  73. void PhysicsScene2D::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) {
  74. PhysicsScene2DEvent *newEvent = new PhysicsScene2DEvent();
  75. newEvent->entity1 = ((PhysicsScene2DEntity*)contact->GetFixtureA()->GetBody()->GetUserData())->getEntity();
  76. newEvent->entity2 = ((PhysicsScene2DEntity*)contact->GetFixtureB()->GetBody()->GetUserData())->getEntity();
  77. if(((PhysicsScene2DEntity*)contact->GetFixtureA()->GetBody()->GetUserData())->collisionOnly ||
  78. ((PhysicsScene2DEntity*)contact->GetFixtureB()->GetBody()->GetUserData())->collisionOnly) {
  79. contact->SetEnabled(false);
  80. }
  81. b2Manifold *manifold = contact->GetManifold();
  82. b2Vec2 nor = manifold->localNormal;
  83. b2Vec2 point = manifold->points[0].localPoint;
  84. b2WorldManifold w_manifold;
  85. contact->GetWorldManifold(&w_manifold);
  86. b2Vec2 w_nor = w_manifold.normal;
  87. newEvent->localCollisionNormal.x = nor.x;
  88. newEvent->localCollisionNormal.y = nor.y;
  89. newEvent->worldCollisionNormal.x = w_nor.x;
  90. newEvent->worldCollisionNormal.y = w_nor.y;
  91. newEvent->localCollisionPoint.x = point.x * worldScale;
  92. newEvent->localCollisionPoint.y = point.y * worldScale;
  93. newEvent->worldCollisionPoint.x = w_manifold.points[0].x * worldScale;
  94. newEvent->worldCollisionPoint.y = w_manifold.points[0].y * worldScale;
  95. newEvent->impactStrength = 0;
  96. newEvent->frictionStrength = 0;
  97. newEvent->contact = contact;
  98. for(int i=0; i < manifold->pointCount; i++) {
  99. if(impulse->normalImpulses[i] > newEvent->impactStrength)
  100. newEvent->impactStrength = impulse->normalImpulses[i];
  101. if(impulse->tangentImpulses[i] > newEvent->frictionStrength)
  102. newEvent->frictionStrength = impulse->tangentImpulses[i];
  103. }
  104. newEvent->setEventCode(PhysicsScene2DEvent::EVENT_SOLVE_SHAPE_COLLISION);
  105. eventsToDispatch.push_back(newEvent);
  106. }
  107. void PhysicsScene2D::EndContact (b2Contact *contact) {
  108. PhysicsScene2DEvent *newEvent = new PhysicsScene2DEvent();
  109. newEvent->entity1 = ((PhysicsScene2DEntity*)contact->GetFixtureA()->GetBody()->GetUserData())->getEntity();
  110. newEvent->entity2 = ((PhysicsScene2DEntity*)contact->GetFixtureB()->GetBody()->GetUserData())->getEntity();
  111. newEvent->contact = contact;
  112. for(int i=0; i < contacts.size(); i++) {
  113. if(contacts[i] == contact) {
  114. contacts.erase(contacts.begin()+i);
  115. break;
  116. }
  117. }
  118. newEvent->setEventCode(PhysicsScene2DEvent::EVENT_END_SHAPE_COLLISION);
  119. eventsToDispatch.push_back(newEvent);
  120. }
  121. bool PhysicsScene2D::isEntityColliding(Entity *ent1) {
  122. PhysicsScene2DEntity *pEnt1 = getPhysicsByEntity(ent1);
  123. if(pEnt1 == NULL)
  124. return false;
  125. for(int i=0; i < contacts.size(); i++) {
  126. Entity *cEnt1 = ((PhysicsScene2DEntity*)contacts[i]->GetFixtureA()->GetBody()->GetUserData())->getEntity();
  127. Entity *cEnt2 = ((PhysicsScene2DEntity*)contacts[i]->GetFixtureB()->GetBody()->GetUserData())->getEntity();
  128. if(cEnt1 == ent1 || cEnt2 == ent1) {
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. bool PhysicsScene2D::testEntityCollision(Entity *ent1, Entity *ent2) {
  135. PhysicsScene2DEntity *pEnt1 = getPhysicsByEntity(ent1);
  136. PhysicsScene2DEntity *pEnt2 = getPhysicsByEntity(ent2);
  137. if(pEnt1 == NULL || pEnt2 == NULL)
  138. return false;
  139. for(int i=0; i < contacts.size(); i++) {
  140. Entity *cEnt1 = ((PhysicsScene2DEntity*)contacts[i]->GetFixtureA()->GetBody()->GetUserData())->getEntity();
  141. Entity *cEnt2 = ((PhysicsScene2DEntity*)contacts[i]->GetFixtureB()->GetBody()->GetUserData())->getEntity();
  142. if((cEnt1 == ent1 && cEnt2 == ent2) || (cEnt1 == ent2 && cEnt2 == ent1)) {
  143. return true;
  144. }
  145. }
  146. return false;
  147. }
  148. PhysicsScene2D::PhysicsScene2D() : Scene() {
  149. init(10.0f, 1.0f/60.0f,10,10,Vector2(0.0f, 10.0f));
  150. }
  151. PhysicsScene2D::PhysicsScene2D(Number worldScale, Number freq, int velIterations, int posIterations): Scene() {
  152. init(worldScale, 1.0f/freq,velIterations, posIterations, Vector2(0.0f, 10.0f));
  153. }
  154. void PhysicsScene2D::init(Number worldScale, Number physicsTimeStep, int velIterations, int posIterations, Vector2 physicsGravity) {
  155. cyclesLeftOver = 0.0;
  156. this->worldScale = worldScale;
  157. timeStep = physicsTimeStep;
  158. velocityIterations = velIterations;
  159. positionIterations = posIterations;
  160. b2Vec2 gravity(physicsGravity.x,physicsGravity.y);
  161. bool doSleep = true;
  162. world = new b2World(gravity, doSleep);
  163. world->SetContactListener(this);
  164. }
  165. void PhysicsScene2D::setGravity(Vector2 newGravity) {
  166. world->SetGravity(b2Vec2(newGravity.x, newGravity.y));
  167. }
  168. PhysicsScene2DEntity *PhysicsScene2D::getPhysicsByEntity(Entity *ent) {
  169. for(int i=0; i<physicsChildren.size();i++) {
  170. if(physicsChildren[i]->getEntity() == ent)
  171. return physicsChildren[i];
  172. }
  173. return NULL;
  174. }
  175. void PhysicsScene2D::destroyJoint(PhysicsJoint *joint) {
  176. world->DestroyJoint(joint->box2DJoint);
  177. }
  178. PhysicsJoint *PhysicsScene2D::createRevoluteJoint(Entity *ent1, Entity *ent2, Number ax, Number ay, bool collideConnected, bool enableLimit, Number lowerLimit, Number upperLimit, bool motorEnabled, Number motorSpeed, Number maxTorque) {
  179. PhysicsScene2DEntity *pEnt1 = getPhysicsByEntity(ent1);
  180. PhysicsScene2DEntity *pEnt2 = getPhysicsByEntity(ent2);
  181. if(pEnt1 == NULL || pEnt2 == NULL)
  182. {
  183. return NULL;
  184. }
  185. b2Vec2 anchor((ent1->getPosition().x+ax)/worldScale, (ent1->getPosition().y+ay)/worldScale);
  186. b2RevoluteJointDef *jointDef = new b2RevoluteJointDef();
  187. jointDef->collideConnected = collideConnected;
  188. jointDef->lowerAngle = lowerLimit * (PI/180.0f);
  189. jointDef->upperAngle = upperLimit * (PI/180.0f);
  190. jointDef->enableLimit = enableLimit;
  191. jointDef->motorSpeed = motorSpeed;
  192. jointDef->maxMotorTorque = maxTorque;
  193. jointDef->enableMotor = motorEnabled;
  194. jointDef->Initialize(pEnt1->body, pEnt2->body, anchor);
  195. PhysicsJoint *joint = new PhysicsJoint();
  196. joint->box2DJoint = world->CreateJoint(jointDef);
  197. return joint;
  198. }
  199. PhysicsJoint *PhysicsScene2D::createPrismaticJoint(Entity *ent1, Entity *ent2, Vector2 worldAxis, Number ax, Number ay, bool collideConnected, Number lowerTranslation, Number upperTranslation, bool enableLimit, Number motorSpeed, Number motorForce, bool motorEnabled) {
  200. PhysicsScene2DEntity *pEnt1 = getPhysicsByEntity(ent1);
  201. PhysicsScene2DEntity *pEnt2 = getPhysicsByEntity(ent2);
  202. if(pEnt1 == NULL || pEnt2 == NULL)
  203. return NULL;
  204. b2Vec2 anchor((ent1->getPosition().x+ax)/worldScale, (ent1->getPosition().y+ay)/worldScale);
  205. b2PrismaticJointDef *jointDef = new b2PrismaticJointDef();
  206. jointDef->collideConnected = collideConnected;
  207. jointDef->lowerTranslation = lowerTranslation/worldScale;
  208. jointDef->upperTranslation = upperTranslation/worldScale;
  209. jointDef->enableLimit = enableLimit;
  210. jointDef->motorSpeed = motorSpeed;
  211. jointDef->maxMotorForce = motorForce;
  212. jointDef->enableMotor = motorEnabled;
  213. b2Vec2 _worldAxis(worldAxis.x, worldAxis.y);
  214. jointDef->Initialize(pEnt1->body, pEnt2->body, anchor, _worldAxis);
  215. PhysicsJoint *joint = new PhysicsJoint();
  216. joint->box2DJoint = world->CreateJoint(jointDef);
  217. return joint;
  218. }
  219. void PhysicsScene2D::wakeUp(Entity *ent) {
  220. PhysicsScene2DEntity *pEnt = getPhysicsByEntity(ent);
  221. if(pEnt == NULL)
  222. return;
  223. pEnt->body->SetAwake(true);
  224. }
  225. Vector2 PhysicsScene2D::getVelocity(Entity *ent) {
  226. PhysicsScene2DEntity *pEnt = getPhysicsByEntity(ent);
  227. if(pEnt == NULL)
  228. return Vector2(0,0);
  229. b2Vec2 vec = pEnt->body->GetLinearVelocity();
  230. return Vector2(vec.x, vec.y);
  231. }
  232. void PhysicsScene2D::setAngularVelocity(Entity *ent, Number spin) {
  233. PhysicsScene2DEntity *pEnt = getPhysicsByEntity(ent);
  234. if(pEnt == NULL)
  235. return;
  236. pEnt->body->SetAngularVelocity(spin);
  237. }
  238. void PhysicsScene2D::setVelocity(Entity *ent, Number fx, Number fy) {
  239. PhysicsScene2DEntity *pEnt = getPhysicsByEntity(ent);
  240. if(pEnt == NULL)
  241. return;
  242. pEnt->setVelocity(fx, fy);
  243. }
  244. void PhysicsScene2D::setVelocityX(Entity *ent, Number fx) {
  245. PhysicsScene2DEntity *pEnt = getPhysicsByEntity(ent);
  246. if(pEnt == NULL)
  247. return;
  248. pEnt->setVelocityX(fx);
  249. }
  250. void PhysicsScene2D::setVelocityY(Entity *ent, Number fy) {
  251. PhysicsScene2DEntity *pEnt = getPhysicsByEntity(ent);
  252. if(pEnt == NULL)
  253. return;
  254. pEnt->setVelocityY(fy);
  255. }
  256. PhysicsScene2DEntity *PhysicsScene2D::addCollisionChild(Entity *newEntity, int entType, int groupIndex, bool sensorOnly) {
  257. PhysicsScene2DEntity *ret;
  258. ret = addPhysicsChild(newEntity, entType, false, 0,0,0, sensorOnly, false, groupIndex);
  259. newEntity->ignoreParentMatrix = false;
  260. ret->collisionOnly = true;
  261. return ret;
  262. }
  263. PhysicsScene2DEntity *PhysicsScene2D::trackCollisionChild(Entity *newEntity, int entType, int groupIndex) {
  264. PhysicsScene2DEntity *ret;
  265. ret = trackPhysicsChild(newEntity, entType, false, 0,0.0,0, true, false, groupIndex);
  266. ret->collisionOnly = true;
  267. newEntity->ignoreParentMatrix = false;
  268. return ret;
  269. }
  270. void PhysicsScene2D::setTransform(Entity *ent, Vector2 pos, Number angle) {
  271. PhysicsScene2DEntity *pEnt = getPhysicsByEntity(ent);
  272. if(pEnt == NULL)
  273. return;
  274. pEnt->setTransform(pos, angle);
  275. }
  276. void PhysicsScene2D::applyForce(Entity *ent, Number fx, Number fy) {
  277. PhysicsScene2DEntity *pEnt = getPhysicsByEntity(ent);
  278. if(pEnt == NULL)
  279. return;
  280. pEnt->body->SetAwake(true);
  281. b2Vec2 f = b2Vec2(fx,fy);
  282. b2Vec2 p = pEnt->body->GetWorldPoint(b2Vec2(0.0f, 0.0f));
  283. pEnt->body->ApplyForce(f, p);
  284. }
  285. void PhysicsScene2D::applyImpulse(Entity *ent, Number fx, Number fy) {
  286. PhysicsScene2DEntity *pEnt = getPhysicsByEntity(ent);
  287. if(pEnt == NULL)
  288. return;
  289. pEnt->applyImpulse(fx, fy);
  290. }
  291. PhysicsJoint *PhysicsScene2D::createDistanceJoint(Entity *ent1, Entity *ent2, bool collideConnected) {
  292. PhysicsScene2DEntity *pEnt1 = getPhysicsByEntity(ent1);
  293. PhysicsScene2DEntity *pEnt2 = getPhysicsByEntity(ent2);
  294. if(pEnt1 == NULL || pEnt2 == NULL)
  295. return NULL;
  296. b2Vec2 a1(ent1->getPosition().x/worldScale, ent1->getPosition().y/worldScale);
  297. b2Vec2 a2(ent2->getPosition().x/worldScale, ent2->getPosition().y/worldScale);
  298. b2DistanceJointDef *jointDef = new b2DistanceJointDef();
  299. jointDef->Initialize(pEnt1->body, pEnt2->body, a1, a2);
  300. jointDef->collideConnected = collideConnected;
  301. PhysicsJoint *joint = new PhysicsJoint();
  302. joint->box2DJoint = world->CreateJoint(jointDef);
  303. return joint;
  304. }
  305. /*
  306. b2MouseJoint *PhysicsScene2D::createMouseJoint(Entity *ent1, Vector2 *mp) {
  307. PhysicsScene2DEntity *pEnt1 = getPhysicsByEntity(ent1);
  308. if(pEnt1 == NULL)
  309. return NULL;
  310. b2MouseJointDef *mj = new b2MouseJointDef();
  311. mj->bodyA = world->GetGroundBody();
  312. mj->bodyB = pEnt1->body;
  313. b2Vec2 mpos(mp->x/10.0f, mp->y/10.0f);
  314. mj->target = mpos;
  315. #ifdef TARGET_Number_IS_FIXED
  316. mj->maxForce = (pEnt1->body->GetMass() < 16.0)? (1000.0f * pEnt1->body->GetMass()) : Number(16000.0);
  317. #else
  318. mj->maxForce = 1000.0f * pEnt1->body->GetMass();
  319. #endif
  320. b2MouseJoint *m_mouseJoint = (b2MouseJoint*)world->CreateJoint(mj);
  321. pEnt1->body->SetAwake(true);
  322. Logger::log("OK %d!\n", m_mouseJoint);
  323. return m_mouseJoint;
  324. }
  325. */
  326. Entity *PhysicsScene2D::getEntityAtPosition(Number x, Number y) {
  327. Entity *ret = NULL;
  328. b2Vec2 mousePosition;
  329. mousePosition.x = x/worldScale;
  330. mousePosition.y = y/worldScale;
  331. for(int i=0;i<physicsChildren.size();i++) {
  332. PhysicsScene2DEntity *ent = physicsChildren[i];
  333. if(ent->fixture) {
  334. for (b2Fixture* f = ent->body->GetFixtureList(); f; f = f->GetNext()) {
  335. if(f->TestPoint(mousePosition)) {
  336. return ent->getEntity();
  337. }
  338. }
  339. }
  340. }
  341. return ret;
  342. }
  343. bool PhysicsScene2D::testEntityAtPosition(Entity *ent, Number x, Number y) {
  344. PhysicsScene2DEntity *pEnt = getPhysicsByEntity(ent);
  345. if(pEnt == NULL)
  346. return false;
  347. b2Vec2 mousePosition;
  348. mousePosition.x = x/worldScale;
  349. mousePosition.y = y/worldScale;
  350. if(pEnt->fixture) {
  351. for (b2Fixture* f = pEnt->body->GetFixtureList(); f; f = f->GetNext()) {
  352. if(f->TestPoint(mousePosition))
  353. return true;
  354. else
  355. return false;
  356. }
  357. }
  358. return false;
  359. }
  360. void PhysicsScene2D::destroyMouseJoint(b2MouseJoint *mJoint) {
  361. world->DestroyJoint(mJoint);
  362. mJoint = NULL;
  363. }
  364. PhysicsScene2DEntity *PhysicsScene2D::addPhysicsChild(Entity *newEntity, int entType, bool isStatic, Number friction, Number density, Number restitution, bool isSensor, bool fixedRotation, int groupIndex) {
  365. addChild(newEntity);
  366. return trackPhysicsChild(newEntity, entType, isStatic, friction, density, restitution, isSensor, fixedRotation, groupIndex);
  367. }
  368. PhysicsScene2DEntity *PhysicsScene2D::trackPhysicsChild(Entity *newEntity, int entType, bool isStatic, Number friction, Number density, Number restitution, bool isSensor, bool fixedRotation, int groupIndex) {
  369. PhysicsScene2DEntity *newPhysicsEntity = new PhysicsScene2DEntity(newEntity, world, worldScale, entType, isStatic, friction, density, restitution, isSensor,fixedRotation, groupIndex);
  370. physicsChildren.push_back(newPhysicsEntity);
  371. newPhysicsEntity->body->SetAwake(true);
  372. return newPhysicsEntity;
  373. }
  374. void PhysicsScene2D::stopTrackingChild(Entity *entity) {
  375. PhysicsScene2DEntity *physicsEntityToRemove = getPhysicsByEntity(entity);
  376. if(!physicsEntityToRemove) {
  377. return;
  378. }
  379. world->DestroyBody(physicsEntityToRemove->body);
  380. physicsEntityToRemove->body = NULL;
  381. for(int i=0;i<physicsChildren.size();i++) {
  382. if(physicsChildren[i] == physicsEntityToRemove) {
  383. physicsChildren.erase(physicsChildren.begin()+i);
  384. }
  385. }
  386. delete physicsEntityToRemove;
  387. }
  388. void PhysicsScene2D::removePhysicsChild(Entity *entityToRemove) {
  389. stopTrackingChild(entityToRemove);
  390. Scene::removeEntity(entityToRemove);
  391. }
  392. void PhysicsScene2D::removeChild(Entity *entityToRemove) {
  393. if(getPhysicsByEntity(entityToRemove)) {
  394. removePhysicsChild(entityToRemove);
  395. } else {
  396. Scene::removeEntity(entityToRemove);
  397. }
  398. }
  399. void PhysicsScene2D::Shutdown() {
  400. }
  401. PhysicsScene2D::~PhysicsScene2D() {
  402. for(int i=0; i<physicsChildren.size();i++) {
  403. delete physicsChildren[i];
  404. }
  405. delete world;
  406. }
  407. PhysicsScene2DEntity *PhysicsScene2D::getPhysicsEntityByFixture(b2Fixture *fixture) {
  408. for(int i=0; i < physicsChildren.size(); i++) {
  409. for (b2Fixture* f = physicsChildren[i]->body->GetFixtureList(); f; f = f->GetNext()) {
  410. if(f == fixture)
  411. return physicsChildren[i];
  412. }
  413. }
  414. return NULL;
  415. }
  416. PhysicsScene2DEntity *PhysicsScene2D::getPhysicsEntityByShape(b2Shape *shape) {
  417. for(int i=0; i < physicsChildren.size(); i++) {
  418. for (b2Fixture *f = physicsChildren[i]->body->GetFixtureList(); f; f = f->GetNext()) {
  419. if(f->GetShape() == shape)
  420. return physicsChildren[i];
  421. }
  422. }
  423. return NULL;
  424. }
  425. void PhysicsScene2D::handleEvent(Event *event) {
  426. Scene::handleEvent(event);
  427. }
  428. void PhysicsScene2D::Update() {
  429. Number elapsed = CoreServices::getInstance()->getCore()->getElapsed() + cyclesLeftOver;
  430. while(elapsed > timeStep) {
  431. elapsed -= timeStep;
  432. for(int i=0; i<physicsChildren.size();i++) {
  433. physicsChildren[i]->Update();
  434. }
  435. world->Step(timeStep, velocityIterations,positionIterations);
  436. for(int i=0; i < eventsToDispatch.size(); i++) {
  437. dispatchEvent(eventsToDispatch[i], eventsToDispatch[i]->getEventCode());
  438. }
  439. eventsToDispatch.clear();
  440. }
  441. cyclesLeftOver = elapsed;
  442. Scene::Update();
  443. }