diff --git a/src/modules/physics/box2d/Body.cpp b/src/modules/physics/box2d/Body.cpp index e28d1f027..d5c9dd770 100644 --- a/src/modules/physics/box2d/Body.cpp +++ b/src/modules/physics/box2d/Body.cpp @@ -28,6 +28,9 @@ #include "World.h" #include "Physics.h" +// Needed for luax_pushjoint. +#include "wrap_Joint.h" + namespace love { namespace physics @@ -440,6 +443,30 @@ int Body::getFixtureList(lua_State *L) const return 1; } +int Body::getJointList(lua_State *L) const +{ + lua_newtable(L); + const b2JointEdge *je = body->GetJointList(); + int i = 1; + + do + { + if (!je) + break; + + Joint *joint = (Joint *) Memoizer::find(je->joint); + if (!joint) + throw love::Exception("A joint has escaped Memoizer!"); + + luax_pushjoint(L, joint); + lua_rawseti(L, -2, i); + i++; + } + while ((je = je->next)); + + return 1; +} + int Body::getContactList(lua_State *L) const { lua_newtable(L); diff --git a/src/modules/physics/box2d/Body.h b/src/modules/physics/box2d/Body.h index e951d242b..1a3a6d379 100644 --- a/src/modules/physics/box2d/Body.h +++ b/src/modules/physics/box2d/Body.h @@ -393,6 +393,11 @@ public: **/ int getFixtureList(lua_State *L) const; + /** + * Get an array of all Joints attached to this Body. + **/ + int getJointList(lua_State *L) const; + /** * Get an array of all active Contacts attached to this Body. * This list changes during World:update and you may miss some collisions diff --git a/src/modules/physics/box2d/wrap_Body.cpp b/src/modules/physics/box2d/wrap_Body.cpp index 1b29613d5..ead95be98 100644 --- a/src/modules/physics/box2d/wrap_Body.cpp +++ b/src/modules/physics/box2d/wrap_Body.cpp @@ -539,6 +539,15 @@ int w_Body_getFixtureList(lua_State *L) return n; } +int w_Body_getJointList(lua_State *L) +{ + Body *t = luax_checkbody(L, 1); + lua_remove(L, 1); + int n = 0; + luax_catchexcept(L, [&](){ n = t->getJointList(L); }); + return n; +} + int w_Body_getContactList(lua_State *L) { Body *t = luax_checkbody(L, 1); @@ -623,6 +632,7 @@ static const luaL_Reg functions[] = { "isFixedRotation", w_Body_isFixedRotation }, { "getWorld", w_Body_getWorld }, { "getFixtureList", w_Body_getFixtureList }, + { "getJointList", w_Body_getJointList }, { "getContactList", w_Body_getContactList }, { "destroy", w_Body_destroy }, { "setUserData", w_Body_setUserData }, diff --git a/src/modules/physics/box2d/wrap_Body.h b/src/modules/physics/box2d/wrap_Body.h index f6e65f253..d046b526d 100644 --- a/src/modules/physics/box2d/wrap_Body.h +++ b/src/modules/physics/box2d/wrap_Body.h @@ -85,6 +85,7 @@ int w_Body_setFixedRotation(lua_State *L); int w_Body_isFixedRotation(lua_State *L); int w_Body_getWorld(lua_State *L); int w_Body_getFixtureList(lua_State *L); +int w_Body_getJointList(lua_State *L); int w_Body_getContactList(lua_State *L); int w_Body_destroy(lua_State *L); int w_Body_setUserData(lua_State *L); diff --git a/src/modules/physics/box2d/wrap_GearJoint.cpp b/src/modules/physics/box2d/wrap_GearJoint.cpp index 74eaaef5d..a340110d2 100644 --- a/src/modules/physics/box2d/wrap_GearJoint.cpp +++ b/src/modules/physics/box2d/wrap_GearJoint.cpp @@ -62,42 +62,8 @@ int w_GearJoint_getJoints(lua_State *L) j2 = t->getJointB(); }); - auto pushjoint = [](lua_State *L, Joint *j) -> void - { - if (j == nullptr) - return lua_pushnil(L); - - switch (j->getType()) - { - case Joint::JOINT_DISTANCE: - return luax_pushtype(L, "DistanceJoint", PHYSICS_DISTANCE_JOINT_T, j); - case Joint::JOINT_REVOLUTE: - return luax_pushtype(L, "RevoluteJoint", PHYSICS_REVOLUTE_JOINT_T, j); - case Joint::JOINT_PRISMATIC: - return luax_pushtype(L, "PrismaticJoint", PHYSICS_PRISMATIC_JOINT_T, j); - case Joint::JOINT_MOUSE: - return luax_pushtype(L, "MouseJoint", PHYSICS_MOUSE_JOINT_T, j); - case Joint::JOINT_PULLEY: - return luax_pushtype(L, "PulleyJoint", PHYSICS_PULLEY_JOINT_T, j); - case Joint::JOINT_GEAR: - return luax_pushtype(L, "GearJoint", PHYSICS_GEAR_JOINT_T, j); - case Joint::JOINT_FRICTION: - return luax_pushtype(L, "FrictionJoint", PHYSICS_FRICTION_JOINT_T, j); - case Joint::JOINT_WELD: - return luax_pushtype(L, "WeldJoint", PHYSICS_WELD_JOINT_T, j); - case Joint::JOINT_WHEEL: - return luax_pushtype(L, "WheelJoint", PHYSICS_WHEEL_JOINT_T, j); - case Joint::JOINT_ROPE: - return luax_pushtype(L, "RopeJoint", PHYSICS_ROPE_JOINT_T, j); - case Joint::JOINT_MOTOR: - return luax_pushtype(L, "MotorJoint", PHYSICS_MOTOR_JOINT_T, j); - default: - return lua_pushnil(L); - } - }; - - pushjoint(L, j1); - pushjoint(L, j2); + luax_pushjoint(L, j1); + luax_pushjoint(L, j2); return 2; } diff --git a/src/modules/physics/box2d/wrap_Joint.cpp b/src/modules/physics/box2d/wrap_Joint.cpp index fe790f072..a8ce0b314 100644 --- a/src/modules/physics/box2d/wrap_Joint.cpp +++ b/src/modules/physics/box2d/wrap_Joint.cpp @@ -30,6 +30,40 @@ namespace physics namespace box2d { +void luax_pushjoint(lua_State *L, Joint *j) +{ + if (j == nullptr) + return lua_pushnil(L); + + switch (j->getType()) + { + case Joint::JOINT_DISTANCE: + return luax_pushtype(L, "DistanceJoint", PHYSICS_DISTANCE_JOINT_T, j); + case Joint::JOINT_REVOLUTE: + return luax_pushtype(L, "RevoluteJoint", PHYSICS_REVOLUTE_JOINT_T, j); + case Joint::JOINT_PRISMATIC: + return luax_pushtype(L, "PrismaticJoint", PHYSICS_PRISMATIC_JOINT_T, j); + case Joint::JOINT_MOUSE: + return luax_pushtype(L, "MouseJoint", PHYSICS_MOUSE_JOINT_T, j); + case Joint::JOINT_PULLEY: + return luax_pushtype(L, "PulleyJoint", PHYSICS_PULLEY_JOINT_T, j); + case Joint::JOINT_GEAR: + return luax_pushtype(L, "GearJoint", PHYSICS_GEAR_JOINT_T, j); + case Joint::JOINT_FRICTION: + return luax_pushtype(L, "FrictionJoint", PHYSICS_FRICTION_JOINT_T, j); + case Joint::JOINT_WELD: + return luax_pushtype(L, "WeldJoint", PHYSICS_WELD_JOINT_T, j); + case Joint::JOINT_WHEEL: + return luax_pushtype(L, "WheelJoint", PHYSICS_WHEEL_JOINT_T, j); + case Joint::JOINT_ROPE: + return luax_pushtype(L, "RopeJoint", PHYSICS_ROPE_JOINT_T, j); + case Joint::JOINT_MOTOR: + return luax_pushtype(L, "MotorJoint", PHYSICS_MOTOR_JOINT_T, j); + default: + return lua_pushnil(L); + } +} + Joint *luax_checkjoint(lua_State *L, int idx) { Joint *t = luax_checktype(L, idx, "Joint", PHYSICS_JOINT_T); diff --git a/src/modules/physics/box2d/wrap_Joint.h b/src/modules/physics/box2d/wrap_Joint.h index 75e8a7852..9d3ae4eb5 100644 --- a/src/modules/physics/box2d/wrap_Joint.h +++ b/src/modules/physics/box2d/wrap_Joint.h @@ -32,6 +32,7 @@ namespace physics namespace box2d { +void luax_pushjoint(lua_State *L, Joint *j); Joint *luax_checkjoint(lua_State *L, int idx); int w_Joint_getType(lua_State *L); int w_Joint_getBodies(lua_State *L);