2014-02-17 146 views
0

使用下面的图书馆我“米:4x4矩阵乘法为了

https://github.com/greggman/tdl/blob/master/tdl/math.js

然而,问题是不同的结果时,我采取不同的方式相同的步骤,我越来越:

//Example 1 
m = tdl.math.matrix4.identity(); 
tdl.math.matrix4.rotateX(m, 90*(Math.PI/180)); 
tdl.math.matrix4.translate(m, [10,20,30]); 

console.log (m); //output down below 1 

//Example2 
i = tdl.math.matrix4.identity(); 
r = tdl.math.matrix4.rotationX(90*(Math.PI/180)); 
t = tdl.math.matrix4.translation([10,20,30]); 
m = tdl.math.matrix4.mul(i, r); 
m = tdl.math.matrix4.mul(m, t); 

console.log(m); // output down below 2 

这两种方法都有动作的顺序相同,即

  1. 身份
  2. 绕×90度
  3. 10倍平移,20Y

但是,输出是不一样的,这里是输出1:

[1, 0, 0, 0, 0, 6.123233995736766e-17, 1, 0, 0, -1, 6.123233995736766e-17, 0, 10, -30, 20.000000000000004, 1] 

这里是输出2

[1, 0, 0, 0, 0, 6.123233995736766e-17, 1, 0, 0, -1, 6.123233995736766e-17, 0, 10, 20, 30, 1] 

为什么结果不同,当我似乎采取相同的步骤?

回答

3

因为矩阵的乘法因左边的矩阵而不同。换句话说

matA * matB != matB * matA 

尝试

i = tdl.math.matrix4.identity(); 
r = tdl.math.matrix4.rotationX(90*(Math.PI/180)); 
t = tdl.math.matrix4.translation([10,20,30]); 
m = tdl.math.matrix4.mul(r, i); 
m = tdl.math.matrix4.mul(t, m); 

console.log(m); // output down below 

输出

[1, 0, 0, 0, 0, 6.123031769111886e-17, 1, 0, 0, -1, 6.123031769111886e-17, 0, 10, -30, 20.000000000000004, 1] 
+0

我可以看到,然而,这只是行主要发生。那么我应该追加而不是前置权?你现在正在预先考虑。 – David

+0

好吧,因为我自己的矩阵类现在和给定的库有相同的结果,我只会接受我仍然是noob的事实,然后继续前进。谢谢你今天帮我两次:)这真是令人厌恶。 – David

+0

这就是主要专业和专业专业之间的差异。 rowMajorA * rowMajorB使用rowMajorMultiply = columnMajorB * columnMajorA使用columnMajorMultiply – gman