Jump to content
  • Advertisement
Sign in to follow this  
SAE Superman

Getting direction of movement from x, y velocity vectors

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

Ok, so I've been out of school for far too long and I see I've become a little rusty on my math [grin]. Here's my problem...I have an object with an x_vel component and a y_vel component and I need to derive the direction from them (0-360 degrees with 0 being straight North). I need it to be able to handle all four quadrants and you can of course have zero for either the x_vel or y_vel components...also, x is positive right and y is positive up. Thanks in advance!!

Share this post


Link to post
Share on other sites
Advertisement
[edit] If you have the function atan2, just use
angle = atan2(-x_vel, y_vel)

This will give you an answer in Radians.

You don't need to use the extra code jyk uses; you can trick the function into doing that for you with no extra work by passing in the arguments that way. This will return a value in radians, though; be sure to multiply by 180/pi if you want degrees.

Otherwise, you can make your own direction->angle function.


double dir2angle(double x, double y)
{
const PiOver2 = 1.570796
if(y==0)
return (x > 0)?3*PiOver2:PiOver2
return ((y > 0.0)?2*PiOver2:0.0) - atan(x/y)
}

This will return an answer in radians based on the positive y axis = 0, but otherwise it's the same as atan2.


The reason that we have these weird correction factors is that most people define the angle as starting along the positive x axis and going clockwise. There's nothing wrong with defining it differently in your program, but if you do, you'll have to account for that difference when looking at other people's code. Also, most languages use radians in their standard libraries, so if you use degrees you'll have to convert back and forth often. (I just edited my examples to use radians instead of degrees.)

[Edited by - nagromo on May 7, 2006 10:05:59 PM]

Share this post


Link to post
Share on other sites
Assuming C++:
angle = atan2(y_vel, x_vel);
angle = RadToDeg(angle); // You have to supply RadToDeg()
angle -= 90.0f;
if (angle < 0.0f) {
angle += 360.0f;
}
I think that should cover it (although I may have made a mistake somewhere).

If you're working in a language that doesn't have atan2() or an equivalent function, you may have to get more involved in the details (maybe that's what nagromo is going to post).

Share this post


Link to post
Share on other sites
Thanks guys...I didn't design what I have to program for, so I have to live with the data that's given to me. The reason why the y-axis has to be zero is that the angle derived is the "heading" of an object (0 = North, 90 = East, etc).

Thanks again for the help [grin]

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!