1. Bill Meltsner
  2. love-ios

Commits

Bill Meltsner  committed 0a05fba

Update World contact callback definitions, get ready for collision overhaul

  • Participants
  • Parent commits 56378b5
  • Branches box2d-update

Comments (0)

Files changed (6)

File src/modules/physics/box2d/Contact.cpp

View file
 {
 namespace box2d
 {
-	Contact::Contact(World * world, const b2ContactPoint * point)
-		: point(*point), world(world)
+	Contact::Contact(World * world, b2Contact * contact)
+		: contact(contact), world(world)
 	{
 		world->retain();
 	}

File src/modules/physics/box2d/Contact.h

View file
 
 	private:
 
-		// The Box2D contact point.
-		b2ContactPoint point;
+		// The Box2D contact.
+		b2Contact* contact;
 
 		// The parent world. Needed for scaling.
 		World * world;
 		* data pointed to.
 		* @param point Pointer to the Box2D contact.
 		**/
-		Contact(World * world, const b2ContactPoint * point);
+		Contact(World * world, const b2Contact * contact);
 
 		virtual ~Contact();
 

File src/modules/physics/box2d/World.cpp

View file
 
 #include "World.h"
 
+#include "Fixture.h"
 #include "Shape.h"
 #include "Contact.h"
 #include "Physics.h"
 			delete ref;
 	}
 
-	void World::ContactCallback::add(World * world, const b2ContactPoint* point)
+	void World::ContactCallback::add(World * world, const b2Contact* contact)
 	{
 		/**
 		* We must copy contacts, since we're not allowed to process
 		**/
 
 		if(ref != 0)
-			contacts.push_back(new Contact(world, point));
+			contacts.push_back(new Contact(world, contact));
 	}
 
 	void World::ContactCallback::process()
 
 				// Push first userdata.
 				{
-					shapeudata * d = (shapeudata *)(contacts[i]->point.shape1->GetUserData());
+					fixtureudata * d = (fixtureudata *)(contacts[i]->contact->GetFixtureA()->GetUserData());
 					if(d->ref != 0)
 						d->ref->push();
 					else
 						lua_pushnil(L);
 				}
 
-				// Push first userdata.
+				// Push second userdata.
 				{
-					shapeudata * d = (shapeudata *)(contacts[i]->point.shape2->GetUserData());
+					fixtureudata * d = (fixtureudata *)(contacts[i]->contact->GetFixtureB()->GetUserData());
 					if(d->ref != 0)
 						d->ref->push();
 					else
 	World::World()
 		: world(NULL)
 	{
-		world = new b2World(b2Vec2(0,0), true);
+		world = new b2World(b2Vec2(0,0));
+		world->SetAllowSleeping(true);
 		world->SetContactListener(this);
 		b2BodyDef def;
-		groundBody = world->CreateBody(def);
+		groundBody = world->CreateBody(&def);
 	}
 
 	World::World(b2Vec2 gravity, bool sleep)
 		: world(NULL)
 	{
-		world = new b2World(Physics::scaleDown(gravity), sleep);
+		world = new b2World(Physics::scaleDown(gravity));
+		world->SetAllowSleeping(sleep);
 		world->SetContactListener(this);
 	}
 
 
 	void World::update(float dt)
 	{
-		world->Step(dt, 10);
+		world->Step(dt, 8, 6);
 
 
-		add.process();
-		persist.process();
-		remove.process();
-		result.process();
+		begin.process();
+		end.process();
+		presolve.process();
+		postsolve.process();
 	}
 
-	void World::Add(const b2ContactPoint* point)
+	void World::BeginContact(b2Contact* contact)
 	{
-		add.add(this, point);
+		begin.add(this, contact);
 	}
 
-	void World::Persist(const b2ContactPoint* point)
+	void World::EndContact(b2Contact* contact)
 	{
-		persist.add(this, point);
+		end.add(this, contact);
 	}
 
-	void World::Remove(const b2ContactPoint* point)
+	void World::PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
 	{
-		remove.add(this, point);
+		presolve.add(this, contact);
 	}
 
-	void World::Result(const b2ContactPoint* point)
+	void World::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
 	{
-		result.add(this, point);
+		postsolve.add(this, contact);
 	}
 
 	int World::setCallbacks(lua_State * L)
 		switch(n)
 		{
 		case 4:
-			if (result.ref) delete result.ref;
-			result.ref = luax_refif(L, LUA_TFUNCTION);
+			if (postsolve.ref) delete postsolve.ref;
+			postsolve.ref = luax_refif(L, LUA_TFUNCTION);
 		case 3:
-			if (remove.ref) delete remove.ref;
-			remove.ref = luax_refif(L, LUA_TFUNCTION);
+			if (presolve.ref) delete presolve.ref;
+			presolve.ref = luax_refif(L, LUA_TFUNCTION);
 		case 2:
-			if (persist.ref) delete persist.ref;
-			persist.ref = luax_refif(L, LUA_TFUNCTION);
+			if (end.ref) delete end.ref;
+			end.ref = luax_refif(L, LUA_TFUNCTION);
 		case 1:
-			if (add.ref) delete add.ref;
-			add.ref = luax_refif(L, LUA_TFUNCTION);
+			if (begin.ref) delete begin.ref;
+			begin.ref = luax_refif(L, LUA_TFUNCTION);
 		}
 
 		return 0;
 
 	int World::getCallbacks(lua_State * L)
 	{
-		add.ref ? add.ref->push() : lua_pushnil(L);
-		persist.ref ? persist.ref->push() : lua_pushnil(L);
-		remove.ref ? remove.ref->push() : lua_pushnil(L);
-		result.ref ? result.ref->push() : lua_pushnil(L);
+		begin.ref ? begin.ref->push() : lua_pushnil(L);
+		end.ref ? end.ref->push() : lua_pushnil(L);
+		presolve.ref ? presolve.ref->push() : lua_pushnil(L);
+		postsolve.ref ? postsolve.ref->push() : lua_pushnil(L);
 		return lua_gettop(L);
 	}
 
 
 	int World::getGravity(lua_State * L)
 	{
-		b2Vec2 v = Physics::scaleUp(world->m_gravity);
+		b2Vec2 v = Physics::scaleUp(world->GetGravity());
 		lua_pushnumber(L, v.x);
 		lua_pushnumber(L, v.y);
 		return 2;
 	}
 
-	void World::setAllowSleep(bool allow)
+	void World::setAllowSleeping(bool allow)
 	{
-		world->m_allowSleep = allow;
+		world->SetAllowSleeping(allow);
 	}
 
-	bool World::isAllowSleep() const
+	bool World::getAllowSleeping() const
 	{
-		return world->m_allowSleep;
+		return world->GetAllowSleeping();
 	}
 
 	int World::getBodyCount()

File src/modules/physics/box2d/World.h

View file
 			std::vector<Contact *> contacts;
 			ContactCallback();
 			~ContactCallback();
-			void add(World * world, const b2ContactPoint* point);
+			void add(World * world, const b2Contact* contact);
 			void process();
 		};
 
         b2Body * groundBody;
 
 		// Contact callbacks.
-		ContactCallback add, persist, remove, result;
+		ContactCallback begin, end, presolve, postsolve;
 
 	public:
 
 		void update(float dt);
 
 		// From b2ContactListener
-		void Add(const b2ContactPoint* point);
-		void Persist(const b2ContactPoint* point);
-		void Remove(const b2ContactPoint* point);
-		void Result(const b2ContactPoint* point);
+		void BeginContact(b2Contact* contact);
+		void EndContact(b2Contact* contact);
+		void PreSolve(b2Contact* contact, const b2Manifold* oldManifold);
+		void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse);
 
 		/**
 		* Receives up to four Lua functions as arguments. Each function is
-		* collision callback for the four events (in order): add, persist,
-		* remove and result. The value "nil" is accepted if one or more events
-		* are uninteresting.
+		* collision callback for the four events (in order): begin, end,
+		* presolve and postsolve. The value "nil" is accepted if one or
+		* more events are uninteresting.
 		**/
 		int setCallbacks(lua_State * L);
 
 		* Sets whether this World allows sleep.
 		* @param allow True to allow, false to disallow.
 		**/
-		void setAllowSleep(bool allow);
+		void setAllowSleeping(bool allow);
 
 		/**
 		* Returns whether this World allows sleep.
 		* @return True if allowed, false if disallowed.
 		**/
-		bool isAllowSleep() const;
+		bool getAllowSleeping() const;
 
 		/**
 		* Get the current body count.

File src/modules/physics/box2d/wrap_World.cpp

View file
 		return t->getGravity(L);
 	}
 
-	int w_World_setAllowSleep(lua_State * L)
+	int w_World_setAllowSleeping(lua_State * L)
 	{
 		World * t = luax_checkworld(L, 1);
 		bool b = luax_toboolean(L, 2);
-		t->setAllowSleep(b);
+		t->setAllowSleeping(b);
 		return 0;
 	}
 
-	int w_World_isAllowSleep(lua_State * L)
+	int w_World_getAllowSleeping(lua_State * L)
 	{
 		World * t = luax_checkworld(L, 1);
-		luax_pushboolean(L, t->isAllowSleep());
+		luax_pushboolean(L, t->getAllowSleeping());
 		return 1;
 	}
 
 		{ "getCallbacks", w_World_getCallbacks },
 		{ "setGravity", w_World_setGravity },
 		{ "getGravity", w_World_getGravity },
-		{ "setAllowSleep", w_World_setAllowSleep },
-		{ "isAllowSleep", w_World_isAllowSleep },
+		{ "setAllowSleeping", w_World_setAllowSleeping },
+		{ "getAllowSleeping", w_World_getAllowSleeping },
 		{ "getBodyCount", w_World_getBodyCount },
 		{ "getJointCount", w_World_getJointCount },
 		{ 0, 0 }

File src/modules/physics/box2d/wrap_World.h

View file
 	int w_World_getCallbacks(lua_State * L);
 	int w_World_setGravity(lua_State * L);
 	int w_World_getGravity(lua_State * L);
-	int w_World_setAllowSleep(lua_State * L);
-	int w_World_isAllowSleep(lua_State * L);
+	int w_World_setAllowSleeping(lua_State * L);
+	int w_World_getAllowSleeping(lua_State * L);
 	int w_World_getBodyCount(lua_State * L);
 	int w_World_getJointCount(lua_State * L);
 	int luaopen_world(lua_State * L);