# Relative angle from point a (with direction) to point b

This topic is 3499 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys. I'm trying to figure out how to find the relative angle from point a to point b. Point A has a direction to it. Wikipedia failed me (I haven't got the faintest grasp on trig), so I turn to you guys! To better illustrate, see this: Point A is the center point, point B is the sphere at the end. There are 3 arc angles I need to find, arc x, arc y, and arc z. The big arrow is the direction of point A. I'm on a hunt for a good solid algorithm I can use to determine arc n (any arc), negative or positive in relative angle. Any help you can offer is highly appreciated!

##### Share on other sites
Given a point A and another point B, the vector from point A to point B will be:
B - A

But we want the normalised vector:
C = normalise(B - A)

Then, we use the cartesian to spherical coordinates formula, obtaining 2 angles:
omega = atan(C.y/C.x), theta = asin(C.z)

##### Share on other sites
thanks, but what does normalize() do? How does it work? And what is Omega and theta? Also, how do I get just the arc n angle?

##### Share on other sites
I suppose you already know what's a vector. The process of scaling vectors to unit vectors is known as normalization. The vector length is scaled to 1. Actually unit vectors is a set of numbers, which has been expressed in the same ratio as the original vector but the components are scaled to a fraction that ranges between -1 & +1.
You will get the normalized vector by dividing the X, Y and Z coord of a vector by the total vector length.

For e.g.
unit_vector.X = vector.X / vector_length
same for Y & Z.
The vector length is calculated by pythagorean theorem. Look it up it's really easy.
for e.g.
Math.sqrt(vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z)

Good luck[smile]

##### Share on other sites
Quote:
 Original post by Chwagathanks, but what does normalize() do? How does it work?
Normalising a vector divides it by its own length, resulting in a vector of unit length. In psuedo code:
vec B; len = sqrt(B.x*B.x + B.y*B.y + B.z*B.z); B /= len;
Quote:
 And what is Omega and theta?
Basically, omega is the rotation about the z-axis, and theta is the rotation about the y-axis. If you apply these rotations, you should end up with the correct direction.
Quote:
 Also, how do I get just the arc n angle?
You can use trigonometry to find the rotation about an arbitrary axis - i.e. for the z-axis you would use atan(x/y), etc.

However, in order to rotate from direction A to direction B, you really only need to rotate about any 2 axes (the third is unnecessary). The algorithm in my first post gives a method to obtain those two angles.

##### Share on other sites
This isn't for rotational translation, I just need to determine 3 angles given 2 coordinates and a normal. (Simply determining the angle will do, actually). So you're saying that arc x is(assuming point a is at 0,0,0) atan(y/z), arc y is atan(x/z), and arc z is (x/y)? Will this work for negative angles?

##### Share on other sites
Quote:
 Original post by ChwagaSo you're saying that arc x is(assuming point a is at 0,0,0) atan(y/z), arc y is atan(x/z), and arc z is (x/y)?
Pretty much. Recall that tan(theta) = opposite/adjacent for a right angle triangle, and any two components of a vector are necessarily at right angles to each other, so atan(opposite/adjacent) = theta, where theta is some angle.
Quote:
 Will this work for negative angles?
IIRC, it will only return angles in the first quadrant. However, there is another function specially designed for this, which will handle all vectors: z = atan2(y, x)

##### Share on other sites
So does it matter if I do atan2(x,y) or atan2(y,x)? Would it make a different?

hello?

##### Share on other sites
Quote:
 Original post by ChwagaSo does it matter if I do atan2(x,y) or atan2(y,x)? Would it make a different?
One will give you a clockwise angle, the other anti-clockwise.

• 9
• 23
• 10
• 19