From 835a30062f18bd9f147203fefd4d61792f088cc7 Mon Sep 17 00:00:00 2001 From: D4rkl1ght3r <86805404+D4rkl1ght3r@users.noreply.github.com> Date: Wed, 14 May 2025 18:25:56 +0200 Subject: [PATCH] Add Lua Helpers --- DATA/SCRIPTS/HELPERS/convert.lua | 63 ++++++++++++++++++++++++++++++++ DATA/SCRIPTS/HELPERS/math.lua | 47 ++++++++++++++++++++++++ DATA/SCRIPTS/HELPERS/utils.lua | 14 +++++++ DATA/SCRIPTS/helpers.lua | 3 ++ 4 files changed, 127 insertions(+) create mode 100644 DATA/SCRIPTS/HELPERS/convert.lua create mode 100644 DATA/SCRIPTS/HELPERS/math.lua create mode 100644 DATA/SCRIPTS/HELPERS/utils.lua create mode 100644 DATA/SCRIPTS/helpers.lua diff --git a/DATA/SCRIPTS/HELPERS/convert.lua b/DATA/SCRIPTS/HELPERS/convert.lua new file mode 100644 index 00000000..813a0401 --- /dev/null +++ b/DATA/SCRIPTS/HELPERS/convert.lua @@ -0,0 +1,63 @@ +Convert = {} +Convert["QuatToMatrix"] = function(quat) + + local x = quat[1] + local y = quat[2] + local z = quat[3] + local w = quat[4] + + local x2 = x + x + local y2 = y + y + local z2 = z + z + + local xx = x * x2 + local yx = y * x2 + local yy = y * y2 + local zx = z * x2 + local zy = z * y2 + local zz = z * z2 + local wx = w * x2 + local wy = w * y2 + local wz = w * z2 + + local result = { + { 1 - yy - zz, yx + wz, zx - wy }, + { yx - wz, 1 - xx - zz, zy + wx }, + { zx + wy, zy - wx, 1 - xx - yy } + } + + return result +end + +Convert["EulerToQuat"] = function (x, y, z) + + local b = y * 0.5; + local c = x * 0.5; + local d = z * 0.5; + + local sp = sin(b) + local cp = cos(b) + local sy = sin(c) + local cy = cos(c) + local sr = sin(d) + local cr = cos(d) + local a0 = sp * cy * cr + local b0 = cp * sy * sr + local a1 = cp * sy * cr + local b1 = sp * cy * sr + local a2 = cp * cy * sr + local b2 = sp * sy * cr + local a3 = cp * cy * cr + local b3 = sp * sy * sr + + local result = { Math.round(a0 + b0, 5), Math.round(a1 - b1, 5), Math.round(a2 + b2, 5), Math.round(a3 - b3, 5) } + return result +end + +-- Should be called in place of orient like this: orient = Convert.EulerToMatrix(x, y, z), + +Convert["EulerToMatrix"] = function (x, y, z) + local q = Convert.EulerToQuat(y, x, z) + local m = Convert.QuatToMatrix(q) + return m +end \ No newline at end of file diff --git a/DATA/SCRIPTS/HELPERS/math.lua b/DATA/SCRIPTS/HELPERS/math.lua new file mode 100644 index 00000000..ccbcba75 --- /dev/null +++ b/DATA/SCRIPTS/HELPERS/math.lua @@ -0,0 +1,47 @@ +Math = {} +Math["round"] = function (num, numDecimalPlaces) + local mult = 10^(numDecimalPlaces or 0) + return floor(num * mult + 0.5) / mult +end + +function sphereCollision(a, b) + local ax = a.center[1] + local ay = a.center[2] + local az = a.center[3] + + local bx = b.center[1] + local by = b.center[2] + local bz = b.center[2] + + local threshold = (a.radius + b.radius) * (a.radius + b.radius) + + local dx = bx - ax + local dy = by - ay + local dz = bz - az + + local distSq = dx * dx + dy * dy + dz * dz + + return distSq < threshold +end + +-- Default functions +Math["abs"] = abs +Math["asin"] = asin +Math["atan"] = atan +Math["atan2"] = atan2 +Math["ceil"] = ceil +Math["cos"] = cos +Math["sin"] = sin +Math["tan"] = tan +Math["deg"] = deg +Math["floor"] = floor +Math["log"] = log +Math["log10"] = log10 +Math["max"] = max +Math["min"] = min +Math["mod"] = mod +Math["sqrt"] = sqrt +Math["frexp"] = frexp +Math["ldexp"] = ldexp +Math["random"] = random +Math["randomseed"] = randomseed \ No newline at end of file diff --git a/DATA/SCRIPTS/HELPERS/utils.lua b/DATA/SCRIPTS/HELPERS/utils.lua new file mode 100644 index 00000000..7645b3e6 --- /dev/null +++ b/DATA/SCRIPTS/HELPERS/utils.lua @@ -0,0 +1,14 @@ +Utils = {} +Utils["writeVariables"] = function() + filename = "lua.variables.log" + remove(filename) + local LogVars + LogVars = function(x, y) + local file = appendto(filename) + write(file, tostring(x).." ( "..tostring(y).." )\n") + flush(file) + closefile(file) + end + foreachvar(LogVars) + filename = nil +end \ No newline at end of file diff --git a/DATA/SCRIPTS/helpers.lua b/DATA/SCRIPTS/helpers.lua new file mode 100644 index 00000000..24292ef7 --- /dev/null +++ b/DATA/SCRIPTS/helpers.lua @@ -0,0 +1,3 @@ +dofile('..\\DATA\\SCRIPTS\\HELPERS\\math.lua') +dofile('..\\DATA\\SCRIPTS\\HELPERS\\convert.lua') +dofile('..\\DATA\\SCRIPTS\\HELPERS\\utils.lua') \ No newline at end of file