From 799034573fa328a133f47f203fb6e387c3ae1fea Mon Sep 17 00:00:00 2001 From: Bruce Berrios Date: Fri, 9 Aug 2024 00:31:35 -0400 Subject: [PATCH] Add `setCollisionMask` and `getCollisionMask` to Shape's API --- src/modules/physics/box2d/Shape.cpp | 15 +++++++++++++++ src/modules/physics/box2d/Shape.h | 2 ++ src/modules/physics/box2d/wrap_Shape.cpp | 20 ++++++++++++++++++++ testing/tests/physics.lua | 8 ++++---- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/modules/physics/box2d/Shape.cpp b/src/modules/physics/box2d/Shape.cpp index cec11f5c3..c73b01b29 100644 --- a/src/modules/physics/box2d/Shape.cpp +++ b/src/modules/physics/box2d/Shape.cpp @@ -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(); @@ -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. diff --git a/src/modules/physics/box2d/Shape.h b/src/modules/physics/box2d/Shape.h index 43f754598..26ea87e6d 100644 --- a/src/modules/physics/box2d/Shape.h +++ b/src/modules/physics/box2d/Shape.h @@ -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); diff --git a/src/modules/physics/box2d/wrap_Shape.cpp b/src/modules/physics/box2d/wrap_Shape.cpp index 50ca1c8dc..a279bb702 100644 --- a/src/modules/physics/box2d/wrap_Shape.cpp +++ b/src/modules/physics/box2d/wrap_Shape.cpp @@ -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); @@ -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); @@ -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 }, diff --git a/testing/tests/physics.lua b/testing/tests/physics.lua index 5e4662526..c571ca1d1 100644 --- a/testing/tests/physics.lua +++ b/testing/tests/physics.lua @@ -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