Reputation: 31
I'm trying to use the math.library on Lua (version 5.4) and I tried to use the math.atan2 here:
math.randomseed (os.time())
local Player_x, Player_y = 400,300
local Enemy_x = math.random (0,800)
local Enemy_y = math.random (0,600)
local angle = math.atan2((Enemy_y - Player_y), (Enemy_x - Player_x))
local angle_degree = math.deg (angle)
print (Enemy_x..","..Enemy_y)
print ("The angle from Enemy to the Player: "..angle_degree.." degree.")
But when I tried to put run on my Visual Studio Code, it said that it was "discontinued" and deprecated. Is there a sort of way to fix this problem or is there a simple substitute of this situation?
Upvotes: 3
Views: 5928
Reputation: 166
the range of atan() is pi/2(up) to -pi/2(down) wich is half the circle atan2() is pi to -pi wich is the whole circle
if you only use tan a extra value has to be added on
angle = math.atan((Enemy_y-Player_y)/(Enemy_x-Player_x));
if(Enemy_x<= Player_x and angle < 0)then--4th qudrent
angle = (2*math.pi) + angle;
elseif(angle < 0) then--2nd qutial
angle = math.pi+angle;
elseif(angle >= 0 and Enemy_x > Player_x)then
angle = angle + (math.pi);--3rth
end
x increases ->
Upvotes: 0
Reputation: 43
Be aware that math.atan is not a drop-in replacement for math.atan2. math.atan2(2,2) ==> 0.7859 vs math.atan(2,2) ==> 1.1071 Whatever the difference is: they're not the same. – Ian Boyd
For anyone running into this like me: Make sure you are actually using lua 5.3 or above. In 5.2 the second argument is simply ignored, so writing math.atan(2, 2)
is equal to writing math.atan(2)
, hence the difference.
Upvotes: 3
Reputation: 28974
math.atan2
was deprecated in Lua 5.3
https://www.lua.org/manual/5.3/manual.html#8.2
The following functions were deprecated in the mathematical library: atan2, cosh, sinh, tanh, pow, frexp, and ldexp. You can replace math.pow(x,y) with x^y; you can replace math.atan2 with math.atan, which now accepts one or two arguments; ...
local angle = math.atan((Enemy_y - Player_y), (Enemy_x - Player_x))
Upvotes: 5