From 680dd65c196aaa8268917adb0dc2c2b068ae72c9 Mon Sep 17 00:00:00 2001 From: Bill Meltsner Date: Sat, 24 Sep 2011 17:29:42 -0400 Subject: [PATCH] Add love.physics.getDistance(fixtureA, fixtureB) - gets the distance between and the closest points of two fixtures --HG-- branch : box2d-update --- src/modules/physics/box2d/Fixture.h | 2 ++ src/modules/physics/box2d/Physics.cpp | 25 ++++++++++++++++++++++ src/modules/physics/box2d/Physics.h | 9 ++++++++ src/modules/physics/box2d/wrap_Physics.cpp | 5 +++++ src/modules/physics/box2d/wrap_Physics.h | 1 + 5 files changed, 42 insertions(+) diff --git a/src/modules/physics/box2d/Fixture.h b/src/modules/physics/box2d/Fixture.h index a1060667b..c3fdfb581 100644 --- a/src/modules/physics/box2d/Fixture.h +++ b/src/modules/physics/box2d/Fixture.h @@ -57,6 +57,8 @@ namespace box2d **/ class Fixture : public Object { + friend class Physics; + protected: Body * body; diff --git a/src/modules/physics/box2d/Physics.cpp b/src/modules/physics/box2d/Physics.cpp index 9c76a7ff0..466c07fc3 100644 --- a/src/modules/physics/box2d/Physics.cpp +++ b/src/modules/physics/box2d/Physics.cpp @@ -208,6 +208,31 @@ namespace box2d return new Fixture(body, shape, density); } + int Physics::getDistance(lua_State * L) + { + Fixture * fixtureA = luax_checktype(L, 1, "Fixture", PHYSICS_FIXTURE_T); + Fixture * fixtureB = luax_checktype(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) { Physics::meter = meter; diff --git a/src/modules/physics/box2d/Physics.h b/src/modules/physics/box2d/Physics.h index b26f61e42..89ff428a5 100644 --- a/src/modules/physics/box2d/Physics.h +++ b/src/modules/physics/box2d/Physics.h @@ -258,6 +258,15 @@ namespace box2d 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. * @param pixels The number of pixels in one meter. (1m ~= 3.3ft). diff --git a/src/modules/physics/box2d/wrap_Physics.cpp b/src/modules/physics/box2d/wrap_Physics.cpp index e84839187..1b11e05ec 100644 --- a/src/modules/physics/box2d/wrap_Physics.cpp +++ b/src/modules/physics/box2d/wrap_Physics.cpp @@ -272,6 +272,11 @@ namespace box2d return 1; } + int w_getDistance(lua_State * L) + { + return instance->getDistance(L); + } + int w_setMeter(lua_State * L) { int arg1 = luaL_checkint(L, 1); diff --git a/src/modules/physics/box2d/wrap_Physics.h b/src/modules/physics/box2d/wrap_Physics.h index 8de805600..5a086b01e 100644 --- a/src/modules/physics/box2d/wrap_Physics.h +++ b/src/modules/physics/box2d/wrap_Physics.h @@ -69,6 +69,7 @@ namespace box2d int w_newWeldJoint(lua_State * L); int w_newWheelJoint(lua_State * L); int w_newRopeJoint(lua_State * L); + int w_getDistance(lua_State * L); int w_setMeter(lua_State * L); int w_getMeter(lua_State * L); extern "C" LOVE_EXPORT int luaopen_love_physics(lua_State * L);