diff --git a/src/api/l_physics.c b/src/api/l_physics.c index 206bcf90..12b0bf82 100644 --- a/src/api/l_physics.c +++ b/src/api/l_physics.c @@ -26,8 +26,7 @@ static int l_lovrPhysicsNewWorld(lua_State* L) { .maxColliders = 65536, .maxColliderPairs = 65536, .maxContacts = 16384, - .allowSleep = true, - .gravity = { 0.f, -9.81f, 0.f } + .allowSleep = true }; if (lua_istable(L, 1)) { @@ -64,9 +63,6 @@ static int l_lovrPhysicsNewWorld(lua_State* L) { } lua_pop(L, 1); } else { // Deprecated - info.gravity[0] = luax_optfloat(L, 1, 0.f); - info.gravity[1] = luax_optfloat(L, 2, -9.81f); - info.gravity[2] = luax_optfloat(L, 3, 0.f); info.allowSleep = lua_gettop(L) < 4 || lua_toboolean(L, 4); if (lua_type(L, 5) == LUA_TTABLE) { info.tagCount = luax_len(L, 5); @@ -86,6 +82,15 @@ static int l_lovrPhysicsNewWorld(lua_State* L) { } World* world = lovrWorldCreate(&info); + + if (!lua_istable(L, 1)) { + float gravity[3]; + gravity[0] = luax_optfloat(L, 1, 0.f); + gravity[1] = luax_optfloat(L, 2, -9.81f); + gravity[2] = luax_optfloat(L, 3, 0.f); + lovrWorldSetGravity(world, gravity); + } + luax_pushtype(L, World, world); lovrRelease(world, lovrWorldDestroy); return 1; diff --git a/src/api/l_physics_collider.c b/src/api/l_physics_collider.c index 081ce4da..6e9eeff8 100644 --- a/src/api/l_physics_collider.c +++ b/src/api/l_physics_collider.c @@ -225,12 +225,11 @@ static int l_lovrColliderSetMass(lua_State* L) { static int l_lovrColliderGetMassData(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); - float cx, cy, cz, mass; - float inertia[6]; - lovrColliderGetMassData(collider, &cx, &cy, &cz, &mass, inertia); - lua_pushnumber(L, cx); - lua_pushnumber(L, cy); - lua_pushnumber(L, cz); + float centerOfMass[3], mass, inertia[6]; + lovrColliderGetMassData(collider, centerOfMass, &mass, inertia); + lua_pushnumber(L, centerOfMass[0]); + lua_pushnumber(L, centerOfMass[1]); + lua_pushnumber(L, centerOfMass[2]); lua_pushnumber(L, mass); lua_newtable(L); for (int i = 0; i < 6; i++) { @@ -242,14 +241,13 @@ static int l_lovrColliderGetMassData(lua_State* L) { static int l_lovrColliderSetMassData(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); - float cx = luax_checkfloat(L, 2); - float cy = luax_checkfloat(L, 3); - float cz = luax_checkfloat(L, 4); - float mass = luax_checkfloat(L, 5); + float centerOfMass[3]; + int index = luax_readvec3(L, 2, centerOfMass, NULL); + float mass = luax_checkfloat(L, index++); float inertia[6]; - if (lua_istable(L, 6) && luax_len(L, 6) >= 6) { + if (lua_istable(L, index) && luax_len(L, index) >= 6) { for (int i = 0; i < 6; i++) { - lua_rawgeti(L, 6, i + 1); + lua_rawgeti(L, index, i + 1); if (!lua_isnumber(L, -1)) { return luaL_argerror(L, 6, "Expected 6 numbers or a table with 6 numbers"); } @@ -258,25 +256,25 @@ static int l_lovrColliderSetMassData(lua_State* L) { lua_pop(L, 1); } } else { - for (int i = 6; i < 12; i++) { + for (int i = index; i < index + 6; i++) { if (lua_isnumber(L, i)) { - inertia[i - 6] = lua_tonumber(L, i); + inertia[i - index] = lua_tonumber(L, i); } else { return luaL_argerror(L, i, "Expected 6 numbers or a table with 6 numbers"); } } } - lovrColliderSetMassData(collider, cx, cy, cz, mass, inertia); + lovrColliderSetMassData(collider, centerOfMass, mass, inertia); return 0; } static int l_lovrColliderGetPosition(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); - float x, y, z; - lovrColliderGetPosition(collider, &x, &y, &z); - lua_pushnumber(L, x); - lua_pushnumber(L, y); - lua_pushnumber(L, z); + float position[3]; + lovrColliderGetPosition(collider, position); + lua_pushnumber(L, position[0]); + lua_pushnumber(L, position[1]); + lua_pushnumber(L, position[2]); return 3; } @@ -284,13 +282,13 @@ static int l_lovrColliderSetPosition(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); float position[3]; luax_readvec3(L, 2, position, NULL); - lovrColliderSetPosition(collider, position[0], position[1], position[2]); + lovrColliderSetPosition(collider, position); return 0; } static int l_lovrColliderGetOrientation(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); - float angle, x, y, z, orientation[4]; + float orientation[4], angle, x, y, z; lovrColliderGetOrientation(collider, orientation); quat_getAngleAxis(orientation, &angle, &x, &y, &z); lua_pushnumber(L, angle); @@ -310,13 +308,13 @@ static int l_lovrColliderSetOrientation(lua_State* L) { static int l_lovrColliderGetPose(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); - float x, y, z, angle, ax, ay, az, orientation[4]; - lovrColliderGetPosition(collider, &x, &y, &z); + float position[3], orientation[4], angle, ax, ay, az; + lovrColliderGetPosition(collider, position); lovrColliderGetOrientation(collider, orientation); quat_getAngleAxis(orientation, &angle, &ax, &ay, &az); - lua_pushnumber(L, x); - lua_pushnumber(L, y); - lua_pushnumber(L, z); + lua_pushnumber(L, position[0]); + lua_pushnumber(L, position[1]); + lua_pushnumber(L, position[2]); lua_pushnumber(L, angle); lua_pushnumber(L, ax); lua_pushnumber(L, ay); @@ -329,18 +327,18 @@ static int l_lovrColliderSetPose(lua_State* L) { float position[3], orientation[4]; int index = luax_readvec3(L, 2, position, NULL); luax_readquat(L, index, orientation, NULL); - lovrColliderSetPosition(collider, position[0], position[1], position[2]); + lovrColliderSetPosition(collider, position); lovrColliderSetOrientation(collider, orientation); return 0; } static int l_lovrColliderGetLinearVelocity(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); - float x, y, z; - lovrColliderGetLinearVelocity(collider, &x, &y, &z); - lua_pushnumber(L, x); - lua_pushnumber(L, y); - lua_pushnumber(L, z); + float velocity[3]; + lovrColliderGetLinearVelocity(collider, velocity); + lua_pushnumber(L, velocity[0]); + lua_pushnumber(L, velocity[1]); + lua_pushnumber(L, velocity[2]); return 3; } @@ -348,17 +346,17 @@ static int l_lovrColliderSetLinearVelocity(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); float velocity[3]; luax_readvec3(L, 2, velocity, NULL); - lovrColliderSetLinearVelocity(collider, velocity[0], velocity[1], velocity[2]); + lovrColliderSetLinearVelocity(collider, velocity); return 0; } static int l_lovrColliderGetAngularVelocity(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); - float x, y, z; - lovrColliderGetAngularVelocity(collider, &x, &y, &z); - lua_pushnumber(L, x); - lua_pushnumber(L, y); - lua_pushnumber(L, z); + float velocity[3]; + lovrColliderGetAngularVelocity(collider, velocity); + lua_pushnumber(L, velocity[0]); + lua_pushnumber(L, velocity[1]); + lua_pushnumber(L, velocity[2]); return 3; } @@ -366,7 +364,7 @@ static int l_lovrColliderSetAngularVelocity(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); float velocity[3]; luax_readvec3(L, 2, velocity, NULL); - lovrColliderSetAngularVelocity(collider, velocity[0], velocity[1], velocity[2]); + lovrColliderSetAngularVelocity(collider, velocity); return 0; } @@ -412,10 +410,9 @@ static int l_lovrColliderApplyForce(lua_State* L) { if (lua_gettop(L) >= index) { float position[3]; luax_readvec3(L, index, position, NULL); - lovrColliderApplyForceAtPosition(collider, force[0], force[1], force[2], - position[0], position[1], position[2]); + lovrColliderApplyForceAtPosition(collider, force, position); } else { - lovrColliderApplyForce(collider, force[0], force[1], force[2]); + lovrColliderApplyForce(collider, force); } return 0; @@ -423,9 +420,9 @@ static int l_lovrColliderApplyForce(lua_State* L) { static int l_lovrColliderApplyTorque(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); - float force[3]; - luax_readvec3(L, 2, force, NULL); - lovrColliderApplyTorque(collider, force[0], force[1], force[2]); + float torque[3]; + luax_readvec3(L, 2, torque, NULL); + lovrColliderApplyTorque(collider, torque); return 0; } @@ -453,83 +450,77 @@ static int l_lovrColliderApplyAngularImpulse(lua_State* L) { static int l_lovrColliderGetLocalCenter(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); - float x, y, z; - lovrColliderGetLocalCenter(collider, &x, &y, &z); - lua_pushnumber(L, x); - lua_pushnumber(L, y); - lua_pushnumber(L, z); + float center[3]; + lovrColliderGetLocalCenter(collider, center); + lua_pushnumber(L, center[0]); + lua_pushnumber(L, center[1]); + lua_pushnumber(L, center[2]); return 3; } static int l_lovrColliderGetLocalPoint(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); - float world[3]; + float world[3], local[3]; luax_readvec3(L, 2, world, NULL); - float x, y, z; - lovrColliderGetLocalPoint(collider, world[0], world[1], world[2], &x, &y, &z); - lua_pushnumber(L, x); - lua_pushnumber(L, y); - lua_pushnumber(L, z); + lovrColliderGetLocalPoint(collider, world, local); + lua_pushnumber(L, local[0]); + lua_pushnumber(L, local[1]); + lua_pushnumber(L, local[2]); return 3; } static int l_lovrColliderGetWorldPoint(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); - float local[3]; + float local[3], world[3]; luax_readvec3(L, 2, local, NULL); - float wx, wy, wz; - lovrColliderGetWorldPoint(collider, local[0], local[1], local[2], &wx, &wy, &wz); - lua_pushnumber(L, wx); - lua_pushnumber(L, wy); - lua_pushnumber(L, wz); + lovrColliderGetWorldPoint(collider, local, world); + lua_pushnumber(L, world[0]); + lua_pushnumber(L, world[1]); + lua_pushnumber(L, world[2]); return 3; } static int l_lovrColliderGetLocalVector(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); - float world[3]; + float world[3], local[3]; luax_readvec3(L, 2, world, NULL); - float x, y, z; - lovrColliderGetLocalVector(collider, world[0], world[1], world[2], &x, &y, &z); - lua_pushnumber(L, x); - lua_pushnumber(L, y); - lua_pushnumber(L, z); + lovrColliderGetLocalVector(collider, world, local); + lua_pushnumber(L, local[0]); + lua_pushnumber(L, local[1]); + lua_pushnumber(L, local[2]); return 3; } static int l_lovrColliderGetWorldVector(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); - float local[3]; + float local[3], world[3]; luax_readvec3(L, 2, local, NULL); - float wx, wy, wz; - lovrColliderGetWorldVector(collider, local[0], local[1], local[2], &wx, &wy, &wz); - lua_pushnumber(L, wx); - lua_pushnumber(L, wy); - lua_pushnumber(L, wz); + lovrColliderGetWorldVector(collider, local, world); + lua_pushnumber(L, world[0]); + lua_pushnumber(L, world[1]); + lua_pushnumber(L, world[2]); return 3; } static int l_lovrColliderGetLinearVelocityFromLocalPoint(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); - float local[3]; - luax_readvec3(L, 2, local, NULL); - float vx, vy, vz; - lovrColliderGetLinearVelocityFromLocalPoint(collider, local[0], local[1], local[2], &vx, &vy, &vz); - lua_pushnumber(L, vx); - lua_pushnumber(L, vy); - lua_pushnumber(L, vz); + float point[3], velocity[3]; + luax_readvec3(L, 2, point, NULL); + lovrColliderGetLinearVelocityFromLocalPoint(collider, point, velocity); + lua_pushnumber(L, velocity[0]); + lua_pushnumber(L, velocity[1]); + lua_pushnumber(L, velocity[2]); return 3; } static int l_lovrColliderGetLinearVelocityFromWorldPoint(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); - float world[3]; - luax_readvec3(L, 2, world, NULL); - float vx, vy, vz; - lovrColliderGetLinearVelocityFromWorldPoint(collider, world[0], world[1], world[2], &vx, &vy, &vz); - lua_pushnumber(L, vx); - lua_pushnumber(L, vy); - lua_pushnumber(L, vz); + float point[3], velocity[3]; + luax_readvec3(L, 2, point, NULL); + lovrColliderGetLinearVelocityFromWorldPoint(collider, point, velocity); + lua_pushnumber(L, velocity[0]); + lua_pushnumber(L, velocity[1]); + lua_pushnumber(L, velocity[2]); return 3; } diff --git a/src/api/l_physics_shapes.c b/src/api/l_physics_shapes.c index ced3443d..f2638af1 100644 --- a/src/api/l_physics_shapes.c +++ b/src/api/l_physics_shapes.c @@ -52,7 +52,7 @@ Shape* luax_newsphereshape(lua_State* L, int index) { Shape* luax_newboxshape(lua_State* L, int index) { float size[3]; luax_readscale(L, index, size, 3, NULL); - return lovrBoxShapeCreate(size[0], size[1], size[2]); + return lovrBoxShapeCreate(size); } Shape* luax_newcapsuleshape(lua_State* L, int index) { @@ -266,12 +266,11 @@ static int l_lovrShapeSetUserData(lua_State* L) { static int l_lovrShapeGetMass(lua_State* L) { Shape* shape = luax_checkshape(L, 1); float density = luax_checkfloat(L, 2); - float cx, cy, cz, mass; - float inertia[6]; - lovrShapeGetMass(shape, density, &cx, &cy, &cz, &mass, inertia); - lua_pushnumber(L, cx); - lua_pushnumber(L, cy); - lua_pushnumber(L, cz); + float centerOfMass[3], mass, inertia[6]; + lovrShapeGetMass(shape, density, centerOfMass, &mass, inertia); + lua_pushnumber(L, centerOfMass[0]); + lua_pushnumber(L, centerOfMass[1]); + lua_pushnumber(L, centerOfMass[2]); lua_pushnumber(L, mass); lua_newtable(L); for (int i = 0; i < 6; i++) { @@ -320,11 +319,11 @@ const luaL_Reg lovrSphereShape[] = { static int l_lovrBoxShapeGetDimensions(lua_State* L) { BoxShape* box = luax_checktype(L, 1, BoxShape); - float w, h, d; - lovrBoxShapeGetDimensions(box, &w, &h, &d); - lua_pushnumber(L, w); - lua_pushnumber(L, h); - lua_pushnumber(L, d); + float dimensions[3]; + lovrBoxShapeGetDimensions(box, dimensions); + lua_pushnumber(L, dimensions[0]); + lua_pushnumber(L, dimensions[1]); + lua_pushnumber(L, dimensions[2]); return 3; } diff --git a/src/api/l_physics_world.c b/src/api/l_physics_world.c index 6d7a0b86..c8e5ff5c 100644 --- a/src/api/l_physics_world.c +++ b/src/api/l_physics_world.c @@ -105,7 +105,7 @@ static int l_lovrWorldNewCollider(lua_State* L) { Shape* shape = luax_totype(L, 2, Shape); float position[3]; luax_readvec3(L, 2 + !!shape, position, NULL); - Collider* collider = lovrColliderCreate(world, shape, position[0], position[1], position[2]); + Collider* collider = lovrColliderCreate(world, shape, position); luax_pushtype(L, Collider, collider); lovrRelease(collider, lovrColliderDestroy); return 1; @@ -116,7 +116,7 @@ static int l_lovrWorldNewBoxCollider(lua_State* L) { float position[3]; int index = luax_readvec3(L, 2, position, NULL); BoxShape* shape = luax_newboxshape(L, index); - Collider* collider = lovrColliderCreate(world, shape, position[0], position[1], position[2]); + Collider* collider = lovrColliderCreate(world, shape, position); lovrColliderInitInertia(collider, shape); luax_pushtype(L, Collider, collider); lovrRelease(collider, lovrColliderDestroy); @@ -129,7 +129,7 @@ static int l_lovrWorldNewCapsuleCollider(lua_State* L) { float position[3]; int index = luax_readvec3(L, 2, position, NULL); CapsuleShape* shape = luax_newcapsuleshape(L, index); - Collider* collider = lovrColliderCreate(world, shape, position[0], position[1], position[2]); + Collider* collider = lovrColliderCreate(world, shape, position); lovrColliderInitInertia(collider, shape); luax_pushtype(L, Collider, collider); lovrRelease(collider, lovrColliderDestroy); @@ -142,7 +142,7 @@ static int l_lovrWorldNewCylinderCollider(lua_State* L) { float position[3]; int index = luax_readvec3(L, 2, position, NULL); CylinderShape* shape = luax_newcylindershape(L, index); - Collider* collider = lovrColliderCreate(world, shape, position[0], position[1], position[2]); + Collider* collider = lovrColliderCreate(world, shape, position); lovrColliderInitInertia(collider, shape); luax_pushtype(L, Collider, collider); lovrRelease(collider, lovrColliderDestroy); @@ -155,7 +155,7 @@ static int l_lovrWorldNewSphereCollider(lua_State* L) { float position[3]; int index = luax_readvec3(L, 2, position, NULL); SphereShape* shape = luax_newsphereshape(L, index); - Collider* collider = lovrColliderCreate(world, shape, position[0], position[1], position[2]); + Collider* collider = lovrColliderCreate(world, shape, position); lovrColliderInitInertia(collider, shape); luax_pushtype(L, Collider, collider); lovrRelease(collider, lovrColliderDestroy); @@ -166,7 +166,8 @@ static int l_lovrWorldNewSphereCollider(lua_State* L) { static int l_lovrWorldNewMeshCollider(lua_State* L) { World* world = luax_checktype(L, 1, World); MeshShape* shape = luax_newmeshshape(L, 2); - Collider* collider = lovrColliderCreate(world, shape, 0.f, 0.f, 0.f); + float position[3] = { 0.f, 0.f, 0.f }; + Collider* collider = lovrColliderCreate(world, shape, position); lovrColliderInitInertia(collider, shape); luax_pushtype(L, Collider, collider); lovrRelease(collider, lovrColliderDestroy); @@ -177,7 +178,8 @@ static int l_lovrWorldNewMeshCollider(lua_State* L) { static int l_lovrWorldNewTerrainCollider(lua_State* L) { World* world = luax_checktype(L, 1, World); TerrainShape* shape = luax_newterrainshape(L, 2); - Collider* collider = lovrColliderCreate(world, shape, 0.f, 0.f, 0.f); + float position[3] = { 0.f, 0.f, 0.f }; + Collider* collider = lovrColliderCreate(world, shape, position); lovrColliderSetKinematic(collider, true); luax_pushtype(L, Collider, collider); lovrRelease(collider, lovrColliderDestroy); @@ -311,7 +313,7 @@ static int l_lovrWorldRaycast(lua_State* L) { index = luax_readvec3(L, index, end, NULL); luaL_checktype(L, index, LUA_TFUNCTION); lua_settop(L, index); - lovrWorldRaycast(world, start[0], start[1], start[2], end[0], end[1], end[2], raycastCallback, L); + lovrWorldRaycast(world, start, end, raycastCallback, L); return 0; } @@ -323,7 +325,7 @@ static int l_lovrWorldRaycastAny(lua_State* L) { index = luax_readvec3(L, index, end, NULL); RaycastData data = { 0 }; data.tag = lua_tostring(L, index); - lovrWorldRaycast(world, start[0], start[1], start[2], end[0], end[1], end[2], raycastAnyCallback, &data); + lovrWorldRaycast(world, start, end, raycastAnyCallback, &data); if (data.collider) { luax_pushtype(L, Collider, data.collider); lua_pushnumber(L, data.position[0]); @@ -348,7 +350,7 @@ static int l_lovrWorldRaycastClosest(lua_State* L) { index = luax_readvec3(L, index, end, NULL); RaycastData data = { .distance = FLT_MAX }; data.tag = lua_tostring(L, index); - lovrWorldRaycast(world, start[0], start[1], start[2], end[0], end[1], end[2], raycastClosestCallback, &data); + lovrWorldRaycast(world, start, end, raycastClosestCallback, &data); if (data.shape) { luax_pushtype(L, Collider, data.collider); lua_pushnumber(L, data.position[0]); diff --git a/src/modules/physics/physics.h b/src/modules/physics/physics.h index 07e1a756..417efcba 100644 --- a/src/modules/physics/physics.h +++ b/src/modules/physics/physics.h @@ -48,7 +48,6 @@ typedef struct { bool allowSleep; const char* tags[MAX_TAGS]; uint32_t tagCount; - float gravity[3]; // Deprecated } WorldInfo; World* lovrWorldCreate(WorldInfo* info); @@ -63,7 +62,7 @@ void lovrWorldComputeOverlaps(World* world); int lovrWorldGetNextOverlap(World* world, Shape** a, Shape** b); int lovrWorldCollide(World* world, Shape* a, Shape* b, float friction, float restitution); void lovrWorldGetContacts(World* world, Shape* a, Shape* b, Contact contacts[MAX_CONTACTS], uint32_t* count); -void lovrWorldRaycast(World* world, float x1, float y1, float z1, float x2, float y2, float z2, RaycastCallback callback, void* userdata); +void lovrWorldRaycast(World* world, float start[3], float end[3], RaycastCallback callback, void* userdata); bool lovrWorldQueryBox(World* world, float position[3], float size[3], QueryCallback callback, void* userdata); bool lovrWorldQuerySphere(World* world, float position[3], float radius, QueryCallback callback, void* userdata); void lovrWorldGetGravity(World* world, float gravity[3]); @@ -89,7 +88,7 @@ void lovrWorldSetSleepingAllowed(World* world, bool allowed); // Collider -Collider* lovrColliderCreate(World* world, Shape* shape, float x, float y, float z); +Collider* lovrColliderCreate(World* world, Shape* shape, float position[3]); void lovrColliderDestroy(void* ref); void lovrColliderDestroyData(Collider* collider); bool lovrColliderIsDestroyed(Collider* collider); @@ -99,8 +98,8 @@ void lovrColliderInitInertia(Collider* collider, Shape* shape); World* lovrColliderGetWorld(Collider* collider); Shape* lovrColliderGetShape(Collider* collider, uint32_t child); void lovrColliderSetShape(Collider* collider, Shape* shape); -void lovrColliderGetShapeOffset(Collider* collider, float* position, float* orientation); -void lovrColliderSetShapeOffset(Collider* collider, float* position, float* orientation); +void lovrColliderGetShapeOffset(Collider* collider, float position[3], float orientation[4]); +void lovrColliderSetShapeOffset(Collider* collider, float position[3], float orientation[4]); Joint* lovrColliderEnumerateJoints(Collider* collider, Joint* joint, void** private); const char* lovrColliderGetTag(Collider* collider); bool lovrColliderSetTag(Collider* collider, const char* tag); @@ -122,33 +121,33 @@ bool lovrColliderIsAwake(Collider* collider); void lovrColliderSetAwake(Collider* collider, bool awake); float lovrColliderGetMass(Collider* collider); void lovrColliderSetMass(Collider* collider, float mass); -void lovrColliderGetMassData(Collider* collider, float* cx, float* cy, float* cz, float* mass, float inertia[6]); -void lovrColliderSetMassData(Collider* collider, float cx, float cy, float cz, float mass, float inertia[6]); -void lovrColliderGetPosition(Collider* collider, float* x, float* y, float* z); -void lovrColliderSetPosition(Collider* collider, float x, float y, float z); -void lovrColliderGetOrientation(Collider* collider, float* orientation); -void lovrColliderSetOrientation(Collider* collider, float* orientation); -void lovrColliderGetLinearVelocity(Collider* collider, float* x, float* y, float* z); -void lovrColliderSetLinearVelocity(Collider* collider, float x, float y, float z); -void lovrColliderGetAngularVelocity(Collider* collider, float* x, float* y, float* z); -void lovrColliderSetAngularVelocity(Collider* collider, float x, float y, float z); +void lovrColliderGetMassData(Collider* collider, float centerOfMass[3], float* mass, float inertia[6]); +void lovrColliderSetMassData(Collider* collider, float centerOfMass[3], float mass, float inertia[6]); +void lovrColliderGetPosition(Collider* collider, float position[3]); +void lovrColliderSetPosition(Collider* collider, float position[3]); +void lovrColliderGetOrientation(Collider* collider, float orientation[4]); +void lovrColliderSetOrientation(Collider* collider, float orientation[4]); +void lovrColliderGetLinearVelocity(Collider* collider, float velocity[3]); +void lovrColliderSetLinearVelocity(Collider* collider, float velocity[3]); +void lovrColliderGetAngularVelocity(Collider* collider, float velocity[3]); +void lovrColliderSetAngularVelocity(Collider* collider, float velocity[3]); void lovrColliderGetLinearDamping(Collider* collider, float* damping, float* threshold); void lovrColliderSetLinearDamping(Collider* collider, float damping, float threshold); void lovrColliderGetAngularDamping(Collider* collider, float* damping, float* threshold); void lovrColliderSetAngularDamping(Collider* collider, float damping, float threshold); -void lovrColliderApplyForce(Collider* collider, float x, float y, float z); -void lovrColliderApplyForceAtPosition(Collider* collider, float x, float y, float z, float cx, float cy, float cz); -void lovrColliderApplyTorque(Collider* collider, float x, float y, float z); +void lovrColliderApplyForce(Collider* collider, float force[3]); +void lovrColliderApplyForceAtPosition(Collider* collider, float force[3], float position[3]); +void lovrColliderApplyTorque(Collider* collider, float torque[3]); void lovrColliderApplyLinearImpulse(Collider* collider, float impulse[3]); void lovrColliderApplyLinearImpulseAtPosition(Collider* collider, float impulse[3], float position[3]); void lovrColliderApplyAngularImpulse(Collider* collider, float impulse[3]); -void lovrColliderGetLocalCenter(Collider* collider, float* x, float* y, float* z); -void lovrColliderGetLocalPoint(Collider* collider, float wx, float wy, float wz, float* x, float* y, float* z); -void lovrColliderGetWorldPoint(Collider* collider, float x, float y, float z, float* wx, float* wy, float* wz); -void lovrColliderGetLocalVector(Collider* collider, float wx, float wy, float wz, float* x, float* y, float* z); -void lovrColliderGetWorldVector(Collider* collider, float x, float y, float z, float* wx, float* wy, float* wz); -void lovrColliderGetLinearVelocityFromLocalPoint(Collider* collider, float x, float y, float z, float* vx, float* vy, float* vz); -void lovrColliderGetLinearVelocityFromWorldPoint(Collider* collider, float wx, float wy, float wz, float* vx, float* vy, float* vz); +void lovrColliderGetLocalCenter(Collider* collider, float center[3]); +void lovrColliderGetLocalPoint(Collider* collider, float world[3], float local[3]); +void lovrColliderGetWorldPoint(Collider* collider, float local[3], float world[3]); +void lovrColliderGetLocalVector(Collider* collider, float world[3], float local[3]); +void lovrColliderGetWorldVector(Collider* collider, float local[3], float world[3]); +void lovrColliderGetLinearVelocityFromLocalPoint(Collider* collider, float point[3], float velocity[3]); +void lovrColliderGetLinearVelocityFromWorldPoint(Collider* collider, float point[3], float velocity[3]); void lovrColliderGetAABB(Collider* collider, float aabb[6]); // Shapes @@ -166,14 +165,14 @@ typedef enum { void lovrShapeDestroy(void* ref); void lovrShapeDestroyData(Shape* shape); ShapeType lovrShapeGetType(Shape* shape); -void lovrShapeGetMass(Shape* shape, float density, float* cx, float* cy, float* cz, float* mass, float inertia[6]); +void lovrShapeGetMass(Shape* shape, float density, float centerOfMass[3], float* mass, float inertia[6]); void lovrShapeGetAABB(Shape* shape, float position[3], float orientation[4], float aabb[6]); SphereShape* lovrSphereShapeCreate(float radius); float lovrSphereShapeGetRadius(SphereShape* sphere); -BoxShape* lovrBoxShapeCreate(float w, float h, float d); -void lovrBoxShapeGetDimensions(BoxShape* box, float* w, float* h, float* d); +BoxShape* lovrBoxShapeCreate(float dimensions[3]); +void lovrBoxShapeGetDimensions(BoxShape* box, float dimensions[3]); CapsuleShape* lovrCapsuleShapeCreate(float radius, float length); float lovrCapsuleShapeGetRadius(CapsuleShape* capsule); @@ -189,13 +188,13 @@ TerrainShape* lovrTerrainShapeCreate(float* vertices, uint32_t n, float scaleXZ, CompoundShape* lovrCompoundShapeCreate(Shape** shapes, float* positions, float* orientations, uint32_t count, bool freeze); bool lovrCompoundShapeIsFrozen(CompoundShape* shape); -void lovrCompoundShapeAddChild(CompoundShape* shape, Shape* child, float* position, float* orientation); -void lovrCompoundShapeReplaceChild(CompoundShape* shape, uint32_t index, Shape* child, float* position, float* orientation); +void lovrCompoundShapeAddChild(CompoundShape* shape, Shape* child, float position[3], float orientation[4]); +void lovrCompoundShapeReplaceChild(CompoundShape* shape, uint32_t index, Shape* child, float position[3], float orientation[4]); void lovrCompoundShapeRemoveChild(CompoundShape* shape, uint32_t index); Shape* lovrCompoundShapeGetChild(CompoundShape* shape, uint32_t index); uint32_t lovrCompoundShapeGetChildCount(CompoundShape* shape); -void lovrCompoundShapeGetChildOffset(CompoundShape* shape, uint32_t index, float* position, float* orientation); -void lovrCompoundShapeSetChildOffset(CompoundShape* shape, uint32_t index, float* position, float* orientation); +void lovrCompoundShapeGetChildOffset(CompoundShape* shape, uint32_t index, float position[3], float orientation[4]); +void lovrCompoundShapeSetChildOffset(CompoundShape* shape, uint32_t index, float position[3], float orientation[4]); // These tokens need to exist for Lua bindings #define lovrSphereShapeDestroy lovrShapeDestroy diff --git a/src/modules/physics/physics_jolt.c b/src/modules/physics/physics_jolt.c index 27b821a5..e7916519 100644 --- a/src/modules/physics/physics_jolt.c +++ b/src/modules/physics/physics_jolt.c @@ -132,8 +132,6 @@ World* lovrWorldCreate(WorldInfo* info) { world->system = JPH_PhysicsSystem_Create(&settings); world->bodies = JPH_PhysicsSystem_GetBodyInterface(world->system); - lovrWorldSetGravity(world, info->gravity); - for (uint32_t i = 0; i < info->tagCount; i++) { size_t size = strlen(info->tags[i]) + 1; world->tags[i] = lovrMalloc(size); @@ -198,10 +196,10 @@ void lovrWorldGetContacts(World* world, Shape* a, Shape* b, Contact contacts[MAX // } -void lovrWorldRaycast(World* world, float x1, float y1, float z1, float x2, float y2, float z2, RaycastCallback callback, void* userdata) { +void lovrWorldRaycast(World* world, float start[3], float end[3], RaycastCallback callback, void* userdata) { const JPH_NarrowPhaseQuery* query = JPC_PhysicsSystem_GetNarrowPhaseQueryNoLock(world->system); - const JPH_RVec3 origin = { x1, y1, z1 }; - const JPH_Vec3 direction = { x2 - x1, y2 - y1, z2 - z1 }; + const JPH_RVec3 origin = { start[0], start[1], start[2] }; + const JPH_Vec3 direction = { end[0] - start[0], end[1] - start[1], end[2] - start[2] }; JPH_AllHit_CastRayCollector* collector = JPH_AllHit_CastRayCollector_Create(); JPH_NarrowPhaseQuery_CastRayAll(query, &origin, &direction, collector, NULL, NULL, NULL); @@ -219,9 +217,9 @@ void lovrWorldRaycast(World* world, float x1, float y1, float z1, float x2, floa } JPH_RVec3 position = { - x1 + hits[i].fraction * (x2 - x1), - y1 + hits[i].fraction * (y2 - y1), - z1 + hits[i].fraction * (z2 - z1) + start[0] + hits[i].fraction * direction.x, + start[1] + hits[i].fraction * direction.y, + start[2] + hits[i].fraction * direction.z }; JPH_Vec3 normal; @@ -243,10 +241,10 @@ static bool lovrWorldQueryShape(World* world, JPH_Shape* shape, float position[3 mat4_scale(m, scale[0], scale[1], scale[2]); JPH_Vec3 direction = { 0.f, 0.f, 0.f }; - JPH_RVec3 base_offset = { 0.f, 0.f, 0.f }; + JPH_RVec3 baseOffset = { 0.f, 0.f, 0.f }; const JPH_NarrowPhaseQuery* query = JPC_PhysicsSystem_GetNarrowPhaseQueryNoLock(world->system); JPH_AllHit_CastShapeCollector_Reset(state.castShapeCollector); - JPH_NarrowPhaseQuery_CastShape(query, shape, &transform, &direction, &base_offset, state.castShapeCollector); + JPH_NarrowPhaseQuery_CastShape(query, shape, &transform, &direction, &baseOffset, state.castShapeCollector); size_t count; JPH_AllHit_CastShapeCollector_GetHits(state.castShapeCollector, &count); @@ -275,11 +273,12 @@ bool lovrWorldQuerySphere(World* world, float position[3], float radius, QueryCa void lovrWorldGetGravity(World* world, float gravity[3]) { JPH_Vec3 g; JPH_PhysicsSystem_GetGravity(world->system, &g); - vec3_init(gravity, &g.x); + vec3_set(gravity, g.x, g.y, g.z); } void lovrWorldSetGravity(World* world, float gravity[3]) { - JPH_PhysicsSystem_SetGravity(world->system, &(const JPH_Vec3) { gravity[0], gravity[1], gravity[2] }); + JPH_Vec3 g = { gravity[0], gravity[1], gravity[2] }; + JPH_PhysicsSystem_SetGravity(world->system, &g); } const char* lovrWorldGetTagName(World* world, uint32_t tag) { @@ -343,7 +342,7 @@ void lovrWorldSetAngularDamping(World* world, float damping, float threshold) { // Collider -Collider* lovrColliderCreate(World* world, Shape* shape, float x, float y, float z) { +Collider* lovrColliderCreate(World* world, Shape* shape, float position[3]) { uint32_t count = JPH_PhysicsSystem_GetNumBodies(world->system); uint32_t limit = JPH_PhysicsSystem_GetMaxBodies(world->system); lovrCheck(count < limit, "Too many colliders!"); @@ -354,11 +353,11 @@ Collider* lovrColliderCreate(World* world, Shape* shape, float x, float y, float collider->shape = shape ? shape : state.pointShape; collider->tag = UNTAGGED; - const JPH_RVec3 position = { x, y, z }; - const JPH_Quat rotation = { 0.f, 0.f, 0.f, 1.f }; + JPH_RVec3 p = { position[0], position[1], position[2] }; + JPH_Quat q = { 0.f, 0.f, 0.f, 1.f }; JPH_MotionType type = JPH_MotionType_Dynamic; JPH_ObjectLayer objectLayer = UNTAGGED * 2 + 1; - JPH_BodyCreationSettings* settings = JPH_BodyCreationSettings_Create3(collider->shape->shape, &position, &rotation, type, objectLayer); + JPH_BodyCreationSettings* settings = JPH_BodyCreationSettings_Create3(collider->shape->shape, &p, &q, type, objectLayer); collider->body = JPH_BodyInterface_CreateBody(world->bodies, settings); collider->id = JPH_Body_GetID(collider->body); JPH_BodyCreationSettings_Destroy(settings); @@ -485,32 +484,32 @@ void lovrColliderSetShape(Collider* collider, Shape* shape) { } } -void lovrColliderGetShapeOffset(Collider* collider, float* position, float* orientation) { +void lovrColliderGetShapeOffset(Collider* collider, float position[3], float orientation[4]) { const JPH_Shape* shape = JPH_BodyInterface_GetShape(collider->world->bodies, collider->id); if (JPH_Shape_GetSubType(shape) == JPH_ShapeSubType_RotatedTranslated) { - JPH_Vec3 jposition; - JPH_Quat jrotation; - JPH_RotatedTranslatedShape_GetPosition((JPH_RotatedTranslatedShape*) shape, &jposition); - JPH_RotatedTranslatedShape_GetRotation((JPH_RotatedTranslatedShape*) shape, &jrotation); - vec3_init(position, &jposition.x); - quat_init(orientation, &jrotation.x); + JPH_Vec3 p; + JPH_Quat q; + JPH_RotatedTranslatedShape_GetPosition((JPH_RotatedTranslatedShape*) shape, &p); + JPH_RotatedTranslatedShape_GetRotation((JPH_RotatedTranslatedShape*) shape, &q); + vec3_set(position, p.x, p.y, p.z); + quat_set(orientation, q.x, q.y, q.z, q.w); } else { vec3_set(position, 0.f, 0.f, 0.f); quat_identity(orientation); } } -void lovrColliderSetShapeOffset(Collider* collider, float* position, float* orientation) { +void lovrColliderSetShapeOffset(Collider* collider, float position[3], float orientation[4]) { const JPH_Shape* shape = JPH_BodyInterface_GetShape(collider->world->bodies, collider->id); if (JPH_Shape_GetSubType(shape) == JPH_ShapeSubType_RotatedTranslated) { JPH_Shape_Destroy((JPH_Shape*) shape); } - JPH_Vec3 jposition = { position[0], position[1], position[2] }; - JPH_Quat jrotation = { orientation[0], orientation[1], orientation[2], orientation[3] }; - shape = (JPH_Shape*) JPH_RotatedTranslatedShape_Create(&jposition, &jrotation, collider->shape->shape); + JPH_Vec3 p = { position[0], position[1], position[2] }; + JPH_Quat q = { orientation[0], orientation[1], orientation[2], orientation[3] }; + shape = (JPH_Shape*) JPH_RotatedTranslatedShape_Create(&p, &q, collider->shape->shape); bool updateMass = collider->shape && (collider->shape->type == SHAPE_MESH || collider->shape->type == SHAPE_TERRAIN); JPH_BodyInterface_SetShape(collider->world->bodies, collider->id, shape, updateMass, JPH_Activation_Activate); } @@ -646,74 +645,56 @@ void lovrColliderSetMass(Collider* collider, float mass) { JPH_MotionProperties_SetMassProperties(motionProperties, JPH_AllowedDOFs_All, massProperties); } -void lovrColliderGetMassData(Collider* collider, float* cx, float* cy, float* cz, float* mass, float inertia[6]) { +void lovrColliderGetMassData(Collider* collider, float centerOfMass[3], float* mass, float inertia[6]) { // } -void lovrColliderSetMassData(Collider* collider, float cx, float cy, float cz, float mass, float inertia[6]) { +void lovrColliderSetMassData(Collider* collider, float centerOfMass[3], float mass, float inertia[6]) { // } -void lovrColliderGetPosition(Collider* collider, float* x, float* y, float* z) { - JPH_RVec3 position; - JPH_Body_GetPosition(collider->body, &position); - *x = position.x; - *y = position.y; - *z = position.z; +void lovrColliderGetPosition(Collider* collider, float position[3]) { + JPH_RVec3 p; + JPH_Body_GetPosition(collider->body, &p); + vec3_set(position, p.x, p.y, p.z); } -void lovrColliderSetPosition(Collider* collider, float x, float y, float z) { - JPH_RVec3 position = { x, y, z }; - JPH_BodyInterface_SetPosition(collider->world->bodies, collider->id, &position, JPH_Activation_Activate); +void lovrColliderSetPosition(Collider* collider, float position[3]) { + JPH_RVec3 p = { position[0], position[1], position[2] }; + JPH_BodyInterface_SetPosition(collider->world->bodies, collider->id, &p, JPH_Activation_Activate); } void lovrColliderGetOrientation(Collider* collider, float* orientation) { - JPH_Quat quat; - JPH_Body_GetRotation(collider->body, &quat); - orientation[0] = quat.x; - orientation[1] = quat.y; - orientation[2] = quat.z; - orientation[3] = quat.w; + JPH_Quat q; + JPH_Body_GetRotation(collider->body, &q); + quat_set(orientation, q.x, q.y, q.z, q.w); } void lovrColliderSetOrientation(Collider* collider, float* orientation) { - JPH_Quat rotation = { - .x = orientation[0], - .y = orientation[1], - .z = orientation[2], - .w = orientation[3] - }; - JPH_BodyInterface_SetRotation( - collider->world->bodies, - collider->id, - &rotation, - JPH_Activation_Activate); + JPH_Quat q = { orientation[0], orientation[1], orientation[2], orientation[3] }; + JPH_BodyInterface_SetRotation(collider->world->bodies, collider->id, &q, JPH_Activation_Activate); } -void lovrColliderGetLinearVelocity(Collider* collider, float* x, float* y, float* z) { - JPH_Vec3 velocity; - JPH_BodyInterface_GetLinearVelocity(collider->world->bodies, collider->id, &velocity); - *x = velocity.x; - *y = velocity.y; - *z = velocity.z; +void lovrColliderGetLinearVelocity(Collider* collider, float velocity[3]) { + JPH_Vec3 v; + JPH_BodyInterface_GetLinearVelocity(collider->world->bodies, collider->id, &v); + vec3_set(velocity, v.x, v.y, v.z); } -void lovrColliderSetLinearVelocity(Collider* collider, float x, float y, float z) { - const JPH_Vec3 velocity = { x, y, z }; - JPH_BodyInterface_SetLinearVelocity(collider->world->bodies, collider->id, &velocity); +void lovrColliderSetLinearVelocity(Collider* collider, float velocity[3]) { + JPH_Vec3 v = { velocity[0], velocity[1], velocity[2] }; + JPH_BodyInterface_SetLinearVelocity(collider->world->bodies, collider->id, &v); } -void lovrColliderGetAngularVelocity(Collider* collider, float* x, float* y, float* z) { - JPH_Vec3 velocity; - JPH_BodyInterface_GetAngularVelocity(collider->world->bodies, collider->id, &velocity); - *x = velocity.x; - *y = velocity.y; - *z = velocity.z; +void lovrColliderGetAngularVelocity(Collider* collider, float velocity[3]) { + JPH_Vec3 v; + JPH_BodyInterface_GetAngularVelocity(collider->world->bodies, collider->id, &v); + vec3_set(velocity, v.x, v.y, v.z); } -void lovrColliderSetAngularVelocity(Collider* collider, float x, float y, float z) { - JPH_Vec3 velocity = { x, y, z }; - JPH_BodyInterface_SetAngularVelocity(collider->world->bodies, collider->id, &velocity); +void lovrColliderSetAngularVelocity(Collider* collider, float velocity[3]) { + JPH_Vec3 v = { velocity[0], velocity[1], velocity[2] }; + JPH_BodyInterface_SetAngularVelocity(collider->world->bodies, collider->id, &v); } void lovrColliderGetLinearDamping(Collider* collider, float* damping, float* threshold) { @@ -744,104 +725,84 @@ void lovrColliderSetAngularDamping(Collider* collider, float damping, float thre } } -void lovrColliderApplyForce(Collider* collider, float x, float y, float z) { - JPH_Vec3 force = { x, y, z }; - JPH_BodyInterface_AddForce(collider->world->bodies, collider->id, &force); +void lovrColliderApplyForce(Collider* collider, float force[3]) { + JPH_Vec3 f = { force[0], force[1], force[2] }; + JPH_BodyInterface_AddForce(collider->world->bodies, collider->id, &f); } -void lovrColliderApplyForceAtPosition(Collider* collider, float x, float y, float z, float cx, float cy, float cz) { - JPH_Vec3 force = { x, y, z }; - JPH_RVec3 position = { cx, cy, cz }; - JPH_BodyInterface_AddForce2(collider->world->bodies, collider->id, &force, &position); +void lovrColliderApplyForceAtPosition(Collider* collider, float force[3], float position[3]) { + JPH_Vec3 f = { force[0], force[1], force[2] }; + JPH_RVec3 p = { position[0], position[1], position[2] }; + JPH_BodyInterface_AddForce2(collider->world->bodies, collider->id, &f, &p); } -void lovrColliderApplyTorque(Collider* collider, float x, float y, float z) { - JPH_Vec3 torque = { x, y, z }; - JPH_BodyInterface_AddTorque(collider->world->bodies, collider->id, &torque); +void lovrColliderApplyTorque(Collider* collider, float torque[3]) { + JPH_Vec3 t = { torque[0], torque[1], torque[2] }; + JPH_BodyInterface_AddTorque(collider->world->bodies, collider->id, &t); } void lovrColliderApplyLinearImpulse(Collider* collider, float impulse[3]) { - JPH_Vec3 vector = { impulse[0], impulse[1], impulse[2] }; - JPH_BodyInterface_AddImpulse(collider->world->bodies, collider->id, &vector); + JPH_Vec3 v = { impulse[0], impulse[1], impulse[2] }; + JPH_BodyInterface_AddImpulse(collider->world->bodies, collider->id, &v); } void lovrColliderApplyLinearImpulseAtPosition(Collider* collider, float impulse[3], float position[3]) { - JPH_Vec3 vector = { impulse[0], impulse[1], impulse[2] }; - JPH_Vec3 point = { position[0], position[1], position[2] }; - JPH_BodyInterface_AddImpulse2(collider->world->bodies, collider->id, &vector, &point); + JPH_Vec3 v = { impulse[0], impulse[1], impulse[2] }; + JPH_Vec3 p = { position[0], position[1], position[2] }; + JPH_BodyInterface_AddImpulse2(collider->world->bodies, collider->id, &v, &p); } void lovrColliderApplyAngularImpulse(Collider* collider, float impulse[3]) { - JPH_Vec3 vector = { impulse[0], impulse[1], impulse[2] }; - JPH_BodyInterface_AddAngularImpulse(collider->world->bodies, collider->id, &vector); + JPH_Vec3 v = { impulse[0], impulse[1], impulse[2] }; + JPH_BodyInterface_AddAngularImpulse(collider->world->bodies, collider->id, &v); } -void lovrColliderGetLocalCenter(Collider* collider, float* x, float* y, float* z) { +void lovrColliderGetLocalCenter(Collider* collider, float center[3]) { // todo: applicable for CompoundShape and OffsetCenterOfMassShape - *x = 0.f; - *y = 0.f; - *z = 0.f; + vec3_set(center, 0.f, 0.f, 0.f); } -void lovrColliderGetLocalPoint(Collider* collider, float wx, float wy, float wz, float* x, float* y, float* z) { - float position[4] = { wx, wy, wz, 1.f }; +void lovrColliderGetLocalPoint(Collider* collider, float world[3], float local[3]) { JPH_RMatrix4x4 transform; - float* m = &transform.m11; JPH_Body_GetWorldTransform(collider->body, &transform); - mat4_invert(m); - mat4_mulVec4(m, position); - *x = position[0]; - *y = position[1]; - *z = position[2]; + vec3_init(local, world); + mat4_invert(&transform.m11); + mat4_mulPoint(&transform.m11, local); } -void lovrColliderGetWorldPoint(Collider* collider, float x, float y, float z, float* wx, float* wy, float* wz) { - float position[4] = { x, y, z, 1.f }; +void lovrColliderGetWorldPoint(Collider* collider, float local[3], float world[3]) { JPH_RMatrix4x4 transform; - float* m = &transform.m11; JPH_Body_GetWorldTransform(collider->body, &transform); - mat4_mulVec4(m, position); - *wx = position[0]; - *wy = position[1]; - *wz = position[2]; + vec3_init(world, local); + mat4_mulPoint(&transform.m11, world); } -void lovrColliderGetLocalVector(Collider* collider, float wx, float wy, float wz, float* x, float* y, float* z) { - float direction[4] = { wx, wy, wz, 0.f }; +void lovrColliderGetLocalVector(Collider* collider, float world[3], float local[3]) { JPH_RMatrix4x4 transform; - float* m = &transform.m11; JPH_Body_GetWorldTransform(collider->body, &transform); - mat4_invert(m); - mat4_mulVec4(m, direction); - *x = direction[0]; - *y = direction[1]; - *z = direction[2]; + vec3_init(local, world); + mat4_invert(&transform.m11); + mat4_mulDirection(&transform.m11, local); } -void lovrColliderGetWorldVector(Collider* collider, float x, float y, float z, float* wx, float* wy, float* wz) { - float direction[4] = { x, y, z, 0.f }; +void lovrColliderGetWorldVector(Collider* collider, float local[3], float world[3]) { JPH_RMatrix4x4 transform; - float* m = &transform.m11; JPH_Body_GetWorldTransform(collider->body, &transform); - mat4_mulVec4(m, direction); - *wx = direction[0]; - *wy = direction[1]; - *wz = direction[2]; + vec3_init(world, local); + mat4_mulDirection(&transform.m11, world); } -void lovrColliderGetLinearVelocityFromLocalPoint(Collider* collider, float x, float y, float z, float* vx, float* vy, float* vz) { +void lovrColliderGetLinearVelocityFromLocalPoint(Collider* collider, float point[3], float velocity[3]) { float wx, wy, wz; - lovrColliderGetWorldPoint(collider, x, y, z, &wx, &wy, &wz); - lovrColliderGetLinearVelocityFromWorldPoint(collider, wx, wy, wz, vx, vy, vz); + lovrColliderGetWorldPoint(collider, point, velocity); + lovrColliderGetLinearVelocityFromWorldPoint(collider, point, velocity); } -void lovrColliderGetLinearVelocityFromWorldPoint(Collider* collider, float wx, float wy, float wz, float* vx, float* vy, float* vz) { - JPH_RVec3 point = { wx, wy, wz }; - JPH_Vec3 velocity; - JPH_BodyInterface_GetPointVelocity(collider->world->bodies, collider->id, &point, &velocity); - *vx = velocity.x; - *vy = velocity.y; - *vz = velocity.z; +void lovrColliderGetLinearVelocityFromWorldPoint(Collider* collider, float point[3], float velocity[3]) { + JPH_RVec3 p = { point[0], point[1], point[2] }; + JPH_Vec3 v; + JPH_BodyInterface_GetPointVelocity(collider->world->bodies, collider->id, &p, &v); + vec3_set(velocity, v.x, v.y, v.z); } void lovrColliderGetAABB(Collider* collider, float aabb[6]) { @@ -882,7 +843,7 @@ ShapeType lovrShapeGetType(Shape* shape) { return shape->type; } -void lovrShapeGetMass(Shape* shape, float density, float* cx, float* cy, float* cz, float* mass, float inertia[6]) { +void lovrShapeGetMass(Shape* shape, float density, float centerOfMass[3], float* mass, float inertia[6]) { // } @@ -918,22 +879,20 @@ float lovrSphereShapeGetRadius(SphereShape* sphere) { return JPH_SphereShape_GetRadius((JPH_SphereShape*) sphere->shape); } -BoxShape* lovrBoxShapeCreate(float w, float h, float d) { +BoxShape* lovrBoxShapeCreate(float dimensions[3]) { BoxShape* box = lovrCalloc(sizeof(BoxShape)); box->ref = 1; box->type = SHAPE_BOX; - const JPH_Vec3 halfExtent = { w / 2.f, h / 2.f, d / 2.f }; + const JPH_Vec3 halfExtent = { dimensions[0] / 2.f, dimensions[1] / 2.f, dimensions[2] / 2.f }; box->shape = (JPH_Shape*) JPH_BoxShape_Create(&halfExtent, 0.f); JPH_Shape_SetUserData(box->shape, (uint64_t) (uintptr_t) box); return box; } -void lovrBoxShapeGetDimensions(BoxShape* box, float* w, float* h, float* d) { +void lovrBoxShapeGetDimensions(BoxShape* box, float dimensions[3]) { JPH_Vec3 halfExtent; JPH_BoxShape_GetHalfExtent((JPH_BoxShape*) box->shape, &halfExtent); - *w = halfExtent.x * 2.f; - *h = halfExtent.y * 2.f; - *d = halfExtent.z * 2.f; + vec3_set(dimensions, halfExtent.x * 2.f, halfExtent.y * 2.f, halfExtent.z * 2.f); } CapsuleShape* lovrCapsuleShapeCreate(float radius, float length) { @@ -1097,7 +1056,7 @@ uint32_t lovrCompoundShapeGetChildCount(CompoundShape* shape) { return JPH_CompoundShape_GetNumSubShapes((JPH_CompoundShape*) shape->shape); } -void lovrCompoundShapeGetChildOffset(CompoundShape* shape, uint32_t index, float* position, float* orientation) { +void lovrCompoundShapeGetChildOffset(CompoundShape* shape, uint32_t index, float position[3], float orientation[4]) { lovrCheck(index < lovrCompoundShapeGetChildCount(shape), "CompoundShape has no child at index %d", index + 1); const JPH_Shape* child; JPH_Vec3 pos; @@ -1108,7 +1067,7 @@ void lovrCompoundShapeGetChildOffset(CompoundShape* shape, uint32_t index, float quat_init(orientation, &rot.x); } -void lovrCompoundShapeSetChildOffset(CompoundShape* shape, uint32_t index, float* position, float* orientation) { +void lovrCompoundShapeSetChildOffset(CompoundShape* shape, uint32_t index, float position[3], float orientation[4]) { lovrCheck(!lovrCompoundShapeIsFrozen(shape), "CompoundShape is frozen and can not be changed"); lovrCheck(index < lovrCompoundShapeGetChildCount(shape), "CompoundShape has no child at index %d", index + 1); JPH_Vec3 pos = { position[0], position[1], position[2] }; diff --git a/src/modules/physics/physics_ode.c b/src/modules/physics/physics_ode.c index 519f21f9..757df7c8 100644 --- a/src/modules/physics/physics_ode.c +++ b/src/modules/physics/physics_ode.c @@ -1,4 +1,5 @@ #include "physics.h" +#include "core/maf.h" #include "util.h" #include #include @@ -162,7 +163,6 @@ World* lovrWorldCreate(WorldInfo* info) { dHashSpaceSetLevels(world->space, -4, 8); world->contactGroup = dJointGroupCreate(0); arr_init(&world->overlaps); - lovrWorldSetGravity(world, info->gravity); lovrWorldSetSleepingAllowed(world, info->allowSleep); for (uint32_t i = 0; i < info->tagCount; i++) { size_t size = strlen(info->tags[i]) + 1; @@ -327,14 +327,14 @@ void lovrWorldGetContacts(World* world, Shape* a, Shape* b, Contact contacts[MAX } } -void lovrWorldRaycast(World* world, float x1, float y1, float z1, float x2, float y2, float z2, RaycastCallback callback, void* userdata) { +void lovrWorldRaycast(World* world, float start[3], float end[3], RaycastCallback callback, void* userdata) { RaycastData data = { .callback = callback, .userdata = userdata, .shouldStop = false }; - float dx = x2 - x1; - float dy = y2 - y1; - float dz = z2 - z1; + float dx = start[0] - end[0]; + float dy = start[1] - end[1]; + float dz = start[2] - end[2]; float length = sqrtf(dx * dx + dy * dy + dz * dz); dGeomID ray = dCreateRay(world->space, length); - dGeomRaySet(ray, x1, y1, z1, dx, dy, dz); + dGeomRaySet(ray, start[0], start[1], start[2], end[0], end[1], end[2]); dSpaceCollide2(ray, (dGeomID) world->space, &data, raycastCallback); dGeomDestroy(ray); } @@ -454,7 +454,7 @@ bool lovrWorldIsCollisionEnabledBetween(World* world, const char* tag1, const ch return (world->masks[i] & (1 << j)) && (world->masks[j] & (1 << i)); } -Collider* lovrColliderCreate(World* world, Shape* shape, float x, float y, float z) { +Collider* lovrColliderCreate(World* world, Shape* shape, float position[3]) { Collider* collider = lovrCalloc(sizeof(Collider)); collider->ref = 1; collider->body = dBodyCreate(world->id); @@ -466,7 +466,7 @@ Collider* lovrColliderCreate(World* world, Shape* shape, float x, float y, float arr_init(&collider->joints); lovrColliderSetShape(collider, shape); - lovrColliderSetPosition(collider, x, y, z); + lovrColliderSetPosition(collider, position); // Adjust the world's collider list if (!collider->world->head) { @@ -529,9 +529,9 @@ void lovrColliderSetEnabled(Collider* collider, bool enable) { void lovrColliderInitInertia(Collider* collider, Shape* shape) { // compute inertia matrix for default density const float density = 1.0f; - float cx, cy, cz, mass, inertia[6]; - lovrShapeGetMass(shape, density, &cx, &cy, &cz, &mass, inertia); - lovrColliderSetMassData(collider, cx, cy, cz, mass, inertia); + float centerOfMass[3], mass, inertia[6]; + lovrShapeGetMass(shape, density, centerOfMass, &mass, inertia); + lovrColliderSetMassData(collider, centerOfMass, mass, inertia); } World* lovrColliderGetWorld(Collider* collider) { @@ -569,7 +569,7 @@ void lovrColliderSetShape(Collider* collider, Shape* shape) { } } -void lovrColliderGetShapeOffset(Collider* collider, float* position, float* orientation) { +void lovrColliderGetShapeOffset(Collider* collider, float position[3], float orientation[4]) { const dReal* p = dGeomGetOffsetPosition(collider->shape->id); position[0] = p[0]; position[1] = p[1]; @@ -582,7 +582,7 @@ void lovrColliderGetShapeOffset(Collider* collider, float* position, float* orie orientation[3] = q[0]; } -void lovrColliderSetShapeOffset(Collider* collider, float* position, float* orientation) { +void lovrColliderSetShapeOffset(Collider* collider, float position[3], float orientation[4]) { dGeomSetOffsetPosition(collider->shape->id, position[0], position[1], position[2]); dReal q[4] = { orientation[3], orientation[0], orientation[1], orientation[2] }; dGeomSetOffsetQuaternion(collider->shape->id, q); @@ -709,12 +709,10 @@ void lovrColliderSetMass(Collider* collider, float mass) { dBodySetMass(collider->body, &m); } -void lovrColliderGetMassData(Collider* collider, float* cx, float* cy, float* cz, float* mass, float inertia[6]) { +void lovrColliderGetMassData(Collider* collider, float centerOfMass[3], float* mass, float inertia[6]) { dMass m; dBodyGetMass(collider->body, &m); - *cx = m.c[0]; - *cy = m.c[1]; - *cz = m.c[2]; + vec3_set(centerOfMass, m.c[0], m.c[1], m.c[2]); *mass = m.mass; // Diagonal @@ -728,30 +726,25 @@ void lovrColliderGetMassData(Collider* collider, float* cx, float* cy, float* cz inertia[5] = m.I[9]; } -void lovrColliderSetMassData(Collider* collider, float cx, float cy, float cz, float mass, float inertia[6]) { +void lovrColliderSetMassData(Collider* collider, float centerOfMass[3], float mass, float inertia[6]) { dMass m; dBodyGetMass(collider->body, &m); - dMassSetParameters(&m, mass, cx, cy, cz, inertia[0], inertia[1], inertia[2], inertia[3], inertia[4], inertia[5]); + dMassSetParameters(&m, mass, centerOfMass[0], centerOfMass[1], centerOfMass[2], inertia[0], inertia[1], inertia[2], inertia[3], inertia[4], inertia[5]); dBodySetMass(collider->body, &m); } -void lovrColliderGetPosition(Collider* collider, float* x, float* y, float* z) { - const dReal* position = dBodyGetPosition(collider->body); - *x = position[0]; - *y = position[1]; - *z = position[2]; +void lovrColliderGetPosition(Collider* collider, float position[3]) { + const dReal* p = dBodyGetPosition(collider->body); + vec3_set(position, p[0], p[1], p[2]); } -void lovrColliderSetPosition(Collider* collider, float x, float y, float z) { - dBodySetPosition(collider->body, x, y, z); +void lovrColliderSetPosition(Collider* collider, float position[3]) { + dBodySetPosition(collider->body, position[0], position[1], position[2]); } -void lovrColliderGetOrientation(Collider* collider, float* orientation) { +void lovrColliderGetOrientation(Collider* collider, float orientation[4]) { const dReal* q = dBodyGetQuaternion(collider->body); - orientation[0] = q[1]; - orientation[1] = q[2]; - orientation[2] = q[3]; - orientation[3] = q[0]; + quat_set(orientation, q[1], q[2], q[3], q[0]); } void lovrColliderSetOrientation(Collider* collider, float* orientation) { @@ -759,28 +752,24 @@ void lovrColliderSetOrientation(Collider* collider, float* orientation) { dBodySetQuaternion(collider->body, q); } -void lovrColliderGetLinearVelocity(Collider* collider, float* x, float* y, float* z) { - const dReal* velocity = dBodyGetLinearVel(collider->body); - *x = velocity[0]; - *y = velocity[1]; - *z = velocity[2]; +void lovrColliderGetLinearVelocity(Collider* collider, float velocity[3]) { + const dReal* v = dBodyGetLinearVel(collider->body); + vec3_set(velocity, v[0], v[1], v[2]); } -void lovrColliderSetLinearVelocity(Collider* collider, float x, float y, float z) { +void lovrColliderSetLinearVelocity(Collider* collider, float velocity[3]) { dBodyEnable(collider->body); - dBodySetLinearVel(collider->body, x, y, z); + dBodySetLinearVel(collider->body, velocity[0], velocity[1], velocity[2]); } -void lovrColliderGetAngularVelocity(Collider* collider, float* x, float* y, float* z) { - const dReal* velocity = dBodyGetAngularVel(collider->body); - *x = velocity[0]; - *y = velocity[1]; - *z = velocity[2]; +void lovrColliderGetAngularVelocity(Collider* collider, float velocity[3]) { + const dReal* v = dBodyGetAngularVel(collider->body); + vec3_set(velocity, v[0], v[1], v[2]); } -void lovrColliderSetAngularVelocity(Collider* collider, float x, float y, float z) { +void lovrColliderSetAngularVelocity(Collider* collider, float velocity[3]) { dBodyEnable(collider->body); - dBodySetAngularVel(collider->body, x, y, z); + dBodySetAngularVel(collider->body, velocity[0], velocity[1], velocity[2]); } void lovrColliderGetLinearDamping(Collider* collider, float* damping, float* threshold) { @@ -803,19 +792,19 @@ void lovrColliderSetAngularDamping(Collider* collider, float damping, float thre dBodySetAngularDampingThreshold(collider->body, threshold); } -void lovrColliderApplyForce(Collider* collider, float x, float y, float z) { +void lovrColliderApplyForce(Collider* collider, float force[3]) { dBodyEnable(collider->body); - dBodyAddForce(collider->body, x, y, z); + dBodyAddForce(collider->body, force[0], force[1], force[2]); } -void lovrColliderApplyForceAtPosition(Collider* collider, float x, float y, float z, float cx, float cy, float cz) { +void lovrColliderApplyForceAtPosition(Collider* collider, float force[3], float position[3]) { dBodyEnable(collider->body); - dBodyAddForceAtPos(collider->body, x, y, z, cx, cy, cz); + dBodyAddForceAtPos(collider->body, force[0], force[1], force[2], position[0], position[1], position[2]); } -void lovrColliderApplyTorque(Collider* collider, float x, float y, float z) { +void lovrColliderApplyTorque(Collider* collider, float torque[3]) { dBodyEnable(collider->body); - dBodyAddTorque(collider->body, x, y, z); + dBodyAddTorque(collider->body, torque[0], torque[1], torque[2]); } void lovrColliderApplyLinearImpulse(Collider* collider, float impulse[3]) { @@ -830,60 +819,46 @@ void lovrColliderApplyAngularImpulse(Collider* collider, float impulse[3]) { // } -void lovrColliderGetLocalCenter(Collider* collider, float* x, float* y, float* z) { +void lovrColliderGetLocalCenter(Collider* collider, float center[3]) { dMass m; dBodyGetMass(collider->body, &m); - *x = m.c[0]; - *y = m.c[1]; - *z = m.c[2]; + vec3_set(center, m.c[0], m.c[1], m.c[2]); } -void lovrColliderGetLocalPoint(Collider* collider, float wx, float wy, float wz, float* x, float* y, float* z) { - dReal local[4]; - dBodyGetPosRelPoint(collider->body, wx, wy, wz, local); - *x = local[0]; - *y = local[1]; - *z = local[2]; +void lovrColliderGetLocalPoint(Collider* collider, float world[3], float local[3]) { + dReal point[4]; + dBodyGetPosRelPoint(collider->body, world[0], world[1], world[2], point); + vec3_set(local, point[0], point[1], point[2]); } -void lovrColliderGetWorldPoint(Collider* collider, float x, float y, float z, float* wx, float* wy, float* wz) { - dReal world[4]; - dBodyGetRelPointPos(collider->body, x, y, z, world); - *wx = world[0]; - *wy = world[1]; - *wz = world[2]; +void lovrColliderGetWorldPoint(Collider* collider, float local[3], float world[3]) { + dReal point[4]; + dBodyGetRelPointPos(collider->body, local[0], local[1], local[2], point); + vec3_set(world, point[0], point[1], point[2]); } -void lovrColliderGetLocalVector(Collider* collider, float wx, float wy, float wz, float* x, float* y, float* z) { - dReal local[4]; - dBodyVectorFromWorld(collider->body, wx, wy, wz, local); - *x = local[0]; - *y = local[1]; - *z = local[2]; +void lovrColliderGetLocalVector(Collider* collider, float world[3], float local[3]) { + dReal vector[4]; + dBodyVectorFromWorld(collider->body, world[0], world[1], world[2], local); + vec3_set(local, vector[0], vector[1], vector[2]); } -void lovrColliderGetWorldVector(Collider* collider, float x, float y, float z, float* wx, float* wy, float* wz) { - dReal world[4]; - dBodyVectorToWorld(collider->body, x, y, z, world); - *wx = world[0]; - *wy = world[1]; - *wz = world[2]; +void lovrColliderGetWorldVector(Collider* collider, float local[3], float world[3]) { + dReal vector[4]; + dBodyVectorToWorld(collider->body, local[0], local[1], local[2], world); + vec3_set(world, vector[0], vector[1], vector[2]); } -void lovrColliderGetLinearVelocityFromLocalPoint(Collider* collider, float x, float y, float z, float* vx, float* vy, float* vz) { - dReal velocity[4]; - dBodyGetRelPointVel(collider->body, x, y, z, velocity); - *vx = velocity[0]; - *vy = velocity[1]; - *vz = velocity[2]; +void lovrColliderGetLinearVelocityFromLocalPoint(Collider* collider, float point[3], float velocity[3]) { + dReal vector[4]; + dBodyGetRelPointVel(collider->body, point[0], point[1], point[2], vector); + vec3_set(velocity, vector[0], vector[1], vector[2]); } -void lovrColliderGetLinearVelocityFromWorldPoint(Collider* collider, float wx, float wy, float wz, float* vx, float* vy, float* vz) { - dReal velocity[4]; - dBodyGetPointVel(collider->body, wx, wy, wz, velocity); - *vx = velocity[0]; - *vy = velocity[1]; - *vz = velocity[2]; +void lovrColliderGetLinearVelocityFromWorldPoint(Collider* collider, float point[3], float velocity[3]) { + dReal vector[4]; + dBodyGetPointVel(collider->body, point[0], point[1], point[2], velocity); + vec3_set(velocity, vector[0], vector[1], vector[2]); } void lovrColliderGetAABB(Collider* collider, float aabb[6]) { @@ -938,7 +913,7 @@ Collider* lovrShapeGetCollider(Shape* shape) { return shape->collider; } -void lovrShapeGetMass(Shape* shape, float density, float* cx, float* cy, float* cz, float* mass, float inertia[6]) { +void lovrShapeGetMass(Shape* shape, float density, float centerOfMass[3], float* mass, float inertia[6]) { dMass m; dMassSetZero(&m); switch (shape->type) { @@ -987,9 +962,9 @@ void lovrShapeGetMass(Shape* shape, float density, float* cx, float* cy, float* const dReal* rotation = dGeomGetOffsetRotation(shape->id); dMassRotate(&m, rotation); - *cx = m.c[0]; - *cy = m.c[1]; - *cz = m.c[2]; + centerOfMass[0] = m.c[0]; + centerOfMass[1] = m.c[1]; + centerOfMass[2] = m.c[2]; *mass = m.mass; // Diagonal @@ -1026,26 +1001,19 @@ void lovrSphereShapeSetRadius(SphereShape* sphere, float radius) { dGeomSphereSetRadius(sphere->id, radius); } -BoxShape* lovrBoxShapeCreate(float w, float h, float d) { +BoxShape* lovrBoxShapeCreate(float dimensions[3]) { BoxShape* box = lovrCalloc(sizeof(BoxShape)); box->ref = 1; box->type = SHAPE_BOX; - box->id = dCreateBox(0, w, h, d); + box->id = dCreateBox(0, dimensions[0], dimensions[1], dimensions[2]); dGeomSetData(box->id, box); return box; } -void lovrBoxShapeGetDimensions(BoxShape* box, float* w, float* h, float* d) { - dReal dimensions[4]; - dGeomBoxGetLengths(box->id, dimensions); - *w = dimensions[0]; - *h = dimensions[1]; - *d = dimensions[2]; -} - -void lovrBoxShapeSetDimensions(BoxShape* box, float w, float h, float d) { - lovrCheck(w > 0.f && h > 0.f && d > 0.f, "BoxShape dimensions must be positive"); - dGeomBoxSetLengths(box->id, w, h, d); +void lovrBoxShapeGetDimensions(BoxShape* box, float dimensions[3]) { + dReal d[4]; + dGeomBoxGetLengths(box->id, d); + vec3_set(dimensions, d[0], d[1], d[2]); } CapsuleShape* lovrCapsuleShapeCreate(float radius, float length) { @@ -1064,22 +1032,12 @@ float lovrCapsuleShapeGetRadius(CapsuleShape* capsule) { return radius; } -void lovrCapsuleShapeSetRadius(CapsuleShape* capsule, float radius) { - lovrCheck(radius > 0.f, "CapsuleShape dimensions must be positive"); - dGeomCapsuleSetParams(capsule->id, radius, lovrCapsuleShapeGetLength(capsule)); -} - float lovrCapsuleShapeGetLength(CapsuleShape* capsule) { dReal radius, length; dGeomCapsuleGetParams(capsule->id, &radius, &length); return length; } -void lovrCapsuleShapeSetLength(CapsuleShape* capsule, float length) { - lovrCheck(length > 0.f, "CapsuleShape dimensions must be positive"); - dGeomCapsuleSetParams(capsule->id, lovrCapsuleShapeGetRadius(capsule), length); -} - CylinderShape* lovrCylinderShapeCreate(float radius, float length) { lovrCheck(radius > 0.f && length > 0.f, "CylinderShape dimensions must be positive"); CylinderShape* cylinder = lovrCalloc(sizeof(CylinderShape)); @@ -1096,22 +1054,12 @@ float lovrCylinderShapeGetRadius(CylinderShape* cylinder) { return radius; } -void lovrCylinderShapeSetRadius(CylinderShape* cylinder, float radius) { - lovrCheck(radius > 0.f, "CylinderShape dimensions must be positive"); - dGeomCylinderSetParams(cylinder->id, radius, lovrCylinderShapeGetLength(cylinder)); -} - float lovrCylinderShapeGetLength(CylinderShape* cylinder) { dReal radius, length; dGeomCylinderGetParams(cylinder->id, &radius, &length); return length; } -void lovrCylinderShapeSetLength(CylinderShape* cylinder, float length) { - lovrCheck(length > 0.f, "CylinderShape dimensions must be positive"); - dGeomCylinderSetParams(cylinder->id, lovrCylinderShapeGetRadius(cylinder), length); -} - MeshShape* lovrMeshShapeCreate(int vertexCount, float* vertices, int indexCount, dTriIndex* indices) { MeshShape* mesh = lovrCalloc(sizeof(MeshShape)); mesh->ref = 1;