Advertisement Jump to content
Sign in to follow this  

find direction from point A to point B

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

I'm not very good at math, but heres what I'd like to do ...

I have an object located on a 2d grid, so I know its current point. I get direction by moving the point a short distance in the forward direction so I know where its pointing. My target location is a secondary point. My goal is to find direction to the next point.

So basically, I just want to turn my object to the correct angle and move it to a specified location.

Can anyone help w/ how this is done?

Share this post

Link to post
Share on other sites
The direction that goes from point A to point B is indicated by the vector V = B - A. It is convenient to represent a direction by a unit-length vector, so you may want to normalize that vector.

An alternative representation of a direction in 2D is an angle (typically measured from the positive x axis and going counterclockwise). You can obtain that as

angle = atan2(V.y, V.x)

But, as has been mentioned many times in several recent threads, you can usually avoid using the angle because most of the time all you want to do is compute its sine and cosine, which you already know (cos(angle) = V.y and sin(angle) = V.x, if V has been normalized ). Using unit vectors (or unit complex numbers, which is almost the same thing) instead of angles often requires fewer special cases, and the resulting code tends to be cleaner, faster, easier to read and easier to get right.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. 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!