From 81dfba071cf61cf60ec1329b4b8608a70293ef02 Mon Sep 17 00:00:00 2001 From: bjorn Date: Tue, 16 May 2017 14:26:09 -0600 Subject: [PATCH] Shape:getCategory; Shape:setCategory; --- src/api/types/shapes.c | 27 +++++++++++++++++++++++++++ src/physics/physics.c | 8 ++++++++ src/physics/physics.h | 3 +++ 3 files changed, 38 insertions(+) diff --git a/src/api/types/shapes.c b/src/api/types/shapes.c index 4c3751bb..3d53bd29 100644 --- a/src/api/types/shapes.c +++ b/src/api/types/shapes.c @@ -108,3 +108,30 @@ int l_lovrShapeSetOrientation(lua_State* L) { lovrShapeSetOrientation(shape, angle, x, y, z); return 0; } + +int l_lovrShapeGetCategory(lua_State* L) { + Shape* shape = luax_checktypeof(L, 1, Shape); + uint32_t category = lovrShapeGetCategory(shape); + int count = 0; + + for (int i = 0; i < 32; i++) { + if (category & (1 << i)) { + lua_pushinteger(L, i + 1); + count++; + } + } + + return count; +} + +int l_lovrShapeSetCategory(lua_State* L) { + Shape* shape = luax_checktypeof(L, 1, Shape); + uint32_t category = 0; + + for (int i = 2; i <= lua_gettop(L); i++) { + category |= (1 << luaL_checkinteger(L, i)); + } + + lovrShapeSetCategory(shape, category); + return 0; +} diff --git a/src/physics/physics.c b/src/physics/physics.c index 8ff7bc0c..2d583f95 100644 --- a/src/physics/physics.c +++ b/src/physics/physics.c @@ -325,3 +325,11 @@ void lovrShapeSetOrientation(Shape* shape, float angle, float x, float y, float float axis[3] = { x, y, z }; dGeomSetOffsetQuaternion(shape->id, quat_fromAngleAxis(quaternion, angle, axis)); } + +uint32_t lovrShapeGetCategory(Shape* shape) { + return dGeomGetCategoryBits(shape->id); +} + +void lovrShapeSetCategory(Shape* shape, uint32_t category) { + dGeomSetCategoryBits(shape->id, category); +} diff --git a/src/physics/physics.h b/src/physics/physics.h index 95d6bc64..6f219a8e 100644 --- a/src/physics/physics.h +++ b/src/physics/physics.h @@ -1,4 +1,5 @@ #include "util.h" +#include #include typedef enum { @@ -86,3 +87,5 @@ void lovrShapeGetPosition(Shape* shape, float* x, float* y, float* z); void lovrShapeSetPosition(Shape* shape, float x, float y, float z); void lovrShapeGetOrientation(Shape* shape, float* angle, float* x, float* y, float* z); void lovrShapeSetOrientation(Shape* shape, float angle, float x, float y, float z); +uint32_t lovrShapeGetCategory(Shape* shape); +void lovrShapeSetCategory(Shape* shape, uint32_t category);