Monogame - Simple First-person Camera

Full disclaimer: This is not how anyone should be using quaternions. The dungeon-crawler game is simply a learning/prototype project and I needed to get this working. This is just a means of using a basic understanding of how to manipulate quaternions to do what was needed.

Quaternions…I kind of understand them but not really. I understand them enough to achieve what I want done. Eventually after studying them I’ll get it I’m sure but for now I’ll continue in ignorance.

.

A simple fps cam is suprisingly involved. Originally for simplicity this game did not use mouse-look but when I realised that it was worth the effort I implemented it.

.

The above image shows my (probably wrong) camera class. But it works. Essentially I store to two rotations for looking up-down and left-right in seperate quaternions (lines 16-17).

When the player wants to rotate in particular direction, this is simply added to the relevent quaternion representing an axis, using the functions on lines 32 and 38.

Multiplying two quaternions essentially adds the rotations together. What this means is that when before passing the camera matrix to the shader, you simply create a rotation matrix from the two quaternions multiplied together (line 29). You then multiply your view matrix by the rotation matrix and voila.

The pitch angle (up-down) is stored to limit the rotation from going too far either above or below.