1. Download our Official Android App: Forums for Android!

Apps [SOLVED, was a bug] screen rotation and remapCoordinateSystem

Discussion in 'Android Development' started by werner291, Aug 11, 2013.

  1. werner291

    werner291 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    8
    Joined:
    Apr 8, 2013

    Apr 8, 2013
    8
    0
    5
    Hello,

    I'm developing a game where you have to fly some kind of plane, and I'd like to use sensor-based controls. I've got it working on one device in one orientation, but it totally fails in any other situation. It must be related to this piece from the remapCoordinateSystem() documentation:

    In "remapCoordinateSystem (float[] inR, int X, int Y, float[] outR)", what are X and Y?

    Here's my code so far:
    Code (Text):
    1. public void onSensorChanged(SensorEvent event) {
    2.         if (event.sensor == mAccelerometer) {
    3.             System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length);
    4.             mLastAccelerometerSet = true;
    5.         } else if (event.sensor == mMagnetometer) {
    6.             System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length);
    7.             mLastMagnetometerSet = true;
    8.         }
    9.         if (mLastAccelerometerSet && mLastMagnetometerSet) {
    10.             SensorManager.getRotationMatrix(mR, null, mLastAccelerometer, mLastMagnetometer);
    11.             // Fix acellerometer behaving differently on diferent devices
    12.             int rotation = ((WindowManager) getApplicationContext().getSystemService(this.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
    13.             switch (rotation){
    14.                 case Surface.ROTATION_0:
    15.                     SensorManager.remapCoordinateSystem(mR, SensorManager.AXIS_X, SensorManager.AXIS_Y, mR);
    16.                     break;
    17.                 case Surface.ROTATION_90:
    18.                     SensorManager.remapCoordinateSystem(mR, SensorManager.AXIS_Y, SensorManager.AXIS_X, mR);
    19.                     break;
    20.                 case Surface.ROTATION_180:
    21.                     SensorManager.remapCoordinateSystem(mR, SensorManager.AXIS_MINUS_X, SensorManager.AXIS_MINUS_Y, mR);
    22.                     break;
    23.                 case Surface.ROTATION_270:
    24.                     SensorManager.remapCoordinateSystem(mR, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, mR);
    25.                     break;
    26.                 // Do not remap if ROTATION_0
    27.             }
    28.             SensorManager.getOrientation(mR, mOrientation);
    29.             Log.i("", String.format("Pitch: %f %f", mOrientation[1] * 180 / Math.PI, mOrientation[2] * 180 / Math.PI));
    30.         }
    31.  
    32.         float requestedPitch = -(mOrientation[1] - pitchReference) * 2f;
    33.         if (requestedPitch > 1f) requestedPitch = 1f;
    34.         else if (requestedPitch < -1f) requestedPitch = -1f;
    35.  
    36.         float requestedYaw = -(mOrientation[2]) * 2f;
    37.         if (requestedYaw > 1f) requestedYaw = 1f;
    38.         else if (requestedYaw < -1f) requestedYaw = -1f;
    39.  
    40.         mGameManager.setSteer(invertX?-requestedYaw:requestedYaw,invertY?-requestedPitch:requestedPitch);
    41.     }
    EDIT (modified code): I'm starting to get it... the controls work for both 0 and 180, but on the 90 and 270 rotations, the pitch and the roll seem to allways be exactly equal?
     

    Advertisement

  2. werner291

    werner291 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    8
    Joined:
    Apr 8, 2013

    Apr 8, 2013
    8
    0
    5
    Solved. It appears that, contrary to the documentation, inR and outR CANNOT be the same array or it might cause data corruption.
    I've just submitted a bug report.
     

Share This Page

Loading...