Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert 3d 4x4 Rotation Matrix into 2d

Say we have a 4x4 matrix with indices like so:

00 01 02 03
10 11 12 13
20 21 22 23
30 31 32 33

How does one convert the rotation data (ignoring the z axis, if that helps) contained in this matrix into a single 2d rotational angle (in radians)?

Background: I have a 3D .dae animation exported from Blender into the Collada format. The animation is technically 2d, all of the z axis values are 0. I'm trying to convert the 4x4 matrices into 2d translation, rotation and scale data.

like image 238
Nathanael Weiss Avatar asked Dec 13 '22 02:12

Nathanael Weiss


2 Answers

Scale matrix S looks like this:

sx 0  0  0
0  sy 0  0
0  0  sz 0
0  0  0  1

Translation matrix T looks like this:

1  0  0  0
0  1  0  0
0  0  1  0
tx ty tz 1

Z-axis rotation matrix Rlooks like this:

 cos(a) sin(a)  0  0
-sin(a) cos(a)  0  0
   0      0     1  0
   0      0     0  1

If you have a transformation matrix M, it is a result of a number of multiplications of R, T and S matrices. Looking at M, the order and number of those multiplications is unknown. However, if we assume that M=S*R*T we can decompose it into separate matrices. Firstly let's calculate S*R*T:

        ( sx*cos(a) sx*sin(a) 0  0)       (m11 m12 m13 m14)
S*R*T = (-sy*sin(a) sy*cos(a) 0  0) = M = (m21 m22 m23 m24)
        (     0         0     sz 0)       (m31 m32 m33 m34)
        (     tx        ty    tz 1)       (m41 m42 m43 m44)

Since we know it's a 2D transformation, getting translation is straightforward:

translation = vector2D(tx, ty) = vector2D(m41, m42)

To calculate rotation and scale, we can use sin(a)^2+cos(a)^2=1:

(m11 / sx)^2 + (m12 / sx)^2 = 1
(m21 / sy)^2 + (m22 / sy)^2 = 1

m11^2 + m12^2 = sx^2
m21^2 + m22^2 = sy^2

sx = sqrt(m11^2 + m12^2)
sy = sqrt(m21^2 + m22^2)

scale = vector2D(sx, sy)

rotation_angle = atan2(sx*m22, sy*m12)
like image 99
miloszmaki Avatar answered Dec 30 '22 18:12

miloszmaki


this library has routines for converting a 4x4 matrix into its 5 components - rotation, translation, scale, shear, and perspective. You should be able to take the formulas and just drop the 3rd component of the 3d vectors.

like image 32
jterrace Avatar answered Dec 30 '22 19:12

jterrace