1

我有一个用 OpenGL 绘制的 3D 箭头,它指向坐标 (0, 0, 0),我希望它根据我的 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;
    }

并计算我必须旋转箭头的度数

gl.glRotatef(degrees, 0.0f, 1.0f, 0.0f);
arrow.draw(gl);

有什么办法吗?另一种可能性是将 GPS 位置转换为 OpenGL 坐标并使用 GLU.gluLookAt 指向它吗?

谢谢。

4

2 回答 2

0

这似乎是一个纯粹的数学问题。

你的问题很模糊,如果不更准确地了解你的场景是如何设置的以及你想要什么,我认为我无法帮助你。

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

于 2012-04-12T23:06:09.493 回答
0

计算方位然后将箭头旋转你得到的度数应该不复杂。我在 2D 中做了同样的事情,虽然不是在 OpenGL 中。我的代码基于 Radar 示例 (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 是使用 Radar 样本中的 GeoUtils.bearing 方法计算的,该方法负责复杂的数学运算。mOrientation 只是传感器侦听器的方向。所以这个想法是计算你想要指向的 GPS 位置的方位(mBearing)和手机的当前方向(mOrientation)之间的差异。这给了我们角轴承ToTarget。然后,在沿 y 轴绘制箭头之前,我们将视图围绕其中心旋转该角度。这与绘制以bearingToTarget 度数旋转的箭头相同。

在绘制箭头之前,您应该能够通过将视图围绕屏幕中心旋转 BearingToTarget 度数来在 OpenGL 中应用相同的逻辑。您旋转的确切点取决于您的视图是如何设置的。为简单起见,请将箭头的起点放在原点。然后您可以使用 glRotatef 简单地围绕原点旋转。否则,您首先需要平移到旋转中心,旋转然后再平移回来(这是用于围绕点旋转的常见 OpenGL 技术)。

于 2012-04-12T23:21:38.250 回答