1
0
Fork 0
mirror of https://github.com/bjornbytes/lovr.git synced 2024-07-02 12:33:52 +00:00
lovr/src/api/l_physics_shapes.c

304 lines
8.4 KiB
C
Raw Normal View History

2017-12-10 20:40:37 +00:00
#include "api.h"
2017-05-16 18:23:13 +00:00
#include "physics/physics.h"
void luax_pushshape(lua_State* L, Shape* shape) {
switch (shape->type) {
case SHAPE_SPHERE: luax_pushtype(L, SphereShape, shape); break;
case SHAPE_BOX: luax_pushtype(L, BoxShape, shape); break;
case SHAPE_CAPSULE: luax_pushtype(L, CapsuleShape, shape); break;
case SHAPE_CYLINDER: luax_pushtype(L, CylinderShape, shape); break;
2019-11-12 00:27:30 +00:00
default: lovrThrow("Unreachable");
}
}
Shape* luax_checkshape(lua_State* L, int index) {
Proxy* p = lua_touserdata(L, index);
2019-09-07 22:07:07 +00:00
if (p) {
const uint64_t hashes[] = {
hash64("SphereShape", strlen("SphereShape")),
hash64("BoxShape", strlen("BoxShape")),
hash64("CapsuleShape", strlen("CapsuleShape")),
hash64("CylinderShape", strlen("CylinderShape"))
};
for (size_t i = 0; i < sizeof(hashes) / sizeof(hashes[0]); i++) {
if (p->hash == hashes[i]) {
return p->object;
}
}
}
2019-09-07 22:07:07 +00:00
luaL_typerror(L, index, "Shape");
return NULL;
}
2019-02-17 22:52:22 +00:00
static int l_lovrShapeDestroy(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
2017-05-20 04:24:23 +00:00
lovrShapeDestroyData(shape);
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrShapeGetType(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
luax_pushenum(L, ShapeTypes, lovrShapeGetType(shape));
2017-05-16 18:23:13 +00:00
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrShapeGetCollider(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
luax_pushtype(L, Collider, lovrShapeGetCollider(shape));
2017-05-16 18:23:13 +00:00
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrShapeIsEnabled(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
2017-05-16 18:29:18 +00:00
lua_pushboolean(L, lovrShapeIsEnabled(shape));
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrShapeSetEnabled(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
2017-10-31 08:14:09 +00:00
bool enabled = lua_toboolean(L, 2);
2017-05-16 18:29:18 +00:00
lovrShapeSetEnabled(shape, enabled);
return 0;
}
2017-05-16 18:33:55 +00:00
2019-10-10 22:36:28 +00:00
static int l_lovrShapeIsSensor(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
lua_pushboolean(L, lovrShapeIsSensor(shape));
return 1;
}
static int l_lovrShapeSetSensor(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
bool sensor = lua_toboolean(L, 2);
lovrShapeSetSensor(shape, sensor);
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrShapeGetUserData(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
2019-03-17 07:58:01 +00:00
union { int i; void* p; } ref = { .p = lovrShapeGetUserData(shape) };
lua_rawgeti(L, LUA_REGISTRYINDEX, ref.i);
2017-05-16 18:33:55 +00:00
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrShapeSetUserData(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
2019-03-17 07:58:01 +00:00
union { int i; void* p; } ref = { .p = lovrShapeGetUserData(shape) };
if (ref.i) {
luaL_unref(L, LUA_REGISTRYINDEX, ref.i);
2017-05-16 18:33:55 +00:00
}
if (lua_gettop(L) < 2) {
lua_pushnil(L);
}
lua_settop(L, 2);
2019-03-17 07:58:01 +00:00
ref.i = luaL_ref(L, LUA_REGISTRYINDEX);
lovrShapeSetUserData(shape, ref.p);
2017-05-16 18:33:55 +00:00
return 0;
}
2017-05-16 18:46:15 +00:00
2019-02-17 22:52:22 +00:00
static int l_lovrShapeGetPosition(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
2017-05-16 18:46:15 +00:00
float x, y, z;
lovrShapeGetPosition(shape, &x, &y, &z);
lua_pushnumber(L, x);
lua_pushnumber(L, y);
lua_pushnumber(L, z);
return 3;
}
2019-02-17 22:52:22 +00:00
static int l_lovrShapeSetPosition(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
float x = luax_checkfloat(L, 2);
float y = luax_checkfloat(L, 3);
float z = luax_checkfloat(L, 4);
2017-05-16 18:46:15 +00:00
lovrShapeSetPosition(shape, x, y, z);
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrShapeGetOrientation(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
float angle, x, y, z;
lovrShapeGetOrientation(shape, &angle, &x, &y, &z);
lua_pushnumber(L, angle);
lua_pushnumber(L, x);
lua_pushnumber(L, y);
lua_pushnumber(L, z);
return 4;
}
2019-02-17 22:52:22 +00:00
static int l_lovrShapeSetOrientation(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
float angle = luax_checkfloat(L, 2);
float x = luax_checkfloat(L, 3);
float y = luax_checkfloat(L, 4);
float z = luax_checkfloat(L, 5);
lovrShapeSetOrientation(shape, angle, x, y, z);
return 0;
}
2017-05-16 20:26:09 +00:00
2019-02-17 22:52:22 +00:00
static int l_lovrShapeGetMass(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
float density = luax_checkfloat(L, 2);
2017-05-17 00:25:08 +00:00
float cx, cy, cz, mass;
2017-05-17 01:13:38 +00:00
float inertia[6];
2017-05-25 00:47:59 +00:00
lovrShapeGetMass(shape, density, &cx, &cy, &cz, &mass, inertia);
2017-05-17 00:25:08 +00:00
lua_pushnumber(L, cx);
lua_pushnumber(L, cy);
lua_pushnumber(L, cz);
lua_pushnumber(L, mass);
lua_newtable(L);
2017-05-17 01:13:38 +00:00
for (int i = 0; i < 6; i++) {
2017-05-17 00:25:08 +00:00
lua_pushnumber(L, inertia[i]);
lua_rawseti(L, -2, i + 1);
}
return 5;
}
2019-02-17 22:52:22 +00:00
static int l_lovrShapeGetAABB(lua_State* L) {
Shape* shape = luax_checkshape(L, 1);
2017-05-25 00:47:59 +00:00
float aabb[6];
lovrShapeGetAABB(shape, aabb);
for (int i = 0; i < 6; i++) {
lua_pushnumber(L, aabb[i]);
}
return 6;
}
#define lovrShape \
{ "destroy", l_lovrShapeDestroy }, \
{ "getType", l_lovrShapeGetType }, \
{ "getCollider", l_lovrShapeGetCollider }, \
{ "isEnabled", l_lovrShapeIsEnabled }, \
{ "setEnabled", l_lovrShapeSetEnabled }, \
2019-10-10 22:36:28 +00:00
{ "isSensor", l_lovrShapeIsSensor }, \
{ "setSensor", l_lovrShapeSetSensor }, \
{ "getUserData", l_lovrShapeGetUserData }, \
{ "setUserData", l_lovrShapeSetUserData }, \
{ "getPosition", l_lovrShapeGetPosition }, \
{ "setPosition", l_lovrShapeSetPosition }, \
{ "getOrientation", l_lovrShapeGetOrientation }, \
{ "setOrientation", l_lovrShapeSetOrientation }, \
{ "getMass", l_lovrShapeGetMass }, \
{ "getAABB", l_lovrShapeGetAABB }
2017-05-16 21:37:05 +00:00
2019-02-17 22:52:22 +00:00
static int l_lovrSphereShapeGetRadius(lua_State* L) {
2017-05-16 21:37:05 +00:00
SphereShape* sphere = luax_checktype(L, 1, SphereShape);
lua_pushnumber(L, lovrSphereShapeGetRadius(sphere));
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrSphereShapeSetRadius(lua_State* L) {
2017-05-16 21:37:05 +00:00
SphereShape* sphere = luax_checktype(L, 1, SphereShape);
float radius = luax_checkfloat(L, 2);
2017-05-16 21:37:05 +00:00
lovrSphereShapeSetRadius(sphere, radius);
return 0;
}
const luaL_Reg lovrSphereShape[] = {
lovrShape,
2017-05-16 21:21:10 +00:00
{ "getRadius", l_lovrSphereShapeGetRadius },
{ "setRadius", l_lovrSphereShapeSetRadius },
{ NULL, NULL }
};
2017-05-16 21:37:05 +00:00
2019-02-17 22:52:22 +00:00
static int l_lovrBoxShapeGetDimensions(lua_State* L) {
2017-05-16 21:37:05 +00:00
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;
}
2019-02-17 22:52:22 +00:00
static int l_lovrBoxShapeSetDimensions(lua_State* L) {
2017-05-16 21:37:05 +00:00
BoxShape* box = luax_checktype(L, 1, BoxShape);
float x = luax_checkfloat(L, 2);
float y = luax_checkfloat(L, 3);
float z = luax_checkfloat(L, 4);
2017-05-16 21:37:05 +00:00
lovrBoxShapeSetDimensions(box, x, y, z);
return 0;
}
const luaL_Reg lovrBoxShape[] = {
lovrShape,
2017-05-16 21:37:05 +00:00
{ "getDimensions", l_lovrBoxShapeGetDimensions },
{ "setDimensions", l_lovrBoxShapeSetDimensions },
{ NULL, NULL }
};
2017-05-16 21:52:41 +00:00
2019-02-17 22:52:22 +00:00
static int l_lovrCapsuleShapeGetRadius(lua_State* L) {
2017-05-16 21:52:41 +00:00
CapsuleShape* capsule = luax_checktype(L, 1, CapsuleShape);
lua_pushnumber(L, lovrCapsuleShapeGetRadius(capsule));
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrCapsuleShapeSetRadius(lua_State* L) {
2017-05-16 21:52:41 +00:00
CapsuleShape* capsule = luax_checktype(L, 1, CapsuleShape);
float radius = luax_checkfloat(L, 2);
2017-05-16 21:52:41 +00:00
lovrCapsuleShapeSetRadius(capsule, radius);
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrCapsuleShapeGetLength(lua_State* L) {
2017-05-16 21:52:41 +00:00
CapsuleShape* capsule = luax_checktype(L, 1, CapsuleShape);
lua_pushnumber(L, lovrCapsuleShapeGetLength(capsule));
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrCapsuleShapeSetLength(lua_State* L) {
2017-05-16 21:52:41 +00:00
CapsuleShape* capsule = luax_checktype(L, 1, CapsuleShape);
float length = luax_checkfloat(L, 2);
2017-05-16 21:52:41 +00:00
lovrCapsuleShapeSetLength(capsule, length);
return 0;
}
const luaL_Reg lovrCapsuleShape[] = {
lovrShape,
2017-05-16 21:52:41 +00:00
{ "getRadius", l_lovrCapsuleShapeGetRadius },
{ "setRadius", l_lovrCapsuleShapeSetRadius },
{ "getLength", l_lovrCapsuleShapeGetLength },
{ "setLength", l_lovrCapsuleShapeSetLength },
{ NULL, NULL }
};
2017-05-16 21:56:20 +00:00
2019-02-17 22:52:22 +00:00
static int l_lovrCylinderShapeGetRadius(lua_State* L) {
2017-05-16 21:56:20 +00:00
CylinderShape* cylinder = luax_checktype(L, 1, CylinderShape);
lua_pushnumber(L, lovrCylinderShapeGetRadius(cylinder));
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrCylinderShapeSetRadius(lua_State* L) {
2017-05-16 21:56:20 +00:00
CylinderShape* cylinder = luax_checktype(L, 1, CylinderShape);
float radius = luax_checkfloat(L, 2);
2017-05-16 21:56:20 +00:00
lovrCylinderShapeSetRadius(cylinder, radius);
return 0;
}
2019-02-17 22:52:22 +00:00
static int l_lovrCylinderShapeGetLength(lua_State* L) {
2017-05-16 21:56:20 +00:00
CylinderShape* cylinder = luax_checktype(L, 1, CylinderShape);
lua_pushnumber(L, lovrCylinderShapeGetLength(cylinder));
return 1;
}
2019-02-17 22:52:22 +00:00
static int l_lovrCylinderShapeSetLength(lua_State* L) {
2017-05-16 21:56:20 +00:00
CylinderShape* cylinder = luax_checktype(L, 1, CylinderShape);
float length = luax_checkfloat(L, 2);
2017-05-16 21:56:20 +00:00
lovrCylinderShapeSetLength(cylinder, length);
return 0;
}
const luaL_Reg lovrCylinderShape[] = {
lovrShape,
2017-05-16 21:56:20 +00:00
{ "getRadius", l_lovrCylinderShapeGetRadius },
{ "setRadius", l_lovrCylinderShapeSetRadius },
{ "getLength", l_lovrCylinderShapeGetLength },
{ "setLength", l_lovrCylinderShapeSetLength },
{ NULL, NULL }
};