🎉 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
If I know the location of two points on a 2d plane, how do i find the angle from one to the other? Thnxs.
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).
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).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement