1

I have a transformed Matrix A and an another transformed Matrix B. How can I COPY the rotation of Matrix B to Matrix A?

2 Answers2

2

Generally the "top-left" 3x3 block of coefficients will be the rotation/scale/shear component of a 4x4 (or 3x4) transformation matrix.

If both matrices have no scale (including reflection) or shear, you can simply copy this block of coefficients, something like:

matrixA[0][0] = matrixB[0][0];
matrixA[0][1] = matrixB[0][1];
matrixA[0][2] = matrixB[0][2];

matrixA[1][0] = matrixB[1][0];
...
matrixA[2][2] = matrixB[2][2];
// Leave row [3] and column [3] alone.

If both matrices contain some scale aligned to the axes of the source space (eg. if these are ModelView matrices, and they contain scale solely along the model's local x, y, and z axes, as is a common convention in games), but still no shearing, then it's a little bit trickier but still feasible. The details will depend on your environment's conventions for matrix storage and multiplication.

In principle you can take the basis vectors from matrix B (these will be the rows of the top-left 3x3 block, or the columns, depending on your environment), normalize them, then multiply each by the corresponding scale coefficient for A, something like:

(abusing some hlsl notation to keep this compact...)

matrixA[0].xyz = scaleA.x * normalize(matrixB[0].xyz);
matrixA[1].xyz = scaleA.y * normalize(matrixB[1].xyz);
matrixA[2].xyz = scaleA.z * normalize(matrixB[2].xyz);

(You may need to transpose your matrices before & after this operation, if your environment uses the opposite row/column convention)

If either matrix contains shear, or scaling that isn't aligned to axes within the source coordinate space, then it becomes ambiguous what "copying the rotation" from one to the other would mean. Fortunately we don't encounter those types of matrices too often in gamedev, apart from perspective projection matrices.

DMGregory
  • 134,153
  • 22
  • 242
  • 357
  • I can't believe I'm this stupid. "simply copy this block of coefficients" - this is the answer to my problem. Thank you. –  Apr 26 '14 at 00:29
  • 1
    There's nothing "stupid" about asking a question when you're unsure - and now this question will provide a resource others can find easily if they find themselves wondering the same thing. :) – DMGregory Apr 26 '14 at 05:50
0

I assume you work with a homogenous transformation matrix like those used with OpenGL.

Your question implies that you assume that one can extract a rotation part from that transformation matrix. Or more mathematically speaking, that it is possible to factor the transformation into a product of simpler transformations, where one of the simpler transformations is a rotation.

It turns out that this is not such easy for an arbitrary homogenous transformation matrices. In general there are many decompositions possible with different resulting rotation matrices which might or might not be useful for your purpose.

E.g. you can try a polar decomposition to factor a homogenous transformation matrix M into

M = P T R N S = Q S

where

  • P is a simple perspective matrix
  • Tis a translation matrix
  • Ris a rotation matrix
  • N is either I (identity matrix) or -I
  • S is a stretch matrix
  • Q is an orthogonal matrix (length and angle preserving, i.e. a combination of rotations and reflections)

The situation can be simpler if you can assume that the transformation matrix is the result of multiplying only a restricted set of intermediate transformation, see this blog post.

Also see this SO entry.

And this Math.SE entry on SVD, but beware, read the comments about the numerical and graphical properties of that decomposition in the above paper about polar decomposition.

mvw
  • 181
  • 1
  • 9