PolyPhysicsScreen.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. using namespace Polycode;
  21. /*
  22. void PhysicsScreen::Add(const b2ContactPoint* point) {
  23. if (numContactPoints == MAX_B2DCONTACTPOINTS) {
  24. return;
  25. }
  26. ContactPoint* cp = m_points + numContactPoints;
  27. cp->shape1 = point->shape1;
  28. cp->shape2 = point->shape2;
  29. cp->position = point->position;
  30. cp->normal = point->normal;
  31. cp->id = point->id;
  32. cp->state = e_contactAdded;
  33. ++numContactPoints;
  34. }
  35. void PhysicsScreen::Persist(const b2ContactPoint* point) {
  36. if (numContactPoints == MAX_B2DCONTACTPOINTS) {
  37. return;
  38. }
  39. ContactPoint* cp = m_points + numContactPoints;
  40. cp->shape1 = point->shape1;
  41. cp->shape2 = point->shape2;
  42. cp->position = point->position;
  43. cp->normal = point->normal;
  44. cp->id = point->id;
  45. cp->state = e_contactPersisted;
  46. ++numContactPoints;
  47. }
  48. void PhysicsScreen::Remove(const b2ContactPoint* point) {
  49. if (numContactPoints == MAX_B2DCONTACTPOINTS) {
  50. return;
  51. }
  52. ContactPoint* cp = m_points + numContactPoints;
  53. cp->shape1 = point->shape1;
  54. cp->shape2 = point->shape2;
  55. cp->position = point->position;
  56. cp->normal = point->normal;
  57. cp->id = point->id;
  58. cp->state = e_contactRemoved;
  59. ++numContactPoints;
  60. }
  61. */
  62. void PhysicsScreen::BeginContact (b2Contact *contact) {
  63. PhysicsScreenEvent *newEvent = new PhysicsScreenEvent();
  64. newEvent->entity1 = getPhysicsEntityByFixture(contact->GetFixtureA());
  65. newEvent->entity2 = getPhysicsEntityByFixture(contact->GetFixtureB());
  66. dispatchEvent(newEvent, PhysicsScreenEvent::EVENT_NEW_SHAPE_COLLISION);
  67. }
  68. void PhysicsScreen::EndContact (b2Contact *contact) {
  69. }
  70. PhysicsScreen::PhysicsScreen() : Screen() {
  71. init(10.0f, 1.0f/60.0f,10,Vector2(0.0f, 10.0f));
  72. }
  73. PhysicsScreen::PhysicsScreen(float worldScale, float freq) : Screen() {
  74. init(worldScale, 1.0f/freq,10,Vector2(0.0f, 10.0f));
  75. }
  76. void PhysicsScreen::init(float worldScale, float physicsTimeStep, int physicsIterations, Vector2 physicsGravity) {
  77. this->worldScale = worldScale;
  78. numContactPoints = 0;
  79. timeStep = physicsTimeStep;
  80. iterations = physicsIterations;
  81. b2Vec2 gravity(physicsGravity.x,physicsGravity.y);
  82. bool doSleep = true;
  83. world = new b2World(gravity, doSleep);
  84. world->SetContactListener(this);
  85. updateTimer = new Timer(true, 3);
  86. updateTimer->addEventListener(this, Timer::EVENT_TRIGGER);
  87. }
  88. void PhysicsScreen::setGravity(Vector2 newGravity) {
  89. world->SetGravity(b2Vec2(newGravity.x, newGravity.y));
  90. }
  91. PhysicsScreenEntity *PhysicsScreen::getPhysicsByScreenEntity(ScreenEntity *ent) {
  92. for(int i=0; i<physicsChildren.size();i++) {
  93. if(physicsChildren[i]->getScreenEntity() == ent)
  94. return physicsChildren[i];
  95. }
  96. return NULL;
  97. }
  98. void PhysicsScreen::destroyJoint(PhysicsJoint *joint) {
  99. world->DestroyJoint(joint->box2DJoint);
  100. }
  101. PhysicsJoint *PhysicsScreen::createRevoluteJoint(ScreenEntity *ent1, ScreenEntity *ent2, float ax, float ay, bool enableLimit, float lowerLimit, float upperLimit, bool motorEnabled, float motorSpeed, float maxTorque) {
  102. PhysicsScreenEntity *pEnt1 = getPhysicsByScreenEntity(ent1);
  103. PhysicsScreenEntity *pEnt2 = getPhysicsByScreenEntity(ent2);
  104. if(pEnt1 == NULL || pEnt2 == NULL)
  105. return NULL;
  106. b2Vec2 anchor((ent1->getPosition().x+ax)/worldScale, (ent1->getPosition().y+ay)/worldScale);
  107. b2RevoluteJointDef *jointDef = new b2RevoluteJointDef();
  108. jointDef->collideConnected = false;
  109. jointDef->lowerAngle = lowerLimit * (PI/180.0f);
  110. jointDef->upperAngle = upperLimit * (PI/180.0f);
  111. jointDef->enableLimit = enableLimit;
  112. jointDef->motorSpeed = motorSpeed;
  113. jointDef->maxMotorTorque = maxTorque;
  114. jointDef->enableMotor = motorEnabled;
  115. jointDef->Initialize(pEnt1->body, pEnt2->body, anchor);
  116. PhysicsJoint *joint = new PhysicsJoint();
  117. joint->box2DJoint = world->CreateJoint(jointDef);
  118. return joint;
  119. }
  120. void PhysicsScreen::createPrismaticJoint(ScreenEntity *ent1, ScreenEntity *ent2, bool collideConnected) {
  121. }
  122. void PhysicsScreen::wakeUp(ScreenEntity *ent) {
  123. PhysicsScreenEntity *pEnt = getPhysicsByScreenEntity(ent);
  124. if(pEnt == NULL)
  125. return;
  126. pEnt->body->SetAwake(true);
  127. }
  128. Vector2 PhysicsScreen::getVelocity(ScreenEntity *ent) {
  129. PhysicsScreenEntity *pEnt = getPhysicsByScreenEntity(ent);
  130. if(pEnt == NULL)
  131. return Vector2(0,0);
  132. b2Vec2 vec = pEnt->body->GetLinearVelocity();
  133. return Vector2(vec.x, vec.y);
  134. }
  135. void PhysicsScreen::setSpin(ScreenEntity *ent, float spin) {
  136. PhysicsScreenEntity *pEnt = getPhysicsByScreenEntity(ent);
  137. if(pEnt == NULL)
  138. return;
  139. pEnt->body->SetAngularVelocity(spin);
  140. }
  141. void PhysicsScreen::setVelocity(ScreenEntity *ent, float fx, float fy) {
  142. PhysicsScreenEntity *pEnt = getPhysicsByScreenEntity(ent);
  143. if(pEnt == NULL)
  144. return;
  145. pEnt->body->SetAwake(true);
  146. b2Vec2 f = pEnt->body->GetLinearVelocity();
  147. if(fx != 0)
  148. f.x = fx;
  149. if(fy != 0)
  150. f.y = fy;
  151. pEnt->body->SetLinearVelocity(f);
  152. }
  153. void PhysicsScreen::setVelocityX(ScreenEntity *ent, float fx) {
  154. PhysicsScreenEntity *pEnt = getPhysicsByScreenEntity(ent);
  155. if(pEnt == NULL)
  156. return;
  157. pEnt->body->SetAwake(true);
  158. b2Vec2 f = pEnt->body->GetLinearVelocity();
  159. f.x = fx;
  160. pEnt->body->SetLinearVelocity(f);
  161. }
  162. void PhysicsScreen::setVelocityY(ScreenEntity *ent, float fy) {
  163. PhysicsScreenEntity *pEnt = getPhysicsByScreenEntity(ent);
  164. if(pEnt == NULL)
  165. return;
  166. pEnt->body->SetAwake(true);
  167. b2Vec2 f = pEnt->body->GetLinearVelocity();
  168. f.y = fy;
  169. pEnt->body->SetLinearVelocity(f);
  170. }
  171. PhysicsScreenEntity *PhysicsScreen::addCollisionChild(ScreenEntity *newEntity, int entType) {
  172. PhysicsScreenEntity *ret;
  173. ret = addPhysicsChild(newEntity, entType, 0,0.1,0, true);
  174. ret->collisionOnly = true;
  175. return ret;
  176. }
  177. void PhysicsScreen::setTransform(ScreenEntity *ent, Vector2 pos, float angle) {
  178. PhysicsScreenEntity *pEnt = getPhysicsByScreenEntity(ent);
  179. if(pEnt == NULL)
  180. return;
  181. pEnt->setTransform(pos, angle);
  182. }
  183. void PhysicsScreen::applyForce(ScreenEntity *ent, float fx, float fy) {
  184. PhysicsScreenEntity *pEnt = getPhysicsByScreenEntity(ent);
  185. if(pEnt == NULL)
  186. return;
  187. pEnt->body->SetAwake(true);
  188. b2Vec2 f = b2Vec2(fx,fy);
  189. b2Vec2 p = pEnt->body->GetWorldPoint(b2Vec2(0.0f, 0.0f));
  190. pEnt->body->ApplyForce(f, p);
  191. }
  192. void PhysicsScreen::applyImpulse(ScreenEntity *ent, float fx, float fy) {
  193. PhysicsScreenEntity *pEnt = getPhysicsByScreenEntity(ent);
  194. if(pEnt == NULL)
  195. return;
  196. pEnt->body->SetAwake(true);
  197. b2Vec2 f = b2Vec2(fx,fy);
  198. b2Vec2 p = pEnt->body->GetWorldPoint(b2Vec2(0.0f, 0.0f));
  199. pEnt->body->ApplyLinearImpulse(f, p);
  200. }
  201. void PhysicsScreen::createDistanceJoint(ScreenEntity *ent1, ScreenEntity *ent2, bool collideConnected) {
  202. PhysicsScreenEntity *pEnt1 = getPhysicsByScreenEntity(ent1);
  203. PhysicsScreenEntity *pEnt2 = getPhysicsByScreenEntity(ent2);
  204. if(pEnt1 == NULL || pEnt2 == NULL)
  205. return;
  206. b2Vec2 a1(ent1->getPosition().x/worldScale, ent1->getPosition().y/worldScale);
  207. b2Vec2 a2(ent2->getPosition().x/worldScale, ent2->getPosition().y/worldScale);
  208. b2DistanceJointDef *jointDef = new b2DistanceJointDef();
  209. jointDef->Initialize(pEnt1->body, pEnt2->body, a1, a2);
  210. jointDef->collideConnected = collideConnected;
  211. world->CreateJoint(jointDef);
  212. }
  213. /*
  214. b2MouseJoint *PhysicsScreen::createMouseJoint(ScreenEntity *ent1, Vector2 *mp) {
  215. PhysicsScreenEntity *pEnt1 = getPhysicsByScreenEntity(ent1);
  216. if(pEnt1 == NULL)
  217. return NULL;
  218. b2MouseJointDef *mj = new b2MouseJointDef();
  219. mj->bodyA = world->GetGroundBody();
  220. mj->bodyB = pEnt1->body;
  221. b2Vec2 mpos(mp->x/10.0f, mp->y/10.0f);
  222. mj->target = mpos;
  223. #ifdef TARGET_FLOAT32_IS_FIXED
  224. mj->maxForce = (pEnt1->body->GetMass() < 16.0)? (1000.0f * pEnt1->body->GetMass()) : float32(16000.0);
  225. #else
  226. mj->maxForce = 1000.0f * pEnt1->body->GetMass();
  227. #endif
  228. b2MouseJoint *m_mouseJoint = (b2MouseJoint*)world->CreateJoint(mj);
  229. pEnt1->body->SetAwake(true);
  230. Logger::log("OK %d!\n", m_mouseJoint);
  231. return m_mouseJoint;
  232. }
  233. */
  234. Vector2 PhysicsScreen::getEntityCollisionNormal(ScreenEntity *ent1, ScreenEntity *ent2) {
  235. PhysicsScreenEntity *pEnt1 = getPhysicsByScreenEntity(ent1);
  236. PhysicsScreenEntity *pEnt2 = getPhysicsByScreenEntity(ent2);
  237. if(pEnt1 == NULL || pEnt2 == NULL)
  238. return Vector2(0,0);
  239. PhysicsScreenEntity *rEnt1;
  240. PhysicsScreenEntity *rEnt2;
  241. for (int32 i = 0; i < numContactPoints; ++i)
  242. {
  243. ContactPoint* point = m_points + i;
  244. if (point->state == 0 || point->state == 1) {
  245. rEnt1= getPhysicsEntityByShape(point->shape1);
  246. rEnt2 = getPhysicsEntityByShape(point->shape2);
  247. if((rEnt1 == pEnt1 && rEnt2 == pEnt2) ||
  248. (rEnt1 == pEnt2 && rEnt2 == pEnt1)) {
  249. return Vector2(point->normal.x,point->normal.y);
  250. }
  251. }
  252. }
  253. return Vector2(0,0);
  254. }
  255. bool PhysicsScreen::areEntitiesColliding(ScreenEntity *ent1, ScreenEntity *ent2) {
  256. PhysicsScreenEntity *pEnt1 = getPhysicsByScreenEntity(ent1);
  257. PhysicsScreenEntity *pEnt2 = getPhysicsByScreenEntity(ent2);
  258. if(pEnt1 == NULL || pEnt2 == NULL)
  259. return false;
  260. PhysicsScreenEntity *rEnt1;
  261. PhysicsScreenEntity *rEnt2;
  262. for (int32 i = 0; i < numContactPoints; ++i)
  263. {
  264. ContactPoint* point = m_points + i;
  265. if (point->state == 0 || point->state == 1) {
  266. rEnt1= getPhysicsEntityByShape(point->shape1);
  267. rEnt2 = getPhysicsEntityByShape(point->shape2);
  268. if((rEnt1 == pEnt1 && rEnt2 == pEnt2) ||
  269. (rEnt1 == pEnt2 && rEnt2 == pEnt1)) {
  270. return true;
  271. }
  272. }
  273. }
  274. return false;
  275. }
  276. ScreenEntity *PhysicsScreen::getEntityAtPosition(float x, float y) {
  277. ScreenEntity *ret = NULL;
  278. b2Vec2 mousePosition;
  279. mousePosition.x = x/worldScale;
  280. mousePosition.y = y/worldScale;
  281. for(int i=0;i<physicsChildren.size();i++) {
  282. PhysicsScreenEntity *ent = physicsChildren[i];
  283. if(ent->shape->TestPoint(ent->body->GetTransform(), mousePosition))
  284. return ent->getScreenEntity();
  285. }
  286. return ret;
  287. }
  288. bool PhysicsScreen::testEntityAtPosition(ScreenEntity *ent, float x, float y) {
  289. PhysicsScreenEntity *pEnt = getPhysicsByScreenEntity(ent);
  290. if(pEnt == NULL)
  291. return false;
  292. b2Vec2 mousePosition;
  293. mousePosition.x = x/worldScale;
  294. mousePosition.y = y/worldScale;
  295. if(pEnt->shape->TestPoint(pEnt->body->GetTransform(), mousePosition))
  296. return true;
  297. else
  298. return false;
  299. }
  300. void PhysicsScreen::destroyMouseJoint(b2MouseJoint *mJoint) {
  301. world->DestroyJoint(mJoint);
  302. mJoint = NULL;
  303. }
  304. PhysicsScreenEntity *PhysicsScreen::addPhysicsChild(ScreenEntity *newEntity, int entType, float friction, float density, float restitution, bool isSensor, bool fixedRotation) {
  305. addChild(newEntity);
  306. newEntity->setPositionMode(ScreenEntity::POSITION_CENTER);
  307. PhysicsScreenEntity *newPhysicsEntity = new PhysicsScreenEntity(newEntity, world, worldScale, entType, friction, density, restitution, isSensor,fixedRotation);
  308. physicsChildren.push_back(newPhysicsEntity);
  309. newPhysicsEntity->body->SetAwake(true);
  310. return newPhysicsEntity;
  311. }
  312. void PhysicsScreen::removePhysicsChild(PhysicsScreenEntity *entityToRemove) {
  313. world->DestroyBody(entityToRemove->body);
  314. removeChild(entityToRemove->getScreenEntity());
  315. for(int i=0;i<physicsChildren.size();i++) {
  316. if(physicsChildren[i] == entityToRemove) {
  317. physicsChildren.erase(physicsChildren.begin()+i);
  318. }
  319. }
  320. }
  321. void PhysicsScreen::Shutdown() {
  322. }
  323. PhysicsScreen::~PhysicsScreen() {
  324. delete world;
  325. for(int i=0; i<physicsChildren.size();i++) {
  326. delete physicsChildren[i];
  327. }
  328. }
  329. PhysicsScreenEntity *PhysicsScreen::getPhysicsEntityByFixture(b2Fixture *fixture) {
  330. for(int i=0; i < physicsChildren.size(); i++) {
  331. if(physicsChildren[i]->fixture == fixture)
  332. return physicsChildren[i];
  333. }
  334. return NULL;
  335. }
  336. PhysicsScreenEntity *PhysicsScreen::getPhysicsEntityByShape(b2Shape *shape) {
  337. for(int i=0; i < physicsChildren.size(); i++) {
  338. if(physicsChildren[i]->shape == shape)
  339. return physicsChildren[i];
  340. }
  341. return NULL;
  342. }
  343. void PhysicsScreen::handleEvent(Event *event) {
  344. numContactPoints = 0;
  345. if(event->getDispatcher() == updateTimer) {
  346. world->Step(timeStep, iterations,iterations);
  347. }
  348. /*
  349. for (int32 i = 0; i < numContactPoints; ++i)
  350. {
  351. ContactPoint* point = m_points + i;
  352. if (point->state == 0) {
  353. PhysicsScreenEvent *newEvent = new PhysicsScreenEvent();
  354. newEvent->entity1 = getPhysicsEntityByShape(point->shape1);
  355. newEvent->entity2 = getPhysicsEntityByShape(point->shape2);
  356. dispatchEvent(newEvent, PhysicsScreenEvent::EVENT_NEW_SHAPE_COLLISION);
  357. } else if (point->state == 1) {
  358. // Persist
  359. // DrawPoint(point->position, 5.0f, b2Color(0.3f, 0.3f, 0.95f));
  360. } else {
  361. // Remove
  362. // DrawPoint(point->position, 10.0f, b2Color(0.95f, 0.3f, 0.3f));
  363. dispatchEvent(new PhysicsScreenEvent(), PhysicsScreenEvent::EVENT_END_SHAPE_COLLISION);
  364. }
  365. }
  366. */
  367. Screen::handleEvent(event);
  368. }
  369. void PhysicsScreen::Update() {
  370. // unsigned int elapsed = updateTimer->getTicks();
  371. // if(updateTimer->hasElapsed()) {
  372. // world->Step(timeStep, iterations);
  373. for(int i=0; i<physicsChildren.size();i++) {
  374. physicsChildren[i]->Update();
  375. }
  376. // }
  377. }