Created
August 6, 2015 02:18
-
-
Save robmiracle/94edfa802eba7b88d902 to your computer and use it in GitHub Desktop.
Code snippets for the Corona SDK Tutorial: Game controllers and axes (https://coronalabs.com/blog/2013/09/24/tutorial-controllers-and-axis)
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
-- Calculate the angle to rotate the square. Using simple right angle math, we can | |
-- determine the base and height of a right triangle where one point is 0,0 | |
-- (stick center) and the values returned from the two axis numbers returned | |
-- from the stick | |
-- This will give us a 0-90 value, so we have to map it to the quadrant | |
-- based on if the values for the two axis are positive or negative | |
-- Negative Y, positive X is top-right area | |
-- Positive X, Positive Y is bottom-right area | |
-- Negative X, positive Y is bottom-left area | |
-- Negative x, negative y is top-left area | |
local function calculateAngle( sideX, sideY ) | |
if ( sideX == 0 or sideY == 0 ) then | |
return nil | |
end | |
local tanX = math.abs( sideY ) / math.abs( sideX ) | |
local atanX = math.atan( tanX ) -- Result in radians | |
local angleX = atanX * 180 / math.pi -- Converted to degrees | |
if ( sideY <; 0 ) then | |
angleX = angleX * -1 | |
end | |
if ( sideX < 0 and sideY < 0 ) then | |
angleX = 270 + math.abs( angleX ) | |
elseif ( sideX < 0 and sideY > 0 ) then | |
angleX = 270 - math.abs( angleX ) | |
elseif ( sideX > 0 and sideY > 0 ) then | |
angleX = 90 + math.abs( angleX ) | |
else | |
angleX = 90 - math.abs( angleX ) | |
end | |
return anglex | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment