2017-05-16 04:59:53 +00:00
|
|
|
#include "api/lovr.h"
|
|
|
|
#include "physics/physics.h"
|
|
|
|
|
2017-05-16 18:17:01 +00:00
|
|
|
map_int_t ShapeTypes;
|
2017-05-25 00:38:03 +00:00
|
|
|
map_int_t JointTypes;
|
2017-05-16 18:17:01 +00:00
|
|
|
|
2017-05-16 04:59:53 +00:00
|
|
|
int l_lovrPhysicsInit(lua_State* L) {
|
|
|
|
lua_newtable(L);
|
|
|
|
luaL_register(L, NULL, lovrPhysics);
|
2017-05-16 05:02:08 +00:00
|
|
|
luax_registertype(L, "World", lovrWorld);
|
2017-05-20 02:11:58 +00:00
|
|
|
luax_registertype(L, "Collider", lovrCollider);
|
2017-05-25 00:38:03 +00:00
|
|
|
luax_extendtype(L, "Joint", "BallJoint", lovrJoint, lovrBallJoint);
|
2017-05-16 21:37:05 +00:00
|
|
|
luax_extendtype(L, "Shape", "SphereShape", lovrShape, lovrSphereShape);
|
|
|
|
luax_extendtype(L, "Shape", "BoxShape", lovrShape, lovrBoxShape);
|
2017-05-16 21:52:41 +00:00
|
|
|
luax_extendtype(L, "Shape", "CapsuleShape", lovrShape, lovrCapsuleShape);
|
2017-05-16 21:56:20 +00:00
|
|
|
luax_extendtype(L, "Shape", "CylinderShape", lovrShape, lovrCylinderShape);
|
2017-05-16 18:17:01 +00:00
|
|
|
|
2017-05-25 00:38:03 +00:00
|
|
|
map_init(&JointTypes);
|
2017-05-25 06:51:27 +00:00
|
|
|
map_set(&JointTypes, "ball", JOINT_BALL);
|
|
|
|
map_set(&JointTypes, "hinge", JOINT_HINGE);
|
2017-05-25 00:38:03 +00:00
|
|
|
|
2017-05-16 18:17:01 +00:00
|
|
|
map_init(&ShapeTypes);
|
|
|
|
map_set(&ShapeTypes, "sphere", SHAPE_SPHERE);
|
|
|
|
map_set(&ShapeTypes, "box", SHAPE_BOX);
|
|
|
|
map_set(&ShapeTypes, "capsule", SHAPE_CAPSULE);
|
|
|
|
map_set(&ShapeTypes, "cylinder", SHAPE_CYLINDER);
|
|
|
|
|
2017-05-16 04:59:53 +00:00
|
|
|
lovrPhysicsInit();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-05-16 05:02:08 +00:00
|
|
|
int l_lovrPhysicsNewWorld(lua_State* L) {
|
2017-05-25 00:38:03 +00:00
|
|
|
World* world = lovrWorldCreate();
|
|
|
|
luax_pushtype(L, World, world);
|
2017-05-16 05:02:08 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-05-25 00:38:03 +00:00
|
|
|
int l_lovrPhysicsNewBallJoint(lua_State* L) {
|
|
|
|
Collider* a = luax_checktype(L, 1, Collider);
|
|
|
|
Collider* b = luax_checktype(L, 2, Collider);
|
|
|
|
float x = luaL_optnumber(L, 3, 0.f);
|
|
|
|
float y = luaL_optnumber(L, 4, 0.f);
|
|
|
|
float z = luaL_optnumber(L, 5, 0.f);
|
|
|
|
Joint* joint = lovrBallJointCreate(a, b, x, y, z);
|
|
|
|
luax_pushtype(L, BallJoint, joint);
|
2017-05-16 21:21:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-05-16 21:37:05 +00:00
|
|
|
int l_lovrPhysicsNewBoxShape(lua_State* L) {
|
|
|
|
float x = luaL_optnumber(L, 1, 1.f);
|
|
|
|
float y = luaL_optnumber(L, 2, x);
|
|
|
|
float z = luaL_optnumber(L, 3, x);
|
2017-05-25 00:38:03 +00:00
|
|
|
BoxShape* box = lovrBoxShapeCreate(x, y, z);
|
|
|
|
luax_pushtype(L, BoxShape, box);
|
2017-05-16 21:37:05 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-05-16 21:52:41 +00:00
|
|
|
int l_lovrPhysicsNewCapsuleShape(lua_State* L) {
|
|
|
|
float radius = luaL_optnumber(L, 1, 1.f);
|
|
|
|
float length = luaL_optnumber(L, 2, 1.f);
|
2017-05-25 00:38:03 +00:00
|
|
|
CapsuleShape* capsule = lovrCapsuleShapeCreate(radius, length);
|
|
|
|
luax_pushtype(L, CapsuleShape, capsule);
|
2017-05-16 21:52:41 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-05-16 21:56:20 +00:00
|
|
|
int l_lovrPhysicsNewCylinderShape(lua_State* L) {
|
|
|
|
float radius = luaL_optnumber(L, 1, 1.f);
|
|
|
|
float length = luaL_optnumber(L, 2, 1.f);
|
2017-05-25 00:38:03 +00:00
|
|
|
CylinderShape* cylinder = lovrCylinderShapeCreate(radius, length);
|
|
|
|
luax_pushtype(L, CylinderShape, cylinder);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-05-25 06:51:27 +00:00
|
|
|
int l_lovrPhysicsNewHingeJoint(lua_State* L) {
|
|
|
|
Collider* a = luax_checktype(L, 1, Collider);
|
|
|
|
Collider* b = luax_checktype(L, 2, Collider);
|
|
|
|
float x = luaL_optnumber(L, 3, 0.f);
|
|
|
|
float y = luaL_optnumber(L, 4, 0.f);
|
|
|
|
float z = luaL_optnumber(L, 5, 0.f);
|
|
|
|
float ax = luaL_optnumber(L, 6, 0.f);
|
|
|
|
float ay = luaL_optnumber(L, 7, 0.f);
|
|
|
|
float az = luaL_optnumber(L, 8, 0.f);
|
|
|
|
Joint* joint = lovrHingeJointCreate(a, b, x, y, z, ax, ay, az);
|
|
|
|
luax_pushtype(L, HingeJoint, joint);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-05-25 00:38:03 +00:00
|
|
|
int l_lovrPhysicsNewSphereShape(lua_State* L) {
|
|
|
|
float radius = luaL_optnumber(L, 1, 1.f);
|
|
|
|
SphereShape* sphere = lovrSphereShapeCreate(radius);
|
|
|
|
luax_pushtype(L, SphereShape, sphere);
|
2017-05-16 21:56:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-05-16 04:59:53 +00:00
|
|
|
const luaL_Reg lovrPhysics[] = {
|
2017-05-16 05:02:08 +00:00
|
|
|
{ "newWorld", l_lovrPhysicsNewWorld },
|
2017-05-25 00:38:03 +00:00
|
|
|
{ "newBallJoint", l_lovrPhysicsNewBallJoint },
|
2017-05-16 21:37:05 +00:00
|
|
|
{ "newBoxShape", l_lovrPhysicsNewBoxShape },
|
2017-05-16 21:52:41 +00:00
|
|
|
{ "newCapsuleShape", l_lovrPhysicsNewCapsuleShape },
|
2017-05-16 21:56:20 +00:00
|
|
|
{ "newCylinderShape", l_lovrPhysicsNewCylinderShape },
|
2017-05-25 06:51:27 +00:00
|
|
|
{ "newHingeJoint", l_lovrPhysicsNewHingeJoint },
|
2017-05-25 00:38:03 +00:00
|
|
|
{ "newSphereShape", l_lovrPhysicsNewSphereShape },
|
2017-05-16 04:59:53 +00:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|