diff --git a/src/modules/physics/box2d/Shape.cpp b/src/modules/physics/box2d/Shape.cpp index f4413d171..61a4e28a5 100644 --- a/src/modules/physics/box2d/Shape.cpp +++ b/src/modules/physics/box2d/Shape.cpp @@ -159,6 +159,67 @@ namespace box2d v[1] = (int)f.maskBits; v[2] = f.groupIndex; } + + int Shape::setCategory(lua_State * L) + { + b2FilterData f = shape->GetFilterData(); + f.categoryBits = (uint16)getBits(L); + shape->SetFilterData(f); + shape->GetBody()->GetWorld()->Refilter(shape); + return 0; + } + + int Shape::setMask(lua_State * L) + { + b2FilterData f = shape->GetFilterData(); + f.maskBits = ~(uint16)getBits(L); + shape->SetFilterData(f); + shape->GetBody()->GetWorld()->Refilter(shape); + return 0; + } + + int Shape::getCategory(lua_State * L) + { + return pushBits(L, shape->GetFilterData().categoryBits); + } + + int Shape::getMask(lua_State * L) + { + return pushBits(L, ~(shape->GetFilterData().maskBits)); + } + + uint16 Shape::getBits(lua_State * L) + { + // Get number of args. + int argc = lua_gettop(L); + + // The new bitset. + std::bitset<16> b; + + for(int i = 1;i<=argc;i++) + { + size_t bpos = (size_t)(lua_tointeger(L, i)-1); + if(bpos < 0 || bpos > 16) + return luaL_error(L, "Values must be in range 1-16."); + b.set(bpos, true); + } + + return (uint16)b.to_ulong(); + } + + int Shape::pushBits(lua_State * L, uint16 bits) + { + // Create a bitset. + std::bitset<16> b((unsigned long)bits); + + // Push all set bits. + for(int i = 0;i<16;i++) + if(b.test(i)) + lua_pushinteger(L, i+1); + + // Count number of set bits. + return (int)b.count(); + } int Shape::setData(lua_State * L) { diff --git a/src/modules/physics/box2d/Shape.h b/src/modules/physics/box2d/Shape.h index 48c9f5c26..1233bc7dd 100644 --- a/src/modules/physics/box2d/Shape.h +++ b/src/modules/physics/box2d/Shape.h @@ -167,6 +167,13 @@ namespace box2d * category (16-bits), mask (16-bits) and group (32-bits/int). **/ void getFilterData(int * v); + + int setCategory(lua_State * L); + int setMask(lua_State * L); + int getCategory(lua_State * L); + int getMask(lua_State * L); + uint16 getBits(lua_State * L); + int pushBits(lua_State * L, uint16 bits); /** * This function stores an in-C reference to diff --git a/src/modules/physics/box2d/wrap_CircleShape.cpp b/src/modules/physics/box2d/wrap_CircleShape.cpp index e87fed2e9..7c305387b 100644 --- a/src/modules/physics/box2d/wrap_CircleShape.cpp +++ b/src/modules/physics/box2d/wrap_CircleShape.cpp @@ -54,6 +54,10 @@ namespace box2d { "testSegment", w_Shape_testSegment }, { "setFilterData", w_Shape_setFilterData }, { "getFilterData", w_Shape_getFilterData }, + { "setCategory", w_Shape_setCategory }, + { "getCategory", w_Shape_getCategory }, + { "setMask", w_Shape_setMask }, + { "getMask", w_Shape_getMask }, { "setData", w_Shape_setData }, { "getData", w_Shape_getData }, { "getBoundingBox", w_Shape_getBoundingBox }, diff --git a/src/modules/physics/box2d/wrap_PolygonShape.cpp b/src/modules/physics/box2d/wrap_PolygonShape.cpp index 494ff650c..bd8173bbc 100644 --- a/src/modules/physics/box2d/wrap_PolygonShape.cpp +++ b/src/modules/physics/box2d/wrap_PolygonShape.cpp @@ -54,6 +54,10 @@ namespace box2d { "testSegment", w_Shape_testSegment }, { "setFilterData", w_Shape_setFilterData }, { "getFilterData", w_Shape_getFilterData }, + { "setCategory", w_Shape_setCategory }, + { "getCategory", w_Shape_getCategory }, + { "setMask", w_Shape_setMask }, + { "getMask", w_Shape_getMask }, { "setData", w_Shape_setData }, { "getData", w_Shape_getData }, { "getBoundingBox", w_Shape_getBoundingBox }, diff --git a/src/modules/physics/box2d/wrap_Shape.cpp b/src/modules/physics/box2d/wrap_Shape.cpp index 4729c5188..f105c1c07 100644 --- a/src/modules/physics/box2d/wrap_Shape.cpp +++ b/src/modules/physics/box2d/wrap_Shape.cpp @@ -144,6 +144,34 @@ namespace box2d lua_pushinteger(L, v[2]); return 3; } + + int w_Shape_setCategory(lua_State * L) + { + Shape * t = luax_checkshape(L, 1); + lua_remove(L, 1); + return t->setCategory(L); + } + + int w_Shape_getCategory(lua_State * L) + { + Shape * t = luax_checkshape(L, 1); + lua_remove(L, 1); + return t->getCategory(L); + } + + int w_Shape_setMask(lua_State * L) + { + Shape * t = luax_checkshape(L, 1); + lua_remove(L, 1); + return t->setMask(L); + } + + int w_Shape_getMask(lua_State * L) + { + Shape * t = luax_checkshape(L, 1); + lua_remove(L, 1); + return t->getMask(L); + } int w_Shape_setData(lua_State * L) { @@ -180,6 +208,10 @@ namespace box2d { "testSegment", w_Shape_testSegment }, { "setFilterData", w_Shape_setFilterData }, { "getFilterData", w_Shape_getFilterData }, + { "setCategory", w_Shape_setCategory }, + { "getCategory", w_Shape_getCategory }, + { "setMask", w_Shape_setMask }, + { "getMask", w_Shape_getMask }, { "setData", w_Shape_setData }, { "getData", w_Shape_getData }, { "getBoundingBox", w_Shape_getBoundingBox }, diff --git a/src/modules/physics/box2d/wrap_Shape.h b/src/modules/physics/box2d/wrap_Shape.h index 0b71dc9f7..32fac8eb2 100644 --- a/src/modules/physics/box2d/wrap_Shape.h +++ b/src/modules/physics/box2d/wrap_Shape.h @@ -46,6 +46,10 @@ namespace box2d int w_Shape_testSegment(lua_State * L); int w_Shape_setFilterData(lua_State * L); int w_Shape_getFilterData(lua_State * L); + int w_Shape_setCategory(lua_State * L); + int w_Shape_getCategory(lua_State * L); + int w_Shape_setMask(lua_State * L); + int w_Shape_getMask(lua_State * L); int w_Shape_setData(lua_State * L); int w_Shape_getData(lua_State * L); int w_Shape_getBoundingBox(lua_State * L);