PhysicsWorld2D.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "DebugRenderer.h"
  25. #include "Graphics.h"
  26. #include "Log.h"
  27. #include "PhysicsEvents2D.h"
  28. #include "PhysicsUtils2D.h"
  29. #include "PhysicsWorld2D.h"
  30. #include "Profiler.h"
  31. #include "Renderer.h"
  32. #include "RigidBody2D.h"
  33. #include "Scene.h"
  34. #include "SceneEvents.h"
  35. #include "Viewport.h"
  36. #include "DebugNew.h"
  37. namespace Urho3D
  38. {
  39. extern const char* SUBSYSTEM_CATEGORY;
  40. static const Vector2 DEFAULT_GRAVITY(0.0f, -9.81f);
  41. static const int DEFAULT_VELOCITY_ITERATIONS = 8;
  42. static const int DEFAULT_POSITION_ITERATIONS = 3;
  43. PhysicsWorld2D::PhysicsWorld2D(Context* context) :
  44. Component(context),
  45. world_(0),
  46. gravity_(DEFAULT_GRAVITY),
  47. velocityIterations_(DEFAULT_VELOCITY_ITERATIONS),
  48. positionIterations_(DEFAULT_POSITION_ITERATIONS),
  49. debugRenderer_(0),
  50. physicsSteping_(false),
  51. applyingTransforms_(false)
  52. {
  53. // Set default debug draw flags
  54. m_drawFlags = e_shapeBit;
  55. // Create Box2D world
  56. world_ = new b2World(ToB2Vec2(gravity_));
  57. // Set contact listener
  58. world_->SetContactListener(this);
  59. // Set debug draw
  60. world_->SetDebugDraw(this);
  61. world_->SetContinuousPhysics(true);
  62. world_->SetSubStepping(true);
  63. }
  64. PhysicsWorld2D::~PhysicsWorld2D()
  65. {
  66. for (unsigned i = 0; i < rigidBodies_.Size(); ++i)
  67. if (rigidBodies_[i])
  68. rigidBodies_[i]->ReleaseBody();
  69. delete world_;
  70. world_ = 0;
  71. }
  72. void PhysicsWorld2D::RegisterObject(Context* context)
  73. {
  74. context->RegisterFactory<PhysicsWorld2D>(SUBSYSTEM_CATEGORY);
  75. ACCESSOR_ATTRIBUTE("Draw Shape", GetDrawShape, SetDrawShape, bool, false, AM_DEFAULT);
  76. ACCESSOR_ATTRIBUTE("Draw Joint", GetDrawJoint, SetDrawJoint, bool, false, AM_DEFAULT);
  77. ACCESSOR_ATTRIBUTE("Draw Aabb", GetDrawAabb, SetDrawAabb, bool, false, AM_DEFAULT);
  78. ACCESSOR_ATTRIBUTE("Draw Pair", GetDrawPair, SetDrawPair, bool, false, AM_DEFAULT);
  79. ACCESSOR_ATTRIBUTE("Draw CenterOfMass", GetDrawCenterOfMass, SetDrawCenterOfMass, bool, false, AM_DEFAULT);
  80. ACCESSOR_ATTRIBUTE("Allow Sleeping", GetAllowSleeping, SetAllowSleeping, bool, false, AM_DEFAULT);
  81. ACCESSOR_ATTRIBUTE("Warm Starting", GetWarmStarting, SetWarmStarting, bool, false, AM_DEFAULT);
  82. ACCESSOR_ATTRIBUTE("Continuous Physics", GetContinuousPhysics, SetContinuousPhysics, bool, false, AM_DEFAULT);
  83. ACCESSOR_ATTRIBUTE("Sub Stepping", GetSubStepping, SetSubStepping, bool, false, AM_DEFAULT);
  84. REF_ACCESSOR_ATTRIBUTE("Gravity", GetGravity, SetGravity, Vector2, DEFAULT_GRAVITY, AM_DEFAULT);
  85. ACCESSOR_ATTRIBUTE("Auto Clear Forces", GetAutoClearForces, SetAutoClearForces, bool, false, AM_DEFAULT);
  86. ACCESSOR_ATTRIBUTE("Velocity Iterations", GetVelocityIterations, SetVelocityIterations, int, DEFAULT_VELOCITY_ITERATIONS, AM_DEFAULT);
  87. ACCESSOR_ATTRIBUTE("Position Iterations", GetPositionIterations, SetPositionIterations, int, DEFAULT_POSITION_ITERATIONS, AM_DEFAULT);
  88. COPY_BASE_ATTRIBUTES(Component);
  89. }
  90. void PhysicsWorld2D::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  91. {
  92. if (debug)
  93. {
  94. PROFILE(Physics2DDrawDebug);
  95. debugRenderer_ = debug;
  96. debugDepthTest_ = depthTest;
  97. world_->DrawDebugData();
  98. debugRenderer_ = 0;
  99. }
  100. }
  101. void PhysicsWorld2D::BeginContact(b2Contact* contact)
  102. {
  103. // Only handle contact event when physics steping
  104. if (!physicsSteping_)
  105. return;
  106. b2Fixture* fixtureA = contact->GetFixtureA();
  107. b2Fixture* fixtureB = contact->GetFixtureB();
  108. if (!fixtureA || !fixtureB)
  109. return;
  110. beginContactInfos_.Push(ContactInfo(contact));
  111. }
  112. void PhysicsWorld2D::EndContact(b2Contact* contact)
  113. {
  114. // Only handle contact event when physics steping
  115. if (!physicsSteping_)
  116. return;
  117. b2Fixture* fixtureA = contact->GetFixtureA();
  118. b2Fixture* fixtureB = contact->GetFixtureB();
  119. if (!fixtureA || !fixtureB)
  120. return;
  121. endContactInfos_.Push(ContactInfo(contact));
  122. }
  123. void PhysicsWorld2D::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
  124. {
  125. if (!debugRenderer_)
  126. return;
  127. Color c = ToColor(color);
  128. for (int i = 0; i < vertexCount - 1; ++i)
  129. debugRenderer_->AddLine(ToVector3(vertices[i]), ToVector3(vertices[i + 1]), c, debugDepthTest_);
  130. debugRenderer_->AddLine(ToVector3(vertices[vertexCount - 1]), ToVector3(vertices[0]), c, debugDepthTest_);
  131. }
  132. void PhysicsWorld2D::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
  133. {
  134. if (!debugRenderer_)
  135. return;
  136. Vector3 v = ToVector3(vertices[0]);
  137. Color c(color.r, color.g, color.b, 0.5f);
  138. for (int i = 1; i < vertexCount - 1; ++i)
  139. debugRenderer_->AddTriangle(v, ToVector3(vertices[i]), ToVector3(vertices[i + 1]), c, debugDepthTest_);
  140. }
  141. void PhysicsWorld2D::DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color)
  142. {
  143. if (!debugRenderer_)
  144. return;
  145. Vector3 p = ToVector3(center);
  146. Color c = ToColor(color);
  147. for (unsigned i = 0; i < 360; i += 30)
  148. {
  149. unsigned j = i + 30;
  150. float x1 = radius * Cos((float)i);
  151. float y1 = radius * Sin((float)i);
  152. float x2 = radius * Cos((float)j);
  153. float y2 = radius * Sin((float)j);
  154. debugRenderer_->AddLine(p + Vector3(x1, y1, 0.0f), p + Vector3(x2, y2, 0.0f), c, debugDepthTest_);
  155. }
  156. }
  157. void PhysicsWorld2D::DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color)
  158. {
  159. if (!debugRenderer_)
  160. return;
  161. Vector3 p = ToVector3(center);
  162. Color c(color.r, color.g, color.b, 0.5f);
  163. for (unsigned i = 0; i < 360; i += 30)
  164. {
  165. unsigned j = i + 30;
  166. float x1 = radius * Cos((float)i);
  167. float y1 = radius * Sin((float)i);
  168. float x2 = radius * Cos((float)j);
  169. float y2 = radius * Sin((float)j);
  170. debugRenderer_->AddTriangle(p, p + Vector3(x1, y1, 0.0f), p + Vector3(x2, y2, 0.0f), c, debugDepthTest_);
  171. }
  172. }
  173. void PhysicsWorld2D::DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
  174. {
  175. if (debugRenderer_)
  176. debugRenderer_->AddLine(ToVector3(p1), ToVector3(p2), ToColor(color), debugDepthTest_);
  177. }
  178. void PhysicsWorld2D::DrawTransform(const b2Transform& xf)
  179. {
  180. if (!debugRenderer_)
  181. return;
  182. const float32 axisScale = 0.4f;
  183. b2Vec2 p1 = xf.p, p2;
  184. p2 = p1 + axisScale * xf.q.GetXAxis();
  185. debugRenderer_->AddLine(Vector3(p1.x, p1.y, 0.0f), Vector3(p2.x, p2.y, 0.0f), Color::RED, debugDepthTest_);
  186. p2 = p1 + axisScale * xf.q.GetYAxis();
  187. debugRenderer_->AddLine(Vector3(p1.x, p1.y, 0.0f), Vector3(p2.x, p2.y, 0.0f), Color::GREEN, debugDepthTest_);
  188. }
  189. void PhysicsWorld2D::Update(float timeStep)
  190. {
  191. using namespace Physics2DPreStep2D;
  192. VariantMap& eventData = GetEventDataMap();
  193. eventData[P_WORLD] = this;
  194. eventData[P_TIMESTEP] = timeStep;
  195. SendEvent(E_PHYSICSPRESTEP2D, eventData);
  196. physicsSteping_ = true;
  197. world_->Step(timeStep, velocityIterations_, positionIterations_);
  198. physicsSteping_ = false;
  199. for (unsigned i = 0; i < rigidBodies_.Size(); ++i)
  200. rigidBodies_[i]->ApplyWorldTransform();
  201. SendBeginContactEvents();
  202. SendEndContactEvents();
  203. using namespace PhysicsPostStep2D;
  204. SendEvent(E_PHYSICSPOSTSTEP2D, eventData);
  205. }
  206. void PhysicsWorld2D::DrawDebugGeometry()
  207. {
  208. DebugRenderer* debug = GetComponent<DebugRenderer>();
  209. if (debug)
  210. DrawDebugGeometry(debug, false);
  211. }
  212. void PhysicsWorld2D::SetDrawShape(bool drawShape)
  213. {
  214. if (drawShape)
  215. m_drawFlags |= e_shapeBit;
  216. else
  217. m_drawFlags &= ~e_shapeBit;
  218. }
  219. void PhysicsWorld2D::SetDrawJoint(bool drawJoint)
  220. {
  221. if (drawJoint)
  222. m_drawFlags |= e_jointBit;
  223. else
  224. m_drawFlags &= ~e_jointBit;
  225. }
  226. void PhysicsWorld2D::SetDrawAabb(bool drawAabb)
  227. {
  228. if (drawAabb)
  229. m_drawFlags |= e_aabbBit;
  230. else
  231. m_drawFlags &= ~e_aabbBit;
  232. }
  233. void PhysicsWorld2D::SetDrawPair(bool drawPair)
  234. {
  235. if (drawPair)
  236. m_drawFlags |= e_pairBit;
  237. else
  238. m_drawFlags &= ~e_pairBit;
  239. }
  240. void PhysicsWorld2D::SetDrawCenterOfMass(bool drawCenterOfMass)
  241. {
  242. if (drawCenterOfMass)
  243. m_drawFlags |= e_centerOfMassBit;
  244. else
  245. m_drawFlags &= ~e_centerOfMassBit;
  246. }
  247. void PhysicsWorld2D::SetAllowSleeping(bool enable)
  248. {
  249. world_->SetAllowSleeping(enable);
  250. }
  251. void PhysicsWorld2D::SetWarmStarting(bool enable)
  252. {
  253. world_->SetWarmStarting(enable);
  254. }
  255. void PhysicsWorld2D::SetContinuousPhysics(bool enable)
  256. {
  257. world_->SetContinuousPhysics(enable);
  258. }
  259. void PhysicsWorld2D::SetSubStepping(bool enable)
  260. {
  261. world_->SetSubStepping(enable);
  262. }
  263. void PhysicsWorld2D::SetGravity(const Vector2& gravity)
  264. {
  265. gravity_ = gravity;
  266. world_->SetGravity(ToB2Vec2(gravity_));
  267. }
  268. void PhysicsWorld2D::SetAutoClearForces(bool enable)
  269. {
  270. world_->SetAutoClearForces(enable);
  271. }
  272. void PhysicsWorld2D::SetVelocityIterations(int velocityIterations)
  273. {
  274. velocityIterations_ = velocityIterations;
  275. }
  276. void PhysicsWorld2D::SetPositionIterations(int positionIterations)
  277. {
  278. positionIterations_ = positionIterations;
  279. }
  280. void PhysicsWorld2D::AddRigidBody(RigidBody2D* rigidBody)
  281. {
  282. if (!rigidBody)
  283. return;
  284. WeakPtr<RigidBody2D> rigidBodyPtr(rigidBody);
  285. if (rigidBodies_.Contains(rigidBodyPtr))
  286. return;
  287. rigidBodies_.Push(rigidBodyPtr);
  288. }
  289. void PhysicsWorld2D::RemoveRigidBody(RigidBody2D* rigidBody)
  290. {
  291. if (!rigidBody)
  292. return;
  293. WeakPtr<RigidBody2D> rigidBodyPtr(rigidBody);
  294. rigidBodies_.Remove(rigidBodyPtr);
  295. }
  296. // Ray cast call back class.
  297. class RayCastCallback : public b2RayCastCallback
  298. {
  299. public:
  300. // Construct.
  301. RayCastCallback(PODVector<PhysicsRaycastResult2D>& results, const Vector2& startPoint, unsigned collisionMask) :
  302. results_(results),
  303. startPoint_(startPoint),
  304. collisionMask_(collisionMask)
  305. {
  306. }
  307. // Called for each fixture found in the query.
  308. virtual float32 ReportFixture(b2Fixture* fixture, const b2Vec2& point, const b2Vec2& normal, float32 fraction)
  309. {
  310. // Ignore sensor
  311. if (fixture->IsSensor())
  312. return true;
  313. if ((fixture->GetFilterData().maskBits & collisionMask_) == 0)
  314. return true;
  315. PhysicsRaycastResult2D result;
  316. result.position_ = ToVector2(point);
  317. result.normal_ = ToVector2(normal);
  318. result.distance_ = (result.position_ - startPoint_).Length();
  319. result.body_ = (RigidBody2D*)(fixture->GetBody()->GetUserData());
  320. results_.Push(result);
  321. return true;
  322. }
  323. protected:
  324. // Physics raycast results.
  325. PODVector<PhysicsRaycastResult2D>& results_;
  326. // Start point.
  327. Vector2 startPoint_;
  328. // Collision mask.
  329. unsigned collisionMask_;
  330. };
  331. void PhysicsWorld2D::Raycast(PODVector<PhysicsRaycastResult2D>& results, const Vector2& startPoint, const Vector2& endPoint, unsigned collisionMask)
  332. {
  333. results.Clear();
  334. RayCastCallback callback(results, startPoint, collisionMask);
  335. world_->RayCast(&callback, ToB2Vec2(startPoint), ToB2Vec2(endPoint));
  336. }
  337. // Single ray cast call back class.
  338. class SingleRayCastCallback : public b2RayCastCallback
  339. {
  340. public:
  341. // Construct.
  342. SingleRayCastCallback(PhysicsRaycastResult2D& result, const Vector2& startPoint, unsigned collisionMask) :
  343. result_(result),
  344. startPoint_(startPoint),
  345. collisionMask_(collisionMask),
  346. minDistance_(M_INFINITY)
  347. {
  348. }
  349. // Called for each fixture found in the query.
  350. virtual float32 ReportFixture(b2Fixture* fixture, const b2Vec2& point, const b2Vec2& normal, float32 fraction)
  351. {
  352. // Ignore sensor
  353. if (fixture->IsSensor())
  354. return true;
  355. if ((fixture->GetFilterData().maskBits & collisionMask_) == 0)
  356. return true;
  357. float distance = (ToVector2(point)- startPoint_).Length();
  358. if (distance < minDistance_)
  359. {
  360. minDistance_ = distance;
  361. result_.position_ = ToVector2(point);
  362. result_.normal_ = ToVector2(normal);
  363. result_.distance_ = distance;
  364. result_.body_ = (RigidBody2D*)(fixture->GetBody()->GetUserData());
  365. }
  366. return true;
  367. }
  368. private:
  369. // Physics raycast result.
  370. PhysicsRaycastResult2D& result_;
  371. // Start point.
  372. Vector2 startPoint_;
  373. // Collision mask.
  374. unsigned collisionMask_;
  375. // Minimum distance.
  376. float minDistance_;
  377. };
  378. void PhysicsWorld2D::RaycastSingle(PhysicsRaycastResult2D& result, const Vector2& startPoint, const Vector2& endPoint, unsigned collisionMask)
  379. {
  380. result.body_ = 0;
  381. SingleRayCastCallback callback(result, startPoint, collisionMask);
  382. world_->RayCast(&callback, ToB2Vec2(startPoint), ToB2Vec2(endPoint));
  383. }
  384. // Point query callback class.
  385. class PointQueryCallback : public b2QueryCallback
  386. {
  387. public:
  388. // Construct.
  389. PointQueryCallback(const b2Vec2& point, unsigned collisionMask) :
  390. point_(point),
  391. collisionMask_(collisionMask),
  392. rigidBody_(0)
  393. {
  394. }
  395. // Called for each fixture found in the query AABB.
  396. virtual bool ReportFixture(b2Fixture* fixture)
  397. {
  398. // Ignore sensor
  399. if (fixture->IsSensor())
  400. return true;
  401. if ((fixture->GetFilterData().maskBits & collisionMask_) == 0)
  402. return true;
  403. if (fixture->TestPoint(point_))
  404. {
  405. rigidBody_ = (RigidBody2D*)(fixture->GetBody()->GetUserData());
  406. return false;
  407. }
  408. return true;
  409. }
  410. // Return rigid body.
  411. RigidBody2D* GetRigidBody() const { return rigidBody_; }
  412. private:
  413. // Point.
  414. b2Vec2 point_;
  415. // Collision mask.
  416. unsigned collisionMask_;
  417. // Rigid body.
  418. RigidBody2D* rigidBody_;
  419. };
  420. RigidBody2D* PhysicsWorld2D::GetRigidBody(const Vector2& point, unsigned collisionMask)
  421. {
  422. PointQueryCallback callback(ToB2Vec2(point), collisionMask);
  423. b2AABB b2Aabb;
  424. Vector2 delta(M_EPSILON, M_EPSILON);
  425. b2Aabb.lowerBound = ToB2Vec2(point - delta);
  426. b2Aabb.upperBound = ToB2Vec2(point + delta);
  427. world_->QueryAABB(&callback, b2Aabb);
  428. return callback.GetRigidBody();
  429. }
  430. RigidBody2D* PhysicsWorld2D::GetRigidBody(int screenX, int screenY, unsigned collisionMask)
  431. {
  432. Renderer* renderer = GetSubsystem<Renderer>();
  433. for (unsigned i = 0; i < renderer->GetNumViewports(); ++i)
  434. {
  435. Viewport* viewport = renderer->GetViewport(i);
  436. // Find a viewport with same scene
  437. if (viewport && viewport->GetScene() == GetScene())
  438. {
  439. Vector3 worldPoint = viewport->ScreenToWorldPoint(screenX, screenY, 0.0f);
  440. return GetRigidBody(Vector2(worldPoint.x_, worldPoint.y_), collisionMask);
  441. }
  442. }
  443. return 0;
  444. }
  445. // Aabb query callback class.
  446. class AabbQueryCallback : public b2QueryCallback
  447. {
  448. public:
  449. // Construct.
  450. AabbQueryCallback(PODVector<RigidBody2D*>& results, unsigned collisionMask) :
  451. results_(results),
  452. collisionMask_(collisionMask)
  453. {
  454. }
  455. // Called for each fixture found in the query AABB.
  456. virtual bool ReportFixture(b2Fixture* fixture)
  457. {
  458. // Ignore sensor
  459. if (fixture->IsSensor())
  460. return true;
  461. if ((fixture->GetFilterData().maskBits & collisionMask_) == 0)
  462. return true;
  463. results_.Push((RigidBody2D*)(fixture->GetBody()->GetUserData()));
  464. return true;
  465. }
  466. private:
  467. // Results.
  468. PODVector<RigidBody2D*>& results_;
  469. // Collision mask.
  470. unsigned collisionMask_;
  471. };
  472. void PhysicsWorld2D::GetRigidBodies(PODVector<RigidBody2D*>& results, const Rect& aabb, unsigned collisionMask)
  473. {
  474. AabbQueryCallback callback(results, collisionMask);
  475. b2AABB b2Aabb;
  476. b2Aabb.lowerBound = ToB2Vec2(aabb.min_);
  477. b2Aabb.upperBound = ToB2Vec2(aabb.max_);
  478. world_->QueryAABB(&callback, b2Aabb);
  479. }
  480. bool PhysicsWorld2D::GetAllowSleeping() const
  481. {
  482. return world_->GetAllowSleeping();
  483. }
  484. bool PhysicsWorld2D::GetWarmStarting() const
  485. {
  486. return world_->GetWarmStarting();
  487. }
  488. bool PhysicsWorld2D::GetContinuousPhysics() const
  489. {
  490. return world_->GetContinuousPhysics();
  491. }
  492. bool PhysicsWorld2D::GetSubStepping() const
  493. {
  494. return world_->GetSubStepping();
  495. }
  496. bool PhysicsWorld2D::GetAutoClearForces() const
  497. {
  498. return world_->GetAutoClearForces();
  499. }
  500. void PhysicsWorld2D::OnNodeSet(Node* node)
  501. {
  502. // Subscribe to the scene subsystem update, which will trigger the physics simulation step
  503. if (node)
  504. {
  505. scene_ = GetScene();
  506. SubscribeToEvent(node, E_SCENESUBSYSTEMUPDATE, HANDLER(PhysicsWorld2D, HandleSceneSubsystemUpdate));
  507. }
  508. }
  509. void PhysicsWorld2D::HandleSceneSubsystemUpdate(StringHash eventType, VariantMap& eventData)
  510. {
  511. using namespace SceneSubsystemUpdate;
  512. Update(eventData[P_TIMESTEP].GetFloat());
  513. }
  514. void PhysicsWorld2D::SendBeginContactEvents()
  515. {
  516. if (beginContactInfos_.Empty())
  517. return;
  518. using namespace PhysicsBeginContact2D;
  519. VariantMap& eventData = GetEventDataMap();
  520. eventData[P_WORLD] = this;
  521. for (unsigned i = 0; i < beginContactInfos_.Size(); ++i)
  522. {
  523. ContactInfo& contactInfo = beginContactInfos_[i];
  524. eventData[P_BODYA] = contactInfo.bodyA_.Get();
  525. eventData[P_BODYB] = contactInfo.bodyB_.Get();
  526. eventData[P_NODEA] = contactInfo.nodeA_.Get();
  527. eventData[P_NODEB] = contactInfo.nodeB_.Get();
  528. SendEvent(E_PHYSICSBEGINCONTACT2D, eventData);
  529. }
  530. beginContactInfos_.Clear();
  531. }
  532. void PhysicsWorld2D::SendEndContactEvents()
  533. {
  534. if (endContactInfos_.Empty())
  535. return;
  536. using namespace PhysicsEndContact2D;
  537. VariantMap& eventData = GetEventDataMap();
  538. eventData[P_WORLD] = this;
  539. for (unsigned i = 0; i < endContactInfos_.Size(); ++i)
  540. {
  541. ContactInfo& contactInfo = endContactInfos_[i];
  542. eventData[P_BODYA] = contactInfo.bodyA_.Get();
  543. eventData[P_BODYB] = contactInfo.bodyB_.Get();
  544. eventData[P_NODEA] = contactInfo.nodeA_.Get();
  545. eventData[P_NODEB] = contactInfo.nodeB_.Get();
  546. SendEvent(E_PHYSICSENDCONTACT2D, eventData);
  547. }
  548. endContactInfos_.Clear();
  549. }
  550. PhysicsWorld2D::ContactInfo::ContactInfo()
  551. {
  552. }
  553. PhysicsWorld2D::ContactInfo::ContactInfo(b2Contact* contact)
  554. {
  555. b2Fixture* fixtureA = contact->GetFixtureA();
  556. b2Fixture* fixtureB = contact->GetFixtureB();
  557. bodyA_ = (RigidBody2D*)(fixtureA->GetBody()->GetUserData());
  558. bodyB_ = (RigidBody2D*)(fixtureB->GetBody()->GetUserData());
  559. nodeA_ = bodyA_->GetNode();
  560. nodeB_ = bodyB_->GetNode();
  561. }
  562. PhysicsWorld2D::ContactInfo::ContactInfo(const ContactInfo& other) :
  563. bodyA_(other.bodyA_),
  564. bodyB_(other.bodyB_),
  565. nodeA_(other.nodeA_),
  566. nodeB_(other.nodeB_)
  567. {
  568. }
  569. }