Jump to content
  • Advertisement
Sign in to follow this  
CuppoJava

Fast way to compare angles.

This topic is 4870 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

Hey I just realized this today and thought I'd share this trick. If you have a vector A, and you want to see if another vector B is within x degrees of it you can use this trick. Instead of going: float angle1 = arctan A float angle2 = arctan B if( |angle1-angle2| < x) //bla bla You can go: A.normalize B.normalize if( A dot B < cos x) //bla bla Caching the cosine makes the entire operation about twenty times faster. I haven't tried caching the squareroot yet. Without caching, the operation below is still 4 times faster. -Cuppo

Share this post


Link to post
Share on other sites
Advertisement
Yea that's the basic idea, except for most of the time you don't know the length of the vector which is why I had the two normalize statements.
-Cuppo

Share this post


Link to post
Share on other sites
Well since normalization is v / v.length the length ought to either be known or easily computed basicly my method saves you the divisions / multiplications that normalization would give per element.

Share this post


Link to post
Share on other sites
You might be able to skip the square root completely if you square both sides. Of course this introduces the problem that angles that are opposite (cos = -1) will look like right on top of each other, since the sign of the dot product would be lost. Anyway, depending on the situation you might be able to rule out that possibility, or check for the sign of the product (as long as cos x > 0).

Share this post


Link to post
Share on other sites
That's nice. I didn't think of that.
So once the cosine is cached, this operation will be blazing.
Thx for that tip avaro.
-Cuppo.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!