Browse Source

Add Body:isTouching(otherbody). See issue #1365.

Alex Szpakowski 7 years ago
parent
commit
02e115c339

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

@@ -422,6 +422,22 @@ bool Body::isFixedRotation() const
 	return body->IsFixedRotation();
 }
 
+bool Body::isTouching(Body *other) const
+{
+	const b2ContactEdge *ce = body->GetContactList();
+	b2Body *otherbody = other->body;
+
+	while (ce != nullptr)
+	{
+		if (ce->other == otherbody)
+			return true;
+
+		ce = ce->next;
+	}
+
+	return false;
+}
+
 World *Body::getWorld() const
 {
 	return world;

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

@@ -382,6 +382,8 @@ public:
 	void setFixedRotation(bool fixed);
 	bool isFixedRotation() const;
 
+	bool isTouching(Body *other) const;
+
 	/**
 	 * Get the World this Body resides in.
 	 */

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

@@ -522,6 +522,14 @@ int w_Body_isFixedRotation(lua_State *L)
 	return 1;
 }
 
+int w_Body_isTouching(lua_State *L)
+{
+	Body *t = luax_checkbody(L, 1);
+	Body *other = luax_checkbody(L, 2);
+	luax_pushboolean(L, t->isTouching(other));
+	return 1;
+}
+
 int w_Body_getWorld(lua_State *L)
 {
 	Body *t = luax_checkbody(L, 1);
@@ -655,6 +663,7 @@ static const luaL_Reg w_Body_functions[] =
 	{ "setAwake", w_Body_setAwake },
 	{ "setFixedRotation", w_Body_setFixedRotation },
 	{ "isFixedRotation", w_Body_isFixedRotation },
+	{ "isTouching", w_Body_isTouching },
 	{ "getWorld", w_Body_getWorld },
 	{ "getFixtures", w_Body_getFixtures },
 	{ "getJoints", w_Body_getJoints },