Calculate the angle from velocity?
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.
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
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
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]
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 );
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 );
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement