Skip to content

Commit

Permalink
Add optional category bit mask to World:getFixturesInArea.
Browse files Browse the repository at this point in the history
  • Loading branch information
slime73 committed Oct 2, 2023
1 parent 28a898d commit 6cb0287
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/modules/physics/box2d/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ bool World::QueryCallback::ReportFixture(b2Fixture *fixture)
return true;
}

World::CollectCallback::CollectCallback(World *world, lua_State *L)
World::CollectCallback::CollectCallback(World *world, uint16 categoryMask, lua_State *L)
: world(world)
, categoryMask(categoryMask)
, L(L)
{
lua_newtable(L);
Expand All @@ -187,6 +188,9 @@ World::CollectCallback::~CollectCallback()

bool World::CollectCallback::ReportFixture(b2Fixture *f)
{
if (categoryMask != 0xFFFF && (categoryMask & f->GetFilterData().categoryBits) == 0)
return true;

Fixture *fixture = (Fixture *)(f->GetUserData().pointer);
if (!fixture)
throw love::Exception("A fixture has escaped Memoizer!");
Expand Down Expand Up @@ -614,10 +618,11 @@ int World::getFixturesInArea(lua_State *L)
float ly = (float)luaL_checknumber(L, 2);
float ux = (float)luaL_checknumber(L, 3);
float uy = (float)luaL_checknumber(L, 4);
uint16 categoryMaskBits = (uint16)luaL_optinteger(L, 5, 0xFFFF);
b2AABB box;
box.lowerBound = Physics::scaleDown(b2Vec2(lx, ly));
box.upperBound = Physics::scaleDown(b2Vec2(ux, uy));
CollectCallback query(this, L);
CollectCallback query(this, categoryMaskBits, L);
world->QueryAABB(&query, box);
return 1;
}
Expand Down
3 changes: 2 additions & 1 deletion src/modules/physics/box2d/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ class World : public Object, public b2ContactListener, public b2ContactFilter, p
class CollectCallback : public b2QueryCallback
{
public:
CollectCallback(World *world, lua_State *L);
CollectCallback(World *world, uint16 categoryMask, lua_State *L);
virtual ~CollectCallback();
bool ReportFixture(b2Fixture *fixture) override;
private:
World *world;
uint16 categoryMask;
lua_State *L;
int i = 1;
};
Expand Down

0 comments on commit 6cb0287

Please sign in to comment.