# Quaternion rotation around center, undefined behavior

Here's my code:

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

// rotating using quaternions
glm_quat(qx, to_radians(a->rx), 1.0f, 0.0f, 0.0f);
glm_quat(qy, to_radians(a->ry), 0.0f, 1.0f, 0.0f);
glm_quat(qz, to_radians(a->rz), 0.0f, 0.0f, 1.0f);

// 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