Converting 3D polar coordinates to cartesian coordinates

21.6k views Asked by At

I've been doing a lot of searching on the math behind this conversion, and the best I've been able to come up with so far is this:

x = sin(horizontal_angle) * cos(vertical_angle)
y = sin(horizontal_angle) * sin(vertical_angle)
z = cos(horizontal_angle)

For arbitrary angles, this works fine. Where I have problems is when one of the rotations is 0 degrees. At 0 degrees (or 180, or 360, or...), sin() is going to be zero, which means both the x and y coordinates I get out of the above formulas will be zero, regardless of what the other angle was set to.

Is there a better formula out there that doesn't mess up at certain angles? My searches so far haven't found one, but there has to be a solution to this issue.

Update: After some experimentation, I found that my main misunderstanding was the fact that I was assuming the poles of my spherical coordinates to be vertical (like latitude and longitude on a planet), while they were actually horizontal (projected into the screen). This was due to the fact that I'm working in screen space (x/y mapped to the screen, z projected into the screen), rather than a traditional 3D environment, but somehow didn't think that would be a contributing factor.

The final formula that worked for me to get the poles oriented correctly:

x = cos(horizontal_angle) * sin(vertical_angle)
y = cos(vertical_angle)
z = sin(horizontal_angle) * sin(vertical_angle)
3

There are 3 answers

0
happydave On BEST ANSWER

Your formula is correct for all angles. But the names that you've given the angles are probably not quite right. What you've called "horizontal angle" is the inclination angle - the angle between the vector and the z-axis. So if "horizontal angle" is 0, then the point lies on the z-axis, which means that it is correct for x and y to both be 0. What you've called "vertical angle" is actually the angle in the x-y plane. If it is 0, then the point lies in the x-z plane, so y is correctly set to 0.

0
RBarryYoung On

The correct conversion formulae are:

x = r * sin(polar) * cos(alpha)
y = r * sin(polar) * sin(alpha)
z = r * cos(polar)

Where:

r     is the Radius
alpha is the horizontal angle from the X axis
polar is the vertical angle from the Z axis

x and y are correctly zero when polar is zero (or 180, 360, etc.) because the vertical angle is aligned with the vertical Z-axis on those values. Likewise when alpha is zero (or 180, 360, etc.), the horizontal angle is aligned with the X-axis, so y must be zero. And when alpha is 90 (or 270, 450, etc.) it is aligned with the Y-axis, making x zero.

0
Alex P. On

Using formulae from Polar to Cartesian Conversion on wikipedia, my calculation came up with:

x = r * cos(polar) * cos(alpha)
y = r * cos(polar) * sin(alpha)
z = r * sin(polar)

where:

r     is the Radius
alpha is the horizontal angle from the X axis
polar is the vertical angle made by r vector with the x-y plane 
      ( 90 - <angle with Z axis> )

Then realized that @RBarryYoung answer is correct and mine as well with different angle considered. Just that I didn't used the correct angle from Z-axis in his answer, but it's complement angle ( 90-polar ) angle made by the vector with the x-y plane. So take that into account.