我正在尝试制作一个指向Unity程序中自定义位置的指南针。但是,它是完全不正确的,它通常指向错误的方向,我似乎还不太清楚我的逻辑出了什么问题。
我在这里使用弯曲的几何轴承方程:http : //www.yourhomenow.com/house/haversine.html
相对于真实北方旋转屏幕指南针。
// Calculate bearing between current location and target location
// Get current GPS location
float lat1 = Input.location.lastData.latitude;
float lon1 = Input.location.lastData.longitude;
float lat2 = TargetLatitude;
float dLon = TargetLongitude - lon1;
// Calculate bearing
var y = Mathf.Sin(dLon) * Mathf.Cos(lat2);
var x = Mathf.Cos(lat1) * Mathf.Sin(lat2) -
Mathf.Sin(lat1) * Mathf.Cos(lat2) * Mathf.Cos(dLon);
var brng = ((Mathf.Atan2(y, x)*(180.0f/Mathf.PI)) + 360.0f) % 360.0f;
Dump.text = brng.ToString();
// Rotate the to target location relative to north. Z axis pointing out of screen.
transform.eulerAngles = new Vector3(0, 0, Mathf.MoveTowardsAngle(transform.localEulerAngles.z, Input.compass.trueHeading + brng, COMPASS_MAXDELTA));
如果我删除该代码中的方位角偏移,则该方位角指向足以用于数字罗盘的北方。
确保所有角度都以弧度而不是度为单位。
Mathf.Deg2Rad
在这样的情况下使用Mathf.Sin(dLon * Mathf.Deg2Rad)
应产生正确的结果。Haversine公式仅适用于弧度角。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句