Skip to content

Instantly share code, notes, and snippets.

@mebens mebens/gist:3991990
Created Nov 1, 2012

Embed
What would you like to do?
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
You can’t perform that action at this time.