• Advertisement
Sign in to follow this  

[Problem] Angle between two points

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement