From fafe0d703955fbc02f79c9e4f1e1d7dc55ba759d Mon Sep 17 00:00:00 2001 From: Bart van Strien Date: Fri, 10 Jun 2011 14:03:56 +0200 Subject: [PATCH] Wait until the end of a physics update before destroying bodies (issue #193) --- src/modules/physics/box2d/Body.cpp | 5 +++++ src/modules/physics/box2d/Body.h | 5 +++++ src/modules/physics/box2d/World.cpp | 13 +++++++++++++ src/modules/physics/box2d/World.h | 10 ++++++++++ src/modules/physics/box2d/wrap_Body.cpp | 2 +- 5 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/modules/physics/box2d/Body.cpp b/src/modules/physics/box2d/Body.cpp index 5487414c8..97109029c 100644 --- a/src/modules/physics/box2d/Body.cpp +++ b/src/modules/physics/box2d/Body.cpp @@ -329,6 +329,11 @@ namespace box2d return 2; } + void Body::destroy() + { + world->destroyBody(this); + } + } // box2d } // physics } // love diff --git a/src/modules/physics/box2d/Body.h b/src/modules/physics/box2d/Body.h index 9444a39d5..a60e514a5 100644 --- a/src/modules/physics/box2d/Body.h +++ b/src/modules/physics/box2d/Body.h @@ -346,6 +346,11 @@ namespace box2d * Get the World this Body resides in. */ World * getWorld() 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 87594530f..c25753471 100644 --- a/src/modules/physics/box2d/World.cpp +++ b/src/modules/physics/box2d/World.cpp @@ -123,6 +123,14 @@ namespace box2d persist.process(); remove.process(); result.process(); + + // 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::Add(const b2ContactPoint* point) @@ -272,6 +280,11 @@ namespace box2d t.upperBound = scaleUp(aabb.upperBound); return t; } + + 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 af3f84ce5..86acdb8bd 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; /** * The World is the "God" container class, @@ -83,6 +84,9 @@ namespace box2d // The length of one meter in pixels. int meter; + + // The list of to be destructed bodies. + std::vector destructBodies; public: @@ -241,6 +245,12 @@ namespace box2d * @return The scaled AABB. **/ b2AABB scaleUp(const b2AABB & aabb); + + /** + * Mark a body for destruction. + * To be called from Body + **/ + void destroyBody(Body * b); }; diff --git a/src/modules/physics/box2d/wrap_Body.cpp b/src/modules/physics/box2d/wrap_Body.cpp index 2d97f4c74..165d73505 100644 --- a/src/modules/physics/box2d/wrap_Body.cpp +++ b/src/modules/physics/box2d/wrap_Body.cpp @@ -462,7 +462,7 @@ namespace box2d p->own = false; Body * t = (Body *)p->data; - t->release(); + t->destroy(); return 0; }