Maths - Forum Discussion about Relative Angles

author-name: eatjason

message-date: 2011-04-10 12:00:23 UTC

I was working with this equation:

angle of 2 relative to 1= atan2(v2.y,v2.x) - atan2(v1.y,v1.x)

To find a signed angle between two vectors.  I have found that
it DOES NOT yieldthe relative angle between two vectors in
every case.  

Lets say you are trying to find the angle between a vector v2
and vector v1, relative to v1.

Let v1 = ( -sqrt(2)/2, -sqrt(2)/2  )
and v2 = ( -sqrt(2)/2, sqrt(2)/2 ).

By observation you can see that the relative angle should be
-pi/2 (if you imagine v1 as the unit-x vector).

(for v1) atan2( -sqrt(2)/2, -sqrt(2)/2) )  = -3pi / 4
(for v2) atan2( sqrt(2)/2, -sqrt(2)/2) )  = 3pi / 4


( 3pi / 4 ) - ( -3pi / 4 ) = 6pi / 4 = 3pi / 2 

which is the angle, but its supposed to be between pi
and -pi, so -pi / 2

What I did to solve this problem is I first found atan2(v1.y,v1.x),
then rotated v2 by the negative that value, then found atan2 of the
components of that new rotated vector (which is basically manually
rotating v2 to be relative to v1, as the x-unit vector)

author-name: martinbaker

message-date: 2011-04-10 15:13:40 UTC

Yes, I guess the issue is that if we are facing in direction 1
and we want to rotate to face in direction 2 we could rotate clockwise
or anti-clockwise.

So a rotation of pi/2 in the clockwise direction is the same as in the
anti-clockwise direction 3pi/4.

Perhaps the web page should have two equations? One case if we always
want to rotate in a clockwise direction and another equation if we
want to rotate clockwise or anti-clockwise depending on which is
the smaller angle?


author-name: eatjason

message-date: 2011-04-10 18:39:08 UTC

I'm afraid it might be a little more complicated than that.
The issue is that the equation as it is yields a clockwise or
anti-clockwise angle depending on where the vectors happen
to be.

Here's another case, getting the angle between v1 and v2 relative to v1:

v1 = ( sqrt(2)/2, -sqrt(2)/2 )
v2 = ( -sqrt(2)/2, -sqrt(2)/2 )

now by observation, you can see that the anti-clockwise rotation
should be 3pi/4, which it is (same as the first example).
now I might have my atan2 wrong, but my understanding is this:

(for v1) atan2( -sqrt(2)/2, sqrt(2)/2) ) = -pi / 4
(for v2) atan2( -sqrt(2)/2, -sqrt(2)/2) ) = -3pi / 4 

now the difference will look like this:
( -3pi / 4 ) - ( -pi / 4 ) = -2pi / 4 = -pi / 2 

what you have here is the clockwise angle relative to v1!

So that means that the equation atan2(v2.y,v2.x) - atan2(v1.y,v1.x)
can yield clockwise or anti-clockwise, based on the orientation of
the two vectors (even if the relative orientation stays the same).
Which is a problem, because generally when you want a relative
orientation, you don't so much care how the whole system is oriented. 

author-name: martinbaker

message-date: 2011-04-11 15:33:29 UTC

This is a useful topic so I have added a new page to discuss it here:

Is the page correct? and am I still missing the point you are making?

Would it be alright with you if I put a link from that page to this
thread as I don't want to take the credit for the points arising
from your message.


author-name: eatjason

message-date: 2011-04-11 19:43:07 UTC

I think that clears things up actually.  If atan2 is between pi
and -pi, obviously a difference can be between 2pi and -2pi.  That
didn't occur to me...  This problem comes up quite a bit in game
design, you have some vehicle that needs to turn in the direction
of some other object, but it can't do it instantly, so you want
to find which way it needs to turn.  My vehicle was working fine
when facing right, but when it tried to chase something to the
left, it started doing funny things.

So I guess the solution is to see if its bigger or smaller than
pi or -pi, and just transform it then.  I was trying to solve
this problem without any if statements, maybe two if statements
will be faster than having to do 2 sins and coses to perform
the rotation i was doing.

Go ahead and link it, that was a useful description of the
problem.  (I guess I didn't need to use all those square
roots to describe it :) )


metadata block
see also:

Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

cover If you are interested in 3D games, this looks like a good book to have on the shelf. If, like me, you want to have know the theory and how it is derived then there is a lot for you here. Including - Graphics pipeline, scenegraph, picking, collision detection, bezier curves, surfaces, key frame animation, level of detail, terrain, quadtrees & octtrees, special effects, numerical methods. Includes CDROM with code.

Other Math Books

Terminology and Notation

Specific to this page here:


This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2023 Martin John Baker - All rights reserved - privacy policy.