# How to calculate angles after subsequent rotations?

I have a class that manipulates a model matrix in opengl:

class ModelObject {
private:
mat4 ScaleMatrix;
mat4 RotationMatrix;
mat4 TranslationMatrix;

public:
vec3 angles;
float size;
vec3 pos;
. . .
}


I have a function that rotates object around its local axises

void rotateLocal(vec3 euler_angles)
{
quat qY = angleAxis(euler_angles.y, getUpv());
mat4 rY = toMat4(qY);
RotationMatrix = rY * RotationMatrix;
quat qZ = angleAxis(euler_angles.z, getFrontv());
mat4 rZ = toMat4(qZ);
RotationMatrix = rZ * RotationMatrix;
quat qX = angleAxis(euler_angles.x, getRightv());
mat4 rX = toMat4(qX);
RotationMatrix = rX * RotationMatrix;
}


But I cannot figure out how to make it change angles property correctly. Many formulas that calculate final euler angles do not work properly. Here is another function that also rotates object, but around global axises

void rotate(vec3 angles)
{
mat4 newRotation = toMat4(quat(angles));
RotationMatrix = newRotation * RotationMatrix;

this->angles += angles;
}


I want rotateLocal also to be syncronized with angles

• Short answer - it's not always possible to do nicely (especially if gimbal locks get triggered). Perhaps you should look into quaternions, they do solve this and similar problems. As for naive approach it may be simplest to try to convert rotation matrix back to Euler with some edge-case cases handled manually. Oct 3, 2022 at 8:39
• Why do you need to store the angles? Perhaps, only store the matrix, or a quaternion? Oct 7, 2022 at 3:59
• @HolyBlackCat this property made to be accessed from outsude, by user, camera or another listener object. They can receive matrix of course, but many functions in my code work only with euler angles, not matrices Oct 7, 2022 at 4:35
• I would try to migrate everything to quaternions, and convert then locally to euler as needed. Oct 7, 2022 at 4:44