diff --git a/src/modules/physics/box2d/Body.cpp b/src/modules/physics/box2d/Body.cpp index 3091af5ab..c755357a6 100644 --- a/src/modules/physics/box2d/Body.cpp +++ b/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; diff --git a/src/modules/physics/box2d/Body.h b/src/modules/physics/box2d/Body.h index d160cf526..95feb7e9d 100644 --- a/src/modules/physics/box2d/Body.h +++ b/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. */ diff --git a/src/modules/physics/box2d/wrap_Body.cpp b/src/modules/physics/box2d/wrap_Body.cpp index a827405ed..c2bdcfec2 100644 --- a/src/modules/physics/box2d/wrap_Body.cpp +++ b/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 },