0

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.

user2236165
  • 741
  • 2
  • 11
  • 24
  • 1
    i hope the answer here (http://stackoverflow.com/questions/13458992/angle-between-two-vectors-2d) will be useful. – Priyank Aug 28 '14 at 10:20

3 Answers3

1

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.

Roy T.
  • 9,429
  • 2
  • 48
  • 70
0

I think you may be looking for the Vector2.Dot method which is used to calculate the product of two vectors, and can be used for angle calculations.

For example:

// the angle between the two vectors is less than 90 degrees. 
Vector2.Dot(vector1.Normalize(), vector2.Normalize()) > 0 

// the angle between the two vectors is more than 90 degrees. 
Vector2.Dot(vector1.Normalize(), vector2.Normalize()) < 0 

// the angle between the two vectors is 90 degrees; that is, the vectors are orthogonal. 
Vector2.Dot(vector1.Normalize(), vector2.Normalize()) == 0 

// the angle between the two vectors is 0 degrees; that is, the vectors point in the same direction and are parallel. 
Vector2.Dot(vector1.Normalize(), vector2.Normalize()) == 1 

// the angle between the two vectors is 180 degrees; that is, the vectors point in opposite directions and are parallel. 
Vector2.Dot(vector1.Normalize(), vector2.Normalize()) == -1 

Is this what you're looking for, or do you need the exact angle?

Nahuel Ianni
  • 3,177
  • 4
  • 23
  • 30
  • Thank you for your answer, but I do need the exact angle. – user2236165 Aug 28 '14 at 10:23
  • Vector2.Dot(vector1.Normalize(), vector2.Normalize()) should give the cos of the angle... – Dennis_E Aug 28 '14 at 10:38
  • I read that the reference point in which XNA calculates angles is (0,0). So if my sprite is in the upper left corner of the screen, the angle between Vector 1 and 2, is 90 degrees. However if I move the sprite around the angle changes according to the reference point. Is it possible to change this reference point? Because the angle between the two points and my sprites center is still 90 degrees, even tough the calculated angle according to the reference point is something else. – user2236165 Aug 28 '14 at 12:19
  • WHile looking for the answer above, I saw different forums with the same problem. No solution was given to them when asking for changes on the reference point. – Nahuel Ianni Aug 28 '14 at 12:27
0

If I understand your question diagram and comments, the Dot product and Acos are not the only bits of info you need. You also need to account for when the sprite is not located at (0,0).

float angleInRadians = (float) Math.Acos(Vector2.Dot(Vector2.Normalize(vector1 - spritePosition), Vector2.Normalize(vector2 - spritePosition)));

int angleInDegrees = MathHelper.ToDegrees(angleInRadians);
Steve H
  • 5,479
  • 4
  • 20
  • 26