我正在使用以下库:
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
两种方式的操作顺序相同,即
- 身份
- 绕 x 旋转 90 度
- 翻译 10 倍、20 倍
但是,输出不一样,这里是输出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]
当我看似采取相同的步骤时,为什么结果不同?
最佳答案
因为矩阵乘法根据左边的矩阵而不同。换句话说
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]
关于javascript - 4x4 矩阵乘法顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21838672/