# [Problem] Angle between two points

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

## Recommended Posts

Hello everyone.

A couple months ago I made a thread asking for a way to find an angle between two points

The code I use works for n,s,w,e, but when I use 8 directions, it brings me NaN as the angle between the two points. The math between the code is something I dont really understand, that is why I ask for your help.

Just one little explanation, the Y axis is inverted due to the fact that the screen coordinates go from up left to down right.

Sorry for my shitty English.

            if (facing == Direction.Down)                orientation = new Vector2(0, 1);            else if (facing == Direction.Up)                orientation = new Vector2(0, -1);            else if (facing == Direction.Right)                orientation = new Vector2(1, 0);            else if (facing == Direction.Left)                orientation = new Vector2(-1, 0);            else if (facing == Direction.UpLeft)                orientation = new Vector2(-1, -1);            else if (facing == Direction.UpRight)                orientation = new Vector2(1, -1);            else if (facing == Direction.DownLeft)                orientation = new Vector2(-1, 1);            else if (facing == Direction.DownRight)                orientation = new Vector2(1, 1);            Vector2 diff = c.position - position;            diff.Normalize();            float angle = (float)MathHelper.ToDegrees((float)Math.Acos(Vector2.Dot(orientation, diff)));

[Edited by - arthurviolence on December 8, 2010 5:41:06 PM]

##### Share on other sites
This is my first time posting here, so sorry if I miss something obvious.

First of all it looks like you are using C# and XNA, next time mention what language you are using.

The 'diff' vector that your created represents the change in each axis required to go from position to c.position, therefore it can represent the angle from position to c.position. Also, from the primary trig ratios we know tan(angle) = opposite/adjacent, or in this case tan(angle) = y/x, therefore angle = arctan(y/x). Because tan will be the same for angles in opposite quadrants, you can only get angles in the range (90, -90), and theres also undefined values at certain spots. Fortunately, people have used this method enough that they programed a version of tan that will take care of these problems for you, called Atan2. So to find an angle, you can simply use

Vector2 diff = c.position - position;
float angle = (float)MathHelper.ToDegrees(Math.Atan2(diff.y, diff.x));

##### Share on other sites
Sorry for not including the language, I interpreted that the class names where expressing everything clearly, but I should indeed always put the language.

I appreciate your reply, explaining everything in a way that I actually understood, theres nothing worse than using a method without understanding how it works.

Thank you for the help.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633287
• Total Posts
3011217
• ### Who's Online (See full list)

There are no registered users currently online

×