PhysicsWorld2D.cpp 20 KB

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