Search code examples
androidopengl-eslinear-algebraandroid-sensorsrotational-matrices

Android Rotation Vector and Rotation Matrix


So I previously asked a question more about what ind of sensor to use for the application I'm building. After some research I know now that it's the rotation vector that I want to use for I want to be able to move a virtual cube on my tablet according to the orientation of the device.

Let's imagine the tablet is in a given orientation O1 and therefore the orientation vector gives me a rotation matrix OM1 Let's imagine now that I lock the screen and move the tablet and rotate it so that it is afterwards, just before unlocking the screen in a given Orientation O2 and the orientation vector (which is stopped during the locked screen) will give when the screen is back on a rotation matrix OM2.

I want my virtual cube to remain in the position it was before the screen was locked when I unlock it again.

Of course for that I have already set up a rotation matrix in my code that keeps in memory the rotation matrix when the system pauses (i.e when I lock the screen). So that when the screen is back the cube is in the right position.

However, when I move my tablet again (rotation) I want the cube to respond to these rotations according to the orientation matrix OM1 it had before the screen was locked and not just based on the orientation matrix that is given at any moment by the rotation vector sensor.

I am guessing that I need to compute somehow a differenceMatrix that will give me how to go from OM1 to OM2. But even with that I don't get what I want yet.

Here are the few operations i've tried:

if(hasLocked == true){
      Matrix.transposeM(transposed,0,mRotationLock,0); //mRotationLock is the rotation matrix that I save when the screen goes off
      Matrix.multiplyMM(mDifferenceRotation, 0, transposed, 0, mRotationMatrix, 0);
      System.arraycopy(mDifferenceRotation,0,mRotationMatrix,0,16);
}

Solution

  • You're missing another matrix you must store and transposing the wrong matrix.

    In addition to the mRotationLock (OM1), when the screen come unlocked you should store the opposite of the current OM2 matrix called OM20T below. You can do this with a transpose. If you had more code I'd show that bit. Then the calculation is:

    rotationMatrix = OM1 * OM20T * OM2 
    

    In other words we are rotating with OM2 to apply the current vector, then OM20T to undo the first direction vector we were facing in after screen on. Then OM1 to apply the rotate as it was before screen off. OM1 * OM20T can be calculated once on screen on.