Android: Problems calculating the Orientation of the Device

前端 未结 4 490
耶瑟儿~
耶瑟儿~ 2020-12-30 09:14

i\'am trying to build a simple Augmented Reality App, so I start working with sensor Data.

According to this thread (Android compass example) and ex

4条回答
  •  情书的邮戳
    2020-12-30 09:42

    Answer from Tíbó is good, but if you log roll value, you will expect irregular numbers. (roll is important for AR Browsers)

    This is due to

    SensorManager.remapCoordinateSystem(mRotationMatrix,
                        SensorManager.AXIS_X, SensorManager.AXIS_Z,
                        mRotationMatrix);
    

    You have to use different matrix for in and out of remap. This following code works for me with a correct roll value:

    @Override
    public void onSensorChanged(SensorEvent event)
    {
        // It is good practice to check that we received the proper sensor event
        if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR)
        {
            // Convert the rotation-vector to a 4x4 matrix.
            SensorManager.getRotationMatrixFromVector(mRotationMatrixFromVector, event.values);
            SensorManager.remapCoordinateSystem(mRotationMatrixFromVector,
                        SensorManager.AXIS_X, SensorManager.AXIS_Z,
                        mRotationMatrix);
            SensorManager.getOrientation(mRotationMatrix, orientationVals);
    
            // Optionally convert the result from radians to degrees
            orientationVals[0] = (float) Math.toDegrees(orientationVals[0]);
            orientationVals[1] = (float) Math.toDegrees(orientationVals[1]);
            orientationVals[2] = (float) Math.toDegrees(orientationVals[2]);
    
            tv.setText(" Yaw: " + orientationVals[0] + "\n Pitch: "
                    + orientationVals[1] + "\n Roll (not used): "
                    + orientationVals[2]);
    
        }
    }
    

提交回复
热议问题