# Getting the angle between to set of coords

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

## Recommended Posts

Hey people I suck at math so I figure one you should be able to help me pretty easily. Problem is this: Im doing this 2d game where you can tell the main character to walk to another coordinate. Inorder to animate him while walking I need to know if he is traveling up, down, left or right (I need to know his direction). What I know is: -Location lx,ly -Destination dx,dy Can someone tell me: How I get the angle of the destination seen from current location? To people telling me to RTFM and google for it. I tried but couldnt find anything probably because I didnt know what to search for :)

##### Share on other sites
angle = atan2(dy-ly, dx-lx);

An angle of zero will be directly along the positive x axis. If you want a different default orientation, you can swap and/or negate the arguments to atan2(), or add some multiple of pi/2 to the result.

##### Share on other sites
thanks mate :)

wasn't expecting such a quick reply

##### Share on other sites
Man.... I WISH I knew how to send pictures on this thing!!! (I don't use these Forums much)

Maths is SOOO much easier with diagrams LOL... Oh well...Here goes:

ONE formula you could use is: (Real shame I can't proove to you with the aid of a diagram)

Angle = arctan ((dy - ly) / (dx - lx))

Not sure what math library you use... But one is the 'atan' funtion in the Win32 API, whose syntax, in this context, would be as follows;

Angle = atan ((dy - ly) / (dx - lx))

Or as jyk has previously shown you, the 'atan2' funtion whose syntax is as follows:

Angle = atan2 ((dy - ly), (dx - lx))

Personally, I wager that 'atan' would be a bit faster as only ONE argument is popped onto the stack when it is called AND that that argument would be the result of the division;

(dy - ly) / (dx - lx)

which I'm assuming 'atan2' would have to work out itself prior before proceeding.

Hope this helps :-)

##### Share on other sites
Quote:
 Original post by TerrorFLOPMan.... I WISH I knew how to send pictures on this thing!!! (I don't use these Forums much)

2. Upload it on this site.
3. Copy and paste the "Hotlink for websites" url and paste into a reply. (If the image is huge, please be considerate of 56k'ers and use the "Thumbnail for websites" instead)

##### Share on other sites
Quote:
 which I'm assuming 'atan2' would have to work out itself prior before proceeding.

Yes, that's actually what atan2 does (y/x), however it doesn't get stumped if 'x' is zero (division by zero). If you were to use atan, you would have to include a special case for if (dx - lx) = 0.

http://www.cplusplus.com/ref/cmath/atan2.html

##### Share on other sites

by using atan2(dy-ly, dx-lx) (and converting to degrees, just for testing since im to dumb to think in radians) I get a very strange "degree system":

         -90          |          |180 ------------- 0          |          |          90

Hope the BB software doesnt screw my ansi art.
Doesn't matter though, I can work with it.
Just find it a bit bizarre.

Is it because im just trowing screen coords at it? (with a y-axis going down instead of up?).
How do I flip my y axis? by negating the y-coords?.

Another thing the atan2() function is designed to work with numbers between -1 and 1 right?.

I can work with the function as it is, actually getting nice "normal" degrees would
only make the program slower since I would need to do the conversion. Just asking
out of curiosity so I can hopefully learn a little, and get a bit better at trig :)

##### Share on other sites
Quote:
 Is it because im just trowing screen coords at it? (with a y-axis going down instead of up?).How do I flip my y axis? by negating the y-coords?.
What you posted is correct given +y-down. atan2() takes as arguments the (possibly scaled) sine and cosine of an angle (in that order). Thinking about the trig will show why you're getting those results.

If you want a different default orientation, you can adjust the arguments to atan2(), or adjust the result directly. Again, thinking about the trig will show you what to do. For example, if you want +90 degrees to be up instead of down, you could negate the first argument to atan2().
Quote:
 Another thing the atan2() function is designed to work with numbers between -1 and 1 right?.
Nope, it doesn't have that restriction. You can think of the arguments to atan2() as k*sin(theta) and k*cos(theta). The only (reasonable) input the function can't handle is when both arguments are (near) zero.
Quote:
 I can work with the function as it is, actually getting nice "normal" degrees would only make the program slower since I would need to do the conversion.
If you're worrying about the impact of adding a negation or an addition here and there, you're probably focusing in the wrong place. There's a lot of good advice on optimization available which I won't repeat here, but in short, don't worry about your 'find angle' function - it's not going to be your bottleneck.

1. 1
Rutin
31
2. 2
3. 3
4. 4
5. 5

• 12
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633314
• Total Posts
3011323
• ### Who's Online (See full list)

There are no registered users currently online

×