From 28c5982d92ad9c00344ddbad9bbd536786086dfe Mon Sep 17 00:00:00 2001 From: Bill Meltsner Date: Sun, 25 Sep 2011 12:18:13 -0400 Subject: [PATCH] Fix table creation for getList() functions, and ignore the ground body --HG-- branch : box2d-update --- src/modules/physics/box2d/Body.cpp | 3 ++- src/modules/physics/box2d/World.cpp | 41 +++++++++++++++-------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/modules/physics/box2d/Body.cpp b/src/modules/physics/box2d/Body.cpp index 7f4786676..8f64660f1 100644 --- a/src/modules/physics/box2d/Body.cpp +++ b/src/modules/physics/box2d/Body.cpp @@ -405,7 +405,8 @@ namespace box2d Fixture * fixture = (Fixture *)Memoizer::find(f); if (!fixture) throw love::Exception("A fixture has escaped Memoizer!"); luax_newtype(L, "Fixture", PHYSICS_FIXTURE_T, (void*)fixture); - lua_rawseti(L, -1, i); + lua_rawseti(L, -2, i); + i++; } while ((f = f->GetNext())); return 1; } diff --git a/src/modules/physics/box2d/World.cpp b/src/modules/physics/box2d/World.cpp index 0e097c781..c0ef5ebff 100644 --- a/src/modules/physics/box2d/World.cpp +++ b/src/modules/physics/box2d/World.cpp @@ -357,7 +357,7 @@ namespace box2d int World::getBodyCount() const { - return world->GetBodyCount(); + return world->GetBodyCount()-1; // ignore the ground body } int World::getJointCount() const @@ -374,15 +374,16 @@ namespace box2d { lua_newtable(L); b2Body * b = world->GetBodyList(); - for (int i = 1; i <= world->GetBodyCount(); i++) { - if (!b) return 1; + int i = 1; + do { + if (!b) break; + if (b == groundBody) continue; Body * body = (Body *)Memoizer::find(b); if (!body) throw love::Exception("A body has escaped Memoizer!"); - body->retain(); luax_newtype(L, "Body", PHYSICS_BODY_T, (void*)body); - lua_rawseti(L, -1, i); - b = b->GetNext(); - } + lua_rawseti(L, -2, i); + i++; + } while ((b = b->GetNext())); return 1; } @@ -390,15 +391,15 @@ namespace box2d { lua_newtable(L); b2Joint * j = world->GetJointList(); - for (int i = 1; i <= world->GetJointCount(); i++) { - if (!j) return 1; + int i = 1; + do { + if (!j) break; Joint * joint = (Joint *)Memoizer::find(j); if (!joint) throw love::Exception("A joint has escaped Memoizer!"); - joint->retain(); luax_newtype(L, "Joint", PHYSICS_JOINT_T, (void*)joint); - lua_rawseti(L, -1, i); - j = j->GetNext(); - } + lua_rawseti(L, -2, i); + i++; + } while ((j = j->GetNext())); return 1; } @@ -406,15 +407,15 @@ namespace box2d { lua_newtable(L); b2Contact * c = world->GetContactList(); - for (int i = 1; i <= world->GetContactCount(); i++) { - if (!c) return 1; - Contact * contact = (Contact *)Memoizer::find(c); + int i = 1; + do { + if (!c) break; + Contact * contact = (Contact *)Memoizer::find(c); if (!contact) throw love::Exception("A contact has escaped Memoizer!"); - contact->retain(); luax_newtype(L, "Contact", PHYSICS_CONTACT_T, (void*)contact); - lua_rawseti(L, -1, i); - c = c->GetNext(); - } + lua_rawseti(L, -2, i); + i++; + } while ((c = c->GetNext())); return 1; }