Browse Source

Fix love.physics.getDistance

Bart van Strien 12 years ago
parent
commit
4de936ed75
1 changed files with 17 additions and 10 deletions
  1. 17 10
      src/modules/physics/box2d/Physics.cpp

+ 17 - 10
src/modules/physics/box2d/Physics.cpp

@@ -249,19 +249,26 @@ 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);
+	b2DistanceProxy pA, pB;
 	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);
+	c.count = 0;
+	try
+	{
+		pA.Set(fixtureA->fixture->GetShape(), 0);
+		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;
+		b2Distance(&o, &c, &i);
+	}
+	catch (love::Exception &e)
+	{
+		luaL_error(L, "%s", e.what());
+	}
 	lua_pushnumber(L, Physics::scaleUp(o.distance));
 	lua_pushnumber(L, Physics::scaleUp(o.pointA.x));
 	lua_pushnumber(L, Physics::scaleUp(o.pointA.y));