2012-02-14 22 views
3

我一直在尋找SO和統一的答案現在幾周,一直無法解決這個問題。我在Unity中有一個3d對象,基於用戶當前的緯度/經度(例如41.23423,-122.87978)實例化,然後我想要將對象移動到新的經緯度,只要它們的位置發生變化。極地笛卡爾轉換的位置變化

因此41.23423,-122,87978在我的3D環境中變成了0,0,0,並且我應用了相對於原始位置的變化。我在比較兩個緯度/經度並計算距離和角度,然後做一個極地到笛卡兒的轉換,並將對象移動到新的位置。

我的問題是,更新正在移動的對象,但產生一些不穩定的結果,我猜是我的代碼中的問題。例如,如果我在兩個位置之間切換,第一個更改將移動到(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; 
    } 
} 
+0

我可能沒有三角函數知識來調試代碼的工作,但我可能是能夠找到另一種解決方案。 但是,我對理解問題有點不解。這是我對你的問題的解釋:你想實例化一個對象,它將保持與地球上的玩家相同的相對位置。 這是正確的嗎? – 2012-02-14 20:44:23

+0

基本上問題是我正在從我的服務器下載基本上是地理緩存數據(圖片,消息等)的標記,而且這些對象都有一個靜態的經緯度。我將用戶置於0,0,然後隨着用戶的位置變化,我需要移動用戶,同時保持其他對象與用戶的適當相對距離(例如,用戶實例化爲0,0,對象爲10,0,然後用戶移至5,0的緯度/長度等價物,10,0的對象現在距離用戶5個單位) – johnnyclem 2012-02-15 17:46:05

回答

0

在您使用System.Math.PI和別人你正在使用PIx = 3.141592653589793一些點 - 究竟是什麼原因呢?另外,從什麼是RADIO,我認爲它是以千米爲單位的地球平均半徑,但價值是waaay。

另外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

+0

Fraser,謝謝你。我今天會嘗試並報告。我不知道爲什麼我使用PIx和Math.PI,但兩者是可以互換的,我想我只是借用了轉換爲弧度方法,該方法引用了PIx常量變量,並沒有更新它以使用Math.PI 。 RADIO變量是地球的半徑,但以英尺爲單位,因爲英尺是我在x空間中使用的x,y,z座標的度量單位 – johnnyclem 2012-02-15 17:47:47

+0

如果它的腳是20902231而不是20925721.8另外,請看一看在這個博客,給出了我提到的vincenty的一個很好的實現 - http://www.gavaghan.org/blog/free-source-code/geodesy-library-vincentys-formula/ - 這個頁面有我認爲你的每一個calc將需要(在javascipt,易於翻譯)http://www.movable-type.co.uk/scripts/latlong.html – Fraser 2012-02-15 21:02:55

+1

非常感謝。我實現了你的方法並且修正了我的常量,我的日誌開始報告正確的距離和角度,但是然後在兩個位置之間跳轉總會得到一個餘數:我將從0,0開始到123,12的新位置,然後回到原來的位置,它將在77,-35而不是0,0。事實證明,這是因爲我沒有轉換回度數(doh!)。正確的代碼現在插入在其他人試圖做到這一點。再次感謝,我擔心在SO上發帖很長一段時間,但我的第一次經歷很棒 – johnnyclem 2012-02-16 02:49:45