# 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.

## 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 on other sites
And in actual code or in english?

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

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 14
• 14
• 9
• ### Forum Statistics

• Total Topics
633370
• Total Posts
3011542
• ### Who's Online (See full list)

There are no registered users currently online

×