Physics world accepts both vec3 and number coords

When creating colliders, setting gravity or casting rays on world,
arguments accept either coordinate numbers as before, or vec3 objects.
For functions that receive more than one set of coordinates, any
combination of coordinates and vectors is accepted.
This commit is contained in:
Josip Miskovic 2020-05-01 10:22:14 +03:00
parent b035f8e4dc
commit 2463737722
1 changed files with 32 additions and 42 deletions

View File

@ -40,10 +40,9 @@ static void raycastCallback(Shape* shape, float x, float y, float z, float nx, f
static int l_lovrWorldNewCollider(lua_State* L) { static int l_lovrWorldNewCollider(lua_State* L) {
World* world = luax_checktype(L, 1, World); World* world = luax_checktype(L, 1, World);
float x = luax_optfloat(L, 2, 0.f); float position[4];
float y = luax_optfloat(L, 3, 0.f); luax_readvec3(L, 2, position, NULL);
float z = luax_optfloat(L, 4, 0.f); Collider* collider = lovrColliderCreate(world, position[0], position[1], position[2]);
Collider* collider = lovrColliderCreate(world, x, y, z);
luax_pushtype(L, Collider, collider); luax_pushtype(L, Collider, collider);
lovrRelease(Collider, collider); lovrRelease(Collider, collider);
return 1; return 1;
@ -51,14 +50,11 @@ static int l_lovrWorldNewCollider(lua_State* L) {
static int l_lovrWorldNewBoxCollider(lua_State* L) { static int l_lovrWorldNewBoxCollider(lua_State* L) {
World* world = luax_checktype(L, 1, World); World* world = luax_checktype(L, 1, World);
float x = luax_optfloat(L, 2, 0.f); float position[4], size[4];
float y = luax_optfloat(L, 3, 0.f); int index = luax_readvec3(L, 2, position, NULL);
float z = luax_optfloat(L, 4, 0.f); luax_readscale(L, index, size, 3, NULL);
float sx = luax_optfloat(L, 5, 1.f); Collider* collider = lovrColliderCreate(world, position[0], position[1], position[2]);
float sy = luax_optfloat(L, 6, sx); BoxShape* shape = lovrBoxShapeCreate(size[0], size[1], size[2]);
float sz = luax_optfloat(L, 7, sx);
Collider* collider = lovrColliderCreate(world, x, y, z);
BoxShape* shape = lovrBoxShapeCreate(sx, sy, sz);
lovrColliderAddShape(collider, shape); lovrColliderAddShape(collider, shape);
lovrColliderInitInertia(collider, shape); lovrColliderInitInertia(collider, shape);
luax_pushtype(L, Collider, collider); luax_pushtype(L, Collider, collider);
@ -69,12 +65,11 @@ static int l_lovrWorldNewBoxCollider(lua_State* L) {
static int l_lovrWorldNewCapsuleCollider(lua_State* L) { static int l_lovrWorldNewCapsuleCollider(lua_State* L) {
World* world = luax_checktype(L, 1, World); World* world = luax_checktype(L, 1, World);
float x = luax_optfloat(L, 2, 0.f); float position[4];
float y = luax_optfloat(L, 3, 0.f); int index = luax_readvec3(L, 2, position, NULL);
float z = luax_optfloat(L, 4, 0.f); float radius = luax_optfloat(L, index++, 1.f);
float radius = luax_optfloat(L, 5, 1.f); float length = luax_optfloat(L, index, 1.f);
float length = luax_optfloat(L, 6, 1.f); Collider* collider = lovrColliderCreate(world, position[0], position[1], position[2]);
Collider* collider = lovrColliderCreate(world, x, y, z);
CapsuleShape* shape = lovrCapsuleShapeCreate(radius, length); CapsuleShape* shape = lovrCapsuleShapeCreate(radius, length);
lovrColliderAddShape(collider, shape); lovrColliderAddShape(collider, shape);
lovrColliderInitInertia(collider, shape); lovrColliderInitInertia(collider, shape);
@ -86,12 +81,11 @@ static int l_lovrWorldNewCapsuleCollider(lua_State* L) {
static int l_lovrWorldNewCylinderCollider(lua_State* L) { static int l_lovrWorldNewCylinderCollider(lua_State* L) {
World* world = luax_checktype(L, 1, World); World* world = luax_checktype(L, 1, World);
float x = luax_optfloat(L, 2, 0.f); float position[4];
float y = luax_optfloat(L, 3, 0.f); int index = luax_readvec3(L, 2, position, NULL);
float z = luax_optfloat(L, 4, 0.f); float radius = luax_optfloat(L, index++, 1.f);
float radius = luax_optfloat(L, 5, 1.f); float length = luax_optfloat(L, index, 1.f);
float length = luax_optfloat(L, 6, 1.f); Collider* collider = lovrColliderCreate(world, position[0], position[1], position[2]);
Collider* collider = lovrColliderCreate(world, x, y, z);
CylinderShape* shape = lovrCylinderShapeCreate(radius, length); CylinderShape* shape = lovrCylinderShapeCreate(radius, length);
lovrColliderAddShape(collider, shape); lovrColliderAddShape(collider, shape);
lovrColliderInitInertia(collider, shape); lovrColliderInitInertia(collider, shape);
@ -103,11 +97,10 @@ static int l_lovrWorldNewCylinderCollider(lua_State* L) {
static int l_lovrWorldNewSphereCollider(lua_State* L) { static int l_lovrWorldNewSphereCollider(lua_State* L) {
World* world = luax_checktype(L, 1, World); World* world = luax_checktype(L, 1, World);
float x = luax_optfloat(L, 2, 0.f); float position[4];
float y = luax_optfloat(L, 3, 0.f); int index = luax_readvec3(L, 2, position, NULL);
float z = luax_optfloat(L, 4, 0.f); float radius = luax_optfloat(L, index, 1.f);
float radius = luax_optfloat(L, 5, 1.f); Collider* collider = lovrColliderCreate(world, position[0], position[1], position[2]);
Collider* collider = lovrColliderCreate(world, x, y, z);
SphereShape* shape = lovrSphereShapeCreate(radius); SphereShape* shape = lovrSphereShapeCreate(radius);
lovrColliderAddShape(collider, shape); lovrColliderAddShape(collider, shape);
lovrColliderInitInertia(collider, shape); lovrColliderInitInertia(collider, shape);
@ -167,10 +160,9 @@ static int l_lovrWorldGetGravity(lua_State* L) {
static int l_lovrWorldSetGravity(lua_State* L) { static int l_lovrWorldSetGravity(lua_State* L) {
World* world = luax_checktype(L, 1, World); World* world = luax_checktype(L, 1, World);
float x = luax_checkfloat(L, 2); float gravity[4];
float y = luax_checkfloat(L, 3); luax_readvec3(L, 2, gravity, NULL);
float z = luax_checkfloat(L, 4); lovrWorldSetGravity(world, gravity[0], gravity[1], gravity[2]);
lovrWorldSetGravity(world, x, y, z);
return 0; return 0;
} }
@ -223,15 +215,13 @@ static int l_lovrWorldSetSleepingAllowed(lua_State* L) {
static int l_lovrWorldRaycast(lua_State* L) { static int l_lovrWorldRaycast(lua_State* L) {
World* world = luax_checktype(L, 1, World); World* world = luax_checktype(L, 1, World);
float x1 = luax_checkfloat(L, 2); float start[4], end[4];
float y1 = luax_checkfloat(L, 3); int index;
float z1 = luax_checkfloat(L, 4); index = luax_readvec3(L, 2, start, NULL);
float x2 = luax_checkfloat(L, 5); index = luax_readvec3(L, index, end, NULL);
float y2 = luax_checkfloat(L, 6); luaL_checktype(L, index, LUA_TFUNCTION);
float z2 = luax_checkfloat(L, 7); lua_settop(L, index);
luaL_checktype(L, 8, LUA_TFUNCTION); lovrWorldRaycast(world, start[0], start[1], start[2], end[0], end[1], end[2], raycastCallback, L);
lua_settop(L, 8);
lovrWorldRaycast(world, x1, y1, z1, x2, y2, z2, raycastCallback, L);
return 0; return 0;
} }