• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

3D First Person Camera Rotation

D

DIYDamian

Guest
I'm very curious if it is possible to get an effect where the 3d camera tilts in a similar way how a 2d view tilts with view_angle. Is it possible to recreate this?
 
you mean leaning? Aka rolling? Yes. The order in which you perform rotations is important, and will change the results. Roll then Pitch then Yaw seems to work well for first person shooters. I can elaborate on the process, but I don't have time at the moment.

But for right now I can leave this. Given those three rotations performed in the order above, you can figure out where your look, right and up vectors will point. your look vector being the direction your character/camera is looking, the up vector being the direction the top of your character's head / top of camera is facing, and right being a vector that points out the right side of your character's head / camera. The right vector is perpendicular to both look and up vectors.

This was constructed using left-hand rotations, to fit in with the left-hand coordinate system that gamemaker uses by default.

Code:
        //x = roll, y = pitch, z = yaw
        //c = cosine, s = sine
        //e.g., cy = cosine of pitch, sz = sine of yaw.
        //don't mix up radians and degrees!

        look_x =  cy*cz;   right_x =  cx*sz+cz*sx*sy;   up_x = sx*sz-cx*cz*sy;
        look_y = -cy*sz;   right_y =  cx*cz-sx*sy*sz;   up_y = cz*sx+cx*sy*sz;
        look_z =  sy;      right_z = -cy*sx;            up_z = cx*cy;

edit: oops, I had some wrong information here before. correction:

If you character is leaning, then it suggests the camera ought to move a little to the left or right, based on the lean, and it will probably also lower a little bit as it does that:

Code:
cam_pos_x = pivot_x + length * sx * sz;
cam_pos_y = pivot_y + length * sx * cz;
cam_pos_z = pivot_z + length * cx;
Taking the location of the pivot point, and then adding a vector to it which has a length that is equal to the distance from the pivot point to the camera, and has a direction based on the roll and yaw rotations.
 
Last edited:
Top