Click here to Skip to main content
Licence CPOL
First Posted 2 Jan 2007
Views 78,597
Downloads 1,284
Bookmarked 23 times

Using XNA to Access an Xbox 360 Joystick

By | 2 Jan 2007 | Article
This article demonstrates how to use the Microsoft XNA Framework to read the state of an Xbox 360 controller
Sample Image - XNA_360_Controller_Input.png

Introduction

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.

Prerequisites

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 Microsoft.Xna.Framework and Microsoft.Xna.Framework.Input.

XNA

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.Windows.Forms and 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.Pressed or 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 GetCapabilities, GetState, and SetVibration. For this demonstration I will only use GetState and SetVibration. These methods take for their first argument a PlayerIndex value. This is used to specify the controller with which we will interact.

The GetState method returns a GamePadState struct. GamePadState contains the state of the controller in several sub-structs named Buttons, DPad, IsConnected, ThumbSticks, and Triggers. The Button and 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.Pressed and 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 System.Windows.Forms with Microsoft.Xna.Framework.Input).

//Setting or clearing a checkbox depending on the state of a button on the controller.

this.gamePadState = GamePad.GetState(this.playerIndex);
this.buttonA.Checked = (this.gamePadState.Buttons.A == Input.ButtonState.Pressed);

The Triggers struct contains the members Left and Right that have floating point to represent how far down the trigger is pressed.

The ThumbSticks member has two members Left and 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:0,Y:0) mean the joystick is in the center position, (X:-1,Y:0) means the joystick is to the far left position, (X:0, 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.

//Turning the slow vibration motor on to 50% maximum
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.

Conclusion

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.

History

  • 20th December, 2006 - Article created

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Joel Ivory Johnson

Software Developer

United States United States

Member

Follow on Twitter Follow on Twitter
I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering.
 
For the past few years I've been providing solutions to clients using Microsoft technologies for web and Windows applications.
 
While most of my CodeProject.com articles are centered around Windows Phone it is only one of the areas in which I work and one of my interests. I also have interest in mobile development on Android and iPhone. Professionally I work with several Microsoft technologies including SQL Server technologies, Silverlight/WPF, ASP.Net and others. My recreational development interest are centered around Artificial Inteligence especially in the area of machine vision.
 


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionButton state Pinmembertom.959:56 7 Mar '12  
GeneralWARNING: READ IF YOU ARE USING (OR PLAN TO RUN THIS ON) 64BIT MACHINE!!!! [modified] PinmemberFocusedWolf10:19 7 Jul '09  
GeneralRe: WARNING: READ IF YOU ARE USING (OR PLAN TO RUN THIS ON) 64BIT MACHINE!!!! PinmemberJoel Ivory Johnson10:57 7 Jul '09  
GeneralRe: WARNING: READ IF YOU ARE USING (OR PLAN TO RUN THIS ON) 64BIT MACHINE!!!! PinmemberJoel Ivory Johnson17:55 12 Jul '09  
GeneralRe: WARNING: READ IF YOU ARE USING (OR PLAN TO RUN THIS ON) 64BIT MACHINE!!!! [modified] Pinmembertom.957:47 7 Mar '12  
QuestionHow do I add VB code to it? Pinmembercmilianxp14:09 29 May '09  
GeneralError when trying to run sample program Pinmembermichaelry16:16 8 Apr '09  
GeneralRe: Error when trying to run sample program PinmemberJoel Ivory Johnson16:21 8 Apr '09  
GeneralRe: Error when trying to run sample program [modified] Pinmembermichaelry16:29 8 Apr '09  
GeneralCould not load file or assembly 'Microsoft.Xna.Framework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6d5c3888ef60e27d' or one of its dependencies. PinmemberJoel Ivory Johnson12:01 25 Nov '07  
GeneralRe: Could not load file or assembly 'Microsoft.Xna.Framework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6d5c3888ef60e27d' or one of its dependencies. PinmemberJoel Ivory Johnson15:58 25 Nov '07  
GeneralController connect Pinmemberkaylee3112:22 12 May '07  
GeneralRe: Controller connect PinmemberJoel Ivory Johnson13:30 12 May '07  
QuestionXNA with PS2 Controller PinmemberTony198619:30 20 Mar '07  
AnswerRe: XNA with PS2 Controller PinmemberJoel Ivory Johnson7:50 21 Mar '07  
GeneralRe: XNA with PS2 Controller PinmemberTony198612:45 21 Mar '07  
Thanks joel,
 
That was what I was afraid of since I needed to use the analog functions of the buttons on a PS2 controller and since DirectX doesn't support this I was hoping XNA would be a viable option. but when I read thru ur code I cldn't see where u actually selected your controller. but know I see thanks for the info.
 
Tony
GeneralC# [modified] Pinmemberquwip108:09 28 Feb '07  
GeneralRe: C# PinmemberJoel Ivory Johnson8:27 28 Feb '07  
GeneralRe: C# [modified] Pinmemberquwip106:25 1 Mar '07  
GeneralRe: C# Pinmemberpmorata0:20 10 Nov '08  
GeneralRe: C# PinmemberJoel Ivory Johnson1:33 10 Nov '08  
GeneralRe: C# Pinmemberpmorata1:36 10 Nov '08  
QuestionController connection to PC PinmemberBenjamin Liedblad21:38 5 Jan '07  
AnswerRe: Controller connection to PC PinmemberJoel Ivory Johnson16:27 6 Jan '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 2 Jan 2007
Article Copyright 2007 by Joel Ivory Johnson
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid