SOLVED Build a matrix without build_matrix?

RujiK

Member
So GMS has this handy function:
Code:
matrix_build(x, y, z, xrotation, yrotation, zrotation, xscale, yscale, zscale);
but I don't care about the positions or scaling, so I really only need matrix_build(xrotation,yrotation,zrotation). I've tried several implementations from the forums and the internet like this:

Code:
var _cy = dcos(cam_pitch),
    _sy = dsin(cam_pitch),
    _cz = dcos(cam_yaw),
    _sz = dsin(cam_yaw),
    _cx = dcos(cam_roll),
    _sx = dsin(cam_roll);
       

cam_view[0] =  _cz*_sy*_sx+_sz*_cx;  cam_view[4] = -_sz*_sy*_sx+_cz*_cx;  cam_view[8] =  -_cy*_sx; //x
cam_view[1] = -_cz*_sy*_cx+_sz*_sx;  cam_view[5] =  _sz*_sy*_cx+_cz*_sx;  cam_view[9] =   _cy*_cx; //y
cam_view[2] =  _cz*_cy;              cam_view[6] = -_sz*_cy;              cam_view[10] =  _sy;     //z
   
cam_view[12] = (-cam_x * cam_view[0] - cam_y * cam_view[4] - cam_z * cam_view[8]);
cam_view[13] = (-cam_x * cam_view[1] - cam_y * cam_view[5] - cam_z * cam_view[9]);
cam_view[14] = (-cam_x * cam_view[2] - cam_y * cam_view[6] - cam_z * cam_view[10]); 
cam_view[15] = 1;

matrix_set(matrix_world,cam_view);
But my results are not the same as matrix_build(0,0,0,cam_roll,cam_pitch,cam_yaw,1,1,1); Does anyone know how to duplicate the matrix_build function without actually using it? My end goal is to convert some GML code to shader code, but I can't duplicate the GMS matrix.

Thanks all. Generous upvotes if that is any incentive.
 

RujiK

Member
Sorry, I'm an idiot. I inexplicably had cam_coordinates set to a non 0 value.

Thanks. Solved.
 

RujiK

Member
No, it was just for testing. I'm actually building a matrix for animation, but somehow I can't get it working inside a vertex shader the same way that it does on the CPU.

I was wondering if maybe my original matrix code was wrong, so I just did a few comparisons between the matrix_build matrix and my matrix.
 
The code you posted shows an Z*Y*X rotation for a view matrix.
Code:
0:  X_x, 4: X_y,  8: X_z   //right
1:  Y_x, 5: Y_y   9: Y_z   //up
2:  Z_x, 6: Z_y   10: Z_z  //look
But if you were making a world matrix it would look more like this:
Code:
0:  X_x, 4: Y_y,  8: Z_z   
1:  X_x, 5: Y_y   9: Z_z  
2:  X_x, 6: Y_y   10: Z_z
And if your intent is to add a translation to that rotation, then 12,13,14 would just be equal to the x,y,z translation (sans multiplication with the x,y,z axes).
 
Top