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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With