Archived

This topic is now archived and is closed to further replies.

Converting between 3D cartesian and spherical coords

This topic is 5045 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've recently just started dealing with spherical coord systems in my programming ventures and have found the following formulas: Cartesian to Spherical formula
Formula for converting from (x, y, z) to (radius, theta, phi)

Spherical to Cartesian Formula
Formula for converting from (radius, theta, phi) to (x, y, z).
Please keep in mind that I'm using the Cartesian coordinate system Cartesian Coord System. My problem is simply that the formulas don't work. For example, the Cartesian point (0, 0, 1) comes out as (1, pi/4, pi/2), which is incorrect (it should be (1, 0, 0) I believe). Are these formulas bad? Should they not work? I have built in special cases where denominators of arctan are 0 to automatically set to pi/2. That, too, should be correct, right? If these formulas are incorrect, could anyone suggest a better (correct) one? Thanks alot, ms [edited by - ms291052 on February 21, 2004 10:03:35 PM]

Share this post


Link to post
Share on other sites
a) The correct term for your "3D polar coordinates" is "spherical coordinates". Polar coordinates are 2D. This naming convention is not very important (at least to me) since you explicitely mentioned that you want 3D but it might be the reason google didn´t help you.

b) googling for "spherical coordinates" gives: http://mathworld.wolfram.com/SphericalCoordinates.html
Don´t bother caring about the stuff below equation (6) since it deals with differential geometry which you´ll most probably not understand if you´ve had trouble figuring out the transformations.

c) Don´t know about your source for the conversions but given that your transformation "from (radius, theta, phi) to (x, y, z)" doesn´t even have a theta or a phi in it the cause of your problem is probably more a problem of inconsistent nomenclature that a problem of wrong equations (I didn´t bother checking them. It´s boring and repetitive work).

d) A thing you might keep in mind: Transformation from cartesian to spherical coordiantes are not invertible over the whole R³ (some pseudo-geniuses might tell you otherwise but this won´t make it true ). For example: Every vector (radius=0, theta, phi) transforms to (x=0, y=0, z=0) so there´s no way transforming the zero-vector into spherical coordinates without risking to run into trouble (this, of course, depends on what you are doing).

e) The link I gave above uses a right handed coordinate system while you seem to use a left handed one. I hope this won´t cause you too much trouble altering the equations as needed ( z -> -z should do the trick).

Share this post


Link to post
Share on other sites
Ok, thank you athiest. I how have working formulas, however, now I''m having another odd problem. I''m using this to allow my particle system to have a smokey effect where it expands and swirls. So everything goes just great until the position of my partical (in spherical coords) has an angle component above pi/2. Ok, fine, so I go back and change all my atan(x/y) to atan2(x, y), and now it works until there is an angle above pi. I''ve tried subtracting pi when the angle goes >pi and adding pi when the angle goes <0. Neither worked. Just as soon as one angle gets there (it''s always phi, theta appears to work just fine) it bounces between 3.13 and 3.14 and creates a spastic effect. Why could this be? Why would changing from atan to atan2 help, and how could I fix this entirely?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Isn''t the formula for converting from spherical coordinates to cartesians as follows:

spherical = (radius, theta, phi)

cartesian = (radius*sin(phi)*cos(theta), radius*sin(phi)*sin(theta), radius*cos(phi))
where radius >= 0, theta is between 0 and 2PI, phi is between 0 and PI.

And the formula to convert cartesian to spherical is:

cartesian = (x, y, z) then,

spherical = ((x^2 + y^2 + z^2)^0.5, arctan(y/x), arccos(z/r))
and in the case where you should compute arctan(y/x) plus x = 0 then make x = 1 only for the arctan(y/x).

Share this post


Link to post
Share on other sites
I believe you are correct, AP, for the most part. When x is 0 in arctan(y/x) I believe you should (as I have been doing) setting arctan(y/x) to pi/2 automatically (tan is undefined at pi/2).

Share this post


Link to post
Share on other sites