Jump to content
  • Advertisement
Sign in to follow this  
digital_phantom

Calculate the angle from velocity?

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

Is it possible to calculate the angle from the velocity? I already know how to get the speed from the velocity by calculating the magnitude sqr(x * x + y * y), so all I need to know know is how to get the angle.

Share this post


Link to post
Share on other sites
Advertisement
Angle between vectors formula:

u · v = |u||v| cos θ

where u and v are the vectors, and θ is the angle between them.

|u| means magnitude of vector u
and u · v is a dot product, not a multiplication

Share this post


Link to post
Share on other sites
What hmmz said. In code:
atan2( y, x );
Or if your language does not specify atan2:
if( x != 0 )
angle = atan( y / x );
else
angle = ( y < 0 ? -M_PI : M_PI );
EDIT: Oops, too late to see hmmz second post! [smile]

Share this post


Link to post
Share on other sites
Thanks everyone,

actually my language doesn't specify atan2 (using VB6).

The whole 'angle = ( y < 0 ? -M_PI : M_PI );' doesn't work currectly when x is zero.

when x is zero, instead of heading down, the angle returns as heading to the left.

What actually works is this: angle = ( y < 0 ? PI * 1.5 : 0 );

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!