我有一個用OpenGL繪製的指向座標(0,0,0)的3D箭頭,我希望它根據我的GPS位置和方向指向特定的GPS位置。如何旋轉opengl 3d對象以指向GPS位置(lat,long)?
我試着計算方位角(用我的手機的方向),並將其調整爲真正的北(不是磁北)。
SensorManager.getOrientation(remappedRotationMatrix, orientation);
// convert radians to degrees
float azimuth = orientation[0];
azimuth = azimuth * 360/(2 * (float) Math.PI);
GeomagneticField geoField = new GeomagneticField(
Double.valueOf(loc.getLatitude()).floatValue(),
Double.valueOf(loc.getLongitude()).floatValue(),
Double.valueOf(loc.getAltitude()).floatValue(),
System.currentTimeMillis());
// converts magnetic north into true north
azimuth -= geoField.getDeclination();
然後從我的位置獲取方位到我想指向的位置。
target.setLatitude(42.806484);
target.setLongitude(-1.632482);
float bearing = loc.bearingTo(target); // (it's already in degrees)
if (bearing < 0) {
bearing = bearing + 360;
}
float degrees = bearing - azimuth;
if (degrees < 0) {
degrees = degrees + 360;
}
和計算我需要旋轉箭頭
gl.glRotatef(degrees, 0.0f, 1.0f, 0.0f);
arrow.draw(gl);
有什麼方法做到這一點的程度?另一種可能性是將GPS位置轉換爲OpenGL座標並使用GLU.gluLookAt指向它?
謝謝。
我已經編輯了這個問題來顯示我的一些代碼,這樣人們就可以更好地理解我在問什麼,我昨天就做不到了。 – richartidus 2012-04-13 08:44:58