2016-12-28 157 views
0

我正在写一个Matrix类,它在C++中有静态的RotationX()RotationY()RotationZ()方法。如果在乘以矢量之前将矩阵相乘,我会得到不同的结果,如果将矩阵单独乘以矢量。仿射旋转矩阵

此代码

Vec4 result1 { 1, 1, 1, 1 }; 
result1 = Matrix::RotationX(ToRadians(-90)) * result1; 
result1 = Matrix::RotationY(ToRadians(90)) * result1; 
result1 = Matrix::RotationZ(ToRadians(90)) * result1; 
// result1 => { -1, -1, -1, 1 } 

给出比这个代码

Vec4 result2 { 1, 1, 1, 1 }; 
auto rotation = Matrix::RotationX(ToRadians(-90)) * 
       Matrix::RotationY(ToRadians(90)) * 
       Matrix::RotationZ(ToRadians(90)); 
result2 = rotation * result2; 
// result2 => { 1, 1, -1, 1 } 

什么是这里的问题不同的结果?我可以提供自己的循环函数实现,但是我想在发布代码之前确定这不是一个仿射变换的概念问题。

回答

2

你的第一个例子对应于矩阵乘法的逆序。比较:

Z * (Y * (X * V)) 
((X * Y) * Z) * V 

但是矩阵乘法不可交换!

+0

我没有意识到我的矩阵顺序不同:)谢谢 – jefftime