# Getting direction of movement from x, y velocity vectors

This topic is 4642 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
 If you have the function atan2, just use
angle = atan2(-x_vel, y_vel)

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 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 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]

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 31
• 16
• 11
• 10
• 12
• ### Forum Statistics

• Total Topics
634118
• Total Posts
3015602
×

## Important Information

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!