Converting a 3x3 rotation matrix to euler angles to 4x4 matrix

I’ve been trying to get a MD3 loader to work for awhile now. With help from goussej I got the loader to load all the geometry, surfaces, skins, and tags. I now, however, found a new issue that I am not quite sure how to tackle.

the MD3 model format stores it’s rotation matrix as a 3x3. The generic model format I use in my game uses euler angles for rotations (before sending it to the shader), expressed like this:

Matrix4f.rotate((float)Math.toRadians(modelAngle.z), new Vector3f(0, 0, 1), modelMatrix, modelMatrix);
Matrix4f.rotate((float)Math.toRadians(modelAngle.y), new Vector3f(0, 1, 0), modelMatrix, modelMatrix);
Matrix4f.rotate((float)Math.toRadians(modelAngle.x), new Vector3f(1, 0, 0), modelMatrix, modelMatrix);

So, to try to break down the 3x3 matrix to euler angles, I did this:

rotX = (float) Math.toDegrees(Math.atan2(tag._axis.m21, tag._axis.m22));
rotY = (float) Math.toDegrees(-Math.asin(tag._axis.m20));
rotZ = (float) Math.toDegrees(Math.atan2(tag._axis.m10, tag._axis.m00));

This doesn’t seem to work quite right, and matrices are not my strong suit.
I will point out that the Z axis is used for height in my engine (instead of the Y axis).

Any ideas? :x

M00. M10 M20 0
M01 M11 M21 0
M02 M12 M22 0
0 0 0 1

Just turn it to this mat4 it will do the same thing

My model class uses euler angles, and it’s used beyond just MD3 models; so I can’t just convert the 3x3 matrix directly to a 4x4 matrix.

If it is just a simple rotation matrix, why not? It does the same task

You can look at Ardor3D or any other engine, they have methods to convert matrices into Euler angles.

I would think that you have to negate the parameter in your asin-call and not the result. Anyway, you have to keep in mind that the angles that you derive from the matrix are only one way to create the resulting matrix. So they are angles that will create the resulting matrix, but they might not be the angles that you expect to get.
It’s a better idea to work with matrices directly anyway…

I guess I’ll just bite the bullet and get rid of my euler angle dependancy.
Seems to work fine now :wink:

One thing that I haven’t been able to get to work right…
I wanted to change the “whole” direction of the md3 model.

My model class now uses a Matrix4f to express it’s current rotation/position. Because the quake player model is made up of three separate models welded together by “tags”, I need to add a new rotation to each model starting from the root one. So to do this, I stored the matrix4 value from the previous model, applied it to the current one, an then added that current models rotation. While this works, I’m not able to set a global direction for the model, as I overwrite the rotation.

So I guess my problem boils down to this…
I have a matrix4f, and I want to add it to a matrix3f.
I tried converting the matrix3f to a matrix4f, and then doing Matrix4f.add(m1, m2, src) but it doesn’t seem to work…

Assuming i understand the question right,

you should multiply them not add them. The initial rotation (the source/root rotation) should be on the right hand side of the multiplication and the additional rotation will be on the left.

so

CombinedMatrix = additionalRotationMatrix * initialRotationMatrix;

Thanks :wink:
Worked perfectly :slight_smile: