Created
November 1, 2012 05:12
-
-
Save mebens/3991990 to your computer and use it in GitHub Desktop.
Calculate 8-axis movement angle with LÖVE and ammo-input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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