diff --git a/src/api/lovr.h b/src/api/lovr.h index 358ec614..ecd0b3e4 100644 --- a/src/api/lovr.h +++ b/src/api/lovr.h @@ -14,6 +14,7 @@ int l_lovrPhysicsInit(lua_State* L); int l_lovrTimerInit(lua_State* L); extern const luaL_Reg lovrAudio[]; +extern const luaL_Reg lovrBoxShape[]; extern const luaL_Reg lovrController[]; extern const luaL_Reg lovrBlob[]; extern const luaL_Reg lovrBody[]; @@ -27,6 +28,7 @@ extern const luaL_Reg lovrMesh[]; extern const luaL_Reg lovrModel[]; extern const luaL_Reg lovrPhysics[]; extern const luaL_Reg lovrShader[]; +extern const luaL_Reg lovrShape[]; extern const luaL_Reg lovrSkybox[]; extern const luaL_Reg lovrSource[]; extern const luaL_Reg lovrSphereShape[]; diff --git a/src/api/physics.c b/src/api/physics.c index 048480bb..f9056826 100644 --- a/src/api/physics.c +++ b/src/api/physics.c @@ -8,7 +8,8 @@ int l_lovrPhysicsInit(lua_State* L) { luaL_register(L, NULL, lovrPhysics); luax_registertype(L, "World", lovrWorld); luax_registertype(L, "Body", lovrBody); - luax_extendtype(L, "Shape", "SphereShape", lovrSphereShape); + luax_extendtype(L, "Shape", "SphereShape", lovrShape, lovrSphereShape); + luax_extendtype(L, "Shape", "BoxShape", lovrShape, lovrBoxShape); map_init(&ShapeTypes); map_set(&ShapeTypes, "sphere", SHAPE_SPHERE); @@ -37,9 +38,18 @@ int l_lovrPhysicsNewSphereShape(lua_State* L) { return 1; } +int l_lovrPhysicsNewBoxShape(lua_State* L) { + float x = luaL_optnumber(L, 1, 1.f); + float y = luaL_optnumber(L, 2, x); + float z = luaL_optnumber(L, 3, x); + luax_pushtype(L, BoxShape, lovrBoxShapeCreate(x, y, z)); + return 1; +} + const luaL_Reg lovrPhysics[] = { { "newWorld", l_lovrPhysicsNewWorld }, { "newBody", l_lovrPhysicsNewBody }, { "newSphereShape", l_lovrPhysicsNewSphereShape }, + { "newBoxShape", l_lovrPhysicsNewBoxShape }, { NULL, NULL } }; diff --git a/src/api/types/shapes.c b/src/api/types/shapes.c index e27c9e62..fe114081 100644 --- a/src/api/types/shapes.c +++ b/src/api/types/shapes.c @@ -163,20 +163,7 @@ int l_lovrShapeSetMask(lua_State* L) { return 0; } -int l_lovrSphereShapeGetRadius(lua_State* L) { - SphereShape* sphere = luax_checktype(L, 1, SphereShape); - lua_pushnumber(L, lovrSphereShapeGetRadius(sphere)); - return 1; -} - -int l_lovrSphereShapeSetRadius(lua_State* L) { - SphereShape* sphere = luax_checktype(L, 1, SphereShape); - float radius = luaL_checknumber(L, 2); - lovrSphereShapeSetRadius(sphere, radius); - return 0; -} - -const luaL_Reg lovrSphereShape[] = { +const luaL_Reg lovrShape[] = { { "getType", l_lovrShapeGetType }, { "getBody", l_lovrShapeGetBody }, { "setBody", l_lovrShapeSetBody }, @@ -192,7 +179,49 @@ const luaL_Reg lovrSphereShape[] = { { "setCategory", l_lovrShapeSetCategory }, { "getMask", l_lovrShapeGetMask }, { "setMask", l_lovrShapeSetMask }, + { NULL, NULL } +}; + +int l_lovrSphereShapeGetRadius(lua_State* L) { + SphereShape* sphere = luax_checktype(L, 1, SphereShape); + lua_pushnumber(L, lovrSphereShapeGetRadius(sphere)); + return 1; +} + +int l_lovrSphereShapeSetRadius(lua_State* L) { + SphereShape* sphere = luax_checktype(L, 1, SphereShape); + float radius = luaL_checknumber(L, 2); + lovrSphereShapeSetRadius(sphere, radius); + return 0; +} + +const luaL_Reg lovrSphereShape[] = { { "getRadius", l_lovrSphereShapeGetRadius }, { "setRadius", l_lovrSphereShapeSetRadius }, { NULL, NULL } }; + +int l_lovrBoxShapeGetDimensions(lua_State* L) { + BoxShape* box = luax_checktype(L, 1, BoxShape); + float x, y, z; + lovrBoxShapeGetDimensions(box, &x, &y, &z); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + lua_pushnumber(L, z); + return 3; +} + +int l_lovrBoxShapeSetDimensions(lua_State* L) { + BoxShape* box = luax_checktype(L, 1, BoxShape); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float z = luaL_checknumber(L, 4); + lovrBoxShapeSetDimensions(box, x, y, z); + return 0; +} + +const luaL_Reg lovrBoxShape[] = { + { "getDimensions", l_lovrBoxShapeGetDimensions }, + { "setDimensions", l_lovrBoxShapeSetDimensions }, + { NULL, NULL } +}; diff --git a/src/luax.c b/src/luax.c index b7a9af90..88c9aa04 100644 --- a/src/luax.c +++ b/src/luax.c @@ -71,13 +71,17 @@ void luax_registertype(lua_State* L, const char* name, const luaL_Reg* functions lua_pop(L, 1); } -void luax_extendtype(lua_State* L, const char* base, const char* name, const luaL_Reg* functions) { +void luax_extendtype(lua_State* L, const char* base, const char* name, const luaL_Reg* baseFunctions, const luaL_Reg* functions) { luax_registertype(L, name, functions); luaL_getmetatable(L, name); lua_pushstring(L, base); lua_setfield(L, -2, "super"); + if (baseFunctions) { + luaL_register(L, NULL, baseFunctions); + } + lua_pop(L, 1); } diff --git a/src/luax.h b/src/luax.h index 68aa6131..6a44aac5 100644 --- a/src/luax.h +++ b/src/luax.h @@ -26,7 +26,7 @@ int luax_preloadmodule(lua_State* L, const char* key, lua_CFunction f); void luax_registertype(lua_State* L, const char* name, const luaL_Reg* functions); -void luax_extendtype(lua_State* L, const char* base, const char* name, const luaL_Reg* functions); +void luax_extendtype(lua_State* L, const char* base, const char* name, const luaL_Reg* baseFunctions, const luaL_Reg* functions); int luax_releasetype(lua_State* L); int luax_getobject(lua_State* L, void* object); void luax_registerobject(lua_State* L, void* object); diff --git a/src/physics/physics.c b/src/physics/physics.c index dd6d4b7d..792d0d28 100644 --- a/src/physics/physics.c +++ b/src/physics/physics.c @@ -359,3 +359,25 @@ float lovrSphereShapeGetRadius(SphereShape* sphere) { void lovrSphereShapeSetRadius(SphereShape* sphere, float radius) { dGeomSphereSetRadius(sphere->id, radius); } + +BoxShape* lovrBoxShapeCreate(float x, float y, float z) { + BoxShape* box = lovrAlloc(sizeof(BoxShape), lovrShapeDestroy); + if (!box) return NULL; + + box->type = SHAPE_BOX; + box->id = dCreateBox(0, x, y, z); + + return box; +} + +void lovrBoxShapeGetDimensions(BoxShape* box, float* x, float* y, float* z) { + float dimensions[3]; + dGeomBoxGetLengths(box->id, dimensions); + *x = dimensions[0]; + *y = dimensions[1]; + *z = dimensions[2]; +} + +void lovrBoxShapeSetDimensions(BoxShape* box, float x, float y, float z) { + dGeomBoxSetLengths(box->id, x, y, z); +} diff --git a/src/physics/physics.h b/src/physics/physics.h index 1b8457ca..eec79ed9 100644 --- a/src/physics/physics.h +++ b/src/physics/physics.h @@ -28,6 +28,7 @@ typedef struct { } Shape; typedef Shape SphereShape; +typedef Shape BoxShape; void lovrPhysicsInit(); void lovrPhysicsDestroy(); @@ -97,3 +98,7 @@ void lovrShapeSetMask(Shape* shape, uint32_t mask); SphereShape* lovrSphereShapeCreate(float radius); float lovrSphereShapeGetRadius(SphereShape* shape); void lovrSphereShapeSetRadius(SphereShape* shape, float radius); + +BoxShape* lovrBoxShapeCreate(float x, float y, float z); +void lovrBoxShapeGetDimensions(BoxShape* shape, float* x, float* y, float* z); +void lovrBoxShapeSetDimensions(BoxShape* shape, float x, float y, float z);