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]