Jump to content
  • Advertisement
Sign in to follow this  
yahn

Fastest angle calculation from xy coordinates (vector)

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!