If you're working with Vector2's in XNA, or any other platform really, some functions you'd think should be on a vector object in your given framework just aren't. With XNA, it was the ability to get an angle from a vector. So I made one. This returns the angle the Vector2 is currently at.
1: public static float GetAngle(Vector2 vector)
2: { 3: float x = vector.X;
4: float y = vector.Y;
5: float angle = 0f;
6:
7: //Gets Angle
8: if (Math.Abs(x) < Math.Pow(10, -6))
9: { 10: if (Math.Abs(y) < Math.Pow(10, -6)) { angle = 0f; } 11: else if (y > 0) { angle = 90f; } 12: else if (y < 0) { angle = 270f; } 13: }
14: else if (x > 0)
15: { 16: if (y >= 0) { angle = (float)(180 * Math.Atan(y / x) / Math.PI); } 17: else if (y < 0) { angle = (float)(180 * Math.Atan(y / x) / Math.PI - (-1) * 360); } 18: }
19: else if (x < 0)
20: { 21: if (y >= 0) { angle = (float)(180 - 180 * Math.Atan(-y / x) / Math.PI); } 22: else if (y < 0) { angle = (float)(-180 - (-1) * 180 * Math.Atan(y / x) / Math.PI - (-1) * 360); } 23: }
24:
25: return angle;
26: }
0 comments:
Post a Comment