需要从Sensor.TYPE_ORIENTATION数据计算旋转矢量

Sim*_*mon 8 android opengl-es vector matrix sensor

我需要从Sensor.TYPE_ORIENTATION获得的数据中计算出一个旋转矢量.

传感器数据定义如下:

  • 必须重新计算这些值才能成为正确的3d位置:值[0]:方位角,磁北方向和Y轴之间的角度,绕Z轴(0到359).0 =北,90 =东,180 =南,270 =西
  • 1:俯仰,绕X轴旋转(-180到180),当z轴向y轴移动时为正值.
  • 值[2]:滚动,绕Y轴旋转(-90到90),当x轴远离z轴时带正值

我需要所有三个值,如Z轴值(从0到360度).我尝试了很多,但无法弄清楚如何做到这一点:/

(编辑:要查看工作代码背后的想法,请查看DroidAR框架ActionWithSensorProcessing类,您可以看到如何处理不同的事件类型.)

我还尝试使用Sensor.TYPE_ACCELEROMETER和Sensor.TYPE_MAGNETIC_FIELD来自行计算这个3d矢量.这是代码:

final float[] inR = new float[16];
// load inR matrix from current sensor data:
SensorManager.getRotationMatrix(inR, null, gravityValues, geomagneticValues);
float[] orientation = new float[3];
SensorManager.getOrientation(inR, orientation);
mapMagAndAcclDataToVector(orientation); //here i do some *360 stuff
orientetionChanged(orientation); //then the correct values are passed (in theorie)
Run Code Online (Sandbox Code Playgroud)

但这不起作用,我认为这很复杂.所以我打赌有一个简单的解决方案,如何重新计算ensor.TYPE_ORIENTATION的值,使它们成为一个3d旋转矢量,但我只是不知道该怎么做.如果你知道答案,请告诉我.

编辑:我应该添加,我想将值传递给OpenGL,使其与轴对齐,如下所示:

gl.glRotatef(values[1], 1, 0, 0);
gl.glRotatef(values[2], 0, 1, 0);
gl.glRotatef(values[0], 0, 0, 1);
Run Code Online (Sandbox Code Playgroud)

但由于奇怪的价值范围,我显然无法做到这一点.与SensorManager.getOrientation中的值相同的问题,因为当您通过水平线时它们也会翻转.

另一个解决方案是使用inR矩阵计算角度,但我认为必须有一个更简单的解决方案(?)

har*_*ara 12

这应该工作(使用Sensor.TYPE_MAGNETIC_FIELD和Sensor.TYPE_ACCELEROMETER输入)

switch (event.sensor.getType()) {
    case Sensor.TYPE_MAGNETIC_FIELD:
        magnitude_values = event.values.clone();
        sensorReady = true;
        break;
    case Sensor.TYPE_ACCELEROMETER:
        accelerometer_values = event.values.clone();
    }   

    if (magnitude_values != null && accelerometer_values != null && sensorReady) {
        sensorReady = false;

        float[] R = new float[16];
        float[] I = new float[16];

        SensorManager.getRotationMatrix(R, I, this.accelerometer_values, this.magnitude_values);

        float[] actual_orientation = new float[3];
        SensorManager.getOrientation(R, actual_orientation);
 }
Run Code Online (Sandbox Code Playgroud)

把这个conde放在你的onSensorChanged中.在actual_orientation中,您将获得指向北方的向量与您的实际位置相关.

如果你想从摄像机的角度来检测北方,你必须像这样改变最后一行代码

float[] outR = new float[16];
SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_X, SensorManager.AXIS_Z, outR);
SensorManager.getOrientation(outR, actual_vals); 
Run Code Online (Sandbox Code Playgroud)


Sim*_*mon 4

好吧,现在我有了一个解决方案,你必须按以下顺序进行 openGl 旋转:

gl.glRotatef(values[2], 0, 1, 0);
gl.glRotatef(values[1], 1, 0, 0);
gl.glRotatef(values[0], 0, 0, 1);
Run Code Online (Sandbox Code Playgroud)

将此与 SensorManager.getOrientation 结合使用,它将起作用。如果有人知道如何使用 Sensor.TYPE_ORIENTATION 数据执行此操作,请告诉我。