title: Inverting the rotation along the X and the Y axis while keeping Z intact
body: <p>I have a <code>simd_float4x4</code> matrix that is the transformation matrix of a camera.</p>
  
  <p>Inside that matrix, we have the <code>tx</code>, <code>ty</code> and <code>tz</code> that is the translation vector and <code>0x</code>, <code>0y</code>, <code>0z</code>, <code>1x</code>, <code>1y</code>, <code>1z</code>, <code>2x</code>, <code>2y</code> and <code>2z</code>, that is the rotation matrix.</p>
  
  <pre><code>┌                  ┐
  |  0x  1x  2x  tx  |
  |  0y  1y  2y  ty  |
  |  0z  1z  2z  tz  |
  |  0   0   0   1   |
  └                  ┘
  </code></pre>
  
  <p>When I test the rotation matrix I have on a 3D application, I see that the rotations along the X and the Y axis are inverted, while the rotation along Z is correct.</p>
  
  <p>So, on axis X and Y, any rotation the real camera did clockwise is shown anticlockwise. </p>
  
  <p>I need to invert those rotations.</p>
  
  <p>Bear with me because my matrix algebra is rusty.</p>
  
  <p>So, the rotation matrices for the axis are, as stated by the next picture of <a href="https://stackoverflow.com/a/38103806/316469">this SO answer</a></p>
  
  <p><a href="https://i.stack.imgur.com/ZA0RP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZA0RP.png" alt="enter image description here"></a></p>
  
  <p>If I am on the right path, I need to multiply my rotation matrix by <code>Rx</code> and <code>Ry</code>, right?</p>
  
  <p>If I want to invert the rotations in <code>X</code> and <code>Y</code>, my <code>Rx</code> and <code>Ry</code> will be equal to:</p>
  
  <pre><code>     ┌             ┐
       |  1   0   0  |
  Rx = |  0  -1   0  |
       |  0   0  -1  |
       └             ┘
  
       ┌             ┐
       |  -1  0   0  |
  Ry = |   0  1   0  |
       |   0  0  -1  |
       └             ┘
  </code></pre>
  
  <p>I can multiply <code>Rx</code> and <code>Ry</code> to obtain the final matrix I have to use:</p>
  
  <pre><code>    ┌              ┐
      |  -1   0   0  |
  R = |   0  -1   0  |
      |   0   0   1  |
      └              ┘
  </code></pre>
  
  <p>So, all I have to do, is to multiply my rotation matrix by <code>R</code> to get the rotations along <code>X</code> and <code>Y</code> inverted, right?</p>
  
  <p>Nope, the final result is the same... the only thing that happens is that the camera appears to have rotated in Y.</p>
  
  <p>I am using this code</p>
  
  <pre><code>import simd
  
  extension simd_float4x4 {
  
    var rotatedXY: simd_float4x4 {
  
      let c0 = SIMD3&lt;Float&gt;(-1.0, 0.0, 0.0)
      let c1 = SIMD3&lt;Float&gt;(0.0, -1.0, 0.0)
      let c2 = SIMD3&lt;Float&gt;(0.0,  0.0, 1.0)
      let inverse = simd_float3x3(c0,c1,c2)
  
      let cx0 = SIMD3&lt;Float&gt;(columns.0.x, columns.0.y, columns.0.z)
      let cx1 = SIMD3&lt;Float&gt;(columns.1.x, columns.1.y, columns.1.z)
      let cx2 = SIMD3&lt;Float&gt;(columns.2.x, columns.2.y, columns.2.z)
      let matrix = simd_float3x3(cx0,cx1,cx2)
  
      let multiplication = matrix * inverse
  
      var resultado = self
  
      resultado.columns.0.x = multiplication.columns.0.x
      resultado.columns.0.y = multiplication.columns.0.y
      resultado.columns.0.z = multiplication.columns.0.z
  
      resultado.columns.1.x = multiplication.columns.1.x
      resultado.columns.1.y = multiplication.columns.1.y
      resultado.columns.1.z = multiplication.columns.1.z
  
      resultado.columns.2.x = multiplication.columns.2.x
      resultado.columns.2.y = multiplication.columns.2.y
      resultado.columns.2.z = multiplication.columns.2.z
  
      return resultado
    }
  }
  </code></pre>
  
  <p>What am I missing?</p>