找到两个矩阵之间的变换矩阵

时间:2018-03-22 10:50:13

标签: math three.js matrix-multiplication

我试图找到两个矩阵之间的变换矩阵,所以我可以保存它,然后将它应用到一个对象上,这样它就会保持相对于另一个对象的位置和旋转。使用这种消化的解决方案:

3d (THREE.js) : difference matrix

我使用了这段代码:

var aInv1 = new THREE.Matrix4().getInverse(firstObject.matrix.clone());
var aMat2 = new THREE.Matrix4().copy(secondObject.matrix.clone());
var aTrans = new THREE.Matrix4().multiplyMatrices(aMat2, aInv1);

矩阵元素的值为:

firstObject.matrix.elements = [ 
    1, 0, 0, 0,
    0, 1, 0, 0,
    0, 0, 1, 0,
    0, 0, -358.483421667927, 1
]

secondObject.matrix.elements = [
    0.5137532240102918, -0.844167465362402, 0.15309773101731067, 0,
    0.8579380075617532, 0.5055071032079361, -0.091678480502733, 0,
    -1.3877787807814457e-17, 0.1784484772418605, 0.983949257314655, 0,
    94.64320536824728, 6.92473686011361, -372.0695450875709, 1
]

我希望转换矩阵变量aTrans元素为94.64320536824728, 6.92473686011361, 13.58,因为这些是位置上的差异,但我得到94.64320536824728, 70.89555757320696, -19.340048577797802, 1

aTrans.matrix.elements = [
    0.5137532240102918, -0.844167465362402, 0.15309773101731067, 0,
    0.8579380075617532, 0.5055071032079361, -0.091678480502733, 0,
    -1.3877787807814457e-17, 0.1784484772418605, 0.983949257314655, 0,
    94.64320536824728, 70.89555757320696, -19.340048577797802, 1
]

我会对这种差异进行任何有根据的解释,或者解决这个问题的其他方法。

1 个答案:

答案 0 :(得分:1)

You cannot simply add the translations because the second matrix might affect the effective translation of the first.

Let's consider a simple example - suppose both matrices only contain a rotation and translation:

M = R + T

R corresponds to the top-left 3x3 sub-matrix, and T is the first 3 elements of the last column. Multiplication rule with an arbitrary 3D point p:

M * p = R * p + T

Two of these give:

M2 * M1 * p = R2 * R1 * p + R2 * T1 + T2

The last column of M2 * M1 is R2 * T1 + T2 instead of simply T1 + T2, i.e. effective translation that M1 imposes on p is R2 * T1, and not simply T1.

相关问题