Rotate a Vector2 around the Z axis on a mathematical level

alt text

alt text

This is what i want to achieve with a givin Vector2 and a float degree i want
to get the new (“rotated”) Vector2, if this makes anysense at all.

Well all you need is a “2x2 rotation matrix”. This can be easily calculate manually like this:

Vector2 Rotate(Vector2 aPoint, float aDegree)
{
    float rad = aDegree * Mathf.Deg2Rad;
    float s = Mathf.Sin(a);
    float c = Mathf.Cos(a);
    return new Vector2(
        aPoint.x * c - aPoint.y * s,
        aPoint.y * c + aPoint.x * s;
    );
}

This should rotate the point counter clockwise around the origin. If you wan to rotate clockwise you can either use a negative angle or flip the signs of the “sin” component

    return new Vector2(
        aPoint.x * c + aPoint.y * s,
        aPoint.y * c - aPoint.x * s;
    );

ps: I’ve written this from scratch without syntax checking. However it should work that way.

edit

If you want to rotate around a certain point you could do:

Vector2 RotateAroundPivot(Vector2 aPoint, Vector3 aPivot, float aDegree)
{
    aPoint -= aPivot;
    float rad = aDegree * Mathf.Deg2Rad;
    float s = Mathf.Sin(a);
    float c = Mathf.Cos(a);
    return aPivot + new Vector2(
        aPoint.x * c - aPoint.y * s,
        aPoint.y * c + aPoint.x * s;
    );
}