Browse Source

Merge pull request #1657 from qaisjp/feature/get-world-points

Fix #1614: add Body:getLocalPoints
Alex Szpakowski 4 years ago
parent
commit
c04b8d19a0

+ 24 - 0
src/modules/physics/box2d/Body.cpp

@@ -346,6 +346,30 @@ void Body::getLocalVector(float x, float y, float &x_o, float &y_o)
 	y_o = v.y;
 }
 
+int Body::getLocalPoints(lua_State *L)
+{
+	int argc = lua_gettop(L);
+	int vcount = (int)argc/2;
+	// at least one point
+	love::luax_assert_argc(L, 2);
+
+	for (int i = 0; i<vcount; i++)
+	{
+		float x = (float)lua_tonumber(L, 1);
+		float y = (float)lua_tonumber(L, 2);
+		// Remove them, so we don't run out of stack space
+		lua_remove(L, 1);
+		lua_remove(L, 1);
+		// Time for scaling
+		b2Vec2 point = Physics::scaleUp(body->GetLocalPoint(Physics::scaleDown(b2Vec2(x, y))));
+		// And then we push the result
+		lua_pushnumber(L, point.x);
+		lua_pushnumber(L, point.y);
+	}
+
+	return argc;
+}
+
 void Body::getLinearVelocityFromWorldPoint(float x, float y, float &x_o, float &y_o)
 {
 	b2Vec2 v = Physics::scaleUp(body->GetLinearVelocityFromWorldPoint(Physics::scaleDown(b2Vec2(x, y))));

+ 6 - 0
src/modules/physics/box2d/Body.h

@@ -317,6 +317,12 @@ public:
 	 **/
 	void getLocalVector(float x, float y, float &x_o, float &y_o);
 
+	/**
+	 * Transforms a series of points (x, y) from world coordinates
+	 * to local coordinates.
+	 **/
+	int getLocalPoints(lua_State *L);
+
 	/**
 	 * Gets the velocity on the Body for the given world point.
 	 * @param x The x-coordinate of the world point.

+ 8 - 0
src/modules/physics/box2d/wrap_Body.cpp

@@ -444,6 +444,13 @@ int w_Body_getLocalVector(lua_State *L)
 	return 2;
 }
 
+int w_Body_getLocalPoints(lua_State *L)
+{
+	Body *t = luax_checkbody(L, 1);
+	lua_remove(L, 1);
+	return t->getLocalPoints(L);
+}
+
 int w_Body_getLinearVelocityFromWorldPoint(lua_State *L)
 {
 	Body *t = luax_checkbody(L, 1);
@@ -679,6 +686,7 @@ static const luaL_Reg w_Body_functions[] =
 	{ "getWorldPoints", w_Body_getWorldPoints },
 	{ "getLocalPoint", w_Body_getLocalPoint },
 	{ "getLocalVector", w_Body_getLocalVector },
+	{ "getLocalPoints", w_Body_getLocalPoints },
 	{ "getLinearVelocityFromWorldPoint", w_Body_getLinearVelocityFromWorldPoint },
 	{ "getLinearVelocityFromLocalPoint", w_Body_getLinearVelocityFromLocalPoint },
 	{ "isBullet", w_Body_isBullet },