diff --git a/src/api/lovr.h b/src/api/lovr.h index 19995528..726a611f 100644 --- a/src/api/lovr.h +++ b/src/api/lovr.h @@ -17,6 +17,7 @@ extern const luaL_Reg lovrAudio[]; extern const luaL_Reg lovrBoxShape[]; extern const luaL_Reg lovrCapsuleShape[]; extern const luaL_Reg lovrController[]; +extern const luaL_Reg lovrCylinderShape[]; extern const luaL_Reg lovrBlob[]; extern const luaL_Reg lovrBody[]; extern const luaL_Reg lovrEvent[]; diff --git a/src/api/physics.c b/src/api/physics.c index c5f07085..4517213c 100644 --- a/src/api/physics.c +++ b/src/api/physics.c @@ -11,6 +11,7 @@ int l_lovrPhysicsInit(lua_State* L) { luax_extendtype(L, "Shape", "SphereShape", lovrShape, lovrSphereShape); luax_extendtype(L, "Shape", "BoxShape", lovrShape, lovrBoxShape); luax_extendtype(L, "Shape", "CapsuleShape", lovrShape, lovrCapsuleShape); + luax_extendtype(L, "Shape", "CylinderShape", lovrShape, lovrCylinderShape); map_init(&ShapeTypes); map_set(&ShapeTypes, "sphere", SHAPE_SPHERE); @@ -54,11 +55,19 @@ int l_lovrPhysicsNewCapsuleShape(lua_State* L) { return 1; } +int l_lovrPhysicsNewCylinderShape(lua_State* L) { + float radius = luaL_optnumber(L, 1, 1.f); + float length = luaL_optnumber(L, 2, 1.f); + luax_pushtype(L, CylinderShape, lovrCylinderShapeCreate(radius, length)); + return 1; +} + const luaL_Reg lovrPhysics[] = { { "newWorld", l_lovrPhysicsNewWorld }, { "newBody", l_lovrPhysicsNewBody }, { "newSphereShape", l_lovrPhysicsNewSphereShape }, { "newBoxShape", l_lovrPhysicsNewBoxShape }, { "newCapsuleShape", l_lovrPhysicsNewCapsuleShape }, + { "newCylinderShape", l_lovrPhysicsNewCylinderShape }, { NULL, NULL } }; diff --git a/src/api/types/shapes.c b/src/api/types/shapes.c index c3ff42f8..b581ec8e 100644 --- a/src/api/types/shapes.c +++ b/src/api/types/shapes.c @@ -259,3 +259,37 @@ const luaL_Reg lovrCapsuleShape[] = { { "setLength", l_lovrCapsuleShapeSetLength }, { NULL, NULL } }; + +int l_lovrCylinderShapeGetRadius(lua_State* L) { + CylinderShape* cylinder = luax_checktype(L, 1, CylinderShape); + lua_pushnumber(L, lovrCylinderShapeGetRadius(cylinder)); + return 1; +} + +int l_lovrCylinderShapeSetRadius(lua_State* L) { + CylinderShape* cylinder = luax_checktype(L, 1, CylinderShape); + float radius = luaL_checknumber(L, 2); + lovrCylinderShapeSetRadius(cylinder, radius); + return 0; +} + +int l_lovrCylinderShapeGetLength(lua_State* L) { + CylinderShape* cylinder = luax_checktype(L, 1, CylinderShape); + lua_pushnumber(L, lovrCylinderShapeGetLength(cylinder)); + return 1; +} + +int l_lovrCylinderShapeSetLength(lua_State* L) { + CylinderShape* cylinder = luax_checktype(L, 1, CylinderShape); + float length = luaL_checknumber(L, 2); + lovrCylinderShapeSetLength(cylinder, length); + return 0; +} + +const luaL_Reg lovrCylinderShape[] = { + { "getRadius", l_lovrCylinderShapeGetRadius }, + { "setRadius", l_lovrCylinderShapeSetRadius }, + { "getLength", l_lovrCylinderShapeGetLength }, + { "setLength", l_lovrCylinderShapeSetLength }, + { NULL, NULL } +}; diff --git a/src/physics/physics.c b/src/physics/physics.c index 817ddb2f..37b3f7c8 100644 --- a/src/physics/physics.c +++ b/src/physics/physics.c @@ -411,3 +411,33 @@ float lovrCapsuleShapeGetLength(CapsuleShape* capsule) { void lovrCapsuleShapeSetLength(CapsuleShape* capsule, float length) { dGeomCapsuleSetParams(capsule->id, lovrCapsuleShapeGetRadius(capsule), length); } + +CylinderShape* lovrCylinderShapeCreate(float radius, float length) { + CylinderShape* cylinder = lovrAlloc(sizeof(CylinderShape), lovrShapeDestroy); + if (!cylinder) return NULL; + + cylinder->type = SHAPE_CYLINDER; + cylinder->id = dCreateCylinder(0, radius, length); + + return cylinder; +} + +float lovrCylinderShapeGetRadius(CylinderShape* cylinder) { + float radius, length; + dGeomCylinderGetParams(cylinder->id, &radius, &length); + return radius; +} + +void lovrCylinderShapeSetRadius(CylinderShape* cylinder, float radius) { + dGeomCylinderSetParams(cylinder->id, radius, lovrCylinderShapeGetLength(cylinder)); +} + +float lovrCylinderShapeGetLength(CylinderShape* cylinder) { + float radius, length; + dGeomCylinderGetParams(cylinder->id, &radius, &length); + return length; +} + +void lovrCylinderShapeSetLength(CylinderShape* cylinder, float length) { + dGeomCylinderSetParams(cylinder->id, lovrCylinderShapeGetRadius(cylinder), length); +} diff --git a/src/physics/physics.h b/src/physics/physics.h index c427a929..e18cc313 100644 --- a/src/physics/physics.h +++ b/src/physics/physics.h @@ -30,6 +30,7 @@ typedef struct { typedef Shape SphereShape; typedef Shape BoxShape; typedef Shape CapsuleShape; +typedef Shape CylinderShape; void lovrPhysicsInit(); void lovrPhysicsDestroy(); @@ -109,3 +110,9 @@ float lovrCapsuleShapeGetRadius(CapsuleShape* capsule); void lovrCapsuleShapeSetRadius(CapsuleShape* capsule, float radius); float lovrCapsuleShapeGetLength(CapsuleShape* capsule); void lovrCapsuleShapeSetLength(CapsuleShape* capsule, float length); + +CylinderShape* lovrCylinderShapeCreate(float radius, float length); +float lovrCylinderShapeGetRadius(CylinderShape* cylinder); +void lovrCylinderShapeSetRadius(CylinderShape* cylinder, float radius); +float lovrCylinderShapeGetLength(CylinderShape* cylinder); +void lovrCylinderShapeSetLength(CylinderShape* cylinder, float length);