C# – Find Angle Between Two Vectors

anglecxna

I have read some of the duplicate answers about angle between two vectors, but I'm still stuck with my problem. I have two vectors and I want that the angle between them to always be 90 degrees. To achieve that I need to find the angle between them, so that I can subtract or add the correct amount of degrees so that the angle between them always is 90 degrees.

enter image description here

The picture illustrates a sprite and two vectors. How do I find the angle A between them two? I have tried to use this code to get the angle between two vectors, but I must have missed something out, because I don't get the correct results:

        public float GetAngleBetween (Vector2 A, Vector2 B)
    {
        float DotProd = Vector2.Dot (A, B);
        float Length = A.Length () * B.Length ();
        return (float)MathHelper.ToDegrees ((float)Math.Acos (DotProd/Length));
    }

Any input is welcome and thank you in advance for any answers.

Best Answer

The actual angle in radians is

Math.ACos(Vector2.Dot(a, b));

Make sure that a and b are normalized vectors or the results can get pretty weird.

Related Topic