# Quaternion rotation around center, undefined behavior

Here's my code:

vec4 qx, qy, qz;
mat4 mx, my, mz;

// rotating using quaternions

// turning the quaternions into matrices
glm_quat_mat4(qx, mx);
glm_quat_mat4(qy, my);
glm_quat_mat4(qz, mz);

mat4 trans = {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};

mat4 rot = {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};

mat4 final;

// combining the rotations into one.
glm_mat4_mulN((mat4* []){&mx, &my, &mz}, 3, rot);

// translating the trans matrix.
glm_translate(trans, (vec3){ a->x, a->y, a->z });

// finally combining the translation with the rotation into one.
glm_mat4_mul(trans, rot, final);


My desired behavior is that the object rotates around its center, but here is what happens instead:

So , it seems that my object is rotating around some weird other undefined point. I have no idea why this happens.

Any ideas? Thank you.

• You applied the rotation and translation in the wrong order. To rotate an object around its center its center has to be at the origin (0,0,0). Note that multiplication of matrices is not commutative. In general: A·B != B·A – Socowi Jun 7 at 10:29
• @Socowi But when I change the order, then the Z axis becomes the Y axis, and my translation becomes messed up – Sebastian Karlsson Jun 7 at 10:31