Public Group

# Finding Angle Between Two Points

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

## Recommended Posts

I'm having issues with finding the angle between two points. I'm trying to get the player's ship to point towards the cross hairs, or, the cursor. So, I figured if I just found the angle between the two points, or vectors, and set that angle as the ships angle, it would point that way. Anyways, I searched Google and ended up with this:
playerShip.Angle = MathHelper.ToDegrees((float)(System.Math.Atan2(xhairs.Y, xhairs.X) - System.Math.Atan2(playerShip.Y, playerShip.X)));


My first thought is, maybe I'm just plain doing it wrong. My second thought is, I started using a system where the coordinates of my sprites are based on 0.0f to 1.0f, being the left/top and right/bottom sides of the screen respectively. That way, the graphics can be scaled up or down based on the current resolution. Would the problem be caused because of the coordinate system?

##### Share on other sites
There is no angle between 2 points, there is only a line between two points.

However, to find the angle between two normalized vectors you can just use the dot product.

cos(angle) = A dot B;

then solve for angle.

Dot Product
Maybe simpler explanation
more fun

-me

##### Share on other sites
There may not technically be an angle between 2 points, but I manage to derive one all the time :)

PointC=(PointB-PointA)/Distance(PointA,PointB);

if(PointC.Y>=0)
Angle=acos(PointC.X);
else
Angle=-acos(PointC.X);

My only comment is that you have to use a real distance algorithm (instead of fast distance which causes jitter). gl :)

##### Share on other sites
Okay, while I read over those, I'm going to rephrase my question without the things that I thought I knew ;-):

If I have two sprites, and I want one of them to turn so that it is facing the other, what do I do?

##### Share on other sites
Quote:
 Original post by UphoreumIf I have two sprites, and I want one of them to turn so that it is facing the other, what do I do?

Calculate the angle between the "forward" vector of the srite that will turn and the vector pointing from that sprite to the target. then rotate based on that angle.

Generally to make it look good you tune objects to have a maximum turn rate per timeslice. If the angle is greater than that angle, then they just turn their max and try again next frame. that way you get smooth turning.

-me

##### Share on other sites
Hidden
Here's my function for doing exactly the thing you want. :-)

const float pi = 3.14159265f;int GetDegreeToTarget(int xpos1, int ypos1, int xpos2, int ypos2) {   float value;   value = atan2(ypos2 - ypos1, xpos2 - xpos1);   return value * (180 / pi) + 90;}

I'm far from any mathematical expert, so it took me quite a lot of trial and error to get this right. But this will make degree 0 straight up, 90 straight to the east, 180 straight down and 270 straight to the west.

To make two ships face each other, simply:

ship1.rotation = GetDegreeToTarget(ship1.xpos, ship1.ypos, ship2.xpos, ship2.ypos);ship2.rotation = GetDegreeToTarget(ship2.xpos, ship2.ypos, ship1.xpos, ship1.ypos);

....and rotate the ships according to their "rotation" value.

When you want to use sin() and cos(), for moving ships in any direction 0-360 degrees, you have to give the angle as radians instead of degrees. Don't ask me why. :-) To convert degress to angles, you do:

radians = angle_in_degrees * (pi / 180);

Good luck!

Nice! I've never tried a tangential approach, I'll try that :)

##### Share on other sites
I did just like you (as far as I can tell), but for some reason, it still doesn't work:

playerShip.Angle = MathHelper.ToDegrees((float)System.Math.Atan2(xhairs.Y - playerShip.Y, xhairs.X - playerShip.X) * (float)(180 / System.Math.PI) + 90);

The ship spins really fast when I move the cross hairs, so it's not just a matter of it facing the wrong direction, it's not even rotating correctly.

##### Share on other sites
Oh nevermind. That whole thing with the 180 / PI part, was converting it to Degrees, but I already had an XNA function to do that for me, so I was converting it twice, making it not work.

It works now ;-) thanks a lot!

##### Share on other sites
Quote:
 Original post by Kada2k6I'm far from any mathematical expert, so it took me quite a lot of trial and error to get this right. But this will make degree 0 straight up, 90 straight to the east, 180 straight down and 270 straight to the west.

That is probably not the best thing to do because you have to convert from your system to the "real" system every time you want to use an angle. It would be better to stick with the convention that 0° is along the positive X axis and 90° is along the positive Y axis. But of course it is up to you.
Quote:
 Original post by Kada2k6When you want to use sin() and cos(), for moving ships in any direction 0-360 degrees, you have to give the angle as radians instead of degrees. Don't ask me why.

In the math world, radians are more convenient than degrees. Most math APIs use radians, but some use degrees. OpenGL uses degrees and it is pretty annoying having to convert all the time.

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 103
• 11
• ### Forum Statistics

• Total Topics
632975
• Total Posts
3009669
• ### Who's Online (See full list)

There are no registered users currently online

×