2017-12-10 20:40:37 +00:00
|
|
|
#include "api.h"
|
2017-05-16 04:59:53 +00:00
|
|
|
#include "physics/physics.h"
|
2019-06-02 07:20:10 +00:00
|
|
|
#include "core/ref.h"
|
2017-05-16 04:59:53 +00:00
|
|
|
|
2018-07-05 03:11:52 +00:00
|
|
|
const char* ShapeTypes[] = {
|
|
|
|
[SHAPE_SPHERE] = "sphere",
|
|
|
|
[SHAPE_BOX] = "box",
|
|
|
|
[SHAPE_CAPSULE] = "capsule",
|
|
|
|
[SHAPE_CYLINDER] = "cylinder",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
const char* JointTypes[] = {
|
|
|
|
[JOINT_BALL] = "ball",
|
|
|
|
[JOINT_DISTANCE] = "distance",
|
|
|
|
[JOINT_HINGE] = "hinge",
|
|
|
|
[JOINT_SLIDER] = "slider",
|
|
|
|
NULL
|
|
|
|
};
|
2017-05-16 18:17:01 +00:00
|
|
|
|
2018-09-27 01:27:38 +00:00
|
|
|
static int l_lovrPhysicsNewWorld(lua_State* L) {
|
2019-01-29 10:28:55 +00:00
|
|
|
float xg = luax_optfloat(L, 1, 0.f);
|
|
|
|
float yg = luax_optfloat(L, 2, -9.81f);
|
|
|
|
float zg = luax_optfloat(L, 3, 0.f);
|
2017-10-31 08:14:09 +00:00
|
|
|
bool allowSleep = lua_gettop(L) < 4 || lua_toboolean(L, 4);
|
2017-05-25 22:01:40 +00:00
|
|
|
const char* tags[16];
|
|
|
|
int tagCount;
|
|
|
|
if (lua_type(L, 5) == LUA_TTABLE) {
|
2019-03-17 07:58:01 +00:00
|
|
|
tagCount = luax_len(L, 5);
|
2017-05-25 22:01:40 +00:00
|
|
|
for (int i = 0; i < tagCount; i++) {
|
|
|
|
lua_rawgeti(L, -1, i + 1);
|
|
|
|
if (lua_isstring(L, -1)) {
|
|
|
|
tags[i] = lua_tostring(L, -1);
|
|
|
|
} else {
|
|
|
|
return luaL_error(L, "World tags must be a table of strings");
|
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tagCount = 0;
|
|
|
|
}
|
|
|
|
World* world = lovrWorldCreate(xg, yg, zg, allowSleep, tags, tagCount);
|
2019-06-02 07:20:10 +00:00
|
|
|
luax_pushtype(L, World, world);
|
2019-04-05 10:41:03 +00:00
|
|
|
lovrRelease(World, world);
|
2017-05-16 05:02:08 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-27 01:27:38 +00:00
|
|
|
static int l_lovrPhysicsNewBallJoint(lua_State* L) {
|
2017-05-25 00:38:03 +00:00
|
|
|
Collider* a = luax_checktype(L, 1, Collider);
|
|
|
|
Collider* b = luax_checktype(L, 2, Collider);
|
2019-01-29 10:28:55 +00:00
|
|
|
float x = luax_checkfloat(L, 3);
|
|
|
|
float y = luax_checkfloat(L, 4);
|
|
|
|
float z = luax_checkfloat(L, 5);
|
2017-05-25 22:20:55 +00:00
|
|
|
BallJoint* joint = lovrBallJointCreate(a, b, x, y, z);
|
2019-06-02 07:20:10 +00:00
|
|
|
luax_pushtype(L, BallJoint, joint);
|
2019-04-05 10:41:03 +00:00
|
|
|
lovrRelease(Joint, joint);
|
2017-05-16 21:21:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-27 01:27:38 +00:00
|
|
|
static int l_lovrPhysicsNewBoxShape(lua_State* L) {
|
2019-01-29 10:28:55 +00:00
|
|
|
float x = luax_optfloat(L, 1, 1.f);
|
|
|
|
float y = luax_optfloat(L, 2, x);
|
|
|
|
float z = luax_optfloat(L, 3, x);
|
2017-05-25 00:38:03 +00:00
|
|
|
BoxShape* box = lovrBoxShapeCreate(x, y, z);
|
2019-06-02 07:20:10 +00:00
|
|
|
luax_pushtype(L, BoxShape, box);
|
2019-04-05 10:41:03 +00:00
|
|
|
lovrRelease(Shape, box);
|
2017-05-16 21:37:05 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-27 01:27:38 +00:00
|
|
|
static int l_lovrPhysicsNewCapsuleShape(lua_State* L) {
|
2019-01-29 10:28:55 +00:00
|
|
|
float radius = luax_optfloat(L, 1, 1.f);
|
|
|
|
float length = luax_optfloat(L, 2, 1.f);
|
2017-05-25 00:38:03 +00:00
|
|
|
CapsuleShape* capsule = lovrCapsuleShapeCreate(radius, length);
|
2019-06-02 07:20:10 +00:00
|
|
|
luax_pushtype(L, CapsuleShape, capsule);
|
2019-04-05 10:41:03 +00:00
|
|
|
lovrRelease(Shape, capsule);
|
2017-05-16 21:52:41 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-27 01:27:38 +00:00
|
|
|
static int l_lovrPhysicsNewCylinderShape(lua_State* L) {
|
2019-01-29 10:28:55 +00:00
|
|
|
float radius = luax_optfloat(L, 1, 1.f);
|
|
|
|
float length = luax_optfloat(L, 2, 1.f);
|
2017-05-25 00:38:03 +00:00
|
|
|
CylinderShape* cylinder = lovrCylinderShapeCreate(radius, length);
|
2019-06-02 07:20:10 +00:00
|
|
|
luax_pushtype(L, CylinderShape, cylinder);
|
2019-04-05 10:41:03 +00:00
|
|
|
lovrRelease(Shape, cylinder);
|
2017-05-25 00:38:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-27 01:27:38 +00:00
|
|
|
static int l_lovrPhysicsNewDistanceJoint(lua_State* L) {
|
2017-06-10 22:13:19 +00:00
|
|
|
Collider* a = luax_checktype(L, 1, Collider);
|
|
|
|
Collider* b = luax_checktype(L, 2, Collider);
|
2019-01-29 10:28:55 +00:00
|
|
|
float x1 = luax_checkfloat(L, 3);
|
|
|
|
float y1 = luax_checkfloat(L, 4);
|
|
|
|
float z1 = luax_checkfloat(L, 5);
|
|
|
|
float x2 = luax_checkfloat(L, 6);
|
|
|
|
float y2 = luax_checkfloat(L, 7);
|
|
|
|
float z2 = luax_checkfloat(L, 8);
|
2017-06-10 22:13:19 +00:00
|
|
|
DistanceJoint* joint = lovrDistanceJointCreate(a, b, x1, y1, z1, x2, y2, z2);
|
2019-06-02 07:20:10 +00:00
|
|
|
luax_pushtype(L, DistanceJoint, joint);
|
2019-04-05 10:41:03 +00:00
|
|
|
lovrRelease(Joint, joint);
|
2017-06-10 22:13:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-27 01:27:38 +00:00
|
|
|
static int l_lovrPhysicsNewHingeJoint(lua_State* L) {
|
2017-05-25 06:51:27 +00:00
|
|
|
Collider* a = luax_checktype(L, 1, Collider);
|
|
|
|
Collider* b = luax_checktype(L, 2, Collider);
|
2019-01-29 10:28:55 +00:00
|
|
|
float x = luax_checkfloat(L, 3);
|
|
|
|
float y = luax_checkfloat(L, 4);
|
|
|
|
float z = luax_checkfloat(L, 5);
|
|
|
|
float ax = luax_checkfloat(L, 6);
|
|
|
|
float ay = luax_checkfloat(L, 7);
|
|
|
|
float az = luax_checkfloat(L, 8);
|
2017-05-25 22:20:55 +00:00
|
|
|
HingeJoint* joint = lovrHingeJointCreate(a, b, x, y, z, ax, ay, az);
|
2019-06-02 07:20:10 +00:00
|
|
|
luax_pushtype(L, HingeJoint, joint);
|
2019-04-05 10:41:03 +00:00
|
|
|
lovrRelease(Joint, joint);
|
2017-05-25 06:51:27 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-27 01:27:38 +00:00
|
|
|
static int l_lovrPhysicsNewSliderJoint(lua_State* L) {
|
2017-05-25 07:48:02 +00:00
|
|
|
Collider* a = luax_checktype(L, 1, Collider);
|
|
|
|
Collider* b = luax_checktype(L, 2, Collider);
|
2019-01-29 10:28:55 +00:00
|
|
|
float ax = luax_checkfloat(L, 3);
|
|
|
|
float ay = luax_checkfloat(L, 4);
|
|
|
|
float az = luax_checkfloat(L, 5);
|
2017-05-25 22:20:55 +00:00
|
|
|
SliderJoint* joint = lovrSliderJointCreate(a, b, ax, ay, az);
|
2019-06-02 07:20:10 +00:00
|
|
|
luax_pushtype(L, SliderJoint, joint);
|
2019-04-05 10:41:03 +00:00
|
|
|
lovrRelease(Joint, joint);
|
2017-05-25 07:48:02 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-27 01:27:38 +00:00
|
|
|
static int l_lovrPhysicsNewSphereShape(lua_State* L) {
|
2019-01-29 10:28:55 +00:00
|
|
|
float radius = luax_optfloat(L, 1, 1.f);
|
2017-05-25 00:38:03 +00:00
|
|
|
SphereShape* sphere = lovrSphereShapeCreate(radius);
|
2019-06-02 07:20:10 +00:00
|
|
|
luax_pushtype(L, SphereShape, sphere);
|
2019-04-05 10:41:03 +00:00
|
|
|
lovrRelease(Shape, sphere);
|
2017-05-16 21:56:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-27 01:27:38 +00:00
|
|
|
static 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-06-10 22:13:19 +00:00
|
|
|
{ "newDistanceJoint", l_lovrPhysicsNewDistanceJoint },
|
2017-05-25 06:51:27 +00:00
|
|
|
{ "newHingeJoint", l_lovrPhysicsNewHingeJoint },
|
2017-05-25 07:48:02 +00:00
|
|
|
{ "newSliderJoint", l_lovrPhysicsNewSliderJoint },
|
2017-05-25 00:38:03 +00:00
|
|
|
{ "newSphereShape", l_lovrPhysicsNewSphereShape },
|
2017-05-16 04:59:53 +00:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
2018-09-27 01:27:38 +00:00
|
|
|
|
|
|
|
int luaopen_lovr_physics(lua_State* L) {
|
|
|
|
lua_newtable(L);
|
|
|
|
luaL_register(L, NULL, lovrPhysics);
|
2019-04-05 10:48:36 +00:00
|
|
|
luax_registertype(L, World);
|
|
|
|
luax_registertype(L, Collider);
|
2019-06-02 07:20:10 +00:00
|
|
|
luax_registertype(L, BallJoint);
|
|
|
|
luax_registertype(L, DistanceJoint);
|
|
|
|
luax_registertype(L, HingeJoint);
|
|
|
|
luax_registertype(L, SliderJoint);
|
|
|
|
luax_registertype(L, SphereShape);
|
|
|
|
luax_registertype(L, BoxShape);
|
|
|
|
luax_registertype(L, CapsuleShape);
|
|
|
|
luax_registertype(L, CylinderShape);
|
2018-11-19 16:08:56 +00:00
|
|
|
if (lovrPhysicsInit()) {
|
|
|
|
luax_atexit(L, lovrPhysicsDestroy);
|
|
|
|
}
|
2018-09-27 01:27:38 +00:00
|
|
|
return 1;
|
|
|
|
}
|