Java 如何旋转opengl 3d对象以指向GPS位置(纬度、经度)?

Java 如何旋转opengl 3d对象以指向GPS位置(纬度、经度)?,java,android,opengl-es,gps,orientation,Java,Android,Opengl Es,Gps,Orientation,我用OpenGL绘制了一个指向坐标0、0、0的3D箭头,我希望它指向一个特定的GPS位置,具体取决于我的GPS位置和方向 我试着用手机的方位来计算方位角,并将其调整为真实的北方而不是磁北方 SensorManager.getOrientation(remappedRotationMatrix, orientation); // convert radians to degrees float azimuth = orientation[0]; azimuth = azi

我用OpenGL绘制了一个指向坐标0、0、0的3D箭头,我希望它指向一个特定的GPS位置,具体取决于我的GPS位置和方向

我试着用手机的方位来计算方位角,并将其调整为真实的北方而不是磁北方

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;
    }
有办法吗?另一种可能是将GPS位置转换为OpenGL坐标并使用GLU.gluLookAt指向它吗


谢谢。

这似乎纯粹是一道数学题

你的问题很模糊,如果不更准确地理解你的场景是如何设置的,你想要什么,我想我帮不了你


你知道如何使用三维旋转矩阵吗?如果没有,您可能应该了解它们是如何工作的。

计算轴承,然后按您得到的角度旋转箭头,应该不会太复杂。我在2D中也做了同样的事情,虽然不是在OpenGL中。我的代码基于雷达样本http://apps-for-android.googlecode.com/svn/trunk/Radar/. 以下是我如何绘制二维箭头:

    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只是传感器侦听器的方向。因此,我们的想法是计算你想要指向mBearing的GPS位置的方位和手机mOrientation的当前方向之间的差异。这为我们提供了指向目标的角度。然后,在沿y轴绘制箭头之前,将视图围绕其中心旋转该角度。这与绘制按方向旋转到目标度的箭头相同


在绘制箭头之前,您应该能够在OpenGL中应用相同的逻辑,方法是围绕屏幕中心旋转视图,旋转角度为目标度。旋转的确切位置取决于视图的设置方式。为了简单起见,将箭头的起点设为原点。然后可以使用glRotatef简单地围绕原点旋转。否则,您将首先需要平移到旋转的中心,旋转,然后再平移回来。这是一种常见的OpenGL技术,用于围绕一个点旋转。

我编辑了这个问题,以显示我的一些代码,以便人们能够更好地理解我的问题,我只是昨天做不到。您的代码看起来是正确的。当你运行它的时候,有什么东西不起作用呢?箭头会不停地旋转,除非我把手机放在一个特定的位置,它似乎可以工作,但是如果我稍微移动一下手机,箭头就会发疯。我认为手机的传感器是问题所在,getOrientation函数提供了非常不同的数据,如果没有必要,我无法找到不移动箭头的方法。我编辑了这个问题以显示我的一些代码,这样人们可以更好地理解我问的问题,我只是昨天做不到。
    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();