Skip to content

Instantly share code, notes, and snippets.

@mebens
Created November 1, 2012 05:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mebens/3991990 to your computer and use it in GitHub Desktop.
Save mebens/3991990 to your computer and use it in GitHub Desktop.
Calculate 8-axis movement angle with LÖVE and ammo-input
-- returns nil if there's no movement
function getDirection()
local xAxis = input.axisDown("left", "right")
local yAxis = input.axisDown("up", "down")
local xAngle = xAxis == 1 and 0 or (xAxis == -1 and math.tau / 2 or nil)
local yAngle = yAxis == 1 and math.tau / 4 or (yAxis == -1 and math.tau * 0.75 or nil)
if xAngle and yAngle then
-- x = 1, y = -1 is a special case the doesn't fit; not sure what I can do about it other than this:
if xAxis == 1 and yAxis == -1 then return yAngle + math.tau / 8 end
return (xAngle + yAngle) / 2
else
return xAngle or yAngle
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment