From f82715d402132674e1c1e65c3005517dee19eb3e Mon Sep 17 00:00:00 2001 From: bjorn Date: Fri, 5 Apr 2024 18:04:07 -0700 Subject: [PATCH] lovrColliderCreate takes shape; Jolt requires a shape, also it saves a bit of work. --- src/api/l_physics_world.c | 20 +++++++------------- src/modules/physics/physics.h | 2 +- src/modules/physics/physics_jolt.c | 10 +++++----- src/modules/physics/physics_ode.c | 3 ++- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/api/l_physics_world.c b/src/api/l_physics_world.c index a2d47b94..99c16067 100644 --- a/src/api/l_physics_world.c +++ b/src/api/l_physics_world.c @@ -104,7 +104,7 @@ static int l_lovrWorldNewCollider(lua_State* L) { World* world = luax_checktype(L, 1, World); float position[3]; luax_readvec3(L, 2, position, NULL); - Collider* collider = lovrColliderCreate(world, position[0], position[1], position[2]); + Collider* collider = lovrColliderCreate(world, NULL, position[0], position[1], position[2]); luax_pushtype(L, Collider, collider); lovrRelease(collider, lovrColliderDestroy); return 1; @@ -114,9 +114,8 @@ static int l_lovrWorldNewBoxCollider(lua_State* L) { World* world = luax_checktype(L, 1, World); float position[3]; int index = luax_readvec3(L, 2, position, NULL); - Collider* collider = lovrColliderCreate(world, position[0], position[1], position[2]); BoxShape* shape = luax_newboxshape(L, index); - lovrColliderSetShape(collider, shape); + Collider* collider = lovrColliderCreate(world, shape, position[0], position[1], position[2]); lovrColliderInitInertia(collider, shape); luax_pushtype(L, Collider, collider); lovrRelease(collider, lovrColliderDestroy); @@ -128,9 +127,8 @@ static int l_lovrWorldNewCapsuleCollider(lua_State* L) { World* world = luax_checktype(L, 1, World); float position[3]; int index = luax_readvec3(L, 2, position, NULL); - Collider* collider = lovrColliderCreate(world, position[0], position[1], position[2]); CapsuleShape* shape = luax_newcapsuleshape(L, index); - lovrColliderSetShape(collider, shape); + Collider* collider = lovrColliderCreate(world, shape, position[0], position[1], position[2]); lovrColliderInitInertia(collider, shape); luax_pushtype(L, Collider, collider); lovrRelease(collider, lovrColliderDestroy); @@ -142,9 +140,8 @@ static int l_lovrWorldNewCylinderCollider(lua_State* L) { World* world = luax_checktype(L, 1, World); float position[3]; int index = luax_readvec3(L, 2, position, NULL); - Collider* collider = lovrColliderCreate(world, position[0], position[1], position[2]); CylinderShape* shape = luax_newcylindershape(L, index); - lovrColliderSetShape(collider, shape); + Collider* collider = lovrColliderCreate(world, shape, position[0], position[1], position[2]); lovrColliderInitInertia(collider, shape); luax_pushtype(L, Collider, collider); lovrRelease(collider, lovrColliderDestroy); @@ -156,9 +153,8 @@ static int l_lovrWorldNewSphereCollider(lua_State* L) { World* world = luax_checktype(L, 1, World); float position[3]; int index = luax_readvec3(L, 2, position, NULL); - Collider* collider = lovrColliderCreate(world, position[0], position[1], position[2]); SphereShape* shape = luax_newsphereshape(L, index); - lovrColliderSetShape(collider, shape); + Collider* collider = lovrColliderCreate(world, shape, position[0], position[1], position[2]); lovrColliderInitInertia(collider, shape); luax_pushtype(L, Collider, collider); lovrRelease(collider, lovrColliderDestroy); @@ -168,9 +164,8 @@ static int l_lovrWorldNewSphereCollider(lua_State* L) { static int l_lovrWorldNewMeshCollider(lua_State* L) { World* world = luax_checktype(L, 1, World); - Collider* collider = lovrColliderCreate(world, 0.f, 0.f, 0.f); MeshShape* shape = luax_newmeshshape(L, 2); - lovrColliderSetShape(collider, shape); + Collider* collider = lovrColliderCreate(world, shape, 0.f, 0.f, 0.f); lovrColliderInitInertia(collider, shape); luax_pushtype(L, Collider, collider); lovrRelease(collider, lovrColliderDestroy); @@ -180,9 +175,8 @@ static int l_lovrWorldNewMeshCollider(lua_State* L) { static int l_lovrWorldNewTerrainCollider(lua_State* L) { World* world = luax_checktype(L, 1, World); - Collider* collider = lovrColliderCreate(world, 0.f, 0.f, 0.f); TerrainShape* shape = luax_newterrainshape(L, 2); - lovrColliderSetShape(collider, shape); + Collider* collider = lovrColliderCreate(world, shape, 0.f, 0.f, 0.f); lovrColliderSetKinematic(collider, true); luax_pushtype(L, Collider, collider); lovrRelease(collider, lovrColliderDestroy); diff --git a/src/modules/physics/physics.h b/src/modules/physics/physics.h index ea83aaf7..cbe5a787 100644 --- a/src/modules/physics/physics.h +++ b/src/modules/physics/physics.h @@ -86,7 +86,7 @@ void lovrWorldSetSleepingAllowed(World* world, bool allowed); // Collider -Collider* lovrColliderCreate(World* world, float x, float y, float z); +Collider* lovrColliderCreate(World* world, Shape* shape, float x, float y, float z); void lovrColliderDestroy(void* ref); void lovrColliderDestroyData(Collider* collider); bool lovrColliderIsDestroyed(Collider* collider); diff --git a/src/modules/physics/physics_jolt.c b/src/modules/physics/physics_jolt.c index ed38ab5e..1dfb055f 100644 --- a/src/modules/physics/physics_jolt.c +++ b/src/modules/physics/physics_jolt.c @@ -315,7 +315,7 @@ void lovrWorldSetAngularDamping(World* world, float damping, float threshold) { // Collider -Collider* lovrColliderCreate(World* world, float x, float y, float z) { +Collider* lovrColliderCreate(World* world, Shape* shape, float x, float y, float z) { uint32_t count = JPH_PhysicsSystem_GetNumBodies(world->system); uint32_t limit = JPH_PhysicsSystem_GetMaxBodies(world->system); lovrCheck(count < limit, "Too many colliders!"); @@ -331,7 +331,7 @@ Collider* lovrColliderCreate(World* world, float x, float y, float z) { const JPH_Quat rotation = { 0.f, 0.f, 0.f, 1.f }; // todo: a placeholder querySphere shape is used in collider, then replaced in lovrColliderAddShape JPH_BodyCreationSettings* settings = JPH_BodyCreationSettings_Create3( - state.querySphere, &position, &rotation, motionType, objectLayer); + shape ? shape->shape : state.querySphere, &position, &rotation, motionType, objectLayer); collider->body = JPH_BodyInterface_CreateBody(world->bodies, settings); JPH_BodyCreationSettings_Destroy(settings); collider->id = JPH_Body_GetID(collider->body); @@ -426,7 +426,7 @@ void lovrColliderSetShape(Collider* collider, Shape* shape) { } void lovrColliderGetShapeOffset(Collider* collider, float* position, float* orientation) { - const JPH_Shape* shape = JPH_BodyInterface_GetShape(collider->world->body_interface, collider->id); + const JPH_Shape* shape = JPH_BodyInterface_GetShape(collider->world->bodies, collider->id); if (JPH_Shape_GetSubType(shape) == JPH_ShapeSubType_RotatedTranslated) { JPH_Vec3 jposition; @@ -442,7 +442,7 @@ void lovrColliderGetShapeOffset(Collider* collider, float* position, float* orie } void lovrColliderSetShapeOffset(Collider* collider, float* position, float* orientation) { - const JPH_Shape* shape = JPH_BodyInterface_GetShape(collider->world->body_interface, collider->id); + 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); @@ -452,7 +452,7 @@ void lovrColliderSetShapeOffset(Collider* collider, float* position, float* orie JPH_Quat jrotation = { orientation[0], orientation[1], orientation[2], orientation[3] }; shape = (JPH_Shape*) JPH_RotatedTranslatedShape_Create(&jposition, &jrotation, collider->shape->shape); bool updateMass = collider->shape->type == SHAPE_MESH || collider->shape->type == SHAPE_TERRAIN; - JPH_BodyInterface_SetShape(collider->world->body_interface, collider->id, shape, updateMass, JPH_Activation_Activate); + JPH_BodyInterface_SetShape(collider->world->bodies, collider->id, shape, updateMass, JPH_Activation_Activate); } Joint** lovrColliderGetJoints(Collider* collider, size_t* count) { diff --git a/src/modules/physics/physics_ode.c b/src/modules/physics/physics_ode.c index 6d1abf2a..9e9f046b 100644 --- a/src/modules/physics/physics_ode.c +++ b/src/modules/physics/physics_ode.c @@ -436,7 +436,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, float x, float y, float z) { +Collider* lovrColliderCreate(World* world, Shape* shape, float x, float y, float z) { Collider* collider = lovrCalloc(sizeof(Collider)); collider->ref = 1; collider->body = dBodyCreate(world->id); @@ -447,6 +447,7 @@ Collider* lovrColliderCreate(World* world, float x, float y, float z) { dBodySetData(collider->body, collider); arr_init(&collider->joints); + lovrColliderSetShape(collider, shape); lovrColliderSetPosition(collider, x, y, z); // Adjust the world's collider list