如何使用 3D 变换矩阵绕 Z 轴旋转?
How to rotate around Z axis using transformation matrix in 3D?
在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 -- 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:
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 },
}
任何人都可以告诉我我做错了什么并更正我的代码吗?
如@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 -- 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:
https://rosettacode.org/wiki/Matrix_multiplication#Lua
现在,我得到了预期的结果:
local expected = {
{ 0, 100, 0, 0 },
}
在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 -- 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: 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 },
}
任何人都可以告诉我我做错了什么并更正我的代码吗?
如@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 -- 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: https://rosettacode.org/wiki/Matrix_multiplication#Lua
现在,我得到了预期的结果:
local expected = {
{ 0, 100, 0, 0 },
}