railcarts/direction.lua
Ciaran Gultnieks 80bbb57df2 Initial commit
2014-03-26 19:10:30 +00:00

62 lines
1.6 KiB
Lua

-- Directions are used throughout to specify movement along an axis. A
-- direction is an integer from 0 to 3, defined as follows:
--
-- 0 = +z
-- 1 = +x
-- 2 = +z
-- 3 = +x
--
-- Thus, incrementing the direction is a clockwise rotation, decrementing
-- is an anticlockwise one.
--
-- This file provides utility functions for converting and manipulating
-- them.
--- Convert a direction to an xz vector of length 1
--@param direction A direction, 0-3
--@return {x,z}
function direction_to_xz(direction)
if direction == 0 then return {x=0,z=1} end
if direction == 1 then return {x=1,z=0} end
if direction == 2 then return {x=0,z=-1} end
return {x=-1,z=0}
end
--- Convert an xz vector to a direction.
-- @param xz An xz vector, which had better be one of those returned by
-- direction_to_xz, or at least a multiple of, otherwise the results will
-- be nonsensical.
-- @return The direction
function xz_to_direction(xz)
if xz.z > 0 then return 0 end
if xz.x > 0 then return 1 end
if xz.z < 0 then return 2 end
return 3
end
--- Get the yaw for a direction
-- @param direction The direction
-- @return The yaw in radians
function direction_to_yaw(direction)
if direction == 3 then
direction = 1
elseif direction == 1 then
direction = 3
end
return direction * (math.pi / 2)
end
--- Get the opposite of a direction
-- @param direction The direction
-- @return The opposite direction
function direction_reverse(direction)
if direction == 0 then return 2 end
if direction == 1 then return 3 end
if direction == 2 then return 0 end
return 1
end