Math.atan2: Difference between revisions

From GiderosMobile
No edit summary
m Text replacement - "<source" to "<syntaxhighlight"
Line 5: Line 5:
=== Description ===
=== 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).
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).
<source lang="lua">
<syntaxhighlight lang="lua">
(number) = math.atan2(v1,v2)
(number) = math.atan2(v1,v2)
</source>
</source>
Line 18: Line 18:
=== Example ===
=== Example ===
'''Aiming at an enemy'''
'''Aiming at an enemy'''
<source lang="lua">
<syntaxhighlight lang="lua">
local playerX, playerY = player:getPosition()
local playerX, playerY = player:getPosition()
local nmeX, nmeY = nme:getPosition()
local nmeX, nmeY = nme:getPosition()

Revision as of 13:28, 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). <syntaxhighlight lang="lua"> (number) = math.atan2(v1,v2) </source>

Parameters

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

Return values

Returns (number) the angle in radian

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)) -- ... </source>

(can be handy: https://math.stackexchange.com/a/2587852)