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

Lua 如何在3D中使用变换矩阵绕Z轴旋转?,lua,3d,matrix-multiplication,transformation-matrix,Lua,3d,Matrix Multiplication,Transformation Matrix,在Lua中,我建立了一个身份矩阵: local ident_matrix = { {1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1}, } local ident_matrix = { {1,1,1,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}, } ident_matrix = { {100,

在Lua中,我建立了一个身份矩阵:

local ident_matrix = {
 {1,0,0,0},
 {0,1,0,0},
 {0,0,1,0},
 {0,0,0,1},
}
local ident_matrix = {
 {1,1,1,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},
}
ident_matrix = {
 {100,0,0,1},
}
然后,我将旋转值定义为90度(弧度):

local r = math.rad(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},
}
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       -- 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 )
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 )
矩阵乘法取自RosettaCode.org:

我希望
旋转的
矩阵输出与以下相同:

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

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

正如@egor skriptunoff的评论所暗示的那样

在Lua中,我建立了一个身份矩阵:

local ident_matrix = {
 {1,0,0,0},
 {0,1,0,0},
 {0,0,1,0},
 {0,0,0,1},
}
local ident_matrix = {
 {1,1,1,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},
}
ident_matrix = {
 {100,0,0,1},
}
然后,我将旋转值定义为90度(弧度):

local r = math.rad(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},
}
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       -- 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 )
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 )
矩阵乘法取自RosettaCode.org:

现在,我得到了我所期望的:

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

正如@egor skriptunoff的评论所暗示的

在Lua中,我建立了一个身份矩阵:

local ident_matrix = {
 {1,0,0,0},
 {0,1,0,0},
 {0,0,1,0},
 {0,0,0,1},
}
local ident_matrix = {
 {1,1,1,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},
}
ident_matrix = {
 {100,0,0,1},
}
然后,我将旋转值定义为90度(弧度):

local r = math.rad(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},
}
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       -- 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 )
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 )
矩阵乘法取自RosettaCode.org:

现在,我得到了我所期望的:

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

为什么要使用4x4矩阵来存储三维空间中的点(而不是1x3矢量)?老实说,我不确定。我认为最简单的成功途径是确保我所有的矩阵都具有相同的维度——尽管我知道A的行必须等于B的列。谢谢!我把我的身份矩阵改成了一行四个单元格,一切正常#Epic。为什么要使用4x4矩阵来存储三维空间中的点(而不是1x3向量)?老实说,我不确定。我认为最简单的成功途径是确保我所有的矩阵都具有相同的维度——尽管我知道A的行必须等于B的列。谢谢!我把我的身份矩阵改成了一行四个单元格,一切正常#史诗。