Hey,
I've just recently started learning graphics programming so please bear with me!
While learning, i learned that when applying rotation in the order RyRxRz (the standard way for euler angles), such that the model matrix is composed by doing SRzRxRyT (given i'm using column matrices) there will be some combination of angles that rotate around "world axis" since y axis rotation is applied first.
So i thought maybe i should construct a quaternion based on the getting the local axis from the rotation matrix inside the model model matrix, but this leads to a few issues which i will mention after giving my code below:
void drawCube(Vec3f pos, Quatf rot, vec3f scale)
{
Matrix4f model = MATRIX4F_IDEN;
model = matrix4fMult(model, matrix4fGiveTranslate(pos.x, pos.y, pos.z));
Matrix4f rotmat = quatfToMatrix4f(rot);
model._00 = rotmat._00;
model._01 = rotmat._01;
model._02 = rotmat._02;
model._10 = rotmat._10;
model._11 = rotmat._11;
model._12 = rotmat._12;
model._20 = rotmat._20;
model._21 = rotmat._21;
model._22 = rotmat._22;
model = matrix4fMult(model, matrix4fGiveScale(scale.x, scale.y, scale.z));
/// .... do a bunch of other stuff below
}
Vec3f cubePos;
Vec3f cubeRot; //stores the angles in each axis in degrees, so it can be changed in the editor u.i
Vec3f cubeScale;
Quatf cubeRotQuat;
void render(void)
{
// Matrix3f rotMat = quatfToMatrix3f(cubeRotQuat);
// Vec3f up = matrix3fMultVector3f(rotMat, (Vec3f){0, 1, 0});
// Vec3f right = matrix3fMultVector3f(rotMat, (Vec3f){1, 0, 0});
// Vec3f fwd = matrix3fMultVector3f(rotMat, (Vec3f){0, 0, 1});
// Quatf rotX = quatfMakeWithRotationDeg(cubeRot.x, right);
// Quatf rotY = quatfMakeWithRotationDeg(cubeRot.y, up);
// Quatf rotZ = quatfMakeWithRotationDeg(cubeRot.z, fwd);
// cubeRotQuat = quatfMult(rotY, quatfMult(rotZ, rotX));
cubeRotQuat = quatfMakeWithRotationDegYXZ(cubeRot.x, cubeRot.y, cubeRot.z);
drawCube(cubePos, cubeRotQuat, cubeScale, cubeScale);
/// .. other stuff below
}
The code above shows a snippet of what im doing, i cant show all of the code since it would be too much. The commented out code in render function is what does the rotation about local axis's.
The issue I'm having with this approach is that when rotating this way, on the local axis's, while rotating the axis's can be changed so it leads to the cube glitching. In other words, if I rotate around the cubes local y axis which lets say points in the direction (-1, 1, 0) normalised, then while rotating, the z and x local axis will be getting updated too, so the code that is commented will then rotate around those updated axis's with the rotation amount passed in, which then leads the cube vertices to jump back and forward and start glitching out.
So this leads to my question of, how can i structure the code or do this in such a way, where all rotation in my engine is done around the local axis - for the general case. Unless i have some u.i setting to switch it to global axis.