Jump to content
  • Advertisement
Sign in to follow this  

Cartesian to Spherical Coordinates and vica versa

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a post running in the openGL forum, but i think maybe the math forum is a better place to try and get some questions answered. I have a point in cartesian coordinates... ( 0.0, 0.0, 5.0 ) x, y, z I need to convert it to Spherical Cordinates, then be able to rotate the point in the XY plane around the Z axis, Given an angle to rotate about. Then convert back to Cartesian coordinates again. Here is what i'm doing and it seems to be wrong. cX = 0.0 cY = 0.0 cZ = 5.0 i have a variable for theta, phi, and radius this is my calculation to convert to spherical Cordinates... radius = sqrt(cX^2 + cY^2 + cZ^2) theta = acos(cX / radius) phi = acos( cX / radius * sin(theta)) then i convert theta and phi to degrees then i convert back to Cartesian, and it doens't even get me close to the coordinates i passed in, using this cX = radius * sin(phi) * cos(theta) cY = radius * sin(phi) * sin(theta) cZ = radius * cos(phi) i without the first part working i can't figure out how to translate the point in spherical coordinates. Im stuck, and do not understand much about Cartesian to spherical coordinate systems. Any light someon can shed on what im doing right will help great amounts.

Share this post

Link to post
Share on other sites
You don't actually have to convert to spherical coordinates to rotate about the Z axis. You can do it with a rotation matrix. But, here is something to help with the approach you've chosen.

The other immediate thing I'll say is that your example point, (0, 0, 5.0), when rotated about the Z axis, becomes.......(0, 0, 5.0)! The same point no matter what your angle of rotation about Z!!!!

Your conversion to spherical coordinates is partially wrong. I think you knew this.

Your formula for radius is correct.

Your formula for theta is ONE of several valid choices for a spherical coordinate representation; HOWEVER, your choice is quite inconvenient for rotating about Z. Really, it was the wrong choice for rotating about Z. Here's another way. We'll choose theta to be the angle of the projection into the xy plane (the azimuth), and phi is the angle measured from the xy plane to the line segment from (0,0) to (cX, cY, cZ). So, we can first calculate phi:

phi = asin(cZ / radius)

Note that it is similar to your theta equation. It could just as easily be acos, but I want phi to represent the angle from the xy plane to the line segment. If I used acos, phi would be measured from the z axis to the line segment. Subtle difference. A personal choice.

Now we can calculate theta. Noting that radius * cos(phi) = the length of the projection of the segment into the xy plane, we can easily calculate theta:

theta = acos(cX / (radius * cos(phi)))

If cos(phi) is zero.....then there is no single value of theta. This is a discontinuity that is well known with the spherical coordinate representation.

Note that my theta is real similar to your phi calculation---though your phi calculation had an error and was incorrect even for your chosen computation of theta (you should not have been using cX to compute phi - either cY or cZ could be used to give a correct phi given your choice of theta, but not cX).

You basically had the right idea...you just don't quite understand your projections yet! The biggest actual error was in your calculation of phi. The other error was in your choice of theta and phi. You had not set yourself up to conveniently do rotation about Z in the spherical coordinate system.

So, from here you mentioned that you converted the angles to degrees. That is fine if you just want to add, say 5 "degrees" to theta to do the rotation about the Z axis. You could also convert 5 "degrees" to radians and add the corresponding radians value to theta.

The thing I want you to recognize is that you must pass in radians to the sin() and cos() calls later on. If you pass in degrees values to sin() and cos(), you're screwed! Let supposed that you do NOT convert to degrees. I consider this the safer choice. So, to do your rotation, do this:

theta += angle of rotation (in radians)

Now you can convert back to Cartesian as below, with phi and theta in radians:

cX = radius * cos(phi) * cos(theta)
cY = radius * cos(phi) * sin(theta)
cZ = radius * sin(phi) = should be identical to starting cZ value

Hope this helps!

Share this post

Link to post
Share on other sites
Thank you allmighty math master!

This has been plagueing me for a few days now.
So i finally got the rotations right.
Now I just need to handle what to do with the up vector and look vector on the backside of the circle to get the desired effect.

again thank you, you have been most helpful and explained the situation very well.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!