Browse Source

Bring back some elements of better Body destruction that got lost in the merge

Bill Meltsner 14 years ago
parent
commit
56ae3a95fc

+ 5 - 0
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:
 
 		/**

+ 13 - 0
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<Body*>::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

+ 10 - 0
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<Body*> 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);
 
 	};