Skip to content

Commit

Permalink
Add setCollisionMask and getCollisionMask to Shape's API
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruception committed Aug 9, 2024
1 parent 1b8a501 commit 7990345
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/modules/physics/box2d/Shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,15 @@ int Shape::setMask(lua_State *L)
return 0;
}

int Shape::setCollisionMask(lua_State *L)
{
throwIfFixtureNotValid();
b2Filter f = fixture->GetFilterData();
f.maskBits = (uint16)getBits(L);
fixture->SetFilterData(f);
return 0;
}

void Shape::setGroupIndex(int index)
{
throwIfFixtureNotValid();
Expand Down Expand Up @@ -310,6 +319,12 @@ int Shape::getMask(lua_State *L)
return pushBits(L, ~(fixture->GetFilterData().maskBits));
}

int Shape::getCollisionMask(lua_State *L)
{
throwIfFixtureNotValid();
return pushBits(L, fixture->GetFilterData().maskBits);
}

uint16 Shape::getBits(lua_State *L)
{
// Get number of args.
Expand Down
2 changes: 2 additions & 0 deletions src/modules/physics/box2d/Shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ class Shape : public love::physics::Shape

int setCategory(lua_State *L);
int setMask(lua_State *L);
int setCollisionMask(lua_State *L);
int getCategory(lua_State *L);
int getMask(lua_State *L);
int getCollisionMask(lua_State *L);
uint16 getBits(lua_State *L);
int pushBits(lua_State *L, uint16 bits);

Expand Down
20 changes: 20 additions & 0 deletions src/modules/physics/box2d/wrap_Shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,15 @@ int w_Shape_setMask(lua_State *L)
return ret;
}

int w_Shape_setCollisionMask(lua_State *L)
{
Shape *t = luax_checkshape(L, 1);
lua_remove(L, 1);
int ret = 0;
luax_catchexcept(L, [&]() { ret = t->setCollisionMask(L); });
return ret;
}

int w_Shape_getMask(lua_State *L)
{
Shape *t = luax_checkshape(L, 1);
Expand All @@ -281,6 +290,15 @@ int w_Shape_getMask(lua_State *L)
return ret;
}

int w_Shape_getCollisionMask(lua_State *L)
{
Shape *t = luax_checkshape(L, 1);
lua_remove(L, 1);
int ret = 0;
luax_catchexcept(L, [&]() { ret = t->getCollisionMask(L); });
return ret;
}

int w_Shape_setUserData(lua_State *L)
{
Shape *t = luax_checkshape(L, 1);
Expand Down Expand Up @@ -372,7 +390,9 @@ const luaL_Reg w_Shape_functions[] =
{ "setCategory", w_Shape_setCategory },
{ "getCategory", w_Shape_getCategory },
{ "setMask", w_Shape_setMask },
{ "setCollisionMask", w_Shape_setCollisionMask },
{ "getMask", w_Shape_getMask },
{ "getCollisionMask", w_Shape_getCollisionMask },
{ "setUserData", w_Shape_setUserData },
{ "getUserData", w_Shape_getUserData },
{ "getBoundingBox", w_Shape_getBoundingBox },
Expand Down
8 changes: 4 additions & 4 deletions testing/tests/physics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -447,15 +447,15 @@ love.test.physics.Shape = function(test)
test:assertEquals(nil, yn2, 'check ray 2 y')

-- check filtering
test:assertEquals(nil, shape2:getMask(), 'check no mask')
shape2:setMask(1, 2, 3)
test:assertEquals(3, #{shape2:getMask()}, 'check set mask')
test:assertEquals(nil, shape2:getCollisionMask(), 'check no mask')
shape2:setCollisionMask(1, 2, 3)
test:assertEquals(3, #{shape2:getCollisionMask()}, 'check set mask')
test:assertEquals(0, shape2:getGroupIndex(), 'check no index')
shape2:setGroupIndex(-1)
test:assertEquals(-1, shape2:getGroupIndex(), 'check set index')
local cat, mask, group = shape2:getFilterData()
test:assertEquals(1, cat, 'check filter cat')
test:assertEquals(65528, mask, 'check filter mask')
test:assertEquals(7, mask, 'check filter mask')
test:assertEquals(-1, group, 'check filter group')

-- check destroyed
Expand Down

0 comments on commit 7990345

Please sign in to comment.