From 9b3ace7094ac830101953a55c640aebefd052a0d Mon Sep 17 00:00:00 2001 From: bjorn Date: Fri, 26 Apr 2024 01:39:46 -0700 Subject: [PATCH] WIP Collider:get/setEnabledAxes; --- src/api/l_physics_collider.c | 55 ++++++++++++++++++++++++++++++ src/modules/physics/physics.h | 2 ++ src/modules/physics/physics_jolt.c | 23 +++++++++++++ src/modules/physics/physics_ode.c | 8 +++++ 4 files changed, 88 insertions(+) diff --git a/src/api/l_physics_collider.c b/src/api/l_physics_collider.c index 44a997d8..2c9e24dc 100644 --- a/src/api/l_physics_collider.c +++ b/src/api/l_physics_collider.c @@ -223,6 +223,59 @@ static int l_lovrColliderSetMassData(lua_State* L) { return 0; } +static int l_lovrColliderGetEnabledAxes(lua_State* L) { + Collider* collider = luax_checktype(L, 1, Collider); + bool translation[3]; + bool rotation[3]; + lovrColliderGetEnabledAxes(collider, translation, rotation); + + char string[3]; + size_t length; + + length = 0; + for (size_t i = 0; i < 3; i++) { + if (translation[i]) { + string[length++] = 'x' + i; + } + } + lua_pushlstring(L, string, length); + + length = 0; + for (size_t i = 0; i < 3; i++) { + if (rotation[i]) { + string[length++] = 'x' + i; + } + } + lua_pushlstring(L, string, length); + + return 2; +} + +static int l_lovrColliderSetEnabledAxes(lua_State* L) { + Collider* collider = luax_checktype(L, 1, Collider); + bool translation[3] = { false, false, false }; + bool rotation[3] = { false, false, false }; + const char* string; + size_t length; + + string = lua_tolstring(L, 2, &length); + for (size_t i = 0; i < length; i++) { + if (string[i] >= 'x' && string[i] <= 'z') { + translation[string[i] - 'x'] = true; + } + } + + string = lua_tolstring(L, 3, &length); + for (size_t i = 0; i < length; i++) { + if (string[i] >= 'x' && string[i] <= 'z') { + rotation[string[i] - 'x'] = true; + } + } + + lovrColliderSetEnabledAxes(collider, translation, rotation); + return 0; +} + static int l_lovrColliderGetPosition(lua_State* L) { Collider* collider = luax_checktype(L, 1, Collider); float position[3]; @@ -598,6 +651,8 @@ const luaL_Reg lovrCollider[] = { { "setMass", l_lovrColliderSetMass }, { "getMassData", l_lovrColliderGetMassData }, { "setMassData", l_lovrColliderSetMassData }, + { "getEnabledAxes", l_lovrColliderGetEnabledAxes }, + { "setEnabledAxes", l_lovrColliderSetEnabledAxes }, { "getPosition", l_lovrColliderGetPosition }, { "setPosition", l_lovrColliderSetPosition }, { "getOrientation", l_lovrColliderGetOrientation }, diff --git a/src/modules/physics/physics.h b/src/modules/physics/physics.h index 78046740..5b0bfb45 100644 --- a/src/modules/physics/physics.h +++ b/src/modules/physics/physics.h @@ -133,6 +133,8 @@ float lovrColliderGetMass(Collider* collider); void lovrColliderSetMass(Collider* collider, float mass); 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 lovrColliderGetEnabledAxes(Collider* collider, bool translation[3], bool rotation[3]); +void lovrColliderSetEnabledAxes(Collider* collider, bool translation[3], bool rotation[3]); void lovrColliderGetPosition(Collider* collider, float position[3]); void lovrColliderSetPosition(Collider* collider, float position[3]); void lovrColliderGetOrientation(Collider* collider, float orientation[4]); diff --git a/src/modules/physics/physics_jolt.c b/src/modules/physics/physics_jolt.c index 6acf5894..457e5b25 100644 --- a/src/modules/physics/physics_jolt.c +++ b/src/modules/physics/physics_jolt.c @@ -641,6 +641,29 @@ void lovrColliderSetMassData(Collider* collider, float centerOfMass[3], float ma // } +void lovrColliderGetEnabledAxes(Collider* collider, bool translation[3], bool rotation[3]) { + // TODO need bindings +} + +void lovrColliderSetEnabledAxes(Collider* collider, bool translation[3], bool rotation[3]) { + JPH_AllowedDOFs dofs = 0; + + for (size_t i = 0; i < 3; i++) { + if (translation[i]) { + dofs |= JPH_AllowedDOFs_TranslationX << i; + } + } + + for (size_t i = 0; i < 3; i++) { + if (rotation[i]) { + dofs |= JPH_AllowedDOFs_RotationX << i; + } + } + + JPH_MotionProperties* motion = JPH_Body_GetMotionProperties(collider->body); + JPH_MotionProperties_SetMassProperties(motion, dofs, NULL); // TODO need to synthesize mass +} + void lovrColliderGetPosition(Collider* collider, float position[3]) { JPH_RVec3 p; JPH_Body_GetPosition(collider->body, &p); diff --git a/src/modules/physics/physics_ode.c b/src/modules/physics/physics_ode.c index 1c1d169b..8d32b94d 100644 --- a/src/modules/physics/physics_ode.c +++ b/src/modules/physics/physics_ode.c @@ -658,6 +658,14 @@ void lovrColliderSetMassData(Collider* collider, float centerOfMass[3], float ma dBodySetMass(collider->body, &m); } +void lovrColliderGetEnabledAxes(Collider* collider, bool translation[3], bool rotation[3]) { + lovrThrow("NYI"); +} + +void lovrColliderSetEnabledAxes(Collider* collider, bool translation[3], bool rotation[3]) { + lovrThrow("NYI"); +} + void lovrColliderGetPosition(Collider* collider, float position[3]) { const dReal* p = dBodyGetPosition(collider->body); vec3_set(position, p[0], p[1], p[2]);