Browse Source

Callbacks now actually fire when they're supposed to, instead of post-step (means PreSolve collision filtering works)

--HG--
branch : box2d-update
Bill Meltsner 14 years ago
parent
commit
16fb73e92a
2 changed files with 36 additions and 87 deletions
  1. 35 80
      src/modules/physics/box2d/World.cpp
  2. 1 7
      src/modules/physics/box2d/World.h

+ 35 - 80
src/modules/physics/box2d/World.cpp

@@ -44,90 +44,50 @@ namespace box2d
 		if(ref != 0)
 			delete ref;
 	}
-
-	void World::ContactCallback::add(b2Contact* contact)
-	{
-		/**
-		* We must copy contacts, since we're not allowed to process
-		* them inside this function. Removing bodies in this function
-		* pretty much guarantees segfault. ^^
-		**/
-
-		if(ref != 0)
-			contacts.push_back(new Contact(contact));
-	}
 	
-	void World::ContactCallback::add(b2Contact* contact, const b2ContactImpulse* impulse)
-	{
-		if(ref != 0) {
-			contacts.push_back(new Contact(contact));
-			// copy the impulse struct
-			b2ContactImpulse * i = new b2ContactImpulse();
-			i->normalImpulses[0] = impulse->normalImpulses[0];
-			i->normalImpulses[1] = impulse->normalImpulses[1];
-			i->tangentImpulses[0] = impulse->tangentImpulses[0];
-			i->tangentImpulses[1] = impulse->tangentImpulses[1];
-			i->count = impulse->count;
-			
-			impulses.push_back(i);
-		}
-	}
-
-	void World::ContactCallback::process()
+	void World::ContactCallback::process(b2Contact* contact, const b2ContactImpulse* impulse)
 	{
 		// Process contacts.
 		if(ref != 0)
 		{
 			lua_State * L = ref->getL();
-			for(int i = 0;i<(int)contacts.size();i++)
-			{
-				// Push the function.
-				ref->push();
+			ref->push();
 
-				// Push first fixture.
-				{
-					Fixture * a = (Fixture *)Memoizer::find(contacts[i]->contact->GetFixtureA());
-					if(a != 0) {
-						a->retain();
-						luax_newtype(L, "Fixture", PHYSICS_FIXTURE_T, (void*)a);
-					}
-					else
-						throw love::Exception("A fixture has escaped Memoizer!");
+			// Push first fixture.
+			{
+				Fixture * a = (Fixture *)Memoizer::find(contact->GetFixtureA());
+				if(a != 0) {
+					a->retain();
+					luax_newtype(L, "Fixture", PHYSICS_FIXTURE_T, (void*)a);
 				}
+				else
+					throw love::Exception("A fixture has escaped Memoizer!");
+			}
 
-				// Push second userdata.
-				{
-					Fixture * b = (Fixture *)Memoizer::find(contacts[i]->contact->GetFixtureB());
-					if(b != 0) {
-						b->retain();
-						luax_newtype(L, "Fixture", PHYSICS_FIXTURE_T, (void*)b);
-					}
-					else
-						throw love::Exception("A fixture has escaped Memoizer!");
+			// Push second userdata.
+			{
+				Fixture * b = (Fixture *)Memoizer::find(contact->GetFixtureB());
+				if(b != 0) {
+					b->retain();
+					luax_newtype(L, "Fixture", PHYSICS_FIXTURE_T, (void*)b);
 				}
+				else
+					throw love::Exception("A fixture has escaped Memoizer!");
+			}
+		
+			Contact * c = new Contact(contact);
 
-				luax_newtype(L, "Contact", (PHYSICS_CONTACT_T), (void*)contacts[i], false);
-				
-				int args = 3;
-				if ((int)impulses.size() > i) {
-					const b2ContactImpulse * impulse = impulses[i];
-					for (int c = 0; c < impulse->count; c++) {
-						lua_pushnumber(L, Physics::scaleUp(impulse->normalImpulses[c]));
-						lua_pushnumber(L, Physics::scaleUp(impulse->tangentImpulses[c]));
-						args += 2;
-					}
+			luax_newtype(L, "Contact", (PHYSICS_CONTACT_T), (void*)c, false);
+			
+			int args = 3;
+			if (impulse) {
+				for (int c = 0; c < impulse->count; c++) {
+					lua_pushnumber(L, Physics::scaleUp(impulse->normalImpulses[c]));
+					lua_pushnumber(L, Physics::scaleUp(impulse->tangentImpulses[c]));
+					args += 2;
 				}
-				lua_call(L, args, 0);
 			}
-
-			// Clear contacts.
-			for(int i = 0;i<(int)contacts.size();i++)
-				delete contacts[i];
-			contacts.clear();
-			// Clear impulses.
-			for(int i = 0;i<(int)impulses.size();i++)
-				delete impulses[i];
-			impulses.clear();
+			lua_call(L, args, 0);
 		}
 
 	}
@@ -250,32 +210,27 @@ namespace box2d
 	void World::update(float dt)
 	{
 		world->Step(dt, 8, 6);
-
-		begin.process();
-		end.process();
-		presolve.process();
-		postsolve.process();
 	}
 
 	void World::BeginContact(b2Contact* contact)
 	{
-		begin.add(contact);
+		begin.process(contact);
 	}
 
 	void World::EndContact(b2Contact* contact)
 	{
-		end.add(contact);
+		end.process(contact);
 	}
 
 	void World::PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
 	{
 		B2_NOT_USED(oldManifold); // not sure what to do with this
-		presolve.add(contact);
+		presolve.process(contact);
 	}
 
 	void World::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
 	{
-		postsolve.add(contact, impulse);
+		postsolve.process(contact, impulse);
 	}
 	
 	bool World::ShouldCollide(b2Fixture * fixtureA, b2Fixture * fixtureB)

+ 1 - 7
src/modules/physics/box2d/World.h

@@ -67,15 +67,9 @@ namespace box2d
 		{
 		public:
 			Reference * ref;
-			std::vector<Contact *> contacts;
-			std::vector<const b2ContactImpulse *> impulses;
-			b2Manifold* oldManifold;
-			b2ContactImpulse* impulse;
 			ContactCallback();
 			~ContactCallback();
-			void add(b2Contact* contact);
-			void add(b2Contact* contact, const b2ContactImpulse* impulse);
-			void process();
+			void process(b2Contact* contact, const b2ContactImpulse* impulse = NULL);
 		};
 		
 		class ContactFilter