C# 到矢量3的纬度和经度未在三维球体上对齐
我正在尝试将纬度和经度转换为矢量3格式。对于给定的纬度和经度,我想将其转换为Vector3,其中标记对象将定位在Vector3位置 这是我的密码:C# 到矢量3的纬度和经度未在三维球体上对齐,c#,unity3d,vector,coordinates,C#,Unity3d,Vector,Coordinates,我正在尝试将纬度和经度转换为矢量3格式。对于给定的纬度和经度,我想将其转换为Vector3,其中标记对象将定位在Vector3位置 这是我的密码: void createLand() { double latitude_rad = (latitude) * Math.PI / 180; double longitude_rad = (longitude) * Math.PI / 180; double xPos = (radiusEarth * Math.Cos((la
void createLand()
{
double latitude_rad = (latitude) * Math.PI / 180;
double longitude_rad = (longitude) * Math.PI / 180;
double xPos = (radiusEarth * Math.Cos((latitude_rad)) * Math.Cos((longitude_rad)));
double yPos = (radiusEarth * Math.Cos((latitude_rad)) * Math.Sin((longitude_rad)));
double zPos = (radiusEarth * Math.Sin((latitude_rad)));
markerPos.x = (float)xPos;
markerPos.y = (float)yPos;
markerPos.z = (float)zPos;
ObjectMarker.position = markerPos;
}
我使用6371作为半径地球,这是伦敦lat:51.509865,lon:-0.118092的输出:
这是北极的输出,lat:90,lon:135:
标记(即发光的小球体)位于错误的位置
我的转换有什么问题吗?或者有其他方法来解决这个问题吗
编辑
可以找到我使用的地球纹理,它是10K图像。我构建了球体并使用Blender应用了纹理-我对球体应用了旋转,以便前视图将反映lat的位置,long:0,0
创建地球对象的代码:
void createEarth()
{
ObjectEarth.gameObject.transform.localScale = 1f * radiusEarth * Vector3.one;
}
编辑2
这是使用Unity的预定义向量时放置标记的位置:
void createLand()
{
ObjectMarker.position = radiusEarth * Vector3.forward;
}
转换到三维空间时使用了错误的轴
- Y是上/下轴,因此它肯定是
李>sin(纬度)
- 0,0位于
,因此z需要是带有cos*cos的李>z=1*radiusarth
- 0,90位于
,因此x需要为负cos*sinx=-1*半径地球
void createLand()
{
double latitude_rad = latitude * Math.PI / 180;
double longitude_rad = longitude * Math.PI / 180;
double zPos = radiusEarth * Math.Cos(latitude_rad) * Math.Cos(longitude_rad);
double xPos = -radiusEarth * Math.Cos(latitude_rad) * Math.Sin(longitude_rad);
double yPos = radiusEarth * Math.Sin(latitude_rad);
markerPos.x = (float)xPos;
markerPos.y = (float)yPos;
markerPos.z = (float)zPos;
ObjectMarker.position = markerPos;
}
它几乎就像下面的球体在两个方向上都向外倾斜了90°。。由于图片与北极点对齐,我认为伦敦将位于正确的位置..你的数学似乎正确,拍摄这些屏幕截图时,球体和摄影机/检查器视图的方向是什么?请包括您正在使用的纹理以及将创建球体并将纹理应用于球体以生成旋转的代码。@NSJacob1-地球球体没有应用任何旋转。当上面的屏幕截图显示时,Inspector被设置为front viewtaken@Ruzihm-我创建了一个编辑,解释如何创建球体以及如何应用纹理。@我根据问题中显示的图像更新了答案。注意
double xPos=-radiuearth*Math.Cos(纬度)×Math.Sin(经度)中的负数
和匹配的markerPos.x=(float)xpo代码>,markerPos.y=(float)yPos
和markerPos.z=(float)zPos代码>刚刚尝试了上面的代码,使用lat、long、90、135,标记按预期与北极对齐,尽管使用0,0时,标记指向地球内部中心的原点sphere@SidS这真的很奇怪,因为double zPos=radiusarth*Math.Cos(纬度)×Math.Cos(经度)代码>应为zPos=radiuserth代码>。。。所以它不应该在地球内部,对吗?似乎我忽略了另一个打字错误,或者是把它复制到你的脚本中的一个错误。我刚刚复制并粘贴了代码,是的,它应该在地球内部。我会检查我的脚本,确保我这边没有问题。答案中的新代码是正确的,问题是我这边的。非常感谢。
void createLand()
{
ObjectMarker.position = radiusEarth * Vector3.up;
}
void createLand()
{
double latitude_rad = latitude * Math.PI / 180;
double longitude_rad = longitude * Math.PI / 180;
double zPos = radiusEarth * Math.Cos(latitude_rad) * Math.Cos(longitude_rad);
double xPos = -radiusEarth * Math.Cos(latitude_rad) * Math.Sin(longitude_rad);
double yPos = radiusEarth * Math.Sin(latitude_rad);
markerPos.x = (float)xPos;
markerPos.y = (float)yPos;
markerPos.z = (float)zPos;
ObjectMarker.position = markerPos;
}