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
{
Contact::Contact(World * world, const b2ContactPoint * point)
: point(*point), world(world)
Contact::Contact(World * world, b2Contact * contact)
: contact(contact), world(world)
{
world->retain();
}
+3 -3
View File
@@ -49,8 +49,8 @@ namespace box2d
private:
// The Box2D contact point.
b2ContactPoint point;
// The Box2D contact.
b2Contact* contact;
// The parent world. Needed for scaling.
World * world;
@@ -63,7 +63,7 @@ namespace box2d
* data pointed to.
* @param point Pointer to the Box2D contact.
**/
Contact(World * world, const b2ContactPoint * point);
Contact(World * world, const b2Contact * contact);
virtual ~Contact();
+41 -38
View File
@@ -20,6 +20,7 @@
#include "World.h"
#include "Fixture.h"
#include "Shape.h"
#include "Contact.h"
#include "Physics.h"
@@ -43,7 +44,7 @@ namespace box2d
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
@@ -52,7 +53,7 @@ namespace box2d
**/
if(ref != 0)
contacts.push_back(new Contact(world, point));
contacts.push_back(new Contact(world, contact));
}
void World::ContactCallback::process()
@@ -68,16 +69,16 @@ namespace box2d
// 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
@@ -99,16 +100,18 @@ namespace box2d
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);
}
@@ -120,33 +123,33 @@ namespace box2d
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)
@@ -157,17 +160,17 @@ namespace box2d
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;
@@ -175,10 +178,10 @@ namespace box2d
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);
}
@@ -189,20 +192,20 @@ namespace box2d
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()
+11 -11
View File
@@ -69,7 +69,7 @@ namespace box2d
std::vector<Contact *> contacts;
ContactCallback();
~ContactCallback();
void add(World * world, const b2ContactPoint* point);
void add(World * world, const b2Contact* contact);
void process();
};
@@ -82,7 +82,7 @@ namespace box2d
b2Body * groundBody;
// Contact callbacks.
ContactCallback add, persist, remove, result;
ContactCallback begin, end, presolve, postsolve;
public:
@@ -111,16 +111,16 @@ namespace box2d
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);
@@ -147,13 +147,13 @@ namespace box2d
* 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.
+6 -6
View File
@@ -70,18 +70,18 @@ namespace box2d
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;
}
@@ -105,8 +105,8 @@ namespace box2d
{ "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 }
+2 -2
View File
@@ -37,8 +37,8 @@ namespace box2d
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);