Ver Fonte

Added Joint:getBodies (see issue #922.)

Alex Szpakowski há 11 anos atrás
pai
commit
775f816304

+ 26 - 0
src/modules/physics/box2d/Joint.cpp

@@ -95,6 +95,32 @@ Joint::Type Joint::getType() const
 	}
 	}
 }
 }
 
 
+Body *Joint::getBodyA() const
+{
+	b2Body *b2body = joint->GetBodyA();
+	if (b2body == nullptr)
+		return nullptr;
+
+	Body *body = (Body *) Memoizer::find(b2body);
+	if (body == nullptr)
+		throw love::Exception("A body has escaped Memoizer!");
+
+	return body;
+}
+
+Body *Joint::getBodyB() const
+{
+	b2Body *b2body = joint->GetBodyB();
+	if (b2body == nullptr)
+		return nullptr;
+
+	Body *body = (Body *) Memoizer::find(b2body);
+	if (body == nullptr)
+		throw love::Exception("A body has escaped Memoizer!");
+
+	return body;
+}
+
 bool Joint::isValid() const
 bool Joint::isValid() const
 {
 {
 	return joint != 0;
 	return joint != 0;

+ 3 - 1
src/modules/physics/box2d/Joint.h

@@ -79,12 +79,14 @@ public:
 	 **/
 	 **/
 	bool isValid() const;
 	bool isValid() const;
 
 
-
 	/**
 	/**
 	 * Gets the type of joint.
 	 * Gets the type of joint.
 	 **/
 	 **/
 	Type getType() const;
 	Type getType() const;
 
 
+	Body *getBodyA() const;
+	Body *getBodyB() const;
+
 	/**
 	/**
 	 * Gets the anchor positions of the Joint in world
 	 * Gets the anchor positions of the Joint in world
 	 * coordinates. This is useful for debugdrawing the joint.
 	 * coordinates. This is useful for debugdrawing the joint.

+ 1 - 0
src/modules/physics/box2d/wrap_DistanceJoint.cpp

@@ -90,6 +90,7 @@ static const luaL_Reg functions[] =
 	{ "getDampingRatio", w_DistanceJoint_getDampingRatio },
 	{ "getDampingRatio", w_DistanceJoint_getDampingRatio },
 	// From Joint.
 	// From Joint.
 	{ "getType", w_Joint_getType },
 	{ "getType", w_Joint_getType },
+	{ "getBodies", w_Joint_getBodies },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionTorque", w_Joint_getReactionTorque },
 	{ "getReactionTorque", w_Joint_getReactionTorque },

+ 1 - 0
src/modules/physics/box2d/wrap_FrictionJoint.cpp

@@ -74,6 +74,7 @@ static const luaL_Reg functions[] =
 	{ "getMaxTorque", w_FrictionJoint_getMaxTorque },
 	{ "getMaxTorque", w_FrictionJoint_getMaxTorque },
 	// From Joint.
 	// From Joint.
 	{ "getType", w_Joint_getType },
 	{ "getType", w_Joint_getType },
+	{ "getBodies", w_Joint_getBodies },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionTorque", w_Joint_getReactionTorque },
 	{ "getReactionTorque", w_Joint_getReactionTorque },

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

@@ -57,6 +57,7 @@ static const luaL_Reg functions[] =
 	{ "getRatio", w_GearJoint_getRatio },
 	{ "getRatio", w_GearJoint_getRatio },
 	// From Joint.
 	// From Joint.
 	{ "getType", w_Joint_getType },
 	{ "getType", w_Joint_getType },
+	{ "getBodies", w_Joint_getBodies },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionTorque", w_Joint_getReactionTorque },
 	{ "getReactionTorque", w_Joint_getReactionTorque },

+ 18 - 0
src/modules/physics/box2d/wrap_Joint.cpp

@@ -21,6 +21,7 @@
 // LOVE
 // LOVE
 #include "wrap_Joint.h"
 #include "wrap_Joint.h"
 #include "common/StringMap.h"
 #include "common/StringMap.h"
+#include "Body.h"
 
 
 namespace love
 namespace love
 {
 {
@@ -46,6 +47,22 @@ int w_Joint_getType(lua_State *L)
 	return 1;
 	return 1;
 }
 }
 
 
+int w_Joint_getBodies(lua_State *L)
+{
+	Joint *t = luax_checkjoint(L, 1);
+	Body *b1 = nullptr;
+	Body *b2 = nullptr;
+
+	luax_catchexcept(L, [&]() {
+		b1 = t->getBodyA();
+		b2 = t->getBodyB();
+	});
+
+	luax_pushtype(L, "Body", PHYSICS_BODY_T, b1);
+	luax_pushtype(L, "Body", PHYSICS_BODY_T, b2);
+	return 2;
+}
+
 int w_Joint_getAnchors(lua_State *L)
 int w_Joint_getAnchors(lua_State *L)
 {
 {
 	Joint *t = luax_checkjoint(L, 1);
 	Joint *t = luax_checkjoint(L, 1);
@@ -99,6 +116,7 @@ int w_Joint_destroy(lua_State *L)
 static const luaL_Reg functions[] =
 static const luaL_Reg functions[] =
 {
 {
 	{ "getType", w_Joint_getType },
 	{ "getType", w_Joint_getType },
+	{ "getBodies", w_Joint_getBodies },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionTorque", w_Joint_getReactionTorque },
 	{ "getReactionTorque", w_Joint_getReactionTorque },

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

@@ -34,6 +34,7 @@ namespace box2d
 
 
 Joint *luax_checkjoint(lua_State *L, int idx);
 Joint *luax_checkjoint(lua_State *L, int idx);
 int w_Joint_getType(lua_State *L);
 int w_Joint_getType(lua_State *L);
+int w_Joint_getBodies(lua_State *L);
 int w_Joint_getAnchors(lua_State *L);
 int w_Joint_getAnchors(lua_State *L);
 int w_Joint_getReactionForce(lua_State *L);
 int w_Joint_getReactionForce(lua_State *L);
 int w_Joint_getReactionTorque(lua_State *L);
 int w_Joint_getReactionTorque(lua_State *L);

+ 1 - 0
src/modules/physics/box2d/wrap_MotorJoint.cpp

@@ -124,6 +124,7 @@ static const luaL_Reg functions[] =
 	{ "getCorrectionFactor", w_MotorJoint_getCorrectionFactor },
 	{ "getCorrectionFactor", w_MotorJoint_getCorrectionFactor },
 	// From Joint.
 	// From Joint.
 	{ "getType", w_Joint_getType },
 	{ "getType", w_Joint_getType },
+	{ "getBodies", w_Joint_getBodies },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionTorque", w_Joint_getReactionTorque },
 	{ "getReactionTorque", w_Joint_getReactionTorque },

+ 1 - 0
src/modules/physics/box2d/wrap_MouseJoint.cpp

@@ -108,6 +108,7 @@ static const luaL_Reg functions[] =
 	{ "getDampingRatio", w_MouseJoint_getDampingRatio },
 	{ "getDampingRatio", w_MouseJoint_getDampingRatio },
 	// From Joint.
 	// From Joint.
 	{ "getType", w_Joint_getType },
 	{ "getType", w_Joint_getType },
+	{ "getBodies", w_Joint_getBodies },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionTorque", w_Joint_getReactionTorque },
 	{ "getReactionTorque", w_Joint_getReactionTorque },

+ 1 - 0
src/modules/physics/box2d/wrap_PrismaticJoint.cpp

@@ -185,6 +185,7 @@ static const luaL_Reg functions[] =
 	{ "getLimits", w_PrismaticJoint_getLimits },
 	{ "getLimits", w_PrismaticJoint_getLimits },
 	// From Joint.
 	// From Joint.
 	{ "getType", w_Joint_getType },
 	{ "getType", w_Joint_getType },
+	{ "getBodies", w_Joint_getBodies },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionTorque", w_Joint_getReactionTorque },
 	{ "getReactionTorque", w_Joint_getReactionTorque },

+ 1 - 0
src/modules/physics/box2d/wrap_PulleyJoint.cpp

@@ -71,6 +71,7 @@ static const luaL_Reg functions[] =
 	{ "getRatio", w_PulleyJoint_getRatio },
 	{ "getRatio", w_PulleyJoint_getRatio },
 	// From Joint.
 	// From Joint.
 	{ "getType", w_Joint_getType },
 	{ "getType", w_Joint_getType },
+	{ "getBodies", w_Joint_getBodies },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionTorque", w_Joint_getReactionTorque },
 	{ "getReactionTorque", w_Joint_getReactionTorque },

+ 1 - 0
src/modules/physics/box2d/wrap_RevoluteJoint.cpp

@@ -185,6 +185,7 @@ static const luaL_Reg functions[] =
 	{ "getLimits", w_RevoluteJoint_getLimits },
 	{ "getLimits", w_RevoluteJoint_getLimits },
 	// From Joint.
 	// From Joint.
 	{ "getType", w_Joint_getType },
 	{ "getType", w_Joint_getType },
+	{ "getBodies", w_Joint_getBodies },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionTorque", w_Joint_getReactionTorque },
 	{ "getReactionTorque", w_Joint_getReactionTorque },

+ 1 - 0
src/modules/physics/box2d/wrap_RopeJoint.cpp

@@ -47,6 +47,7 @@ static const luaL_Reg functions[] =
 	{ "getMaxLength", w_RopeJoint_getMaxLength },
 	{ "getMaxLength", w_RopeJoint_getMaxLength },
 	// From Joint.
 	// From Joint.
 	{ "getType", w_Joint_getType },
 	{ "getType", w_Joint_getType },
+	{ "getBodies", w_Joint_getBodies },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionTorque", w_Joint_getReactionTorque },
 	{ "getReactionTorque", w_Joint_getReactionTorque },

+ 1 - 0
src/modules/physics/box2d/wrap_WeldJoint.cpp

@@ -73,6 +73,7 @@ static const luaL_Reg functions[] =
 	{ "getDampingRatio", w_WeldJoint_getDampingRatio },
 	{ "getDampingRatio", w_WeldJoint_getDampingRatio },
 	// From Joint.
 	// From Joint.
 	{ "getType", w_Joint_getType },
 	{ "getType", w_Joint_getType },
+	{ "getBodies", w_Joint_getBodies },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionTorque", w_Joint_getReactionTorque },
 	{ "getReactionTorque", w_Joint_getReactionTorque },

+ 1 - 0
src/modules/physics/box2d/wrap_WheelJoint.cpp

@@ -149,6 +149,7 @@ static const luaL_Reg functions[] =
 	{ "getSpringDampingRatio", w_WheelJoint_getSpringDampingRatio },
 	{ "getSpringDampingRatio", w_WheelJoint_getSpringDampingRatio },
 	// From Joint.
 	// From Joint.
 	{ "getType", w_Joint_getType },
 	{ "getType", w_Joint_getType },
+	{ "getBodies", w_Joint_getBodies },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getAnchors", w_Joint_getAnchors },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionForce", w_Joint_getReactionForce },
 	{ "getReactionTorque", w_Joint_getReactionTorque },
 	{ "getReactionTorque", w_Joint_getReactionTorque },