【发布时间】:2011-12-28 17:05:58
【问题描述】:
我正在尝试开发自己的增强现实引擎。
在互联网上搜索,我发现这很有用 tutorial。阅读它,我发现重要的是用户位置、点位置和北方之间的方位。
以下图片来自该教程。
接着,我写了一个Objective-C方法来获取beta:
+ (float) calculateBetaFrom:(CLLocationCoordinate2D)user to:(CLLocationCoordinate2D)destination
{
double beta = 0;
double a, b = 0;
a = destination.latitude - user.latitude;
b = destination.longitude - user.longitude;
beta = atan2(a, b) * 180.0 / M_PI;
if (beta < 0.0)
beta += 360.0;
else if (beta > 360.0)
beta -= 360;
return beta;
}
但是,当我尝试它时,效果并不好。
所以,我检查了 iPhone AR Toolkit,看看它是如何工作的(我一直在使用这个工具包,但它对我来说太大了)。
而且,在ARGeoCoordinate.m 中还有另一种如何获得测试版的实现:
- (float)angleFromCoordinate:(CLLocationCoordinate2D)first toCoordinate:(CLLocationCoordinate2D)second {
float longitudinalDifference = second.longitude - first.longitude;
float latitudinalDifference = second.latitude - first.latitude;
float possibleAzimuth = (M_PI * .5f) - atan(latitudinalDifference / longitudinalDifference);
if (longitudinalDifference > 0)
return possibleAzimuth;
else if (longitudinalDifference < 0)
return possibleAzimuth + M_PI;
else if (latitudinalDifference < 0)
return M_PI;
return 0.0f;
}
它使用这个公式:
float possibleAzimuth = (M_PI * .5f) - atan(latitudinalDifference / longitudinalDifference);
为什么 (M_PI * .5f) 在这个公式中?没看懂。
继续搜索,我发现另一个page 谈论如何计算两个位置的距离和方位。在此页面中还有另一个实现:
/**
* Returns the (initial) bearing from this point to the supplied point, in degrees
* see http://williams.best.vwh.net/avform.htm#Crs
*
* @param {LatLon} point: Latitude/longitude of destination point
* @returns {Number} Initial bearing in degrees from North
*/
LatLon.prototype.bearingTo = function(point) {
var lat1 = this._lat.toRad(), lat2 = point._lat.toRad();
var dLon = (point._lon-this._lon).toRad();
var y = Math.sin(dLon) * Math.cos(lat2);
var x = Math.cos(lat1)*Math.sin(lat2) -
Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
var brng = Math.atan2(y, x);
return (brng.toDeg()+360) % 360;
}
哪个是正确的?
【问题讨论】:
-
您解决过这个问题吗?我对您使用的解决方案感兴趣
-
是的,我必须尽快添加自己的答案。
标签: android iphone geolocation augmented-reality