Vector Rotation
Dieser Beitrag steht auch in Deutsch zur Verfügung, schaue ihn dir hier an!

Step 1: Rotationdefinition
As Base we are using the Dot Product for the calculation:

For
we are using
(which has a Rotation of 0). Therefore we have got following simplification:



Because we've got an Square Root in the calculation these values are only for positive Y-Values of the Vector
. To implement negative Y-Values we just simply negate the result whether we have a negative Y-Value.
Step 2: Code Implementation
The Implementation is in C#/XNA very simple because the length of the Vector is given through the .Length method:
rotation = (float)Math.Acos(b.X / (b.Length()));
if (b.Y < 0)
rotation = -rotation;
Step 3: Extensions
To simplify the access to the Method we implement the formula as Extension (siehe Extensions) of the Vector2 struct:
public static class Vector2Extensions //Remark: Static Class
{
///
/// Returns the Angle of the Vector2
public static float GetRotation(this Vector2 v)
//Remark: Static Method and first Parameter must be >>this Vector2<<
{
float rotation;
rotation = (float)Math.Acos(v.X / (v.Length()));
if (v.Y < 0)
rotation *= -1;
return rotation;
}
}
With this code we can directly acces the Rotation as follows:

written by Marvin Pohl aka MindYourByte



