位置变化的极坐标转换为直角坐标系

3
我在SO和Unity Answers上搜索了好几个星期,但没能解决这个问题。我在Unity中有一个3D对象,其实例是基于用户当前的纬度/经度(假设为41.23423、-122.87978)生成的,当用户位置变化时,我想将该对象移动到新的纬度/经度。
因此,41.23423、-122.87978在我的3D环境中变成了0,0,0,以那个初始位置为基础进行更改。
我使用C#进行编码,当位置发生变化时,比较两组纬度/经度并计算距离和角度,然后进行极坐标到直角坐标的转换,并将对象移动到新位置。
我的问题在于更新会移动对象,但产生一些不稳定的结果,我猜测这是代码问题。例如,如果我在两个位置之间切换,第一个更改会移动到(131.6, 0.0, 0.0),但是切换回去不会移动对象。或者在测试过程中,我也看到过第一个更改会产生意外的坐标,但任何后续更改(在两点之间切换)都会产生相对于每个位置的相同的相对值。
我对我正在做的错误没有很好的掌握,所以非常感谢任何帮助。如果这个问题太模糊或是一个重复的问题(代码问题或者可能我没有正确地解决问题),请提前道歉。
下面是移动基于位置的类的代码。
using UnityEngine;
using System.Collections;
using System;

public class CameraMover : MonoBehaviour {
const double PIx = 3.141592653589793;
const double RADIO = 20925721.8;
public CoreLocationData lastLocationData;
public int firstUpdate = 0;
private Vector3 currentVector;
private Vector3 newVector;
// Use this for initialization
void OnEnable () {
CoreLocationManager.locationServicesDidUpdate += locationServicesDidUpdate;

}

// Update is called once per frame
void Update () {

}

public void locationServicesDidUpdate( CoreLocationData locationData )
{
    if (firstUpdate == 0)
    lastLocationData = locationData;

    double newDistance = DistanceBetweenPlaces(lastLocationData.longitude, lastLocationData.latitude, locationData.longitude, locationData.latitude);
    double angleToMove = Angle(lastLocationData.latitude, lastLocationData.longitude, locationData.latitude, locationData.longitude);

    double newX = (newDistance * Math.Cos(angleToMove));
    double newZ = (newDistance * Math.Sin(angleToMove));
    float newXfloat = System.Convert.ToSingle(newX);
    float newZfloat = System.Convert.ToSingle(newZ);

    currentVector = this.gameObject.transform.position;
    newVector = new Vector3(newXfloat, 0.0F, newZfloat);
    this.gameObject.transform.position = Vector3.Lerp(currentVector, newVector, 1);

    Debug.Log(string.Format("Distance To Move is {0}, angle is {1}", newDistance, angleToMove));
    Debug.Log(string.Format("Moving from {0} to {1}", currentVector, newVector));
    lastLocationData = locationData;

    firstUpdate = 1;

}

public static double Radians(double x)
{
    return x * PIx / 180;
}

public static double DistanceBetweenPlaces(
    double lon1,
    double lat1,
    double lon2,
    double lat2)
{
    double dlon = Radians(lon2 - lon1);
    double dlat = Radians(lat2 - lat1);

    double a = (Math.Sin(dlat / 2) * Math.Sin(dlat / 2)) + Math.Cos(Radians(lat1)) * Math.Cos(Radians(lat2)) * (Math.Sin(dlon / 2) * Math.Sin(dlon / 2));
    double angle = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
    return angle * RADIO;
}


public double Angle(double px1, double py1, double px2, double py2)
{
// Negate X and Y values
double pxRes = px2 - px1;
double pyRes = py2 - py1;
double angle = 0.0;
// Calculate the angle
if (pxRes == 0.0)
{
   if (pxRes == 0.0)
         angle = 0.0;
   else if (pyRes > 0.0)          angle = System.Math.PI / 2.0;
   else
         angle = System.Math.PI * 3.0 / 2.0;
}
else if (pyRes == 0.0)
{
   if (pxRes > 0.0)
         angle = 0.0;
   else
         angle = System.Math.PI;
}
else
{
   if (pxRes < 0.0)
         angle = System.Math.Atan(pyRes / pxRes) + System.Math.PI;
   else if (pyRes < 0.0)          angle = System.Math.Atan(pyRes / pxRes) + (2 * System.Math.PI);
   else
         angle = System.Math.Atan(pyRes / pxRes);
}
// Convert to degrees
angle = angle * 180 / System.Math.PI;return angle;

}
}

编辑

正确的实现方式:

using UnityEngine;
using System.Collections;
using System;

public class CameraMover : MonoBehaviour
{
  const double RATIO = 20902231.0029;
  public CoreLocationData lastLocationData;
  public int firstUpdate = 0;
  private Vector3 currentVector;
  private Vector3 newVector;

  // Use this for initialization
  void OnEnable ()
  {
    CoreLocationManager.locationServicesDidUpdate += locationServicesDidUpdate;
  }

  // Update is called once per frame
  void Update () { }

  public void locationServicesDidUpdate( CoreLocationData locationData )
  {
    if (firstUpdate == 0)
    {
      lastLocationData = locationData;
    }

    double newDistance = DistanceBetweenPlaces(lastLocationData.longitude, lastLocationData.latitude, locationData.longitude, locationData.latitude);
    double angleToMove = AngleBetweenPlaces(lastLocationData.latitude, lastLocationData.longitude, locationData.latitude, locationData.longitude);
    double angleToMoveRadians = Deg2Rad(angleToMove);
    double newX = (newDistance * Math.Sin(angleToMoveRadians));
    double newZ = (newDistance * Math.Cos(angleToMoveRadians));
    float newXfloat = System.Convert.ToSingle(newX) * -1;
    float newZfloat = System.Convert.ToSingle(newZ) * -1;

    Debug.Log(string.Format("new x coordinate should be {0} and z should be {1}", newXfloat, newZfloat));

    currentVector = this.gameObject.transform.position;
    this.gameObject.transform.position = new Vector3((currentVector.x + newXfloat),0, (currentVector.z + newZfloat));
    lastLocationData = locationData;
    firstUpdate = 1;
  }

  public static double DistanceBetweenPlaces(
    double lon1,
    double lat1,
    double lon2,
    double lat2)
  {
    double phi1 = Deg2Rad(lat1);
    double phi2 = Deg2Rad(lat2);
    double deltaLamdba = Deg2Rad(lon2 - lon1);
    double d = Math.Acos(
      (Math.Sin(phi1) * Math.Sin(phi2)) + 
      (Math.Cos(phi1) * Math.Cos(phi2) * Math.Cos(deltaLamdba)));

    return d * RATIO;
  }

  public static double Deg2Rad(double degrees)
  {
    return degrees * (Math.PI / 180.0);
  }

  public static double Rad2Deg(double radians)
  {
    return radians * (180.0 / Math.PI);
  }

  public double AngleBetweenPlaces(double lat1, double lon1, double lat2, double lon2)
  {
    var newLat1 = Deg2Rad(lat1);
    var newLat2 = Deg2Rad(lat2);
    var dLon = Deg2Rad(lon2) - Deg2Rad(lon1);
    var y = Math.Sin(dLon) * Math.Cos(newLat2);
    var x = Math.Cos(newLat1) * Math.Sin(newLat2) - Math.Sin(newLat1) * Math.Cos(newLat2) * Math.Cos(dLon);
    var brng = Math.Atan2(y, x);
    return (Rad2Deg(brng) + 360) % 360;
  }
}

我可能没有足够的三角知识来调试你的代码,但我可能能够找到另一个解决方案。然而,我有一点难以理解这个问题。这是我对你的问题的理解:你希望实例化一个对象,在地球仪上与玩家保持相同的相对位置。这正确吗? - Rhys van der Waerden
基本问题是我正在从我的服务器下载标记,这些标记实际上是地理缓存数据(图片、消息等),而且这些对象都有静态的纬度/经度。我将用户放置在0,0位置,然后随着用户位置的变化,我需要移动用户,同时保持其他对象相对于用户的正确距离(例如,用户在0,0位置被实例化,并有一个在10,0位置的对象,然后用户移动到相当于5,0的纬度/经度,那么10,0位置的对象现在距离用户为5个单位)。 - johnnyclem
1个回答

1

在某些地方,您正在使用System.Math.PI,而在其他地方,您正在使用PIx = 3.141592653589793 - 这是什么原因?此外,RADIO是什么,从其用途来看,我会认为它是地球的平均半径(以公里为单位),但该值偏差很大。

此外,DistanceBetweenPlaces方法存在一些错误,无论如何,请尝试以下操作,如果精度有问题,则可以使用Vincenty's formulae,否则请坚持使用简单的余弦定理。

// Earths Mean Radius in Kilometres?
public const double RADIO = 6371;

public static double DistanceBetweenPlaces(
    double lon1,
    double lat1,
    double lon2,
    double lat2)
{
  double phi1 = Deg2Rad(lat1);
  double phi2 = Deg2Rad(lat2);
  double deltaLamdba = ConvertDegreesToRadians(lon2 - lon1);
  double d = Math.Acos((Math.Sin(phi1) * Math.Sin(phi2)) + (Math.Cos(phi1) * Math.Cos(phi2) * Math.Cos(deltaLamdba)));
  return d * RADIO;

}

public static double Deg2Rad(double degrees)
{
    return degrees == 0 ? degrees : (degrees * Math.PI / 180.0);
}

另外,查看这个处理大地测量数据的绝佳资源。

http://www.movable-type.co.uk/scripts/latlong.html


Fraser,谢谢你的回复。我会在今天尝试并反馈结果。我不确定为什么我之前使用了PIx和Math.PI,但这两者是可以互换的。我想我只是借用了将角度转换为弧度的方法,该方法引用了PIx常量变量,但还没有更新为使用Math.PI。RADIO变量是地球的半径,但以英尺为单位,因为英尺是我在三维空间中使用的x、y、z坐标的测量单位。 - johnnyclem
如果是英尺的话,应该是20902231而不是20925721.8。此外,看一下这篇博客,它提供了我提到的Vincenty算法的很好的实现 - http://www.gavaghan.org/blog/free-source-code/geodesy-library-vincentys-formula/ - 这个页面包含了你可能需要的每个计算(使用JavaScript编写,易于翻译)http://www.movable-type.co.uk/scripts/latlong.html。 - Fraser
1
非常感谢。 我实现了您的方法并修复了我的常量,我的日志开始报告正确的距离和角度,但是在两个地点之间跳转始终产生余数:我会从0,0开始前往新位置123,12,然后回到原始位置,它将位于77,-35而不是0,0。 原来是因为我没有转换回度数(doh!)。 现在已在帖子中插入了正确的代码,供其他人尝试使用。 再次感谢,我曾经担心在SO上发布很长时间会被攻击,但我的第一次经历很棒。 - johnnyclem
没关系,很高兴能帮助到您。 - Fraser

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接