CapsuleShape;

This commit is contained in:
bjorn 2017-05-16 15:52:41 -06:00
parent e79c45ffdf
commit 6a166f7102
5 changed files with 85 additions and 4 deletions

View File

@ -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[];

View File

@ -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 }
};

View File

@ -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 }
};

View File

@ -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);
}

View File

@ -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);