Added GearJoint:getJoints (resolves issue #922.)

This commit is contained in:
Alex Szpakowski
2014-11-14 22:06:37 -04:00
parent 78d5db6981
commit 68936e6d2f
4 changed files with 82 additions and 0 deletions
+27
View File
@@ -23,6 +23,7 @@
// Module
#include "Body.h"
#include "World.h"
#include "common/Memoizer.h"
namespace love
{
@@ -60,6 +61,32 @@ float GearJoint::getRatio() const
return joint->GetRatio();
}
Joint *GearJoint::getJointA() const
{
b2Joint *b2joint = joint->GetJoint1();
if (b2joint == nullptr)
return nullptr;
Joint *j = (Joint *) Memoizer::find(b2joint);
if (j == nullptr)
throw love::Exception("A joint has escaped Memoizer!");
return j;
}
Joint *GearJoint::getJointB() const
{
b2Joint *b2joint = joint->GetJoint2();
if (b2joint == nullptr)
return nullptr;
Joint *j = (Joint *) Memoizer::find(b2joint);
if (j == nullptr)
throw love::Exception("A joint has escaped Memoizer!");
return j;
}
} // box2d
} // physics
} // love
+3
View File
@@ -64,6 +64,9 @@ public:
**/
float getRatio() const;
Joint *getJointA() const;
Joint *getJointB() const;
private:
// The Box2D GearJoint object.
b2GearJoint *joint;
@@ -51,10 +51,61 @@ int w_GearJoint_getRatio(lua_State *L)
return 1;
}
int w_GearJoint_getJoints(lua_State *L)
{
GearJoint *t = luax_checkgearjoint(L, 1);
Joint *j1 = nullptr;
Joint *j2 = nullptr;
luax_catchexcept(L, [&]() {
j1 = t->getJointA();
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);
return 2;
}
static const luaL_Reg functions[] =
{
{ "setRatio", w_GearJoint_setRatio },
{ "getRatio", w_GearJoint_getRatio },
{ "getJoints", w_GearJoint_getJoints },
// From Joint.
{ "getType", w_Joint_getType },
{ "getBodies", w_Joint_getBodies },
@@ -36,6 +36,7 @@ namespace box2d
GearJoint *luax_checkgearjoint(lua_State *L, int idx);
int w_GearJoint_setRatio(lua_State *L);
int w_GearJoint_getRatio(lua_State *L);
int w_GearJoint_getJoints(lua_State *L);
extern "C" int luaopen_gearjoint(lua_State *L);
} // box2d