Question

Ive notice when I hold my phone just below the horizon I get > -90. When I begin to tilt the phone so its pointing towards the sky it reflects around -90 so, -88, -90, -88. As its tilted from the ground to the sky.

Has anyone experienced this before. (It doesnt seem to be related to the remapCoordinateSystem). (Ive previously commented it out). When the phone camera is pointed towards the ground the pitch reading is zero. When its pointed towards the ceiling its also zero.

Thanks for any help.

    @Override
    public void onSensorChanged(SensorEvent event) {
        synchronized (MainActivity.this) { // TilteController
            switch (event.sensor.getType()) {
            case Sensor.TYPE_MAGNETIC_FIELD:
                mMagneticValues = event.values.clone();
                break;
            case Sensor.TYPE_ACCELEROMETER:
                mAccelerometerValues = event.values.clone();
                break;
            }

            if (mMagneticValues != null && mAccelerometerValues != null) {
                SensorManager.getRotationMatrix(R, null, mAccelerometerValues, mMagneticValues);

                Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
                int rotation = display.getRotation();

                switch (rotation)
                {
                case Configuration.ORIENTATION_LANDSCAPE:
                    SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_MINUS_X, R);//shouldnt be the same R in and out
                case Configuration.ORIENTATION_PORTRAIT:
                    SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_Y, SensorManager.AXIS_X, R);//shouldnt be the same R in and out
                }


                float[] orientation = new float[3];
                SensorManager.getOrientation(R, orientation);

                mAzimuth = orientation[0];
                mPitch = orientation[1];
                mRoll = orientation[2];

                dirText.setText("Azimuth, Pitch, Roll || " + radStr(mAzimuth) +", "+ radStr(mPitch) +", "+ radStr(mRoll));

                glView.rotate((float)Math.toDegrees(mAzimuth),(float)Math.toDegrees(mPitch),(float)Math.toDegrees(mRoll));
                //glView.azimuth=(float)Math.toDegrees(smooth(mAzimuth));
                glView.pitch=(float)(Math.toDegrees(smooth(mPitch)));//-90 makes it cenetr
                //glView.roll=(float)Math.toDegrees(smooth(-mRoll));
                //Log.i("Azimuth, Pitch, Roll", mAzimuth+", "+mPitch+", "+mRoll);
            }
        }
    }

A temporary fix for my needs involves rotating the matrix before calling get orientation.

Matrix.rotateM(R, 0, 90, 0, 1, 0);

How ever you do a complete flip, it experiences the same issue. (This should be solved by adding in Azimuth) But it isnt a brilliant solution.

So if others are reading this and are trying to make the horizon 0 degrees. Rotate the matrix before, as oppose to rotating your display (Im using OpenGL)

Was it helpful?

Solution

Ending up locking the App in landscape and applying the following

    SensorManager.getRotationMatrix(R, null, mAccelerometerValues, mMagneticValues);

    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int rotation = display.getRotation();

    SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_X, SensorManager.AXIS_Z, R);//shouldnt be the same R in and out

    float[] orientation = new float[3];
    SensorManager.getOrientation(R, orientation);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top