Answers:
http://blog.nobel-joergensen.com/2010/10/22/spherical-coordinates-in-unity/
public static void SphericalToCartesian(float radius, float polar, float elevation, out Vector3 outCart){
float a = radius * Mathf.Cos(elevation);
outCart.x = a * Mathf.Cos(polar);
outCart.y = radius * Mathf.Sin(elevation);
outCart.z = a * Mathf.Sin(polar);
}
public static void CartesianToSpherical(Vector3 cartCoords, out float outRadius, out float outPolar, out float outElevation){
if (cartCoords.x == 0)
cartCoords.x = Mathf.Epsilon;
outRadius = Mathf.Sqrt((cartCoords.x * cartCoords.x)
+ (cartCoords.y * cartCoords.y)
+ (cartCoords.z * cartCoords.z));
outPolar = Mathf.Atan(cartCoords.z / cartCoords.x);
if (cartCoords.x < 0)
outPolar += Mathf.PI;
outElevation = Mathf.Asin(cartCoords.y / outRadius);
}
r
:径向距离θ
:倾角φ
:方位角通过维基百科
public Vector3 getCartesianFor(float radius, float inclination, float azimuth)
{
return new Vector3(radius*Sin(inclination)*Cos(azimuth), radius*Sin(inclination)*Sin(azimuth), radius*Cos(inclination));
}
如果polar
您是指向量的大小,并且elevation
定义为向量与xy平面之间的角度(顾名思义),则该函数为:
public Vector3 getCartesianFor(float elevation, float asimuth, float polar)
{
return new Vector3(polar*Cos(elevation)*Cos(asimuth),
polar*Cos(elevation)*Sin(asimuth),
polar*Sin(inclination));
}
请注意,这与MLM的答案非常相似,这完全取决于您如何定义elevation
角度。我也尊重您的功能模板签名,但我建议您进行一些更改:
asimuth
是错字,正确的拼写是 azimuth
polar
通常是指2D坐标系(r,θ)。向量的大小通常称为radius
radius
作为第一参数。