I understand that:

atan2(vector.y, vector.x) = the angle between the vector and the X axis.

But I wanted to know how to get the angle between two vectors using atan2. So I came across this solution:

atan2(vector1.y - vector2.y, vector1.x - vector2.x)

My question is very simple:

Will the two following formulas produce the same number?

  • atan2(vector1.y - vector2.y, vector1.x - vector2.x)

  • atan2(vector2.y - vector1.y, vector2.x - vector1.x)

If not: How do I know what vector comes first in the subtractions?


enter image description here

  • \$\begingroup\$ You know you could always try the two formulas and see if they produce the same result. \$\endgroup\$
    – House
    Jan 31, 2014 at 15:53
  • \$\begingroup\$ My question was actually how to know which vector should come first in the subtractions \$\endgroup\$ Jan 31, 2014 at 15:53
  • \$\begingroup\$ Think about it like this: End - Start = Vector so that Start + Vector = End \$\endgroup\$
    – MickLH
    Jan 31, 2014 at 16:05
  • \$\begingroup\$ What quadrant is B in relation to A in the picture? And how does velocity have an affect on that? \$\endgroup\$
    – AturSams
    Jan 31, 2014 at 17:53
  • \$\begingroup\$ It should be the blue one. You will get a positive or a negative value depending on which one is further CCW in rotation. If you want to adjust the velocity and need to know the angle that needs to be added, it will be atan2(fromAtoB.y, fromAtoB.x) - atan2(velocityA.y, velocityA.x) \$\endgroup\$
    – AturSams
    Jan 31, 2014 at 18:27

1 Answer 1


The Red is:

atan2(vectorA.y - vectorB.y, vectorA.x - vectorB.x)

The Green is:

atan2(vectorB.y - vectorA.y, vectorB.x - vectorA.x)

The Blue which I think is what you are looking for:

atan2(vectorA.y, vectorA.x) - atan2(vectorB.y, vectorB.x)

You can use abs() if you want the absolute value like I think you do. Sometimes you will get a value that is nearly 2 * PI (greater than 1 * PI), in this case subtract 2 * PI and use abs() again.

enter image description here

  • \$\begingroup\$ Thanks. I'm actually checking the angle between the velocity vector of an object (let's call that object A), and a vector from that object (A) to another object (let's call that object B). So, I do need the signed value of the angle, in order to know what quadrant B is in relation to A. Here's a question: When doing atan2(velocityA.y, velocityA.x) - atan2(fromAtoB.y, fromAtoB.x), I get a number. But when doing atan2(fromAtoB.y, fromAtoB.x) - atan2(velocityA.y, velocityA.x) , I'll get a number that is signed in the opposite way to the first number. Please see next comment \$\endgroup\$ Jan 31, 2014 at 17:27
  • \$\begingroup\$ This is a problem, because as I said, I need to know in what quadrant B is in relation to A. So -135 degrees would mean it's positioned in the upper-left direction to A, and 135 degrees would mean that it's positioned in the lower-left direction to A. This is why, when subtracting the angles of two vectors in order to get the angle between the two, I have to know which angle comes first in the subtraction - the angle of vector 1, or the angle of vector2. What decides this? Which angle is bigger? Anything else? I would really appreciate your help, I'm kind of stuck here. Thanks \$\endgroup\$ Jan 31, 2014 at 17:31
  • \$\begingroup\$ What do you mean by what quadrant X is in relation to Y (draw a picture)? I think you want to do the second option: atan2(fromAtoB.y, fromAtoB.x) - atan2(velocityA.y, velocityA.x). It doesn't make sense to do the first. \$\endgroup\$
    – AturSams
    Jan 31, 2014 at 17:36
  • \$\begingroup\$ Okay, in a second I'll have a picture edited to the question. \$\endgroup\$ Jan 31, 2014 at 17:41
  • \$\begingroup\$ Please see my edit. This is what I meant by 'what quadrant B is in relation to A'. \$\endgroup\$ Jan 31, 2014 at 17:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .