Skip to content

Commit

Permalink
Add Body:getFixture.
Browse files Browse the repository at this point in the history
Convenience method to get the first Fixture attached to the body.
  • Loading branch information
slime73 committed Oct 4, 2023
1 parent 51b4398 commit 2702004
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/modules/physics/box2d/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/modules/physics/box2d/Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ class Body : public love::physics::Body
*/
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.
Expand Down
12 changes: 12 additions & 0 deletions src/modules/physics/box2d/wrap_Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 },
Expand Down

0 comments on commit 2702004

Please sign in to comment.