Note: Finish Chapter 6.5 before starting this chapter if you started the tutorial series before 12/10/2008

This one is quick because I’ve been very busy leading up to christmas, so here goes. This tutorial is on building a first person camera, the type used in most games these days. It can be moved and rotated. Later on we’ll learn how to apply physics to the camera so it can interact with the environment.

using Microsoft.Xna.Framework;
using System;

namespace Innovation
{
    // A simple first person camera. Accepts rotation and translation
    public class FPSCamera : Camera
    {
        // Keeps track of the rotation and translation that have been
        // added via RotateTranslate()
        Vector3 rotation;
        Vector3 translation;

        public FPSCamera(GameScreen Parent) : base(Parent) { }
        public FPSCamera() : base() { }

        // This adds to rotation and translation to change the camera view
        public void RotateTranslate(Vector3 Rotation, Vector3 Translation)
        {
            translation += Translation;
            rotation += Rotation;
        }

        public override void Update()
        {
            // Update the rotation matrix using the rotation vector
            Rotation = MathUtil.Vector3ToMatrix(rotation);

            // Update the position in the direction of rotation
            translation = Vector3.Transform(translation, Rotation);
            Position += translation;

            // Reset translation
            translation = Vector3.Zero;

            // Calculate the new target
            Target = Vector3.Add(Position, Rotation.Forward);

            // Have the base Camera update all the matrices, etc.
            base.Update();
        }
    }
}

Here’s how it would be used in a game. You will need to do a few things first though. First make sure the camera you are making is of type FPSCamera in the LoadContent() method. Then do the following:

KeyboardDevice keyboard = Engine.Services.GetService<KeyboardDevice>();
MouseDevice mouse = Engine.Services.GetService<MouseDevice>();
FPSCamera cam = (FPSCamera)Engine.Services.GetService<Camera>();

Vector3 inputModifier = new Vector3(
    (keyboard.IsKeyDown(Keys.A) ? -1 : 0) + (keyboard.IsKeyDown(Keys.D) ? 1 : 0),
    (keyboard.IsKeyDown(Keys.Q) ? -1 : 0) + (keyboard.IsKeyDown(Keys.E) ? 1 : 0),
    (keyboard.IsKeyDown(Keys.W) ? -1 : 0) + (keyboard.IsKeyDown(Keys.S) ? 1 : 0)
);

cam.RotateTranslate(new Vector3(mouse.Delta.Y * -.002f, mouse.Delta.X * -.002f, 0), inputModifier * .05f);

if (mouse.WasButtonPressed(MouseButtons.Left))
{
    PhysicsActor act = new PhysicsActor(
        Engine.Content.Load<Model>("Content/ig_box"),
        new BoxObject(new Vector3(.5f), cam.Position, Vector3.Zero));

    act.Scale = new Vector3(.5f);
    act.PhysicsObject.Mass = 1000;

    Vector3 dir = cam.Target - cam.Position;
    dir.Normalize();

    act.PhysicsObject.Velocity = dir * 10;
}

And that’s all there is to it! Press the mouse button and you should shoot a box away from the camera!

Download the Code for this Chapter
Back to Game Engine Tutorial Series

« »