Faulty Angle Function

BlueMirage

Trust, but doubt.
Reaction score
39
I'm using a simple function to determine the angle between two points. You should be able to recognize it.

JASS:
function Angle takes real x1, real y1, real x2, real y2 returns real
    local real r = bj_RADTODEG * Atan2(y2 - y1, x2 - x1)
    call BJDebugMsg(R2S(r))
    return r
endfunction


However, for some odd reason this gives me faulty values when r > 180 degrees. If I for example should get 190 degrees, I get -170. 200 degrees gives me -160. 350 degrees gives me -10.

Anybody have an idea why?
 

Romek

Super Moderator
Reaction score
963
-170 and 190, 350 and -10, etc are equal in terms of degrees; so your values are not 'faulty'.
If you insist on a value between 0 and 360 though, you could use:

JASS:
function Angle takes real x1, real y1, real x2, real y2 returns real
    local real r = ModuloReal(bj_RADTODEG * Atan2(y2 - y1, x2 - x1), 360.)
    call BJDebugMsg(R2S(r))
    return r
endfunction
 

Trollvottel

never aging title
Reaction score
262
As Romek said, Atan2 returns values from -180 to 180 degrees ( or -pi to pi)...

if you want to have angles from 0 to 360 degrees you have to use

if r < 0 then
set r = 360 + r
endif
 

BlueMirage

Trust, but doubt.
Reaction score
39
Well, this explains why my spells have been malfunctioning half of the time.
I appreciate it. +rep
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top