Get X,Y,Z rotation values from Matrix4

Is there a way to tell/compute/get specific rotation values (X/Y/Z) from the Matrix4 in degrees (float), please?

Hi!
Are this and this what you’re looking for?
https://jsfiddle.net/prisoner849/gb8vs15x/

Hi
I am not sure.

To clear things more: I am inside JAVA, not JavaScript - my JAVA app and engine I am using (Sunflow) has basically exactly this Matrix4 stuff as a class (so probably they port it from here or vice-versa, IDK), but as I have no option ask them (Sunflow) as they abandon it like 10+ yrs ago I am asking here (as it is the same), thus I cannot use anything from outside this class you are pointing me to, sorry.

Let’s say I have Matrix4 like this:
0.66799456, -0.33590272, 0.6640426, 52.21598, 0.0, 0.8923312, 0.45138136, 39.204815, -0.74416614, -0.3015203, 0.5960723, 47.337257, 0.0, 0.0, 0.0, 1.0

Now how exactly (syntax) I would get float value of, let’s say, angle Y from that Matrix, please?

Can’t someone just make it easy by creating new function inside the Matrix4 class that would do exactly just that like (sorry, JAVA syntax here):

public float getRotationXFromMatrix() {
    float rotX;
    //SOME CODE HERE DIGGING THE VALUE FROMT HE MATRIX4
    return rotX;
}

I would then port it to JAVA gladly… :slight_smile:

So, is it not an option for you to do this:

var euler = new THREE.Euler().setFromRotationMatrix( _your_variable_of_Matrix4_ );
var x = THREE.MathUtils.radToDeg(euler._x);

?

Unfortunately no, it is not.
But I just tried to make a port of those to JAVA and it gives me strange results.
It would be best if someone could show me here in a code direct Matrix4 values manipulation to obtain those rotation values thus I could manage to port it to my JAVA.

My Matrix4 is is of:

m.m00 = x0;
m.m01 = x1;
m.m02 = x2;
m.m03 = x3;

m.m10 = y0;
m.m11 = y1;
m.m12 = y2;
m.m13 = y3;

m.m20 = z0;
m.m21 = z1;
m.m22 = z2;
m.m23 = z3;

m.m30 = s0;
m.m31 = s1;
m.m32 = s2;
m.m33 = s3;

I took this from the three.js:

var te = m.elements;
var m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ];
var m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ];
var m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];

this._y = Math.asin( clamp( m13, - 1, 1 ) );

if ( Math.abs( m13 ) < 0.9999999 ) {

		this._x = Math.atan2( - m23, m33 );
		this._z = Math.atan2( - m12, m11 );

} else {

		this._x = Math.atan2( m32, m22 );
		this._z = 0;

}

And make this JAVA port out of it according to my needs:

public final static HashMap<String, Float> getRotationFromMatrix(Matrix4 m) {

    HashMap<String, Float> hm = new HashMap<>();
    float x, y, z;

    y = (float) Math.asin(new Utils().clamp(m.m02, -1, 1));

    if (Math.abs(m.m02) < 0.9999999) {
        x = (float) Math.atan2(-m.m12, m.m22);
        z = (float) Math.atan2(-m.m01, m.m00);

    } else {
        x = (float) Math.atan2(m.m21, m.m11);
        z = 0;
    }

    hm.put("x", x);
    hm.put("y", y);
    hm.put("z", z);

    return hm;
}

But now I am not sure if it is ported correctly cos it gives me some strange results, hm… :frowning:

Well, I just found out it actually works but in a strange way that it goes just between -90/+90 degrees and then instead of going to 91 it goes backwards to 89. Anyone knows why it does not go whole 360 circle but only 180?

Ah, SOLVED: all was good, I just copied WRONG PART thinking my order is simply XYZ while it actually was YXZ - once corrected, all works as expected! :smiley: