极地笛卡尔转换位置变化

时间:2012-02-14 02:50:12

标签: ios geospatial computational-geometry unity3d

我几周来一直在搜索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;
  }
}

1 个答案:

答案 0 :(得分:0)

在某些时候,您使用System.Math.PI,而在其他人使用的是PIx = 3.141592653589793 - 这是什么原因?另外,RADIO是什么,从它的使用我可以假设它是地球的平均半径,以Km为单位,但价值是可以的。

同样,DistanceBetweenPlaces方法有几个错误,无论如何,尝试以下方法,如果你对准确性有问题,那么我会使用Vincenty's formulae,否则坚持简单的余弦球面定律。 / p>

// 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