Browse Source

World:rayCast and World:queryBoundingBox have user args passed through to the callback function.

Fixes #1194
Sasha Szpakowski 1 year ago
parent
commit
d24ccde864
2 changed files with 10 additions and 2 deletions
  1. 8 2
      src/modules/physics/box2d/World.cpp
  2. 2 0
      src/modules/physics/box2d/World.h

+ 8 - 2
src/modules/physics/box2d/World.cpp

@@ -147,6 +147,7 @@ World::QueryCallback::QueryCallback(World *world, lua_State *L, int idx)
 	, funcidx(idx)
 {
 	luaL_checktype(L, funcidx, LUA_TFUNCTION);
+	userargs = lua_gettop(L) - funcidx;
 }
 
 World::QueryCallback::~QueryCallback()
@@ -162,7 +163,9 @@ bool World::QueryCallback::ReportFixture(b2Fixture *fixture)
 		if (!f)
 			throw love::Exception("A fixture has escaped Memoizer!");
 		luax_pushtype(L, f);
-		lua_call(L, 1, 1);
+		for (int i = 1; i <= userargs; i++)
+			lua_pushvalue(L, funcidx + i);
+		lua_call(L, 1 + userargs, 1);
 		bool cont = luax_toboolean(L, -1);
 		lua_pop(L, 1);
 		return cont;
@@ -199,6 +202,7 @@ World::RayCastCallback::RayCastCallback(World *world, lua_State *L, int idx)
 	, funcidx(idx)
 {
 	luaL_checktype(L, funcidx, LUA_TFUNCTION);
+	userargs = lua_gettop(L) - funcidx;
 }
 
 World::RayCastCallback::~RayCastCallback()
@@ -220,7 +224,9 @@ float World::RayCastCallback::ReportFixture(b2Fixture *fixture, const b2Vec2 &po
 		lua_pushnumber(L, normal.x);
 		lua_pushnumber(L, normal.y);
 		lua_pushnumber(L, fraction);
-		lua_call(L, 6, 1);
+		for (int i = 1; i <= userargs; i++)
+			lua_pushvalue(L, funcidx + i);
+		lua_call(L, 6 + userargs, 1);
 		if (!lua_isnumber(L, -1))
 			luaL_error(L, "Raycast callback didn't return a number!");
 		float fraction = (float) lua_tonumber(L, -1);

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

@@ -100,6 +100,7 @@ public:
 		World *world;
 		lua_State *L;
 		int funcidx;
+		int userargs;
 	};
 
 	class CollectCallback : public b2QueryCallback
@@ -124,6 +125,7 @@ public:
 		World *world;
 		lua_State *L;
 		int funcidx;
+		int userargs;
 	};
 
 	/**