diff --git a/src/api/lovr.h b/src/api/lovr.h index e76a9fcd..358ec614 100644 --- a/src/api/lovr.h +++ b/src/api/lovr.h @@ -29,6 +29,7 @@ extern const luaL_Reg lovrPhysics[]; extern const luaL_Reg lovrShader[]; extern const luaL_Reg lovrSkybox[]; extern const luaL_Reg lovrSource[]; +extern const luaL_Reg lovrSphereShape[]; extern const luaL_Reg lovrTexture[]; extern const luaL_Reg lovrTimer[]; extern const luaL_Reg lovrTransform[]; diff --git a/src/api/physics.c b/src/api/physics.c index 9d7f8be8..048480bb 100644 --- a/src/api/physics.c +++ b/src/api/physics.c @@ -8,6 +8,7 @@ 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); map_init(&ShapeTypes); map_set(&ShapeTypes, "sphere", SHAPE_SPHERE); @@ -30,8 +31,15 @@ int l_lovrPhysicsNewBody(lua_State* L) { return 1; } +int l_lovrPhysicsNewSphereShape(lua_State* L) { + float radius = luaL_optnumber(L, 1, 1.f); + luax_pushtype(L, SphereShape, lovrSphereShapeCreate(radius)); + return 1; +} + const luaL_Reg lovrPhysics[] = { { "newWorld", l_lovrPhysicsNewWorld }, { "newBody", l_lovrPhysicsNewBody }, + { "newSphereShape", l_lovrPhysicsNewSphereShape }, { NULL, NULL } }; diff --git a/src/api/types/shapes.c b/src/api/types/shapes.c index e8a4e866..e27c9e62 100644 --- a/src/api/types/shapes.c +++ b/src/api/types/shapes.c @@ -162,3 +162,37 @@ int l_lovrShapeSetMask(lua_State* L) { lovrShapeSetMask(shape, ~mask); 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[] = { + { "getType", l_lovrShapeGetType }, + { "getBody", l_lovrShapeGetBody }, + { "setBody", l_lovrShapeSetBody }, + { "isEnabled", l_lovrShapeIsEnabled }, + { "setEnabled", l_lovrShapeSetEnabled }, + { "getUserData", l_lovrShapeGetUserData }, + { "setUserData", l_lovrShapeSetUserData }, + { "getPosition", l_lovrShapeGetPosition }, + { "setPosition", l_lovrShapeSetPosition }, + { "getOrientation", l_lovrShapeGetOrientation }, + { "setOrientation", l_lovrShapeSetOrientation }, + { "getCategory", l_lovrShapeGetCategory }, + { "setCategory", l_lovrShapeSetCategory }, + { "getMask", l_lovrShapeGetMask }, + { "setMask", l_lovrShapeSetMask }, + { "getRadius", l_lovrSphereShapeGetRadius }, + { "setRadius", l_lovrSphereShapeSetRadius }, + { NULL, NULL } +}; diff --git a/src/physics/physics.c b/src/physics/physics.c index 46ced2a1..dd6d4b7d 100644 --- a/src/physics/physics.c +++ b/src/physics/physics.c @@ -341,3 +341,21 @@ uint32_t lovrShapeGetMask(Shape* shape) { void lovrShapeSetMask(Shape* shape, uint32_t mask) { dGeomSetCollideBits(shape->id, mask); } + +SphereShape* lovrSphereShapeCreate(float radius) { + SphereShape* sphere = lovrAlloc(sizeof(SphereShape), lovrShapeDestroy); + if (!sphere) return NULL; + + sphere->type = SHAPE_SPHERE; + sphere->id = dCreateSphere(0, radius); + + return sphere; +} + +float lovrSphereShapeGetRadius(SphereShape* sphere) { + return dGeomSphereGetRadius(sphere->id); +} + +void lovrSphereShapeSetRadius(SphereShape* sphere, float radius) { + dGeomSphereSetRadius(sphere->id, radius); +} diff --git a/src/physics/physics.h b/src/physics/physics.h index 9fb3ebf5..1b8457ca 100644 --- a/src/physics/physics.h +++ b/src/physics/physics.h @@ -27,6 +27,8 @@ typedef struct { Body* body; } Shape; +typedef Shape SphereShape; + void lovrPhysicsInit(); void lovrPhysicsDestroy(); @@ -91,3 +93,7 @@ uint32_t lovrShapeGetCategory(Shape* shape); void lovrShapeSetCategory(Shape* shape, uint32_t category); uint32_t lovrShapeGetMask(Shape* shape); void lovrShapeSetMask(Shape* shape, uint32_t mask); + +SphereShape* lovrSphereShapeCreate(float radius); +float lovrSphereShapeGetRadius(SphereShape* shape); +void lovrSphereShapeSetRadius(SphereShape* shape, float radius);