I am trying to work out the angle between two objects, so one can fire a projectile at the other. From trigonometry I know the angle is tan-1(opposite/adjacent), but I cant find an inverse tan function anywhere. How else could I work out the angle?
atan? math.atan , math.atan2
Return the inverse tangent. We can do this by supplying y/x ourselves using math.atan or we can pass y and x to math.atan2 to do this for us.
\> c, s = math.cos(0.8), math.sin(0.8) \> = math.atan(s/c) 0.8 \> = math.atan2(s,c) 0.8
You can use math.atan2 as in the following example
local obj1 = display.newRect(100,100,50,50) local obj2 = display.newRect(200,200,50,50) local deltax = obj2.x - obj1.x -- find the difference of x coords local deltay = obj2.y - obj1.y -- find the difference of y coords local angle = math.atan2(deltay,deltax)\*(180/math.pi) -- radian angle converted to degrees using 180/pi print (angle.. " degrees") -- angle of obj1 in relation to obj 2
atan? math.atan , math.atan2
Return the inverse tangent. We can do this by supplying y/x ourselves using math.atan or we can pass y and x to math.atan2 to do this for us.
\> c, s = math.cos(0.8), math.sin(0.8) \> = math.atan(s/c) 0.8 \> = math.atan2(s,c) 0.8
You can use math.atan2 as in the following example
local obj1 = display.newRect(100,100,50,50) local obj2 = display.newRect(200,200,50,50) local deltax = obj2.x - obj1.x -- find the difference of x coords local deltay = obj2.y - obj1.y -- find the difference of y coords local angle = math.atan2(deltay,deltax)\*(180/math.pi) -- radian angle converted to degrees using 180/pi print (angle.. " degrees") -- angle of obj1 in relation to obj 2