I'm a beginner programmer and I'm creating a project which allows me to walk around rooms via a first person camera. So far, I have the buildings drawn and on, but now I'm stuck and don't know how to make a first-person camera to allow me to walk around.

Could anyone point me towards a camera class I could use, or some useful code?

3 Answers
3

There are several ways of going about this (see tutorials here, here and here for example, with plenty more available on the Internet via Google). The technology used in the resources you may find online may vary slightly (in terms of D3D, XNA, OpenGL, et cetera), but the underlying principles are going to be the same:

your camera object maintains its position and its direction, and optionally another pair of vectors that, alongside the direction, form an orthogonal basis for the camera's coordinate system.

your camera's public API exposes methods for yawing, pitching, and optionally rolling the camera around it's basis vectors -- the act of adjusting the camera orientation will update the basis vectors for subsequent use.

You can elect to store the vectors directly, or recompute the underlying view matrix every time, as you need and prefer. There's a lot of flexibility to the technique, so if you need help beyond those general steps you may want to post a new question with a more specific query.

mouseAxisX and mouseAxisY are defined to be +/-1, depending on whether you want the x or y axis mouselook inverted. Usually games offer this option at least for the vertical axis.

MIN_UPWARDS_TILT_DEG is defined to be 1.0 degrees (so the viewer is allowed to look from -89 deg downwards to +89 degrees upwards, which looks quite convincingly like a full 180 degree vertical range -- the missing 2 degrees at the extremes are quite negligible).

camera.aim_, camera.right_, and camera.up_ are of course 3D vectors, and the rotateAboutAxis() method you can cobble together from wikipedia and any number of online sources. Y_AXIS is a fixed constant (0,1,0) vector.

ASSERT_ORTHONORMAL() is a debug-mode-only sanity check, which never gets compiled in optimized/release mode.

Apologies in advance for the C-style code... then again, here you are taking advice from a guy named Mediocritus! ;^)

For a specific reference to an implementation of camera system using Direct3D, I recommend
this article: http://www.toymaker.info/Games/html/camera.html. The author also describes other methods and features that may be used to expand on the implementation.