Browse Source

Add love.physics.getDistance(fixtureA, fixtureB) - gets the distance between and the closest points of two fixtures

--HG--
branch : box2d-update
Bill Meltsner 14 years ago
parent
commit
680dd65c19

+ 2 - 0
src/modules/physics/box2d/Fixture.h

@@ -57,6 +57,8 @@ namespace box2d
 	**/
 	**/
 	class Fixture : public Object
 	class Fixture : public Object
 	{
 	{
+	friend class Physics;
+	
 	protected:
 	protected:
 		
 		
 		Body * body;
 		Body * body;

+ 25 - 0
src/modules/physics/box2d/Physics.cpp

@@ -208,6 +208,31 @@ namespace box2d
 		return new Fixture(body, shape, density);
 		return new Fixture(body, shape, density);
 	}
 	}
 	
 	
+	int Physics::getDistance(lua_State * L)
+	{
+		Fixture * fixtureA = luax_checktype<Fixture>(L, 1, "Fixture", PHYSICS_FIXTURE_T);
+		Fixture * fixtureB = luax_checktype<Fixture>(L, 2, "Fixture", PHYSICS_FIXTURE_T);
+		b2DistanceInput i;
+		b2DistanceProxy pA;
+		pA.Set(fixtureA->fixture->GetShape(), 0);
+		b2DistanceProxy pB;
+		pB.Set(fixtureB->fixture->GetShape(), 0);
+		i.proxyA = pA;
+		i.proxyB = pB;
+		i.transformA = fixtureA->fixture->GetBody()->GetTransform();
+		i.transformB = fixtureB->fixture->GetBody()->GetTransform();
+		i.useRadii = true;
+		b2DistanceOutput o;
+		b2SimplexCache c;
+		b2Distance(&o, &c, &i);
+		lua_pushnumber(L, o.distance);
+		lua_pushnumber(L, Physics::scaleUp(o.pointA.x));
+		lua_pushnumber(L, Physics::scaleUp(o.pointA.y));
+		lua_pushnumber(L, Physics::scaleUp(o.pointB.x));
+		lua_pushnumber(L, Physics::scaleUp(o.pointB.y));
+		return 5;
+	}
+	
 	void Physics::setMeter(int meter)
 	void Physics::setMeter(int meter)
 	{
 	{
 		Physics::meter = meter;
 		Physics::meter = meter;

+ 9 - 0
src/modules/physics/box2d/Physics.h

@@ -258,6 +258,15 @@ namespace box2d
 		
 		
 		Fixture * newFixture(Body * body, Shape * shape, float density);
 		Fixture * newFixture(Body * body, Shape * shape, float density);
 		
 		
+		/**
+		* Calculates the distance between two Fixtures.
+		* @param fixtureA The first Fixture.
+		* @param fixtureB The sceond Fixture.
+		* @return The distance between them, and the two points closest
+		*         to each other.
+		**/
+		int getDistance(lua_State * L);
+		
 		/**
 		/**
 		* Sets the number of pixels in one meter.
 		* Sets the number of pixels in one meter.
 		* @param pixels The number of pixels in one meter. (1m ~= 3.3ft).
 		* @param pixels The number of pixels in one meter. (1m ~= 3.3ft).

+ 5 - 0
src/modules/physics/box2d/wrap_Physics.cpp

@@ -272,6 +272,11 @@ namespace box2d
 		return 1;
 		return 1;
 	}
 	}
 	
 	
+	int w_getDistance(lua_State * L)
+	{
+		return instance->getDistance(L);
+	}
+	
 	int w_setMeter(lua_State * L)
 	int w_setMeter(lua_State * L)
 	{
 	{
 		int arg1 = luaL_checkint(L, 1);
 		int arg1 = luaL_checkint(L, 1);

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

@@ -69,6 +69,7 @@ namespace box2d
 	int w_newWeldJoint(lua_State * L);
 	int w_newWeldJoint(lua_State * L);
 	int w_newWheelJoint(lua_State * L);
 	int w_newWheelJoint(lua_State * L);
 	int w_newRopeJoint(lua_State * L);
 	int w_newRopeJoint(lua_State * L);
+	int w_getDistance(lua_State * L);
 	int w_setMeter(lua_State * L);
 	int w_setMeter(lua_State * L);
 	int w_getMeter(lua_State * L);
 	int w_getMeter(lua_State * L);
 	extern "C" LOVE_EXPORT int luaopen_love_physics(lua_State * L);
 	extern "C" LOVE_EXPORT int luaopen_love_physics(lua_State * L);