# Fastest angle calculation from xy coordinates (vector)

This topic is 3367 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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.

##### Share on other sites
Like the guy above said, more info helps. Also, you need a relative vector to compute an angle from (the positive x-axis, etc).

##### Share on other sites
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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 12
• 15
• 11
• 12
• ### Forum Statistics

• Total Topics
634152
• Total Posts
3015842
×