Commits

Bart van Strien committed ab6f7f6

Wait until the end of a physics update before destroying bodies (issue #193)

Comments (0)

Files changed (5)

src/modules/physics/box2d/Body.cpp

 		return 2;
 	}
 
+	void Body::destroy()
+	{
+		world->destroyBody(this);
+	}
+
 } // box2d
 } // physics
 } // love

src/modules/physics/box2d/Body.h

 		* Get the World this Body resides in.
 		*/
 		World * getWorld() const;
+		
+		/**
+		 * Mark the body for destruction
+		 **/
+		void destroy();
 	private:
 
 		/**

src/modules/physics/box2d/World.cpp

 		persist.process();
 		remove.process();
 		result.process();
+		
+		// Really destroy all marked bodies.
+		for (std::vector<Body*>::iterator i = destructBodies.begin(); i < destructBodies.end(); i++)
+		{
+			Body * b = *i;
+			b->release();
+		}
+		destructBodies.clear();
 	}
 
 	void World::Add(const b2ContactPoint* point)
 		t.upperBound = scaleUp(aabb.upperBound);
 		return t;
 	}
+	
+	void World::destroyBody(Body * b)
+	{
+		destructBodies.push_back(b);
+	}
 
 } // box2d
 } // physics

src/modules/physics/box2d/World.h

 {
 
 	class Contact;
+	class Body;
 
 	/**
 	* The World is the "God" container class,
 
 		// The length of one meter in pixels.
 		int meter;
+		
+		// The list of to be destructed bodies.
+		std::vector<Body*> destructBodies;
 
 	public:
 
 		* @return The scaled AABB.
 		**/
 		b2AABB scaleUp(const b2AABB & aabb);
+		
+		/**
+		 * Mark a body for destruction.
+		 * To be called from Body
+		 **/
+		void destroyBody(Body * b);
 
 	};
 

src/modules/physics/box2d/wrap_Body.cpp

 		p->own = false;
 
 		Body * t = (Body *)p->data;
-		t->release();
+		t->destroy();
 
 		return 0;
 	}