[SOLVED] Matrix to Quat Conversion

haven’t posted stuff here for a while, but I’m still alive :wink:

I’m currently trying to visualize some cameras that I get from a photogrammetry suite in my playcanvas viewer, however I’m stuck on the coordinate conversion with my limited math skills and their sparse documentation :smiley: Would be glad if somebody could help me out :>

I have a json object containing all my cameras, each with a 3x3 rotation matrix + position (at least I think it’s a 3x3 matrix).

"transform": {
            "rotation": [
            "position": [

Using the center coordinates for positioning my cameras works, but I am stuck on converting the rotation data into a Quat, or Euler angles, so that I can apply it to my playcanvas entity.
pc.Quat has a function “setFromMat4“ but none for a Mat3.

Any ideas?



When you load the JSON file, it probably be easier to make the 3x3 rotation matrix and position into a 4x4 matrix that contains both the orientation and position.

Looking at the code setFromMat4, it actually only uses the 3x3 part of the matrix so creating a function that sets from mat3 is pretty easy if you really need that.

But the question is, how would I do that? :smiley:

First thought so as well, but than I noticed in the documentation, that the Mat4 expects 15 numbers, while I have only 12 (9 rotation + 3 position).
Might be a super dump question, but I dont work with matrixes too often :wink:

                r[0]  = x.x;
                r[1]  = x.y;
                r[2]  = x.z;
                r[3]  = 0;
                r[4]  = y.x;
                r[5]  = y.y;
                r[6]  = y.z;
                r[7]  = 0;
                r[8]  = z.x;
                r[9]  = z.y;
                r[10] = z.z;
                r[11] = 0;
                r[12] = position.x;
                r[13] = position.y;
                r[14] = position.z;
                r[15] = 1;

Just quickly copied from the mat4.js file in the engine source.

One thing to keep in mind that PlayCanvas uses the right hand coordinate system where -Z is ‘forward’ so you may have to do some converting depending on which handiness your current matrices are in.

Yeah that seems to do the job on the first look, if I invert y & z on the matrix.

Will see tomorrow, if everything works as supposed, enough for today :wink:
Nether less, already thanks a lot yaustar, as usual super quick & helpful. I think I would have spent the rest of the evening without a result :smiley: