# [Problem] Angle between two points

## 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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628320
• Total Posts
2982072

• 21
• 9
• 9
• 13
• 11