Update World contact callback definitions, get ready for collision overhaul

--HG--
branch : box2d-update
This commit is contained in:
Bill Meltsner
2011-09-23 17:01:53 -04:00
parent 4980797afc
commit 95f6047417
6 changed files with 65 additions and 62 deletions
+2 -2
View File
@@ -28,8 +28,8 @@ namespace physics
{ {
namespace box2d namespace box2d
{ {
Contact::Contact(World * world, const b2ContactPoint * point) Contact::Contact(World * world, b2Contact * contact)
: point(*point), world(world) : contact(contact), world(world)
{ {
world->retain(); world->retain();
} }
+3 -3
View File
@@ -49,8 +49,8 @@ namespace box2d
private: private:
// The Box2D contact point. // The Box2D contact.
b2ContactPoint point; b2Contact* contact;
// The parent world. Needed for scaling. // The parent world. Needed for scaling.
World * world; World * world;
@@ -63,7 +63,7 @@ namespace box2d
* data pointed to. * data pointed to.
* @param point Pointer to the Box2D contact. * @param point Pointer to the Box2D contact.
**/ **/
Contact(World * world, const b2ContactPoint * point); Contact(World * world, const b2Contact * contact);
virtual ~Contact(); virtual ~Contact();
+41 -38
View File
@@ -20,6 +20,7 @@
#include "World.h" #include "World.h"
#include "Fixture.h"
#include "Shape.h" #include "Shape.h"
#include "Contact.h" #include "Contact.h"
#include "Physics.h" #include "Physics.h"
@@ -43,7 +44,7 @@ namespace box2d
delete ref; 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 * We must copy contacts, since we're not allowed to process
@@ -52,7 +53,7 @@ namespace box2d
**/ **/
if(ref != 0) if(ref != 0)
contacts.push_back(new Contact(world, point)); contacts.push_back(new Contact(world, contact));
} }
void World::ContactCallback::process() void World::ContactCallback::process()
@@ -68,16 +69,16 @@ namespace box2d
// Push first userdata. // Push first userdata.
{ {
shapeudata * d = (shapeudata *)(contacts[i]->point.shape1->GetUserData()); fixtureudata * d = (fixtureudata *)(contacts[i]->contact->GetFixtureA()->GetUserData());
if(d->ref != 0) if(d->ref != 0)
d->ref->push(); d->ref->push();
else else
lua_pushnil(L); 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) if(d->ref != 0)
d->ref->push(); d->ref->push();
else else
@@ -99,16 +100,18 @@ namespace box2d
World::World() World::World()
: world(NULL) : world(NULL)
{ {
world = new b2World(b2Vec2(0,0), true); world = new b2World(b2Vec2(0,0));
world->SetAllowSleeping(true);
world->SetContactListener(this); world->SetContactListener(this);
b2BodyDef def; b2BodyDef def;
groundBody = world->CreateBody(def); groundBody = world->CreateBody(&def);
} }
World::World(b2Vec2 gravity, bool sleep) World::World(b2Vec2 gravity, bool sleep)
: world(NULL) : world(NULL)
{ {
world = new b2World(Physics::scaleDown(gravity), sleep); world = new b2World(Physics::scaleDown(gravity));
world->SetAllowSleeping(sleep);
world->SetContactListener(this); world->SetContactListener(this);
} }
@@ -120,33 +123,33 @@ namespace box2d
void World::update(float dt) void World::update(float dt)
{ {
world->Step(dt, 10); world->Step(dt, 8, 6);
add.process(); begin.process();
persist.process(); end.process();
remove.process(); presolve.process();
result.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) int World::setCallbacks(lua_State * L)
@@ -157,17 +160,17 @@ namespace box2d
switch(n) switch(n)
{ {
case 4: case 4:
if (result.ref) delete result.ref; if (postsolve.ref) delete postsolve.ref;
result.ref = luax_refif(L, LUA_TFUNCTION); postsolve.ref = luax_refif(L, LUA_TFUNCTION);
case 3: case 3:
if (remove.ref) delete remove.ref; if (presolve.ref) delete presolve.ref;
remove.ref = luax_refif(L, LUA_TFUNCTION); presolve.ref = luax_refif(L, LUA_TFUNCTION);
case 2: case 2:
if (persist.ref) delete persist.ref; if (end.ref) delete end.ref;
persist.ref = luax_refif(L, LUA_TFUNCTION); end.ref = luax_refif(L, LUA_TFUNCTION);
case 1: case 1:
if (add.ref) delete add.ref; if (begin.ref) delete begin.ref;
add.ref = luax_refif(L, LUA_TFUNCTION); begin.ref = luax_refif(L, LUA_TFUNCTION);
} }
return 0; return 0;
@@ -175,10 +178,10 @@ namespace box2d
int World::getCallbacks(lua_State * L) int World::getCallbacks(lua_State * L)
{ {
add.ref ? add.ref->push() : lua_pushnil(L); begin.ref ? begin.ref->push() : lua_pushnil(L);
persist.ref ? persist.ref->push() : lua_pushnil(L); end.ref ? end.ref->push() : lua_pushnil(L);
remove.ref ? remove.ref->push() : lua_pushnil(L); presolve.ref ? presolve.ref->push() : lua_pushnil(L);
result.ref ? result.ref->push() : lua_pushnil(L); postsolve.ref ? postsolve.ref->push() : lua_pushnil(L);
return lua_gettop(L); return lua_gettop(L);
} }
@@ -189,20 +192,20 @@ namespace box2d
int World::getGravity(lua_State * 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.x);
lua_pushnumber(L, v.y); lua_pushnumber(L, v.y);
return 2; 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() int World::getBodyCount()
+11 -11
View File
@@ -69,7 +69,7 @@ namespace box2d
std::vector<Contact *> contacts; std::vector<Contact *> contacts;
ContactCallback(); ContactCallback();
~ContactCallback(); ~ContactCallback();
void add(World * world, const b2ContactPoint* point); void add(World * world, const b2Contact* contact);
void process(); void process();
}; };
@@ -82,7 +82,7 @@ namespace box2d
b2Body * groundBody; b2Body * groundBody;
// Contact callbacks. // Contact callbacks.
ContactCallback add, persist, remove, result; ContactCallback begin, end, presolve, postsolve;
public: public:
@@ -111,16 +111,16 @@ namespace box2d
void update(float dt); void update(float dt);
// From b2ContactListener // From b2ContactListener
void Add(const b2ContactPoint* point); void BeginContact(b2Contact* contact);
void Persist(const b2ContactPoint* point); void EndContact(b2Contact* contact);
void Remove(const b2ContactPoint* point); void PreSolve(b2Contact* contact, const b2Manifold* oldManifold);
void Result(const b2ContactPoint* point); void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse);
/** /**
* Receives up to four Lua functions as arguments. Each function is * Receives up to four Lua functions as arguments. Each function is
* collision callback for the four events (in order): add, persist, * collision callback for the four events (in order): begin, end,
* remove and result. The value "nil" is accepted if one or more events * presolve and postsolve. The value "nil" is accepted if one or
* are uninteresting. * more events are uninteresting.
**/ **/
int setCallbacks(lua_State * L); int setCallbacks(lua_State * L);
@@ -147,13 +147,13 @@ namespace box2d
* Sets whether this World allows sleep. * Sets whether this World allows sleep.
* @param allow True to allow, false to disallow. * @param allow True to allow, false to disallow.
**/ **/
void setAllowSleep(bool allow); void setAllowSleeping(bool allow);
/** /**
* Returns whether this World allows sleep. * Returns whether this World allows sleep.
* @return True if allowed, false if disallowed. * @return True if allowed, false if disallowed.
**/ **/
bool isAllowSleep() const; bool getAllowSleeping() const;
/** /**
* Get the current body count. * Get the current body count.
+6 -6
View File
@@ -70,18 +70,18 @@ namespace box2d
return t->getGravity(L); return t->getGravity(L);
} }
int w_World_setAllowSleep(lua_State * L) int w_World_setAllowSleeping(lua_State * L)
{ {
World * t = luax_checkworld(L, 1); World * t = luax_checkworld(L, 1);
bool b = luax_toboolean(L, 2); bool b = luax_toboolean(L, 2);
t->setAllowSleep(b); t->setAllowSleeping(b);
return 0; return 0;
} }
int w_World_isAllowSleep(lua_State * L) int w_World_getAllowSleeping(lua_State * L)
{ {
World * t = luax_checkworld(L, 1); World * t = luax_checkworld(L, 1);
luax_pushboolean(L, t->isAllowSleep()); luax_pushboolean(L, t->getAllowSleeping());
return 1; return 1;
} }
@@ -105,8 +105,8 @@ namespace box2d
{ "getCallbacks", w_World_getCallbacks }, { "getCallbacks", w_World_getCallbacks },
{ "setGravity", w_World_setGravity }, { "setGravity", w_World_setGravity },
{ "getGravity", w_World_getGravity }, { "getGravity", w_World_getGravity },
{ "setAllowSleep", w_World_setAllowSleep }, { "setAllowSleeping", w_World_setAllowSleeping },
{ "isAllowSleep", w_World_isAllowSleep }, { "getAllowSleeping", w_World_getAllowSleeping },
{ "getBodyCount", w_World_getBodyCount }, { "getBodyCount", w_World_getBodyCount },
{ "getJointCount", w_World_getJointCount }, { "getJointCount", w_World_getJointCount },
{ 0, 0 } { 0, 0 }
+2 -2
View File
@@ -37,8 +37,8 @@ namespace box2d
int w_World_getCallbacks(lua_State * L); int w_World_getCallbacks(lua_State * L);
int w_World_setGravity(lua_State * L); int w_World_setGravity(lua_State * L);
int w_World_getGravity(lua_State * L); int w_World_getGravity(lua_State * L);
int w_World_setAllowSleep(lua_State * L); int w_World_setAllowSleeping(lua_State * L);
int w_World_isAllowSleep(lua_State * L); int w_World_getAllowSleeping(lua_State * L);
int w_World_getBodyCount(lua_State * L); int w_World_getBodyCount(lua_State * L);
int w_World_getJointCount(lua_State * L); int w_World_getJointCount(lua_State * L);
int luaopen_world(lua_State * L); int luaopen_world(lua_State * L);