From 2702004bb2780ac89bda56a43b3dad0f659383dc Mon Sep 17 00:00:00 2001 From: Sasha Szpakowski Date: Wed, 4 Oct 2023 20:00:44 -0300 Subject: [PATCH] Add Body:getFixture. Convenience method to get the first Fixture attached to the body. --- src/modules/physics/box2d/Body.cpp | 13 +++++++++++++ src/modules/physics/box2d/Body.h | 5 +++++ src/modules/physics/box2d/wrap_Body.cpp | 12 ++++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/modules/physics/box2d/Body.cpp b/src/modules/physics/box2d/Body.cpp index a21f0be4d..2c8acfd64 100644 --- a/src/modules/physics/box2d/Body.cpp +++ b/src/modules/physics/box2d/Body.cpp @@ -461,6 +461,19 @@ World *Body::getWorld() const return world; } +Fixture *Body::getFixture() const +{ + b2Fixture *f = body->GetFixtureList(); + if (f == nullptr) + return nullptr; + + Fixture *fixture = (Fixture *)(f->GetUserData().pointer); + if (!fixture) + throw love::Exception("A fixture has escaped Memoizer!"); + + return fixture; +} + int Body::getFixtures(lua_State *L) const { lua_newtable(L); diff --git a/src/modules/physics/box2d/Body.h b/src/modules/physics/box2d/Body.h index 563812fe2..0be88c6c7 100644 --- a/src/modules/physics/box2d/Body.h +++ b/src/modules/physics/box2d/Body.h @@ -390,6 +390,11 @@ public: */ World *getWorld() const; + /** + * Gets the first Fixture attached to this Body. + **/ + Fixture *getFixture() const; + /** * Get an array of all the Fixtures attached to this Body. * @return An array of Fixtures. diff --git a/src/modules/physics/box2d/wrap_Body.cpp b/src/modules/physics/box2d/wrap_Body.cpp index 479e5bc27..6ed39ef2e 100644 --- a/src/modules/physics/box2d/wrap_Body.cpp +++ b/src/modules/physics/box2d/wrap_Body.cpp @@ -599,6 +599,17 @@ int w_Body_getWorld(lua_State *L) return 1; } +int w_Body_getFixture(lua_State *L) +{ + Body *t = luax_checkbody(L, 1); + Fixture *f = t->getFixture(); + if (f) + luax_pushtype(L, f); + else + lua_pushnil(L); + return 1; +} + int w_Body_getFixtures(lua_State *L) { Body *t = luax_checkbody(L, 1); @@ -713,6 +724,7 @@ static const luaL_Reg w_Body_functions[] = { "isFixedRotation", w_Body_isFixedRotation }, { "isTouching", w_Body_isTouching }, { "getWorld", w_Body_getWorld }, + { "getFixture", w_Body_getFixture }, { "getFixtures", w_Body_getFixtures }, { "getJoints", w_Body_getJoints }, { "getContacts", w_Body_getContacts },