Skip to content

Commit

Permalink
World:rayCast and World:queryBoundingBox have user args passed throug…
Browse files Browse the repository at this point in the history
…h to the callback function.

Fixes #1194
  • Loading branch information
slime73 committed Oct 2, 2023
1 parent b1609ed commit d24ccde
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/modules/physics/box2d/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/modules/physics/box2d/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class World : public Object, public b2ContactListener, public b2ContactFilter, p
World *world;
lua_State *L;
int funcidx;
int userargs;
};

class CollectCallback : public b2QueryCallback
Expand All @@ -124,6 +125,7 @@ class World : public Object, public b2ContactListener, public b2ContactFilter, p
World *world;
lua_State *L;
int funcidx;
int userargs;
};

/**
Expand Down

0 comments on commit d24ccde

Please sign in to comment.