A Free Camera

I grew up on FPS games, I also got old on them too. I may not know what the hell I'm building but I know the camera is going to a first person one. So lets bring in some of my dodgy old math code that I've long since forgotten how it works, and try and piece together some free floating camera code that will let me fly around my undefined world.

struct player {
        float pitch, yaw;
        float tform[10];
};

The player struct will hold all the camera data I need, this may change at some point in the future but for now this is good enough.

I'm going to re-create the quaternion each frame, there might be some special quat magic that means this code is dumb but I don't know this magic. I'll keep the math high level, because math people will get mad at me.

struct m_vector3 x_axis = m_vec3_from_values(1, 0, 0);
struct m_quaternion pitch = m_quat_from_axis_angle(
        x_axis, 
        game->player.pitch);

struct m_vector3 y_axis = m_vec3_from_values(0, 1, 0);
struct m_quaternion yaw = m_quat_from_axis_angle(
        y_axis, 
        game->player.yaw);

struct m_quaternion rot = m_quat_mul_quat(yaw, pitch);
You spin me right round baby.

After I have a quaternion I can apply some movement to this rotation, that so we can move along the local axis.

struct m_vector3 fwd = m_vec3_from_values(
        0,
        0,
        ds * game->input_device.move_axis[1]);

struct m_vector3 left = m_vec3_from_values(
        ds * game->input_device.move_axis[0],
        0,
        0);

fwd = m_rot_point_with_quat(rot, fwd);
left = m_rot_point_with_quat(rot, left);

struct m_vector3 pos = m_vec3_from_array(
        game->player.tform.position);

pos = m_vec3_add_vec3(pos, fwd);
pos = m_vec3_add_vec3(pos, left);
You put your left foot in, your left out.

I suspect there might be a more graceful way of doing this but lets see where we are.

$ ./build_and_go.sh

Weeeeee! Ok this has made me slightly less disgruntled with the world, a camera that lets me fly around the world.

Poor frame rate not representative of the final product.

It has nothing stopping you do loop the loops, but I see this as a feature rather than a bug at this point.

I need to find a better way of capturing Gif's if I want to make the next big Indie game, as my understanding about game marketing comes from Twitter, and that seems how every big game markets their game. I'll think about that later, likely after I've rm -rf the directory when I give up in a huff, and ponder if Gifs would have saved it.

Subscribe to Republic Of Almost

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe