如何在3D中使用转换矩阵绕Z轴旋转?

在Lua中,我设置了一个单位矩阵:

local ident_matrix = {
 {1,0,0,0},
 {0,1,0,0},
 {0,0,1,0},
 {0,0,0,1},
}

然后将其更新为包含一个在x=100,y=0,z=0处的点:

ident_matrix = {
 {100,0,0,0},
 {0,0,0,0},
 {0,0,0,0},
 {0,0,0,1},
}

然后我将旋转值定义为90度的弧度值:

local r = math.rad(90)

从这里创建一个Z轴旋转矩阵:

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},
}

然后,使用矩阵乘法将Z轴旋转矩阵应用于{100,0,0}点:

local function multiply( aMatrix, bMatrix )
    if #aMatrix[1] ~= #bMatrix then       -- 内部矩阵维度必须一致
        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 )

矩阵乘法来自 RosettaCode.org: https://rosettacode.org/wiki/Matrix_multiplication#Lua

我原本期望的rotated矩阵输出应该与以下相同:

local expected = {
 { 0, 0, 0, 0 },
 { 0, 100, 0, 0 },
 { 0, 0, 0, 0 },
 { 0, 0, 0, 0 },
}

或者,根据左手(?)计算,Y值可能为-100。 实际上我得到的是:

{
 { 0, 100, 0, 0 },
 { 0, 0, 0, 0 },
 { 0, 0, 0, 0 },
 { 0, 0, 0, 1 },
}

有人能告诉我我做错了什么并纠正我的代码吗?

点赞
用户71376
用户71376

正如@egor-skriptunoff的评论所示...

在Lua中,我设置了一个单位矩阵:

local ident_matrix = {
 {1,1,1,1},
}

然后将其更新为在x = 100,y = 0,z = 0处包含一个点:

ident_matrix = {
 {100,0,0,1},
}

然后,我将旋转值定义为90度的弧度:

local r = math.rad(90)

从这里,我创建一个Z轴旋转矩阵:

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},
}

然后,通过矩阵乘法将Z轴旋转矩阵应用于{100,0,0}点:

local function multiply( aMatrix, bMatrix )
    if #aMatrix[1] ~= #bMatrix then       -- 内部矩阵维度必须相等
        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 )

矩阵乘法来自RosettaCode.org: https://rosettacode.org/wiki/Matrix_multiplication#Lua

现在,我得到了我期望的结果:

local expected = {
 { 0, 100, 0, 0 },
}
2016-07-01 09:38:17