diff --git a/src/api/types/body.c b/src/api/types/body.c index dfb7ca78..6f244613 100644 --- a/src/api/types/body.c +++ b/src/api/types/body.c @@ -113,6 +113,33 @@ int l_lovrBodySetAngularDamping(lua_State* L) { return 0; } +int l_lovrBodyApplyForce(lua_State* L) { + Body* body = luax_checktype(L, 1, Body); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float z = luaL_checknumber(L, 4); + + if (lua_gettop(L) > 4) { + float cx = luaL_checknumber(L, 5); + float cy = luaL_checknumber(L, 6); + float cz = luaL_checknumber(L, 7); + lovrBodyApplyForceAtPosition(body, x, y, z, cx, cy, cz); + } else { + lovrBodyApplyForce(body, x, y, z); + } + + return 0; +} + +int l_lovrBodyApplyTorque(lua_State* L) { + Body* body = luax_checktype(L, 1, Body); + float x = luaL_checknumber(L, 2); + float y = luaL_checknumber(L, 3); + float z = luaL_checknumber(L, 4); + lovrBodyApplyTorque(body, x, y, z); + return 0; +} + const luaL_Reg lovrBody[] = { { "getPosition", l_lovrBodyGetPosition }, { "setPosition", l_lovrBodySetPosition }, @@ -126,5 +153,7 @@ const luaL_Reg lovrBody[] = { { "setLinearDamping", l_lovrBodySetLinearDamping }, { "getAngularDamping", l_lovrBodyGetAngularDamping }, { "setAngularDamping", l_lovrBodySetAngularDamping }, + { "applyForce", l_lovrBodyApplyForce }, + { "applyTorque", l_lovrBodyApplyForce }, { NULL, NULL } }; diff --git a/src/physics/physics.c b/src/physics/physics.c index f7939e50..1917b104 100644 --- a/src/physics/physics.c +++ b/src/physics/physics.c @@ -160,3 +160,15 @@ void lovrBodySetAngularDamping(Body* body, float damping, float threshold) { dBodySetAngularDamping(body->id, damping); dBodySetAngularDampingThreshold(body->id, threshold); } + +void lovrBodyApplyForce(Body* body, float x, float y, float z) { + dBodyAddForce(body->id, x, y, z); +} + +void lovrBodyApplyForceAtPosition(Body* body, float x, float y, float z, float cx, float cy, float cz) { + dBodyAddForceAtPos(body->id, x, y, z, cx, cy, cz); +} + +void lovrBodyApplyTorque(Body* body, float x, float y, float z) { + dBodyAddTorque(body->id, x, y, z); +} diff --git a/src/physics/physics.h b/src/physics/physics.h index 39d26b50..2a952700 100644 --- a/src/physics/physics.h +++ b/src/physics/physics.h @@ -41,3 +41,6 @@ void lovrBodyGetLinearDamping(Body* body, float* damping, float* threshold); void lovrBodySetLinearDamping(Body* body, float damping, float threshold); void lovrBodyGetAngularDamping(Body* body, float* damping, float* threshold); void lovrBodySetAngularDamping(Body* body, float damping, float threshold); +void lovrBodyApplyForce(Body* body, float x, float y, float z); +void lovrBodyApplyForceAtPosition(Body* body, float x, float y, float z, float cx, float cy, float cz); +void lovrBodyApplyTorque(Body* body, float x, float y, float z);