How to rotate around Z axis using transformation matrix in 3D?
<p>In Lua, I have set up an identity matrix:</p>

<pre><code>local ident_matrix = {
 {1,0,0,0},
 {0,1,0,0},
 {0,0,1,0},
 {0,0,0,1},
}
</code></pre>

<p>This is then updated to contain a point at x=100, y=0, z=0:</p>

<pre><code>ident_matrix = {
 {100,0,0,0},
 {0,0,0,0},
 {0,0,0,0},
 {0,0,0,1},
}
</code></pre>

<p>I then define my rotation value as 90 degrees in radians:</p>

<pre><code>local r = math.rad(90)
</code></pre>

<p>From this I create a Z-axis rotation matrix:</p>

<pre><code>local rotate_matrix = {
 {math.cos(r),math.sin(r),0,0},
 {-math.sin(r),math.cos(r),0,0},
 {0,0,1,0},
 {0,0,0,1},
}
</code></pre>

<p>And from here apply the Z-axis rotation matrix to the {100,0,0} point using matrix multiplication:</p>

<pre><code>local function multiply( aMatrix, bMatrix )
    if #aMatrix[1] ~= #bMatrix then       -- inner matrix-dimensions must agree
        return nil      
    end 

    local empty = newEmptyMatrix()

    for aRow = 1, #aMatrix do
        for bCol = 1, #bMatrix[1] do
            local sum = empty[aRow][bCol]
            for bRow = 1, #bMatrix do
                sum = sum + aMatrix[aRow][bRow] * bMatrix[bRow][bCol]
            end
            empty[aRow][bCol] = sum
        end
    end

    return empty
end

local rotated = multiply( rotate_matrix, ident_matrix )
</code></pre>

<p>The matrix multiplication is taken from RosettaCode.org:
<a href="https://rosettacode.org/wiki/Matrix_multiplication#Lua" rel="nofollow">https://rosettacode.org/wiki/Matrix_multiplication#Lua</a></p>

<p>I was expecting the <code>rotated</code> matrix output to be the same as:</p>

<pre><code>local expected = {
 { 0, 0, 0, 0 },
 { 0, 100, 0, 0 },
 { 0, 0, 0, 0 },
 { 0, 0, 0, 0 },
}
</code></pre>

<p>Or perhaps, given left-handed (?) calculations, the Y value would be -100.
What I actually end up with is:</p>

<pre><code>{
 { 0, 100, 0, 0 },
 { 0, 0, 0, 0 },
 { 0, 0, 0, 0 },
 { 0, 0, 0, 1 },
}
</code></pre>

<p>Can anyone tell me what I'm doing wrong and correct my code, please?</p>