Add more vec3f functions to Lua

This commit is contained in:
MysterD 2022-03-05 18:10:20 -08:00
parent 369a7cd7e5
commit 7aab694743
2 changed files with 54 additions and 0 deletions

View file

@ -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

View file

@ -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"