Euler numbers are common in 3D manipulation code. On Roblox, they’re formatted to automatically round to be within -180 and 180 degrees. This can make combining Euler numbers on the same axis difficult.
To overcome this difficulty I’ve developed 3 basic algorithms to allow me to treat Euler numbers like regular numbers. They all use radians, however, I’ll be discussing them using degrees because I feel they’re more accessible to think about as many of us were taught about angles using degrees first.
Euler Round
Manipulating a stored Euler number directly can lead to numbers being outside -180 to 180. Usually, Roblox will automatically round that number to be within acceptable ranges when it’s applied to one of their custom data types. However, if you’ve temporarily stored an Euler Number as a regular number it will not do this. If your programming assumes the number to be between 180 and -180 (like the other methods in this post) you’ll need a way to round numbers to be within 180 and -180.
function eulerRound(radNum)
if math.abs(math.deg(radNum)) > 180 then
return -math.sign(radNum) * (math.rad(180) - math.abs(radNum - (math.sign(radNum) * math.rad(180))))
else
return radNum
end
end
Euler Subtract / Distance
This finds the closest distance between two Euler numbers similar to how 5 - 3 = 2 gives the distance between two regular numbers.
This properly handles instances where a large positive Euler number needs to find the quickest route to a large negative one. For example, going from 150 to -170 in traditional math will lead to the conclusion that you need to rotate -320 degrees to reach that point. However with Euler 180 = -180, thus the shortest path is actually to rotate 40 degrees.
function eulerDistance(radNum1, radNum2)
local res1 = eulerRound(radNum2) - eulerRound(radNum1)
local res2 = -math.sign(res1)*(math.rad(360)-math.abs(res1))
if math.abs(res1) < math.abs(res2) then
return res1
else
return res2
end
end
Euler Lerp
In my opinion, the most useful method on this list as well as the product of the two previous methods is Euler Lerp. This method will return a number between two radians in proportion to the alpha (a number from 0 to 1).
function eulerLerp(radNum1, radNum2, alpha)
return eulerRound(radNum1 + (eulerDistance(radNum2, radNum1) * alpha))
end
I can’t vouch for these being the most optimized ways to accomplish these tasks, but they work. If anyone has a faster route I’ll be more than happy to update the code in both this post as well as my own games. Enjoy!