Jump to content
  • Advertisement
Sign in to follow this  
d000hg

Get rotation angle from vector

This topic is 4831 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 feel pretty stupid as a maths graduate, but I'm having one of those moments when you just get confused by the simplest things... From a 2D vector I want to get a rotation angle. The positive x-axis is 0. The answer should be in the range [0,2PI] - the full circle must be covered not just 1/4 or 1/2. I think the positive y-axis will be PI/2, but maybe it's -PI/2 - again this is not a problem since it's just convention which way is which. So, can someone give a simple function for this declaration:
float GetAngleFromVector(Vector2D &vec);
Technically you should normalise vec - I don't care but maybe this would be a useful thing for others but me; it's not THAT trivial is it?

Share this post


Link to post
Share on other sites
Advertisement
angle = atan2(vec.y, vec.x);

No normalization required; defined for all but the (near) zero vector. The angle is returned in the range [-pi,pi], but you can easily adjust that if desired. pi/2 will be along the +y axis.

Share this post


Link to post
Share on other sites
DOH!!!

Thanks a lot though, I'm not afraid to admit such a simple thing confused me and you saved me some time.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!