From 56ae3a95fc54682158021d9eb349043b9e594498 Mon Sep 17 00:00:00 2001 From: Bill Meltsner Date: Wed, 12 Oct 2011 13:51:47 -0500 Subject: [PATCH] Bring back some elements of better Body destruction that got lost in the merge --- src/modules/physics/box2d/Body.h | 5 +++++ src/modules/physics/box2d/World.cpp | 13 +++++++++++++ src/modules/physics/box2d/World.h | 10 ++++++++++ 3 files changed, 28 insertions(+) diff --git a/src/modules/physics/box2d/Body.h b/src/modules/physics/box2d/Body.h index f5b457bab..4db997fff 100644 --- a/src/modules/physics/box2d/Body.h +++ b/src/modules/physics/box2d/Body.h @@ -385,6 +385,11 @@ namespace box2d **/ int getFixtureList(lua_State * L) const; + /** + * Mark the body for destruction + **/ + void destroy(); + private: /** diff --git a/src/modules/physics/box2d/World.cpp b/src/modules/physics/box2d/World.cpp index e35b4b5d6..8e292d7c6 100644 --- a/src/modules/physics/box2d/World.cpp +++ b/src/modules/physics/box2d/World.cpp @@ -210,6 +210,14 @@ namespace box2d void World::update(float dt) { world->Step(dt, 8, 6); + + // Really destroy all marked bodies. + for (std::vector::iterator i = destructBodies.begin(); i < destructBodies.end(); i++) + { + Body * b = *i; + b->release(); + } + destructBodies.clear(); } void World::BeginContact(b2Contact* contact) @@ -422,6 +430,11 @@ namespace box2d world->RayCast(&raycast, v1, v2); return 0; } + + void World::destroyBody(Body * b) + { + destructBodies.push_back(b); + } } // box2d } // physics diff --git a/src/modules/physics/box2d/World.h b/src/modules/physics/box2d/World.h index fc6d3aca8..e345f2fba 100644 --- a/src/modules/physics/box2d/World.h +++ b/src/modules/physics/box2d/World.h @@ -40,6 +40,7 @@ namespace box2d { class Contact; + class Body; class Fixture; /** @@ -106,6 +107,9 @@ namespace box2d // Ground body b2Body * groundBody; + + // The list of to be destructed bodies. + std::vector destructBodies; // Contact callbacks. ContactCallback begin, end, presolve, postsolve; @@ -255,6 +259,12 @@ namespace box2d * Raycasts the World for all Fixtures in the path of the ray. **/ int rayCast(lua_State * L); + + /** + * Mark a body for destruction. + * To be called from Body + **/ + void destroyBody(Body * b); };