Polar Coordinates

Started by
3 comments, last by jollyjeffers 22 years, 2 months ago
Hi, Can someone tell me the formulas for cartesian->polar and polar->cartesian formulas? I''ve been doing some camera work in 3D, and it seems that polar coordinates are the best way for me to represent the camera, but I need to convert them so that D3D can use it... I''ve got the 2D version: x = rCos(Theta) y = rSin(Theta) and back again... but I really haven''t been able to find the 3D formula''s in any of my usual research locations thanks in advance, Jack;

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Advertisement
From an old project of mine:

theta = (theta/180) * pi;
phi = (phi/180) * pi;

xcoord = radius * sin(theta) * cos(phi);
ycoord = radius * sin(theta) * sin(phi);
zcoord = radius * cos(theta);

You might need to switch them round a bit depending on your definitions of phi and theta.

- seb
The theta and phi are switched. There is a convention and to avoid confusion it is generally best to follow it. You have three values on the left and three on the right and six ways you could combine them. You can also get 24 by switching from counter-clockwise to clockwise measurement of angles. The convention is r=sqrt(x^2+y^2+z^2), theta=atan2(y, x) and phi=acos(z/r). It may seem a petty point in that all that matters is that you are consistant in how you use it within your program, but many books an articles use spherical coordinates without explaining which one is which just as many use angles without explaining they are measured counter-clockwise from the x-axis.
Keys to success: Ability, ambition and opportunity.
So:

phi = acos(z/r)
theta = atan2(x,y)

xcoord = radius * sin(phi) * cos(theta);
ycoord = radius * sin(phi) * sin(theta);
zcoord = radius * cos(phi);

and of course

r^2=x^2+y^2+z^2


Righty-ho then. Didn''t know there was a convention. This was from a vector class we had to write years ago.

- seb
thanks very much...

I''ll play around with it a bit later.

Jack;

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

This topic is closed to new replies.

Advertisement