2012-04-12 55 views
1

我有一個用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指向它?

謝謝。

回答

0

這似乎只是一個數學問題。

你的問題非常含糊,我不認爲我可以幫助你,但不能更精確地理解你的場景是如何設置和你想要的。

你知道如何使用3D旋轉矩陣嗎?如果沒有,你可能應該學習他們的工作方式。

+0

我已經編輯了這個問題來顯示我的一些代碼,這樣人們就可以更好地理解我在問什麼,我昨天就做不到了。 – richartidus 2012-04-13 08:44:58

0

計算方位不應該很複雜,然後按照您獲得的度數旋轉箭頭。我在2D中完成了同樣的操作,但不是在OpenGL中。我將我的代碼基於雷達示例(http://apps-for-android.googlecode.com/svn/trunk/Radar/)。這裏是我繪製2D箭頭:

double bearingToTarget = mBearing - mOrientation; 

    // Draw an arrow in direction of target 
    canvas.rotate((float) bearingToTarget, center, center); 
    final int tipX = center; 
    final int tipY = center-radius; 
    canvas.drawLine(center, center, tipX, tipY, mArrowPaint); 
    final int tipLen = 30; 
    final int tipWidth = 20; 
    Path path = new Path(); 
    path.moveTo(tipX, tipY); 
    path.lineTo(tipX + tipWidth/2, tipY + tipLen); 
    path.lineTo(tipX - tipWidth/2, tipY + tipLen); 
    path.lineTo(tipX, tipY); 
    path.close(); 
    canvas.drawPath(path, mArrowPaint); 
    canvas.restore(); 

mBearing使用從雷達樣品這需要複雜的數學運算的護理方法GeoUtils.bearing計算。 mOrientation只是傳感器監聽器的方向。所以我們的想法是計算您想要指向的GPS位置的方位(mBearing)與手機的當前方向(mOrientation)之間的差異。這給了我們angleToTarget的角度。然後,我們沿着y軸繪製箭頭之前,按照該角度旋轉關於其中心的視圖。這與繪製由bearingToTarget度旋轉的箭頭相同。

您應該能夠在OpenGL中應用相同的邏輯,方法是在繪製箭頭之前旋轉關於屏幕中心的視圖,方法是使用bearingToTarget度數。究竟你旋轉什麼點取決於你的視圖是如何設置的。爲了簡單起見,請在原點處製作箭頭的起點。然後你可以簡單地使用glRotatef關於原點旋轉。否則,你首先需要翻譯到旋轉的中心,然後再旋轉,然後再翻譯(這是常見的圍繞某個點旋轉的OpenGL技術)。

+0

我已經編輯了這個問題來顯示我的一些代碼,這樣人們可以更好地理解我在問什麼,我昨天就做不到了。 – richartidus 2012-04-13 08:44:44

+0

你的代碼看起來正確。運行它時究竟有什麼不工作? – Josh 2012-04-13 19:33:24

+0

除非將手機放在特定的位置,否則箭頭會轉動,但如果我將手機稍微移動一點,箭頭就會變得瘋狂。我認爲手機的傳感器是問題,getOrientation函數提供了非常不同的數據,如果沒有必要,我無法找到不移動箭頭的方法 – richartidus 2012-04-15 11:31:49