Function to translate angles of Euler into quaternions.
Arguments: RotX, RotY, RotZ
Return: RotX, RotY, RotZ, RotW
function math.quaternion(x, y, z)
local c = { math.cos(math.rad(-x)), math.cos(math.rad(-y)), math.cos(math.rad(z)) }
local s = { math.sin(math.rad(-x)), math.sin(math.rad(-y)), math.sin(math.rad(z)) }
local t = { c[2] * c[3], s[1] * s[2] * s[3] + c[1] * c[3], c[1] * c[2] }
return
math.drt(1.0 + t[1] - t[2] - t[3], (c[3] * s[1] - c[1] * s[2] * s[3]) - (-s[1] * c[2])),
math.drt(1.0 - t[1] + t[2] - t[3], s[2] + (c[1] * s[2] * c[3] + s[1] * s[3])),
math.drt(1.0 - t[1] - t[2] + t[3], (s[1] * s[2] * c[3] - c[1] * s[3]) - (c[2] * s[3])),
math.drt(1.0 + t[1] + t[2] + t[3])
end
function math.drt(a, b)
a = math.sqrt(math.max(0.0, a)) * 0.5
return (b and ((b < 0) and -math.abs(a) or math.abs(a)) or a)
end