Add Lua Helpers

This commit is contained in:
D4rkl1ght3r
2025-05-14 18:25:56 +02:00
parent 00d25af93a
commit 835a30062f
4 changed files with 127 additions and 0 deletions

View File

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

View File

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

View File

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

3
DATA/SCRIPTS/helpers.lua Normal file
View File

@@ -0,0 +1,3 @@
dofile('..\\DATA\\SCRIPTS\\HELPERS\\math.lua')
dofile('..\\DATA\\SCRIPTS\\HELPERS\\convert.lua')
dofile('..\\DATA\\SCRIPTS\\HELPERS\\utils.lua')