Overview
In other tutorials, we have covered both keyboard input and mouse input. In this tutorial, we will take a look at input from the Xbox controller. We will first take a look at the various components of an Xbox controller, and then we will look at how to get input from it.
Xbox 360 Controllers
Xbox controllers are the preferred method of input for an XNA game since they work on both the Xbox 360 and on Windows. Obviously, if you have an Xbox 360, then you already have a controller. Otherwise, you might be interested in buying one. At the bottom of this tutorial, I've included some tips on buying an Xbox 360 controller that you might be interested in.
The Xbox 360 controller has lots of buttons and controls that give the user a lot of power. An Xbox 360 controller, with all of it's buttons labeled is shown below:
The left and right thumbsticks are small joysticks that can be controlled by your thumbs. These are among the more common input methods on the controller. In addition, both of them can be pressed in, and so they can also be treated as buttons.
The directional pad consists of four buttons, each of which is assigned to a specific direction—up, down, left, and right. Since we are programming a game, we can assign these to anything we want, but they are typically used for navigating menus, rather than actually controlling movement.
In the upper-right part of the controller are four colored buttons that are given letters as names (A, B, X, and Y). These buttons are also commonly used for a variety of purposes.
In the center of the controller is a rounded button with a green ‘X’ on it. This button is called the Xbox Guide button. In XNA games, we won’t be able to direct what happens when this button is pressed, the system that we are on decides what to do with it.
On the left side of the guide button is the back button, and on the right side is the start button. Once again, we can program these buttons to do whatever we want, but they are typically used for menu navigation as well. The back button moves the player out of menus (back to a higher level, perhaps) and the start menu moves the player into deeper menus, or to start the game.
On the top of the controller are two “shoulder” buttons. These buttons are similar to the lettered buttons and are used for a variety of things. Typically, the most commonly used features in a game are assigned to the lettered buttons, while less common features are assigned to the shoulder buttons since they are a little harder to get to.
While we can’t see them in the image above, the Xbox controller also has a left and right trigger on the top of the controller. The triggers have the useful feature of being able to be pressed partway down. The other buttons discussed (not including the thumbsticks) are either pressed or released. The triggers can be pressed all of the way, not at all, or anywhere in between.
Getting Input From the Xbox Controller
Getting input from the Xbox controller is very easy. If you've looked at doing input with the keyboard or mouse, you won't be surprised with what we are going to do. They are all fairly similar. Probably the best place for getting player input is in the Update() method. The line of code below retrieves the current state of a controller:
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
Of course, if you want player two's input, you would just switch it to say PlayerIndex.Two instead of PlayerIndex.One.
Now that we have the current state of the player's controller, we can check to see what state it is currently in. For starters, we can check to see if the controller is even plugged in:
if(gamePadState.IsConnected) { // then it is connected, and we can do stuff here }
To check if the various buttons are being pressed, we will use the GamePadState.Buttons property. For example, the following code checks to see if the 'A' button is being pressed:
if(gamePadState.Buttons.X == ButtonState.Pressed) { // do something }
The directional pad (D-Pad) can be tested in a similar manner. The D-Pad has four buttons on it: Up, Down, Left, and Right. So we could access the D-Pad like this:
if(gamePadState.DPad.Down == ButtonState.Pressed) { // do something }
The state of the two thumbsticks can also be accessed easily. The thumbsticks, however, give us an x- and a y-coordinate that are between -1 and 1. If the x-coordinate is -1, then the thumbstick is tilted all the way to the left. If it is 1, then it is tilted all the way to the right. If the y-coordinate is -1, then it is tilted all the way down, and 1 means it is tilted all the way up. 0 indicates that it is at the center for either of the directions. Below is the code for updating an angle based on the position of the left thumbstick:
float maxSpeed = 0.1f; float changeInAngle = gamePadState.Thumbsticks.Left.X * maxSpeed; // this variable is defined elsewhere angle += changeInAngle;
The triggers are accessed in a very similar manner, though their values range from 0 to 1, where 0 means the trigger is not pressed at all, and 1 means it is pressed all the way down.
float leftTriggerValue = gamePadState.Triggers.Left;
Checking for Button Presses
Just like with other forms of input that we have seen, we might want to have something happen only when a button is first pressed, rather than happening every time that we see the button is pressed. To do this, we will save the state of the Xbox controller, and check to see if the new state is pressed, and the old state wasn't. This would occur when the button is first pressed, and not happen again until the button is released.
We could do this by adding an instance variable to the class to store the old state like below:
private GamePadState oldState;
And then setting up our update function like the code below:
GamePadState newState = GamePad.GetState(PlayerIndex.One); if(newState.Buttons.X == ButtonState.Pressed && oldState.Buttons.X == ButtonState.Released) { // the button has just been pressed // do something here } // At the end, we update the old state to the state we grabbed at the start of this update. // This allows us to reuse it in the next update. oldState = newState;
Tips on Buying an Xbox 360 Controller
If you are looking to buy an Xbox controller, they come in two forms: wired and wireless. The wired controllers are USB, so they will work just fine on a Windows machine.
If you buy a wireless controller, it won't work on a PC unless you also buy a wireless receiver for it. These will usually run about $50, but (I'm almost certain) one is enough for all of your controllers, in case you have more than one controller.
One other thing to remember is that they sell two forms of controllers. First, there is one that is advertised as "For Windows". This version also includes a CD that has the needed drivers on it. The other form is not marketed as "For Windows". It does not come with the CD. However, you can download the drivers from lots of places around the Internet, though usually you can just plug in the controller and allow Windows to find the drivers on its own. The "For Windows" controller will usually cost a little more than the regular one, but since the only difference is the driver CD, which isn't even necessary, make sure you don't pay extra for it!
In addition, Windows recognizes the Xbox controller as a normal joystick, even if you aren't using an XNA game, so you don't have to buy a separate joystick!
What's Next?
We've now seen the basics of getting input from the Xbox controller. You might want to check out how to do input for the keyboard and mouse if you haven't already seen them. Otherwise, you might want to see the tutorial on how to make the Xbox controller vibrate.
Having problems with this tutorial? Try the troubleshooting page! |