Vector3 的纬度和经度未在 3D 球体上对齐 [英] Latitude and Longitude to Vector3 Not Aligning on 3D Sphere

查看:27
本文介绍了Vector3 的纬度和经度未在 3D 球体上对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将纬度和经度转换为 Vector3 格式.对于给定的纬度和经度,我想将其转换为 Vector3,其中一个标记对象将定位在这个 Vector3 位置.

这是我的代码:

void createLand(){double latitude_rad = (纬度) * 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 作为radiusEarth,这是伦敦纬度:51.509865,经度:-0.118092 的输出:

这里是北极的输出,纬度:90,经度:135:

标记(即闪亮的小球体)位置错误.

我的转换有什么问题吗,或者有没有其他方法可以解决这个问题?

编辑...

我使用过的地球纹理可以在

void createLand(){ObjectMarker.position = radiusEarth * Vector3.right;}

void createLand(){ObjectMarker.position = radiusEarth * Vector3.up;}

解决方案

您在转换为 3d 空间时使用了错误的轴.

  • Y 是上/下轴,所以肯定是 sin(latitude) 的那个.
  • 0,0 在 z = 1*radiusEarth 处,所以 z 需要是 cos*cos 的那个.
  • 0,90 位于 x = -1 * radiusEarth,所以 x 需要是负 cos*sin.

总和:

void createLand(){double latitude_rad = 纬度 * Math.PI/180;double longitude_rad = 经度 * 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;}

I am trying to convert latitude and longitude to a Vector3 format. For a given latitude and longitude, I want to convert it into a Vector3, where a marker object will be positioned at this Vector3 location.

Here is my code:

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;
}

I am using 6371 as radiusEarth, Here is the output for London lat:51.509865, lon:-0.118092:

And here is the output for the North Pole, lat:90, lon:135:

The marker (which is the small shiny sphere) is in the wrong position.

Is there anything wrong with my conversion or is there any other way to fix this?

EDIT...

Earth texture that I have used can be found here, it is the 10K image. I built the sphere and applied the texture using Blender - I applied a rotation to the sphere so that the front view will reflect the position of lat,long: 0,0.

Code to create the Earth object:

void createEarth()
{
    ObjectEarth.gameObject.transform.localScale = 1f * radiusEarth * Vector3.one;
}

EDIT 2...

This is where the marker is placed when using Unity's predefined vectors:

void createLand()
{
    ObjectMarker.position = radiusEarth * Vector3.forward;
}

void createLand()
{
    ObjectMarker.position = radiusEarth * Vector3.right;
}

void createLand()
{
    ObjectMarker.position = radiusEarth * Vector3.up;
}

解决方案

You are using the wrong axes for your conversion to 3d space.

  • Y is the up/down axis, so that should definitely be the one that is sin(latitude).
  • 0,0 is at z = 1*radiusEarth, so z needs to be the one with cos*cos.
  • 0,90 is at x = -1 * radiusEarth, so x needs to be negative cos*sin.

Altogether:

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;
}

这篇关于Vector3 的纬度和经度未在 3D 球体上对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆