3

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);
}
LBes
  • 3,366
  • 1
  • 32
  • 66
  • Do you mean actually locking the screen? Like what happens when you turn a device screen off and on? – weston May 23 '15 at 06:54
  • Yes that's what I mean. My problem is not turning it on or off. Basically my problem lies within the rotation matrix. I'm guessing there are some computation I could do to get the results I'm looking for but I'm still looking for the answer ^^' – LBes May 23 '15 at 11:11
  • Suppose at O1 the device lies flat with the device y-axis points north and the device is locked. At O2 the device is rotated so that the x-axis points north. Now suppose the device is lifted up 90 degrees. Do you want to show the cube flip on the side or up? It seems from your question that you want the cube flip on the side. If this is so, it is easy. – Hoan Nguyen May 24 '15 at 03:34

1 Answers1

0

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.

LBes
  • 3,366
  • 1
  • 32
  • 66
weston
  • 54,145
  • 21
  • 145
  • 203
  • Wow ! Thanks a lot ! I will check it out tomorrow to see if it works ! In case something's I will let you know ! But the way you say it sounds logical :-). Thanks again. – LBes May 23 '15 at 22:20
  • It works like a charm. Thanks a lot for this very helpful answer. It all sounds so easy in the end. I don't know how I could not see that ^^. Anyway thanks a lot. I would upvote this answer if I had enough rep ^^' – LBes May 25 '15 at 13:53
  • Glad it worked for you, tell me did you use transpose to find `OM20T`? I wasn't sure if my idea about negating the vector would work. – weston May 25 '15 at 13:54
  • That's what I did indeed. First I get the transpose when the screen gets back on (and only once). Then I use the multiplication just like you gave it and it works perfectly :). – LBes May 25 '15 at 13:55
  • Apparently still have an error. Dunno where it comes from. It works in most cases but doesn't work in some. Should I edit the question to add my current code? – LBes May 25 '15 at 14:01
  • Best to go with another question. Try to diagnose it as best you can yourself first. – weston May 25 '15 at 14:04
  • Ok thanks ! That's what I am going to do then. I will try and fix it by myself for some time and if I can't get near the answer again will create a new post. Do I have anyway to contact you in case I'm stuck again? – LBes May 25 '15 at 14:05
  • Post link to new question here and I'll be sure to take a look for you. – weston May 25 '15 at 14:06
  • Everything works. Was just a small bug in my code. Thanks again :) – LBes May 26 '15 at 11:54