title: Rotating and object from left/right and up/down movements
body: <p>I'm writing a game which involves cubes.  The user can rotate individual cubes, from his perspective, 90deg left, right, up or down.</p>
  
  <p>Naturally as the shape is rotated, the positions of its axes change, so the next rotation might be about a different axis.  For example if the first rotation is "right", I need to rotate in a positive direction around the Y (vertical) axis.  Now the Z axis is where the X axis was, and the X axis is where the Z axis was, but pointing in the opposite direction.</p>
  
  <p>To do this I use a matrix:</p>
  
  <pre><code>// The matrix used to rotate/tip the shape.  This is updated each time we rotate.
  private int  m_rotationMatrix[][] = { {0, 1, 0},        // User wants to rotate left-right
                                        {1, 0, 0} };      // User wants to rotate up-down
  </code></pre>
  
  <p>which I update upon each rotation:</p>
  
  <pre><code>    // If we rotated in the x direction, we need to put the z axis where the x axis was
      if (0 != leftRight)
      {
          int newXMatrix[][] = new int[2][3];
  
          for (int direction=0 ; direction&lt;2 ; ++direction)
          {
              newXMatrix[direction][X_DIMENSION] = m_rotationMatrix[direction][Z_DIMENSION] * (-leftRight);  
              newXMatrix[direction][Y_DIMENSION] = m_rotationMatrix[direction][Y_DIMENSION];          // Unchanged
              newXMatrix[direction][Z_DIMENSION] = m_rotationMatrix[direction][X_DIMENSION] * (leftRight);  
          }
  
          m_rotationMatrix = newXMatrix;
      }
  
      // If we rotated in the y direction, we need to put the z axis where the y axis was
      if (0 != upDown)
      {
          int newXMatrix[][] = new int[2][3];
  
          for (int direction=0 ; direction&lt;2 ; ++direction)
          {
              newXMatrix[direction][X_DIMENSION] = m_rotationMatrix[direction][X_DIMENSION];          // Unchanged
              newXMatrix[direction][Y_DIMENSION] = m_rotationMatrix[direction][Z_DIMENSION] * (upDown); 
              newXMatrix[direction][Z_DIMENSION] = m_rotationMatrix[direction][Y_DIMENSION] * (-upDown);  
          }
  
          m_rotationMatrix = newXMatrix;
      }
  </code></pre>
  
  <p>Now if I turn a shape right, right, right, the resulting matrices are exactly as I think they should be:</p>
  
  <pre><code>                   x    y    z
  Start:  LeftRight  0    1    0
          UpDown     1    0    0
  
  Right1: LeftRight  0    1    0
          UpDown     0    0    1
  
  Right2: LeftRight  0    1    0
          UpDown    -1    0    0   &lt;-- Looks right to me but doesn't work!
  
  Right3: LeftRight  0    1    0
          UpDown     0    0   -1
  </code></pre>
  
  <p>When I test the up-down rotation in each case above, it is correct for three sides, but wrong (upside down) in the Right2 case.  That is to say, if the user does an "up" rotation, that becomes -90deg on the x-rotation, which I think is correct (x-axis is now pointing left) but it causes the box to turn, from the user's point of view, downwards.</p>
  
  <p>I can find no such simple problem with the up-down rotation: after any number of up-down rotations, a single left-right rotation works as expected.</p>
  
  <p>For reference my draw() does this:</p>
  
  <pre><code>    // Set drawing preferences
      gl.glFrontFace(GL10.GL_CW);
  
      // Set this object's rotation and position
      gl.glPushMatrix();
      gl.glTranslatef(m_xPosition, m_yPosition, m_zPosition - m_selectZ);
      gl.glRotatef(m_xRotation, 1, 0, 0);
      gl.glRotatef(m_yRotation, 0, 1, 0);
      gl.glRotatef(m_zRotation, 0, 0, 1);
  
  ...blah blah
  </code></pre>
  
  <p>What is wrong my my rotation?</p>