Browse Source

Added 2-point version of love.physics.newRevoluteJoint (fixes #1175)

airstruck 9 years ago
parent
commit
9a2f99dc3d

+ 1 - 0
changes.txt

@@ -7,6 +7,7 @@ Released: N/A
   * Added 'shaderswitches' field to the table returned by love.graphics.getStats.
   * Added Quad:getTextureDimensions.
   * Added PrismaticJoint:getAxis and WheelJoint:getAxis.
+  * Added 2-point version of love.physics.newRevoluteJoint.
 
   * Fixed love on iOS 6.
   * Fixed os.execute always returning -1 on Linux.

+ 2 - 2
src/modules/physics/box2d/Physics.cpp

@@ -223,9 +223,9 @@ MouseJoint *Physics::newMouseJoint(Body *body, float x, float y)
 	return new MouseJoint(body, x, y);
 }
 
-RevoluteJoint *Physics::newRevoluteJoint(Body *body1, Body *body2, float x, float y, bool collideConnected)
+RevoluteJoint *Physics::newRevoluteJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected)
 {
-	return new RevoluteJoint(body1, body2, x, y, collideConnected);
+	return new RevoluteJoint(body1, body2, xA, yA, xB, yB, collideConnected);
 }
 
 PrismaticJoint *Physics::newPrismaticJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected)

+ 5 - 3
src/modules/physics/box2d/Physics.h

@@ -171,11 +171,13 @@ public:
 
 	/**
 	 * Creates a new RevoluteJoint connecting body1 with body2.
-	 * @param x Anchor along the x-axis. (World coordinates)
-	 * @param y Anchor along the y-axis. (World coordinates)
+	 * @param xA Anchor for body 1 along the x-axis. (World coordinates)
+	 * @param yA Anchor for body 1 along the y-axis. (World coordinates)
+	 * @param xB Anchor for body 2 along the x-axis. (World coordinates)
+	 * @param yB Anchor for body 2 along the y-axis. (World coordinates)
 	 * @param collideConnected Whether the connected bodies should collide with each other. Defaults to false.
 	 **/
-	RevoluteJoint *newRevoluteJoint(Body *body1, Body *body2, float x, float y, bool collideConnected);
+	RevoluteJoint *newRevoluteJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected);
 
 	/**
 	 * Creates a new PrismaticJoint connecting body1 with body2.

+ 3 - 2
src/modules/physics/box2d/RevoluteJoint.cpp

@@ -34,12 +34,13 @@ namespace physics
 namespace box2d
 {
 
-RevoluteJoint::RevoluteJoint(Body *body1, Body *body2, float x, float y, bool collideConnected)
+RevoluteJoint::RevoluteJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected)
 	: Joint(body1, body2)
 	, joint(NULL)
 {
 	b2RevoluteJointDef def;
-	def.Initialize(body1->body, body2->body, Physics::scaleDown(b2Vec2(x,y)));
+	def.Initialize(body1->body, body2->body, Physics::scaleDown(b2Vec2(xA,yA)));
+	def.localAnchorB = body2->body->GetLocalPoint(Physics::scaleDown(b2Vec2(xB, yB)));
 	def.collideConnected = collideConnected;
 	joint = (b2RevoluteJoint *)createJoint(&def);
 }

+ 1 - 1
src/modules/physics/box2d/RevoluteJoint.h

@@ -42,7 +42,7 @@ public:
 	/**
 	 * Creates a new RevoluteJoint connecting body1 and body2.
 	 **/
-	RevoluteJoint(Body *body1, Body *body2, float x, float y, bool collideConnected);
+	RevoluteJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected);
 
 	virtual ~RevoluteJoint();
 

+ 17 - 4
src/modules/physics/box2d/wrap_Physics.cpp

@@ -215,12 +215,25 @@ int w_newRevoluteJoint(lua_State *L)
 {
 	Body *body1 = luax_checkbody(L, 1);
 	Body *body2 = luax_checkbody(L, 2);
-	float x = (float)luaL_checknumber(L, 3);
-	float y = (float)luaL_checknumber(L, 4);
-	bool collideConnected = luax_optboolean(L, 5, false);
+	float xA = (float)luaL_checknumber(L, 3);
+	float yA = (float)luaL_checknumber(L, 4);
+	float xB, yB;
+	bool collideConnected;
+	if (lua_gettop(L) >= 6)
+	{
+		xB = (float)luaL_checknumber(L, 5);
+		yB = (float)luaL_checknumber(L, 6);
+		collideConnected = luax_optboolean(L, 7, false);
+	}
+	else
+	{
+		xB = xA;
+		yB = yA;
+		collideConnected = luax_optboolean(L, 5, false);
+	}
 	RevoluteJoint *j;
 	luax_catchexcept(L, [&]() {
-		j = instance()->newRevoluteJoint(body1, body2, x, y, collideConnected);
+		j = instance()->newRevoluteJoint(body1, body2, xA, yA, xB, yB, collideConnected);
 	});
 	luax_pushtype(L, PHYSICS_REVOLUTE_JOINT_ID, j);
 	j->release();