Browse Source

Added GearJoint:getJoints (resolves issue #922.)

Alex Szpakowski 10 years ago
parent
commit
68936e6d2f

+ 27 - 0
src/modules/physics/box2d/GearJoint.cpp

@@ -23,6 +23,7 @@
 // Module
 // Module
 #include "Body.h"
 #include "Body.h"
 #include "World.h"
 #include "World.h"
+#include "common/Memoizer.h"
 
 
 namespace love
 namespace love
 {
 {
@@ -60,6 +61,32 @@ float GearJoint::getRatio() const
 	return joint->GetRatio();
 	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
 } // box2d
 } // physics
 } // physics
 } // love
 } // love

+ 3 - 0
src/modules/physics/box2d/GearJoint.h

@@ -64,6 +64,9 @@ public:
 	 **/
 	 **/
 	float getRatio() const;
 	float getRatio() const;
 
 
+	Joint *getJointA() const;
+	Joint *getJointB() const;
+
 private:
 private:
 	// The Box2D GearJoint object.
 	// The Box2D GearJoint object.
 	b2GearJoint *joint;
 	b2GearJoint *joint;

+ 51 - 0
src/modules/physics/box2d/wrap_GearJoint.cpp

@@ -51,10 +51,61 @@ int w_GearJoint_getRatio(lua_State *L)
 	return 1;
 	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[] =
 static const luaL_Reg functions[] =
 {
 {
 	{ "setRatio", w_GearJoint_setRatio },
 	{ "setRatio", w_GearJoint_setRatio },
 	{ "getRatio", w_GearJoint_getRatio },
 	{ "getRatio", w_GearJoint_getRatio },
+	{ "getJoints", w_GearJoint_getJoints },
 	// From Joint.
 	// From Joint.
 	{ "getType", w_Joint_getType },
 	{ "getType", w_Joint_getType },
 	{ "getBodies", w_Joint_getBodies },
 	{ "getBodies", w_Joint_getBodies },

+ 1 - 0
src/modules/physics/box2d/wrap_GearJoint.h

@@ -36,6 +36,7 @@ namespace box2d
 GearJoint *luax_checkgearjoint(lua_State *L, int idx);
 GearJoint *luax_checkgearjoint(lua_State *L, int idx);
 int w_GearJoint_setRatio(lua_State *L);
 int w_GearJoint_setRatio(lua_State *L);
 int w_GearJoint_getRatio(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);
 extern "C" int luaopen_gearjoint(lua_State *L);
 
 
 } // box2d
 } // box2d