With the release of XNA, Microsoft has made available a platform for writing games on the PC and for the Xbox 360. XNA provides more simplified access to hardware than managed DirectX did. The Xbox 360 controllers are also fully supported by the XNA Framework. In this article, I'll be using Visual Studio 2005 and the XNA Framework to create a demonstration form that will display the full state of Xbox 360 controllers connected to the system.
Update 11 November 2012 - I've posted another article that makes use of XInput instead of Xna to access the same functionality. XInput gives access to additional information (such as information on the battery) but the version of the code in the other articles requires Windows 8. You can find the article here.
This code example uses assemblies from the XNA Framework and you will need to install the XNA redistributables to run the example program. You can download it from here. In creating your own XNA project, remember to add an assembly reference to
The XNA Framework is based on a subset of the .NET Compact Framework and was designed to run on Windows XP SP2. At the time of this writing, Windows 2003 and Vista are not officially supported, although the Framework does operate on these systems. The Xbox 360 has an XNA game loader which provides a runtime environment for XNA based games.
Since XNA is based on a subset of the .NET Compact Framework, there is no support for the Windows Forms namespace or the network namespace. However, if you are targeting the Windows platform only, you can use
System.Net and other namespaces as needed.
Types of Inputs
The inputs on the controller are either analog or digital. The digital inputs will only have one of two states to indicate whether or not they are being pressed:
ButtonState.Released. The digital inputs on the controller are the buttons labelled A, B, X, Y, LB , RB, Back, Start, the buttons under both of the thumb sticks, and the D-Pad.
The state of the analog inputs is represented with a floating point number. The triggers on the controller will cause values between 0.0 (the trigger is not pressed) and 1.0 (the trigger is pressed all the way down) to be returned. The two thumb sticks return values between -1.0 and +1.0 for the x and y axis, where 0 is the center point for the axis.
The button in the center of the Xbox 360 controller is not accessible. It's only used by the Xbox operating system.
Accessing the Controller
The state of the controller is accessed through the
Microsoft.XNA.Framework..Input.GamePad class. This class only has three methods (beyond those inherited from
System.Object). Those methods are
SetVibration. For this demonstration I will only use
SetVibration. These methods take for their first argument a
PlayerIndex value. This is used to specify the controller with which we will interact.
GetState method returns a
GamePadState contains the state of the controller in several sub-structs named
DPad structs have members named after the 10 buttons on the controller and the four directions on the
DPad that will each be set to
ButtonState.Released (there is also an enumeration in the
System.Windows.Forms namespace, you may need to disambiguate between the two if you are using the
this.gamePadState = GamePad.GetState(this.playerIndex);
this.buttonA.Checked = (this.gamePadState.Buttons.A == Input.ButtonState.Pressed);
Triggers struct contains the members
Right that have floating point to represent how far down the trigger is pressed.
ThumbSticks member has two members
Right. Each one of these is a
Vector2, or a group of floating point X and Y values indicating the position of the joystick. A
Vector with the values (X ,Y ) mean the joystick is in the center position, (X:-1,Y ) means the joystick is to the far left position, (X , Y:1) means the controller is being pressed up, and so on. Note that you will never see the X and Y values in their most extreme state at the same time.
The Two Vibration Motors
The Xbox 360 controllers have two motors for vibration. The left motor causes a slow vibration. The right motor causes a quick vibration. The motors can be turned on with the
SetVibration method. This method takes a
PlayerIndex and two floating point numbers between 0.0f and 1.0f to indicate how strong each motor should vibrate.
GamePad.SetVibration(playerIndex, 0.5f, 0.0f);
About the Program
The example program continually polls the Xbox 360 controller on a timed interval and updates the onscreen state.
Checkboxes are used for the digital inputs. The analog inputs are represented with progress bars. The axis for the thumb sticks will be empty in the most negative position, half full in the center position, and full in the most positive position.
You can also turn the motors on using the numeric up/down inputs at the bottom of the UI. I've only allowed the motors to be on for a limited time and also turn them off when the program is terminating.
You'll find that the XNA Framework greatly simplifies interaction with the Xbox 360 controllers. In my next article, I'll show how to render graphics using XNA and using an XNA input device to control onscreen objects.
- 20th December, 2006 - Article created