将 3D 极坐标转换为笛卡尔坐标

Nai*_*rou 3 math rotation polar-coordinates cartesian-coordinates

我一直在对这种转换背后的数学进行大量搜索,到目前为止我能想到的最好的方法是:

x = sin(horizontal_angle) * cos(vertical_angle)
y = sin(horizontal_angle) * sin(vertical_angle)
z = cos(horizontal_angle)
Run Code Online (Sandbox Code Playgroud)

对于任意角度,这都很好。我遇到问题的地方是当其中一个旋转为 0 度时。在 0 度(或 180、或 360,或...)时,sin() 将为零,这意味着我从上述公式中得到的 x 和 y 坐标都将为零,无论其他角度如何被设置为。

有没有更好的公式,在某些角度不会混乱?到目前为止我的搜索还没有找到,但必须有一个解决方案来解决这个问题。

更新: 经过一些实验,我发现我的主要误解是我假设球坐标的极点是垂直的(就像行星上的纬度和经度),而它们实际上是水平的(投影到屏幕上)。这是因为我在屏幕空间(x/y 映射到屏幕,z 投影到屏幕)中工作,而不是传统的 3D 环境,但不知怎的,我并不认为这会成为一个影响因素。

最终的公式对我有用,使两极正确定向:

x = cos(horizontal_angle) * sin(vertical_angle)
y = cos(vertical_angle)
z = sin(horizontal_angle) * sin(vertical_angle)
Run Code Online (Sandbox Code Playgroud)

RBa*_*ung 9

正确的换算公式是:

x = r * sin(polar) * cos(alpha)
y = r * sin(polar) * sin(alpha)
z = r * cos(polar)
Run Code Online (Sandbox Code Playgroud)

在哪里:

r     is the Radius
alpha is the horizontal angle from the X axis
polar is the vertical angle from the Z axis
Run Code Online (Sandbox Code Playgroud)

x当为零(或 180、360 等)时,和y正确地为零,因为垂直角度与这些值上的垂直 Z 轴对齐。polar同样,当alpha为零(或 180、360 等)时,水平角度与 X 轴对齐,因此y必须为零。当alpha为 90(或 270、450 等)时,它与 Y 轴对齐,为零x


hap*_*ave 5

你的公式对于所有角度都是正确的。但你给角度起的名字可能不太正确。你所说的“水平角”是倾斜角——向量和z轴之间的角度。所以如果“水平角”为0,则该点位于z轴上,这意味着x和y都为0是正确的。你所说的“垂直角”实际上是xy中的角度飞机。如果为 0,则该点位于 xz 平面内,因此 y 正确设置为 0。