diff --git a/src/api/math.c b/src/api/math.c index 4b0c9f58..9ec8b451 100644 --- a/src/api/math.c +++ b/src/api/math.c @@ -54,6 +54,19 @@ int l_lovrMathLookAt(lua_State* L) { return 4; } +int l_lovrMathOrientationToDirection(lua_State* L) { + float angle = luaL_checknumber(L, 1); + float ax = luaL_optnumber(L, 2, 0); + float ay = luaL_optnumber(L, 3, 1); + float az = luaL_optnumber(L, 4, 0); + float v[3]; + lovrMathOrientationToDirection(angle, ax, ay, az, v); + lua_pushnumber(L, v[0]); + lua_pushnumber(L, v[1]); + lua_pushnumber(L, v[2]); + return 3; +} + int l_lovrMathRandom(lua_State* L) { luax_pushtype(L, RandomGenerator, lovrMathGetRandomGenerator()); lua_insert(L, 1); @@ -81,6 +94,7 @@ int l_lovrMathSetRandomSeed(lua_State* L) { const luaL_Reg lovrMath[] = { { "newRandomGenerator", l_lovrMathNewRandomGenerator }, { "newTransform", l_lovrMathNewTransform }, + { "orientationToDirection", l_lovrMathOrientationToDirection }, { "lookAt", l_lovrMathLookAt }, { "random", l_lovrMathRandom }, { "randomNormal", l_lovrMathRandomNormal }, diff --git a/src/math/math.c b/src/math/math.c index be73576e..da752a4c 100644 --- a/src/math/math.c +++ b/src/math/math.c @@ -1,4 +1,6 @@ #include "math.h" +#include "math/vec3.h" +#include #include #include @@ -18,3 +20,11 @@ void lovrMathDestroy() { RandomGenerator* lovrMathGetRandomGenerator() { return generator; } + +void lovrMathOrientationToDirection(float angle, float ax, float ay, float az, vec3 v) { + float sinTheta = sin(angle); + float cosTheta = cos(angle); + v[0] = sinTheta * -ay + (1 - cosTheta) * -az * ax; + v[1] = sinTheta * ax + (1 - cosTheta) * -az * ay; + v[2] = -cosTheta + (1 - cosTheta) * -az * az; +} diff --git a/src/math/math.h b/src/math/math.h index f7548fea..c92ed24c 100644 --- a/src/math/math.h +++ b/src/math/math.h @@ -14,3 +14,4 @@ typedef vec_t(mat4) vec_mat4_t; void lovrMathInit(); void lovrMathDestroy(); RandomGenerator* lovrMathGetRandomGenerator(); +void lovrMathOrientationToDirection(float angle, float ax, float ay, float az, vec3 v);