Math.atan2

From GiderosMobile
Revision as of 09:01, 25 October 2020 by MoKaLux (talk | contribs) (added example)

Available since: Gideros 2011.6
Class: math

Description

Returns the arc tangent of v1/v2 (in radians), but uses the signs of both parameters to find the quadrant of the result (it also handles correctly the case of v2 being zero).

(number) = math.atan2(v1,v2)

Parameters

v1: (number) first value
v2: (number) second value

Return values

Returns (number) the angle in radian

Example

Aiming at an enemy

local playerX, playerY = player:getPosition()
local nmeX, nmeY = nme:getPosition()
-- here we get the nme angle relative to the player
local angle = math.atan2(nmeY - playerY, nmeX - playerX) -- the Ys first!
-- now we can target the nme
missile:setPosition(15 * math.cos(angle), 15 * math.sin(angle))
-- ...