PolyPhysicsScreen.cpp 14 KB

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