Difference between revisions of "Math.atan2"
From GiderosMobile
m (Text replacement - "</source>" to "</syntaxhighlight>") |
|||
(6 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
− | ''' | + | '''Available since:''' Gideros 2011.6<br/> |
− | ''' | + | '''Class:''' [[math]]<br/> |
− | === | + | |
− | + | === 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). |
+ | <syntaxhighlight lang="lua"> | ||
(number) = math.atan2(v1,v2) | (number) = math.atan2(v1,v2) | ||
− | </ | + | </syntaxhighlight> |
− | === | + | |
− | '''v1''': (number) | + | === Parameters === |
− | '''v2''': (number) | + | '''v1''': (number) first value<br/> |
− | === | + | '''v2''': (number) second value<br/> |
− | ''' | + | |
+ | === Return values === | ||
+ | '''Returns''' (number) the angle in radian<br/> | ||
+ | |||
+ | === Example === | ||
+ | '''Aiming at an enemy''' | ||
+ | <syntaxhighlight lang="lua"> | ||
+ | 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)) | ||
+ | -- ... | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | (can be handy: https://math.stackexchange.com/a/2587852) | ||
+ | |||
+ | {{Math}} |
Latest revision as of 14:30, 13 July 2023
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))
-- ...
(can be handy: https://math.stackexchange.com/a/2587852)
- Math
- Math.abs
- Math.acos
- Math.asin
- Math.atan
- Math.atan2
- Math.ceil
- Math.clamp
- Math.cos
- Math.cosh
- Math.cross
- Math.deg
- Math.distance
- Math.distances
- Math.dot
- Math.edge
- Math.exp
- Math.fft
- Math.floor
- Math.fmod
- Math.frexp
- Math.ifft
- Math.inside
- Math.ldexp
- Math.length
- Math.log
- Math.log10
- Math.max
- Math.min
- Math.modf
- Math.nearest
- Math.noise
- Math.normalize
- Math.pow
- Math.rad
- Math.random
- Math.randomseed
- Math.raycast
- Math.round
- Math.sign
- Math.sin
- Math.sinh
- Math.sqrt
- Math.tan
- Math.tanh