I adapted this piece of code from a math book for game developers and the result is correct albeit with slight floating point errors.
void rotate(matrix44_t &m, const float32_t x, const float32_t y, const float32_t z) {
float32_t sin_x = math::sin(x), cos_x = math::cos(x);
float32_t sin_y = math::sin(y), cos_y = math::cos(y);
float32_t sin_z = math::sin(z), cos_z = math::cos(z);
m.data[0] = cos_y * cos_z;
m.data[4] = -(cos_y * sin_z);
m.data[8] = sin_y;
m.data[12] = 0.0f;
m.data[1] = (sin_x * sin_y * cos_z) + (cos_x * sin_z);
m.data[5] = -(sin_x * sin_y * sin_z) + (cos_x * cos_z);
m.data[9] = -(sin_x * cos_y);
m.data[13] = 0.0f;
m.data[2] = -(cos_x * sin_y * cos_z) + (sin_x * sin_z);
m.data[6] = (cos_x * sin_y * sin_z) + (sin_x * cos_z);
m.data[10] = (cos_x * cos_y);
m.data[14] = 0.0f;
m.data[3] = 0.0f;
m.data[7] = 0.0f;
m.data[11] = 0.0f;
m.data[15] = 1.0f;
}
When I call the function like this:
rotate(m1, 0.0f, DEG_2_RAD(90.0f), 0.0f);
I get the following:
[ 0.000001, 0.000000, -1.000000, 0.000000 ]
[ -0.000000, 1.000000, 0.000000, 0.000000 ]
[ 1.000000, -0.000000, 0.000001, 0.000000 ]
[ 0.000000, 0.000000, 0.000000, 1.000000 ]
That is pretty close except for the 1st row, 1st column and the 3rd row, 3rd column which should both be 0.
Is this slight margin of error usually tolerated in game development?