Jump to content
  • Advertisement
Sign in to follow this  
biscon

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


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


1. Make your image.
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 this post


Link to post
Share on other sites
Thanks for your replies guys, you have been very helpful :)

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 this post


Link to post
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.

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!