From 7aab694743b9e9aa5c2c846a385b28bc88843b3f Mon Sep 17 00:00:00 2001 From: MysterD Date: Sat, 5 Mar 2022 18:10:20 -0800 Subject: [PATCH] Add more vec3f functions to Lua --- autogen/lua_constants/built-in.lua | 30 ++++++++++++++++++++++++++++ src/pc/lua/smlua_constants_autogen.c | 24 ++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/autogen/lua_constants/built-in.lua b/autogen/lua_constants/built-in.lua index 27f9326e..c485e540 100644 --- a/autogen/lua_constants/built-in.lua +++ b/autogen/lua_constants/built-in.lua @@ -80,6 +80,36 @@ function vec3f_mul(dest, a) return dest end +function vec3f_normalize(dest) + local invsqrt = 1.0 / math.sqrt(dest.x * dest.x + dest.y * dest.y + dest.z * dest.z) + if invsqrt == 0 then + return dest + end + + dest.x = dest.x * invsqrt + dest.y = dest.y * invsqrt + dest.z = dest.z * invsqrt + + return dest +end + +function vec3f_length(a) + return math.sqrt(a.x * a.x + a.y * a.y + a.z * a.z) +end + +function vec3f_dot(a, b) + return a.x * b.x + a.y * b.y + a.z * b.z +end + +function vec3f_project(vec, onto) + local numerator = vec3f_dot(vec, onto) + local denominator = vec3f_dot(onto, onto) + local out = {} + vec3f_copy(out, onto) + vec3f_mul(out, numerator / denominator) + return out +end + function vec3f_dist(v1, v2) dx = v1.x - v2.x dy = v1.y - v2.y diff --git a/src/pc/lua/smlua_constants_autogen.c b/src/pc/lua/smlua_constants_autogen.c index 783b59f8..53b8c22c 100644 --- a/src/pc/lua/smlua_constants_autogen.c +++ b/src/pc/lua/smlua_constants_autogen.c @@ -72,6 +72,30 @@ char gSmluaConstants[] = "" " dest.z = dest.z * a\n" " return dest\n" "end\n" +"function vec3f_normalize(dest)\n" +" local invsqrt = 1.0 / math.sqrt(dest.x * dest.x + dest.y * dest.y + dest.z * dest.z)\n" +" if invsqrt == 0 then\n" +" return dest\n" +" end\n" +" dest.x = dest.x * invsqrt\n" +" dest.y = dest.y * invsqrt\n" +" dest.z = dest.z * invsqrt\n" +" return dest\n" +"end\n" +"function vec3f_length(a)\n" +" return math.sqrt(a.x * a.x + a.y * a.y + a.z * a.z)\n" +"end\n" +"function vec3f_dot(a, b)\n" +" return a.x * b.x + a.y * b.y + a.z * b.z\n" +"end\n" +"function vec3f_project(vec, onto)\n" +" local numerator = vec3f_dot(vec, onto)\n" +" local denominator = vec3f_dot(onto, onto)\n" +" local out = {}\n" +" vec3f_copy(out, onto)\n" +" vec3f_mul(out, numerator / denominator)\n" +" return out\n" +"end\n" "function vec3f_dist(v1, v2)\n" " dx = v1.x - v2.x\n" " dy = v1.y - v2.y\n"