Angle between two 3D vectors in spherical coordinates

I made a get_angle function inside my Vector3 class but I have a problem with it.

The Y angle is perfectly fine.

The pitch angle (X) it returns is slightly over my destination position when the base vector is over it (and when under the opposite is happening).

The amount of it being wrong is dependant on the height difference.

Angle get_angle(const Vector3f& v) const { return Angle( math::rad_to_deg(atan2(get_distance(v), v.z - z)) - 90.0f, math::rad_to_deg(atan2(v.y - y, v.x - x)), 0.0f); }

It's probably my maths that are bad here.

If I specify the latitudes and longitudes of both point A and B on the surface of a unit sphere, how can I find the great circle angle between the two points? Say if Latitude and longitude of A is ##\epsilon_1 \;\hbox { and }\;\tau_1## respectively. Latitude and longitude of B is ##\epsilon_2 \;\hbox { and }\;\tau_2## respectively. How do I find the angle between A and B? I know in spherical trig., ##\frac {\sin A}{\sin a}\;=\;\frac {\sin B}{\sin b}\;=\;\frac {\sin C}{\sin c}## ##\cos a\;=\;\cos b \cos c-\sin b\sin c\cos A## I know how to find the great circle angle for each point as they both form a right angle spherical triangle. I don't have enough to find the great circle angle between the two points because they don't form a right angle spherical triangle. I found this:

http://en.wikipedia.org/wiki/Great-circle_distance

Can anyone explain how the equation come about?

Thanks

Last edited: Feb 13, 2013

Answers and Replies

Simon Bridge

You mean the angle at the origin (being the center if the Earth?)
How would you normally find the angle between two vectors?

Thanks for the reply. So in spherical coordinates, if latitude is 2ε and longitude is 2##\tau##, then ##\phi=2\tau##, ##\theta=\frac{\pi}{2}-2\epsilon##

Is this correct?

Simon Bridge

I'm sorry: - if you don't answer questions I cannot help you.

-i f you want to know if something is correct, you'll have to explain your reasoning.

I since verified already. Latitude is measuring from equator ( z=0 on rectangular coordinates). θ is measure from the z axis at x=y=0. Whereby ##\theta=\frac {\pi}{2}-2\epsilon##. Say I used ##\hat{OP}=\hat x \cos\phi sin(\frac {\pi}{2}-2\epsilon)+\hat y \sin\phi sin(\frac {\pi}{2}-2\epsilon)+\hat z \cos(\frac {\pi}{2}-2\epsilon)##

to solve the equation, I got the equation given by the Wikipedia link. That was a very good tip by you, all the books using Spherical Trigonometry to solve these kind of problem, you reminded me that I can just use Spherical Coordinates to do all the calculation. I spent two week digging around, listening to the youtube video on spherical geometry and I don't even have to use it anymore. Well, I guess I can't call that waste of time, but it sure take me two weeks as it is not easy to find good article on spherical trig. like the other more common type math. The light bulb lighted up BIG TIME the moment you mentioned Spherical Coordinates!!!

Thanks

Alan

Simon Bridge

Well done - after that all you need to realize is that the two points on the surface, and the origin, form a big triangle where you know the length of two of the sides. The actual coordinate system doesn't matter though - you could have converted them to cartesian to get two (x,y,z) points you are familiar with.

Yes, I miss spoked. It is rectangle coordinates in every sense, not spherical coordinates. Only difference is the scalar amplitude of the x, y and z components are represented by the radius components of the spherical coordinates by using ## \theta \;\hbox { and }\;\phi##.

Spherical coordinates is very different where ##\hat {\phi}\;\hbox { and }\;\hat {\theta}## has to be referenced back to the original position vector. ie, two position vectors have their own ##\hat {\phi}\;\hbox { and }\;\hat {\theta}##. That's the reason spherical coordinates representation of a position vector has no θ and ∅ components, only R component with the given θ and ∅.

Simon Bridge

Spherical coordinates is very different where ##\hat {\phi}\;\text{ and }\;\hat {\theta}## has to be referenced back to the original position vector. ie, two position vectors have their own ##\hat{\phi}\;\text{ and }\;\hat{\theta}##. That's the reason spherical coordinates representation of a position vector has no θ and ∅ components, only R component with the given θ and ∅.

That last but does not make sense. A position vector in spherical coordinates is ##\vec{r}=R\hat{r}+\Theta\hat{\theta}+\Phi\hat{ \phi}## surely?
Otherwise how do you tell where on the sphere you are?

That last but does not make sense. A position vector in spherical coordinates is ##\vec{r}=R\hat{r}+\Theta\hat{\theta}+\Phi\hat{ \phi}## surely?
Otherwise how do you tell where on the sphere you are?


From my understanding, position vector has no θ and ∅ component because position vector is from origin to a point in space. For example from origin to a point P is ##\vec{OP}##. Only vector field has θ and ∅ component as a vector field at point P has directions (R,∅,θ) and magnitude for each component. A position vector has only R component with specified ∅ and θ to indicate the direction of the position vector. I understand that ##\vec A\;=\;\hat x A_x+\hat y A_y+\hat z A_z\;=\;\hat R A_R+\hat {\phi}A_{phi}+\hat {\theta} A_{\theta}## Where ##A_R=\vec A\cdot \hat R\;=\;A_x\cos\phi \sin\theta \;+\;A_y\sin\phi \sin\theta \;+\;A_z\cos\theta ## ##A_{\phi}=\vec A\cdot \hat {\phi}\;=\;-A_x\sin\phi \;+\;A_y\cos\phi## ##A_{\theta}=\vec A\cdot \hat {\theta}\;=\;A_x\cos\phi \cos\theta \;+\;A_y\sin\phi \cos\theta \;-\;A_z\sin\theta ##

You take any position vector in rectangular coordinates and transform into Spherical coordinates, the ∅ and θ components are always 0. For example, you plug in any constants into ##A_x,\;A_y,\;A_z## the θ and ∅ component are always zero. That's how position vector is. You cannot put x, y, z as variable in ##A_x,\;A_y,\;A_z## as this immediately turn it into a vector field. That you will have all three components.

Last edited: Feb 17, 2013

Simon Bridge

A position vector has only R component with specified ∅ and θ to indicate the direction of the position vector.

That's a contradiction.

in a vector field, a vector is assigned to each point in space. However, each point in that space can be pointed to by a vector which, in 3D, must have three components. The two angles you are calling "specified" with R, are the extra two components of the vector.

There are different ways to specify spherical coordinates and different ways to express an angle in spherical coordinates. Without the unit vectors attached to the angles, the size of the angle does not help.

That's a contradiction.

in a vector field, a vector is assigned to each point in space. However, each point in that space can be pointed to by a vector which, in 3D, must have three components. The two angles you are calling "specified" with R, are the extra two components of the vector.

There are different ways to specify spherical coordinates and different ways to express an angle in spherical coordinates. Without the unit vectors attached to the angles, the size of the angle does not help.


That's exactly what I was talking about. Vector field is a function of a point in space, it has all three components. BUT position vector is not a vector field. It is a vector from the origin to a point in space. It is a fixed vector. If you just translate ##\vec A=\hat x 3+\hat y 4+ \hat z 5## or with any constants to Spherical Coordinates, you'll see the θ and ∅ components disappeared.

Remember, ##\vec A=\hat x (x+y)+\hat y (y+z)+ \hat z (x+z)## is a vector field which is position dependent, this has all three components.

Simon Bridge

position vector is ... a fixed vector. If you just translate ##\vec A=\hat x 3+\hat y 4+ \hat z 5## or with any constants to Spherical Coordinates, you'll see the θ and ∅ components disappeared.

Off your example: what is wrong with: ##\vec{A}=(7.0711)\hat{r}+(0.78540)\hat{\theta}+(0.92730)\hat{\phi}## ... the ##\theta## and ##\phi## components seem to be there.

To vanish, they'd have to both be zero... note: with a radial vector field, the angular components do, indeed, vanish and we can say stuff like ##\vec{A}=A_r(r)\hat{r}##.

However, each point in that space can be pointed to by a vector which, in 3D, must have three components.


You do need three numbers to specify a point in a 3D space. If your numbers are the coefficients of the position's vector expansion over some basis vectors, and the basis vectors are those of the spherical coordinate system, then two of the numbers are identically zero. That's because the basis vectors are "co-moving". You still have to use two numbers to specify the orientation of the basis vectors, of course.

Simon Bridge

I think for me to understand the difference you'll have to walk me through the expansion.
What was wrong with the expansion I did in the last post?

vela

Off your example: what is wrong with: ##\vec{A}=(7.0711)\hat{r}+(0.78540)\hat{\theta}+(0.92730)\hat{\phi}## ... the ##\theta## and ##\phi## components seem to be there.

To vanish, they'd have to both be zero... note: with a radial vector field, the angular components do, indeed, vanish and we can say stuff like ##\vec{A}=A_r(r)\hat{r}##.

The point (3,4,5) corresponds to ##r = 5\sqrt{2}, \theta = \cos^{-1}(1/\sqrt{2}), \phi=\tan^{-1}(4/3)##, so we have \begin{align*} \cos\phi &= 3/5 \\ \sin\phi &= 4/5 \\ \cos\theta &= \sqrt{1/2} \\ \sin\theta &= \sqrt{1/2}. \end{align*} The spherical basis vectors for that point are \begin{align*} \hat{r} &= \sin\theta\cos\phi\,\hat{i} + \sin\theta\sin\phi\,\hat{j} + \cos\theta\,\hat{k} = \frac{1}{5\sqrt{2}}(3\hat{i} + 4\hat{j} + 5\hat{k}) \\ \hat{\theta} &= \cos\theta\cos\phi\,\hat{i} + \cos\theta\sin\phi\,\hat{j} - \sin\theta\,\hat{k} = \frac{1}{5\sqrt{2}}(3\hat{i}+4\hat{j}-5\hat{k}) \\ \hat{\phi} &= -\sin\phi\,\hat{i} + \cos\phi\,\hat{j} = \frac{1}{5}(-4 \hat{i} + 3\hat{j})

\end{align*} So ##\vec{r} = r\,\hat{r} = 3\hat{i} + 4 \hat{j} + 5 \hat{k}##. There are no ##\hat{\theta}## and ##\hat{\phi}## components for the position vector.

Last edited: Feb 17, 2013

Simon Bridge

Hi Simon, Sorry, I am just very busy doing other things lately to dig deeper into this to reply your question the last two days. I have to put in the recess lighting, which, has nothing to do with here!! I am not in a calculus class so I am really rusty and I have to review everything before I can reply. It's like "not in mid season condition!!!", everything is rusty, need a kick to get the wheel spinning!!!:rofl:. Like last time, it took one sentence from you to get the wheels turning for me!!! Thankfully Vela show the derivation. But actually you make me thing about another scenario......We know that a position vector cannot have x, y, z as independent variables. But what if it contains variables that is function of....say time t. This means the position vector can trace out a path with time t that is not a vector field in the sense that it is position dependent. And at the same time, the vector is not a constant. I still have to find time to think about this, the little time I have, I am still working on the inverse tangent question. Thanks for all your help, I'll get back to the other question hopefully tonight. Sincerely

Alan

Simon Bridge

It's all right - as House would say "I'm an idiot!" Of course any vector can be written ##\vec{v}=v\hat{v}## which is to say - it has magnitude v in the direction it is pointing in.

I can write ##\vec{r}=(r,\theta ,\phi )^t## because I can encode the spherical coordinates into a vector - but that is not a position vector because it does not transform as a position vector should. What was wrong with my previous derivation was that I forgot to transform the unit vectors.

Thanks for sticking with me.

I was just having a moment of concept blindness.

It's all right - as House would say "I'm an idiot!" Of course any vector can be written ##\vec{v}=v\hat{v}## which is to say - it has magnitude v in the direction it is pointing in.

I can write ##\vec{r}=(r,\theta ,\phi )^t## because I can encode the spherical coordinates into a vector - but that is not a position vector because it does not transform as a position vector should. What was wrong with my previous derivation was that I forgot to transform the unit vectors.

Thanks for sticking with me.


I was just having a moment of concept blindness.

What are you talking about, you are of great help!!!

Thanks

Simon Bridge

... yeah, thinking of lat-long as spherical coordinates was the lynchpin for you ... and then I got sidetracked. Now worries ... enjoy.