Fastest angle calculation from xy coordinates (vector)

Started by
2 comments, last by Wes22 14 years, 5 months ago
I really don't know what the fastest way to do it is. I'm guessing it's by using tan inverse since no square root is needed. Is atan(y/x) the fastest?
Advertisement
The fastest is not computing it. I bet if you tell us what you are trying to do with the angle we can come up with a faster, cleaner solution that doesn't use angles.

Barring that, atan2(y,x) is probably your best bet.
Like the guy above said, more info helps. Also, you need a relative vector to compute an angle from (the positive x-axis, etc).

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Like it has already been said, it depends on what you want to do, and you may be able to avoid angle calculation entirely. However, I recently had a case where I didn't need much precision (whole degrees only), so I just made an init function that creates a simple table with the output of tan() for up to 1-179 degrees. I do that just once per program run, then I have thousands of calls to my FindAngle() function, which searches those pre-generated results for the nearest match to the result of X/Y, with some quadrant adjustments to know when to flip it to degrees 180-359, and to fix degree 0 & 180 weirdness.

It's fairly straight-forward, fast, and in my case, also saves me the overhead of converting floating-point to integer and vice-versa (since the code this supports is all integer). And not to mention, there's no more need to deal with radians after that table is generated.

I'm using my angle calculation to determine which texture to draw for objects in an isometric game... Assuming that the textures are just for angles 0, 90, 180, and 270, there would be a much faster and simpler way to figure out which one to use, but I specifically want to be able to inject textures for any given angle, so this is what I have to do to enable that.

This topic is closed to new replies.

Advertisement