Answers for "Transform angle to grid position"
http://answers.unity.com/questions/369599/transform-angle-to-grid-position.html
The latest answers for the question "Transform angle to grid position"Answer by Golan2781
http://answers.unity.com/answers/371167/view.html
You can determine the relative angular direction of two vectors in the XY-plane using the cross product's Z component (respectively for permuted coordinates). This is apparent if you think of the cross product relation in calculating a surface normal of two vectors or a rotation. If you know you are only working with a 2D plane, this calculation is enough:
var clockwise = true;
if (vec1.y*vec2.x > vec1.x*vec2.y)
clockwise = false;
If you only need world coordinates (i.e. direction 'up' instead of 'forward') you already know one vector for sure (e.g. 'up' is 0,1) so the calculation gets really simple - check whether the horizontal axis (whichever corresponds to -90° and +90°) is positive or negative
Of course, there are other ways to achieve what you want. Assuming you only need world directions:
The arctan of the relative (x/y) will give you the proper angle with proper sign.
You can alternatively try and catch the respective cases directly from the relative coordinates. For example, if y<0 it must be one of the lower fields; you can do this for all coordinates and directons. It would be adviseable to do some kind of normalizing between the two directions, so that for example -9/9 is the same as 1/1 and you probably want treat intermediate values such as 4/3 or 1/0.75 as 'upper right'.
Personally, I'd go for the manual cross product, it should be the most performant solution.Sat, 29 Dec 2012 19:55:06 GMTGolan2781Answer by whydoidoit
http://answers.unity.com/answers/369653/view.html
So you should just be able to take 22.5 from the angle (which is 360/(8*2)), if the result is less than 0 then add 360, then divide the answer by 45 and take the integer as a lookup into an array of coordinates. However Angle returns the angle between a line and another line - which can never be more than 180 degrees. So you also need to know on which side of the red dot, the green dot lies so you can add to the angle.
CS
//Define the angle pairs as shown in your diagram, starting from upwards
Vector2[] angles = new [] { new Vector2(1,0), new Vector2(1,-1), new Vector2(0,-1) ... }
//Get the angle
var angle = targetDir.x < 0 ? 180f + (180f - Vector3.Angle(targetDir, forward)) : Vector3.Angle(targetDir, forward);
//Convert it into an index
var angleIndex = Mathf.FloorToInt((angle < 22.5f ? 360f + angle - 22.5f : angle - 22.5f)/45);
//Get the directions
var direction = angles[angleIndex];Thu, 27 Dec 2012 08:01:26 GMTwhydoidoit