Supporting Gamepad Input

The mouse is currently the only way to move the camera and without support for the Xbox 360 Gamepad the camera will be immobile - which really won't increase the entertainment value of your game. To fix the Camera class, add support for the gamepad instead of the mouse anywhere that you see mouse support code being used. You can choose to surround the GamePad code with a preprocessor directive to have it compile only in the Xbox 360 project, but you might as well leave it in there for the Windows project as well because it's not uncommon to use a gamepad on a PC.

I've listed the modified Camera class here, with the changes highlighted in bold:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace _3D_Game
{

    public class Camera : Microsoft.Xna.Framework.GameComponent
    {
        //Camera matrices
        public Matrix view { get; protected set; }
        public Matrix projection { get; protected set; }

        // Camera vectors
        public Vector3 cameraPosition { get; protected set; }
        Vector3 cameraDirection;
        Vector3 cameraUp;

        //Speed of the camera
        float speed = 3;

#if (!XBOX360)
        // Mouse support
        MouseState prevMouseState;
#endif // Max ...

Get Learning XNA 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.