From 6a166f710258e3af83d356a123cfa18d17fdf827 Mon Sep 17 00:00:00 2001 From: bjorn Date: Tue, 16 May 2017 15:52:41 -0600 Subject: [PATCH] CapsuleShape; --- src/api/lovr.h | 1 + src/api/physics.c | 9 +++++++++ src/api/types/shapes.c | 34 ++++++++++++++++++++++++++++++++++ src/physics/physics.c | 30 ++++++++++++++++++++++++++++++ src/physics/physics.h | 15 +++++++++++---- 5 files changed, 85 insertions(+), 4 deletions(-) diff --git a/src/api/lovr.h b/src/api/lovr.h index ecd0b3e4..19995528 100644 --- a/src/api/lovr.h +++ b/src/api/lovr.h @@ -15,6 +15,7 @@ int l_lovrTimerInit(lua_State* L); extern const luaL_Reg lovrAudio[]; extern const luaL_Reg lovrBoxShape[]; +extern const luaL_Reg lovrCapsuleShape[]; extern const luaL_Reg lovrController[]; extern const luaL_Reg lovrBlob[]; extern const luaL_Reg lovrBody[]; diff --git a/src/api/physics.c b/src/api/physics.c index f9056826..c5f07085 100644 --- a/src/api/physics.c +++ b/src/api/physics.c @@ -10,6 +10,7 @@ int l_lovrPhysicsInit(lua_State* L) { luax_registertype(L, "Body", lovrBody); luax_extendtype(L, "Shape", "SphereShape", lovrShape, lovrSphereShape); luax_extendtype(L, "Shape", "BoxShape", lovrShape, lovrBoxShape); + luax_extendtype(L, "Shape", "CapsuleShape", lovrShape, lovrCapsuleShape); map_init(&ShapeTypes); map_set(&ShapeTypes, "sphere", SHAPE_SPHERE); @@ -46,10 +47,18 @@ int l_lovrPhysicsNewBoxShape(lua_State* L) { return 1; } +int l_lovrPhysicsNewCapsuleShape(lua_State* L) { + float radius = luaL_optnumber(L, 1, 1.f); + float length = luaL_optnumber(L, 2, 1.f); + luax_pushtype(L, CapsuleShape, lovrCapsuleShapeCreate(radius, length)); + return 1; +} + const luaL_Reg lovrPhysics[] = { { "newWorld", l_lovrPhysicsNewWorld }, { "newBody", l_lovrPhysicsNewBody }, { "newSphereShape", l_lovrPhysicsNewSphereShape }, { "newBoxShape", l_lovrPhysicsNewBoxShape }, + { "newCapsuleShape", l_lovrPhysicsNewCapsuleShape }, { NULL, NULL } }; diff --git a/src/api/types/shapes.c b/src/api/types/shapes.c index fe114081..c3ff42f8 100644 --- a/src/api/types/shapes.c +++ b/src/api/types/shapes.c @@ -225,3 +225,37 @@ const luaL_Reg lovrBoxShape[] = { { "setDimensions", l_lovrBoxShapeSetDimensions }, { NULL, NULL } }; + +int l_lovrCapsuleShapeGetRadius(lua_State* L) { + CapsuleShape* capsule = luax_checktype(L, 1, CapsuleShape); + lua_pushnumber(L, lovrCapsuleShapeGetRadius(capsule)); + return 1; +} + +int l_lovrCapsuleShapeSetRadius(lua_State* L) { + CapsuleShape* capsule = luax_checktype(L, 1, CapsuleShape); + float radius = luaL_checknumber(L, 2); + lovrCapsuleShapeSetRadius(capsule, radius); + return 0; +} + +int l_lovrCapsuleShapeGetLength(lua_State* L) { + CapsuleShape* capsule = luax_checktype(L, 1, CapsuleShape); + lua_pushnumber(L, lovrCapsuleShapeGetLength(capsule)); + return 1; +} + +int l_lovrCapsuleShapeSetLength(lua_State* L) { + CapsuleShape* capsule = luax_checktype(L, 1, CapsuleShape); + float length = luaL_checknumber(L, 2); + lovrCapsuleShapeSetLength(capsule, length); + return 0; +} + +const luaL_Reg lovrCapsuleShape[] = { + { "getRadius", l_lovrCapsuleShapeGetRadius }, + { "setRadius", l_lovrCapsuleShapeSetRadius }, + { "getLength", l_lovrCapsuleShapeGetLength }, + { "setLength", l_lovrCapsuleShapeSetLength }, + { NULL, NULL } +}; diff --git a/src/physics/physics.c b/src/physics/physics.c index 792d0d28..817ddb2f 100644 --- a/src/physics/physics.c +++ b/src/physics/physics.c @@ -381,3 +381,33 @@ void lovrBoxShapeGetDimensions(BoxShape* box, float* x, float* y, float* z) { void lovrBoxShapeSetDimensions(BoxShape* box, float x, float y, float z) { dGeomBoxSetLengths(box->id, x, y, z); } + +CapsuleShape* lovrCapsuleShapeCreate(float radius, float length) { + CapsuleShape* capsule = lovrAlloc(sizeof(CapsuleShape), lovrShapeDestroy); + if (!capsule) return NULL; + + capsule->type = SHAPE_CAPSULE; + capsule->id = dCreateCapsule(0, radius, length); + + return capsule; +} + +float lovrCapsuleShapeGetRadius(CapsuleShape* capsule) { + float radius, length; + dGeomCapsuleGetParams(capsule->id, &radius, &length); + return radius; +} + +void lovrCapsuleShapeSetRadius(CapsuleShape* capsule, float radius) { + dGeomCapsuleSetParams(capsule->id, radius, lovrCapsuleShapeGetLength(capsule)); +} + +float lovrCapsuleShapeGetLength(CapsuleShape* capsule) { + float radius, length; + dGeomCapsuleGetParams(capsule->id, &radius, &length); + return length; +} + +void lovrCapsuleShapeSetLength(CapsuleShape* capsule, float length) { + dGeomCapsuleSetParams(capsule->id, lovrCapsuleShapeGetRadius(capsule), length); +} diff --git a/src/physics/physics.h b/src/physics/physics.h index eec79ed9..c427a929 100644 --- a/src/physics/physics.h +++ b/src/physics/physics.h @@ -29,6 +29,7 @@ typedef struct { typedef Shape SphereShape; typedef Shape BoxShape; +typedef Shape CapsuleShape; void lovrPhysicsInit(); void lovrPhysicsDestroy(); @@ -96,9 +97,15 @@ 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); +float lovrSphereShapeGetRadius(SphereShape* sphere); +void lovrSphereShapeSetRadius(SphereShape* sphere, 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); +void lovrBoxShapeGetDimensions(BoxShape* box, float* x, float* y, float* z); +void lovrBoxShapeSetDimensions(BoxShape* box, float x, float y, float z); + +CapsuleShape* lovrCapsuleShapeCreate(float radius, float length); +float lovrCapsuleShapeGetRadius(CapsuleShape* capsule); +void lovrCapsuleShapeSetRadius(CapsuleShape* capsule, float radius); +float lovrCapsuleShapeGetLength(CapsuleShape* capsule); +void lovrCapsuleShapeSetLength(CapsuleShape* capsule, float length);