# Angle between two vectors in spherical coordinates (and gradient)

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

## Recommended Posts

Hi there, I'm searching for a way to determine the angle alpha between two vectors that are given in sphere coordinates. This is what I have come up with so far: The vectors [v and w] can be expressed as: v_x = sin(sigma_v) * cos(theta_v) v_y = sin(sigma_v) * sin(theta_v) v_z = cos(sigma_v) w_x = sin(sigma_w) * cos(theta_w) w_y = sin(sigma_w) * sin(theta_w) w_z = cos(sigma_w) So the angle can be written as: alpha = acos( (v_x*w_x + v_y*w_y + v_z*w_z) / |v| * |w| ) (I denote |v| and |w| as being the length of vector v and w.) This (after applying some trig) simplifies to: alpha = acos(sin(sigma_v)*sin(theta_v)*sin(sigma_w)*sin(theta_w) + sin(sigma_v)*cos(theta_v)*sin(sigma_w)*cos(theta_w) + cos(sigma_v)*cos(sigma_w)) So far, so good - I checked the equation and it seems to behave correctly. My first question would be - is there any way to determine the angle with less calculations? Second question comes right up... To make the following equations a little bit more readable I fixed sigma_v and theta_v to always be 0. If I then want to have the partial diff of the angle on a change of sigma_w. I come up with something like this: alpha'(sigma_w) = -( (sin(sigma_w)*cos(theta_w)) / sqrt(1-cos(sigma_w)^2) ) You see where my problem is? As soon as sigma_w becomes 0 the nominator becomes 0 and the gradient goes right into infinity. That can't be right if you look at the function. (With sigma_v and theta_v fixed at 0, the angle equation becomes alpha = sigma_w...) Can anyone spot my mistake? Grateful greetings, Rogalon

##### Share on other sites
Off the top of my head, I believe you would have some luck with the Law of Cosines. After all, a spherical surface and a flat surface aren't much different in a topological sense. Since the radius of the sphere is 1, you can use radians directly in the equation. What you get is:

alpha2 = sigma_v2 + sigma_w2 - 2(sigma_v)(sigma_w)cos(theta_v - theta_w)

I haven't tested it but my intuition tells me it might work :)

As for your second equation, you definitely made a mistake somewhere. The lengths cancel out of the equation (since they're 1), and you're left with:

alpha = acos(w_z) = acos(cos(sigma_w)) = sigma_w
alpha'(sigma_w) = 1

##### Share on other sites
Hi Zipster,
thanks for your reply. I gave it some thought and as far as I can see your equation would only give me the absolute value of the angle. For the equation I am working on I would need the sign/direction too. Sorry if I didn't make that one clear.

And I guess for the derivative - I didn't make a mistake. I just made a wrong assumption. If I have the signed angle, the derivative is not continuous an sigma_v = sigma_w = 0. The angle function changes the sign at that point and thus has a break, making the derivative go towards infinity.

What do you think? Am I still missing something? If not - do you have any idea on how to "fix" that diff? I want it to be as continous as possible, being 0 at the break. ;-)

Greetings, Roga

##### Share on other sites
Just wanna say this:

You are forgetting the radius on both vector parametrizations. Correct would be:

v_x = Rv * sin(sigma_v) * cos(theta_v)
v_y = Rv * sin(sigma_v) * sin(theta_v)
v_z = Rv * cos(sigma_v)

w_x = Rw * sin(sigma_w) * cos(theta_w)
w_y = Rw * sin(sigma_w) * sin(theta_w)
w_z = Rw * cos(sigma_w)

In case they have same lenght, which would be the unity, then you can just replace |v| and |w| with 1 on your angle formula.

##### Share on other sites
1-cos(x)^2 = sin(x)^2

so the sin(sigma_w) term cancels out the denominator, so Alpha' = cos(theta_w)

##### Share on other sites
Actually, check your work again. I haven't done it out, but it seems wrong that the partial with respect to sigma_w should depend on theta_w. when the other vector is fixed at <0,0,1>, rotating the other vector around the z-axis doesn't change the angle.

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632984
• Total Posts
3009716
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!