Need to calculate rotation-vector from Sensor.TYPE_ORIENTATION data

☆樱花仙子☆ 提交于 2019-12-02 18:36:05

This should work (using Sensor.TYPE_MAGNETIC_FIELD and Sensor.TYPE_ACCELEROMETER inputs)

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);
 }

Put this conde in your onSensorChanged. In actual_orientation you will have the vector that points to north respect to your actual position.

if you want to detect the north from the camera point of view you have to change the last line of code like this

float[] outR = new float[16];
SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_X, SensorManager.AXIS_Z, outR);
SensorManager.getOrientation(outR, actual_vals); 

ok now i got a solution, you have to do the openGl rotation in this order:

gl.glRotatef(values[2], 0, 1, 0);
gl.glRotatef(values[1], 1, 0, 0);
gl.glRotatef(values[0], 0, 0, 1);

use this in combination with SensorManager.getOrientation and it will work. if someone knows how to do this with the Sensor.TYPE_ORIENTATION data, please let me know.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!