将GPS统一坐标为(X,Y,Z),向北旋转 [英] unity GPS coordinates to (X,Y,Z), rotating north
问题描述
我正在使用Unity3D制作一个应用程序,该应用程序使用GPS坐标在3D世界中放置物体",当您在靠近它的真实世界中行走时,您会发现它越来越近.有点像PokemonGO
I'm making an app with Unity3D which uses GPS coordinates to place a "thing" in the 3D world, as you walk in real world close to it, you will find it getting close. kinda like PokemonGO
所以问题是: 我使用函数dist(lat1,lon1,lat2,lon2)设法将对象放置在正确的位置和距离处,该函数可以为我提供两个坐标之间的距离(以米为单位)(见下文).
So the problem is: I managed to place the object at the right place and distance, using a function dist(lat1,lon1,lat2,lon2) which give me the distance in meters between the 2 coordinates (see below).
我接下来要做的是使场景绕Y轴旋转以向北行驶(Z轴+应该指向北).我该怎么做?
What i want to do next is to make the scene turn around the Y axis in order to head north (Z axis+ should be pointing North). how do i do this?
我所做的是这样:
float xx = dist (0, Tlon, 0, lon), zz = dist (Tlat, 0, lat, 0);
Vector3 position = new Vector3 (xx, 0, zz);
position.Normalize ();
float beta = Mathf.Acos(position.z) * Mathf.Rad2Deg;
position = Quaternion.Euler (0,beta-Input.compass.trueHeading,0)*position;
transform.position = position;
但这确实很奇怪,正确"的位置在我的右边和我的左边(为什么?idk)
But it does weird stuff, the "right" position is both at my right and at my left (why? idk)
{dist函数,用于计算2个坐标之间的距离:}
{dist function, used to calculate distances between 2 coords:}
float dist(float lat1, float lon1, float lat2, float lon2){ // generally used geo measurement function
var R = 6378.137; // Radius of earth in KM
var dLat = (lat2 - lat1) * Mathf.PI / 180;
var dLon = (lon2 - lon1) * Mathf.PI / 180;
var a = Mathf.Sin(dLat/2) * Mathf.Sin(dLat/2) +
Mathf.Cos(lat1 * Mathf.PI / 180) * Mathf.Cos(lat2 * Mathf.PI / 180) *
Mathf.Sin(dLon/2) * Mathf.Sin(dLon/2);
var c = 2 * Mathf.Atan2(Mathf.Sqrt(a), Mathf.Sqrt(1-a));
var d = R * c;
return (float)(d * 1000); // meters
}
推荐答案
最简单的方法是将每个地图对象作为父级分配为空,然后旋转.这样,您只需要将transform.rotation = Quaternion.Euler (0,Input.compass.trueHeading,0)
设置为
Simplest way to do this will be if you will have every map object assigned to empty as a parent and then rotate. This way you only need set transform.rotation = Quaternion.Euler (0,Input.compass.trueHeading,0)
on empty as
Input.compass.trueHeading
-相对于 地理北极
Input.compass.trueHeading
- The heading in degrees relative to the geographic North Pole
查看您的代码,其外观应为:
Looking at your code it should look like:
float xx = dist (0, Tlon, 0, lon), zz = dist (Tlat, 0, lat, 0);
Vector3 position = new Vector3 (xx, 0, zz);
position.Normalize ();
position = Quaternion.AngleAxis(Input.compass.trueHeading, Vector3.up) * position;
transform.position = position;
编辑.我不知道为什么示例中的是负号,也许它也应该在我的代码中.
EDIT. I don't know why in example is a minus, maybe it should be in my code too.
transform.rotation =四元数.Euler(0,-Input.compass.trueHeading, 0);
transform.rotation = Quaternion.Euler(0, -Input.compass.trueHeading, 0);
这篇关于将GPS统一坐标为(X,Y,Z),向北旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!