O'Reilly logo

Learning XNA 4.0 by Aaron Reed

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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;

#if (!XBOX360)
        // Mouse support
        MouseState prevMouseState;
#endif // Max yaw/pitch variables float totalYaw = MathHelper.PiOver4 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required