使用旋转矢量传感器

dim*_*ima 6 java rotation orientation opengl-es-2.0 android-sensors

我想知道如何正确使用"旋转矢量传感器"的输出.目前我想出了以下内容并且想要计算偏航和俯仰result[],以便知道设备指向的位置(处于横向模式).但我的结果有问题.偏航计算非常精确但是音高表现得很奇怪.也许任何人都可以指出我如何使用数据的正确方向.我还想知道的另一件事是设备方向(横向或纵向)是否对此传感器的输出有任何影响.提前致谢.

private double max = Math.PI / 2 - 0.01;
private double min = -max;

private float[] rotationVectorAction(float[] values) {
    float[] result = new float[3];
    float vec[] = values;
    float quat[] = new float[4];
    float[] orientation = new float[3];
    SensorManager.getQuaternionFromVector(quat, vec);
    float[] rotMat = new float[9];
    SensorManager.getRotationMatrixFromVector(rotMat, quat);
    SensorManager.getOrientation(rotMat, orientation);
    result[0] = (float) orientation[0];
    result[1] = (float) orientation[1];
    result[2] = (float) orientation[2];     
    return result;
}

private void main () {
    float[] result = rotationVectorAction(sensorInput);
    yaw = result[0];
    pitch = result[1];
    pitch = (float) Math.max(min, pitch);
    pitch = (float) Math.min(max, pitch);
    float dx = (float) (Math.sin(yaw) * (-Math.cos(pitch)));
    float dy = (float) Math.sin(pitch);
    float dz = (float) (Math.cos(yaw) * Math.cos(pitch));
}
Run Code Online (Sandbox Code Playgroud)

在OpenGL ES 2.0中,我将其设置为移动相机:

Matrix.setLookAtM(mVMatrix, 0, 0, 0, 0, dx, dy, dz, 0, 1, 0);
Run Code Online (Sandbox Code Playgroud)

dim*_*ima 5

最后我自己解决了。音调无法正常工作的原因是输入和输出之间的差异SensorManager.getQuaternionFromVector(quat, vec);。该方法期望一个向量vec(x|y|z|w)输出quat如下所示(w|x|y|z)。就我而言,我只需要将quat数组的第一个值移到末尾,它就像一个魅力。