🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Finding an angle given two points

Started by
1 comment, last by David20321 24 years ago
If I know the location of two points on a 2d plane, how do i find the angle from one to the other? Thnxs.
Advertisement
Okay, given that tan(x) = sin(x) / cos(x), we can do this (in C):

angle = atan((y2 - y1) / (x2 - x1));

/* Correct negative angle. (For a 360-degree system.) */
if (x2 < x1)
angle += 180;
else if (x2 == x1)
{
if (y1 < y2)
angle = 90;
else
angle = 270;
}

Note that (x1,y1) is the thing you want to turn towards (x2,y2).
Thank you so much! I had to tweak a little bit to get

angle = atan((x2 - x1) / (z2 - z1))/6.28*360+180;
if (z2 < z1)
angle += 180;
else if (z2 == z1)
{
if (x2 < x1)
angle = 90;
else
angle] = 270;
}

But i would never have gotten it to work without your help.

This topic is closed to new replies.

Advertisement