Browse Source

Add Body:getFixture.

Convenience method to get the first Fixture attached to the body.
Sasha Szpakowski 1 year ago
parent
commit
2702004bb2

+ 13 - 0
src/modules/physics/box2d/Body.cpp

@@ -461,6 +461,19 @@ World *Body::getWorld() const
 	return world;
 	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
 int Body::getFixtures(lua_State *L) const
 {
 {
 	lua_newtable(L);
 	lua_newtable(L);

+ 5 - 0
src/modules/physics/box2d/Body.h

@@ -390,6 +390,11 @@ public:
 	 */
 	 */
 	World *getWorld() const;
 	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.
 	 * Get an array of all the Fixtures attached to this Body.
 	 * @return An array of Fixtures.
 	 * @return An array of Fixtures.

+ 12 - 0
src/modules/physics/box2d/wrap_Body.cpp

@@ -599,6 +599,17 @@ int w_Body_getWorld(lua_State *L)
 	return 1;
 	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)
 int w_Body_getFixtures(lua_State *L)
 {
 {
 	Body *t = luax_checkbody(L, 1);
 	Body *t = luax_checkbody(L, 1);
@@ -713,6 +724,7 @@ static const luaL_Reg w_Body_functions[] =
 	{ "isFixedRotation", w_Body_isFixedRotation },
 	{ "isFixedRotation", w_Body_isFixedRotation },
 	{ "isTouching", w_Body_isTouching },
 	{ "isTouching", w_Body_isTouching },
 	{ "getWorld", w_Body_getWorld },
 	{ "getWorld", w_Body_getWorld },
+	{ "getFixture", w_Body_getFixture },
 	{ "getFixtures", w_Body_getFixtures },
 	{ "getFixtures", w_Body_getFixtures },
 	{ "getJoints", w_Body_getJoints },
 	{ "getJoints", w_Body_getJoints },
 	{ "getContacts", w_Body_getContacts },
 	{ "getContacts", w_Body_getContacts },