MOBILE

XNA Game Studio 4.0 : Xbox 360 Gamepad (part 1) - Reading Gamepad State

8/1/2012 6:20:34 PM
The Xbox 360 gamepad is the controller that is included when you purchase an Xbox 360. It comes in both a wired and wireless model with several colors available to choose from (see Figure 1).
Figure 1. Xbox 360 wireless controller


The Xbox 360 gamepad features several analog and digital inputs, including face buttons, a direction pad, two thumb sticks, two triggers, and the circular Xbox 360 guide button in the center. The guide button is reserved and input from it is not available through the XNA Game Studio APIs. Along with input, the Xbox 360 gamepad also offers the capability to give force feedback to the user in the form of two vibration motors in the controller. The Xbox 360 gamepad can be used with a Windows PC. Wired controllers can plug directly into the PC using the USB connection. Wireless gamepads require a PC wireless receiver designed for the Xbox 360 wireless controller. Windows Phone does not support the Xbox 360 gamepad.

Note

When you create a new XNA Game Studio project, look at the Update method. It contains code that checks whether the Back button has been pressed and if it has to call exit on the game.

Although Windows Phone does not support the Xbox 360 gamepad, there is one exception. The physical back button on Windows Phones is reported as the Back button on the controller for PlayerIndex.One.


Reading Gamepad State

Just as with keyboard and mouse input, the gamepad input is read by polling the current state of a gamepad. The GamePad.GetState method is used to return the GamePadState structure, which can then be used to determine what buttons are currently pressed and the location of the thumb sticks and triggers. Tables 1 and 2 contain the methods and properties exposed by the GamePadState structure.

Table 1. Methods of GamePadState
MethodDescription
IsButtonDownReturns true if the provided button is currently pressed
IsButtonUpReturns true if the provided button is not currently pressed

Table 2. Properties of GamePadState
PropertyTypeDescription
ButtonsGamePadButtonsStructure that contains the state of all gamepad buttons
DPadGamePadDPadStructure that contains the state of all DPad buttons
ThumbSticksGamePadThumbSticksStructure that contains the position values for left and right thumb sticks
TriggersGamePadTriggersStructure that contains the position of the left and right triggers
IsConnectedboolReturns true if the gamepad is connected
PacketNumberintIdentifier

To read the current state of a gamepad, add the following line of code to your game’s Update method:

GamePadState currentGamePadState = GamePad.GetState(PlayerIndex.One);

The GetState method requires a PlayerIndex. In this case, we request the first player’s gamepad state. The Xbox 360 gamepad features four lights around the center guide button that displays the gamepads current PlayerIndex.

Reading Gamepad Buttons

Now that you have the current state of the gamepad, you can check whether any of the buttons are pressed by using the GamePadState.Buttons property. Buttons returns a GamePadButtons structure, which contains properties for all of the buttons supported by gamepads. These properties all are of the ButtonState type, which is the same used for the mouse buttons. Table 3 contains the list of properties exposed by the GamePadButtons structure.

Table 3. Properties of GamePadButtons
PropertyTypeDescription
AButtonStatePressed state of the A button
BButtonStatePressed state of the B button
XButtonStatePressed state of the X button
YButtonStatePressed state of the Y button
BackButtonStatePressed state of the Back button
StartButtonStatePressed state of the Start button
LeftShoulderButtonStatePressed state of the Left Shoulder button
RightShoulderButtonStatePressed state of the Right Shoulder button
LeftStickButtonStatePressed state of the Left Stick button
RightStickButtonStatePressed state of the Right Stick button
BigButtonButtonStatePressed state of the Big Button

Gamepad State Packet Numbers

If you call GamePad.GetState in a tight loop, you might receive the same state more than once. If you are concerned that you might call GetState too quickly, check the PacketNumber property on each GamePadState to verify that the numbers are different. States with the same PacketNumber represent that the device state has not updated.


To determine whether the GamePad buttons are pressed, add the following lines of code to your game:

// Determine if the A button has been pressed
if (currentGamePadState.Buttons.A == ButtonState.Pressed)
{
    // A button is pressed
}
if (currentGamePadState.Buttons.Start == ButtonState.Pressed)
{
    // Start button is pressed
}
if (currentGamePadState.Buttons.RightShoulder == ButtonState.Pressed)
{
    // Right shoulder button is pressed
}
if (currentGamePadState.Buttons.LeftStick == ButtonState.Pressed)
{
    // Left stick is clicked down
}

Some of these buttons are straightforward. The letter buttons A, B, X, and Y correspond to the face buttons on the right side of the controller with the same letter. The Back and Start buttons are to the left and right of the guide buttons in the center of the controller. The LeftShoulder and RightShoulder buttons are the thin buttons on the top left and right of the gamepad. The LeftStick and RightStick are buttons that set when a player depresses the left and right stick into the controller. The GamePadButtons structure also contains a property called BigButton. This button is available on a different type of Xbox 360 gamepad called the Big Button controller.

Reading Gamepad Direction Pad

Along with the face buttons, the Xbox 360 gamepad offers digital input from a directional pad on the left side of the controller. The GamePad.DPad property returns a GamePadDPad structure, which contains ButtonState properties for the Up, Down, Left, and Right directions. Table 4 contains a list properties exposed by the GamePadDPad structure.

Table 4. Properties of GamePadDPad
PropertyTypeDescription
UpButtonStatePressed state of the Up DPad direction
DownButtonStatePressed state of the Down DPad direction
LeftButtonStatePressed state of the Left DPad direction
RightButtonStatePressed state of the Right DPad direction

To determine whether the directions are pressed, use the following lines of code:

// Determine if DPad directions are being pressed
if (currentGamePadState.DPad.Left == ButtonState.Pressed)
{
    // Left DPad button is pressed
}
else if (currentGamePadState.DPad.Right == ButtonState.Pressed)
{
    // Right DPad button is pressed
}
if (currentGamePadState.DPad.Up == ButtonState.Pressed)
{
    // Up DPad button is pressed
}
else if (currentGamePadState.DPad.Down == ButtonState.Pressed)
{
    // Down DPad button is pressed
}

It is important to use else if conditions only on directions that are opposite from one another. It is possible to have two directions pressed if they are next to each other. For example, a user can press Down and Right at the same time.

Reading Gamepad Thumb Sticks

The gamepad has four analog states: two for the left and right thumb sticks and two for the left and right triggers. To read the state of the thumb sticks, use the GamePadState.GamePadThumbSticks property. The GamePadThumbSticks structure contains two properties: Left and Right both of which return Vector2 values. The Vector2 value represents the thumb stick displacement in the X and Y directions. These floating points range from –1.0 to 1.0. Table 5 contains a list of properties exposed by the GamePadThumbSticks structure.

Table 5. Properties of GamePadThumbSticks
PropertyTypeDescription
LeftVector2Position of the left thumb stick
RightVector2Position of the right thumb stick

To read the current X and Y position of the left thumb stick, use the following line of code:

Vector2 leftThumbStick = currentGamePadState.ThumbSticks.Left;

Reading Gamepad Triggers

Finally, the last type of gamepad state are the triggers. To read the current trigger position, use the GamePadState.Triggers property. It returns a GamePadTriggers structure, which contains two float values in the Left and Right properties. Each triggers value is from 0.0, which indicates not depressed, to 1.0, which indicates that the trigger is fully depressed. Table 6 contains a list of the properties exposed by the GamePadTriggers structure.

Table 6. Properties of GamePadTriggers
PropertyTypeDescription
LeftfloatPosition of the left trigger depression
RightfloatPosition of the right trigger depression

To read the current position of the right trigger, use the following line of code:

float rightTrigger = currentGamePadState.Triggers.Right;
Other  
  •  XNA Game Studio 4.0 : Adding Interactivity with User Input - Precision Control of a Mouse
  •  Asus Transformer Pad TF300
  •  Mobile Phone Game Programming : Using Sprite Animation - Building the UFO Example Program
  •  Mobile Phone Game Programming : Using Sprite Animation - Achieving Smooth Animation with the GameCanvas Class
  •  Mobile Phone Game Programming : Using Sprite Animation - Working with the Layer and Sprite Classes
  •  Windows Phone 8 Unveiled
  •  iOS 6 Beta Review (Part 2)
  •  iOS 6 Beta Review (Part 1)
  •  Ipad : Tabletop
  •  Ipad Lion (Part 3) - Other possible features
  •  Ipad Lion (Part 2) - What to expect from the upcoming mountain lion
  •  Ipad Lion (Part 1) - Lion paving the way for mountain lion's destiny
  •  The Effect Of IOS And Facebook On Shutterbugs (Part 2)
  •  The Effect Of IOS And Facebook On Shutterbugs (Part 1)
  •  Flipboard : The best streamlined news delivery application in iOS
  •  Apple's Undiscovered Country: The Future Of The Mac
  •  Sageone Payroll : The rock of wages
  •  iPhone, iPod touch and iPad. : Ourcast
  •  iPhone, iPad and Mac : Moneywiz 1.3.4 - Cash in hand
  •  Do More With Mail (Part 5) - Postcard, Email’n Walk, eMailGanizer pro
  •  
    Most View
    Information Theory
    Apple Store Insider Guide (Part 3)
    Microsoft SQL Server 2008 R2 : Hierarchyid Data Type (part 2) - Modifying the Hierarchy
    SQL Server 2008: Monitoring Resource Governor
    Motorola Xoom 2 - General Tablet Use
    Visual Studio 2010 : Introducing the Visual Studio Extensibility - Building a Visual Studio Package
    Windows Server 2008 : The Discovery Phase - Understanding the Existing Environment
    SQL Server 2008 Command-Line Utilities : The tablediff Command-Line Utility
    Web Security : Attacking AJAX - Intercepting and Modifying Server Responses, Subverting AJAX with Injected Data
    Sharepoint 2007: Upload a File - Upload a File from the Web Interface
    Top 10
    ASP.NET 4 in VB 2010 : The Data Controls - Sorting and Paging the GridView
    Microsoft Content Management Server Development : A Date-Time Picker Placeholder Control (part 2)
    Microsoft Content Management Server Development : A Date-Time Picker Placeholder Control (part 1)
    Microsoft Content Management Server Development : Building SharePoint Web Parts - Configuring the Web Part, Debugging the Web Part
    Windows Server 2008 R2 networking : Planning and Deploying DNS (part 4) - Monitoring and troubleshooting DNS
    Windows Server 2008 R2 networking : Planning and Deploying DNS (part 3) - Setting up DNS zones
    Windows Server 2008 R2 networking : Planning and Deploying DNS (part 2) - Installing the DNS Server role, Configuring DNS Servers
    Windows Server 2008 R2 networking : Planning and Deploying DNS (part 1) - Designing a DNS infrastructure
    Windows Server 2008 R2 networking : Routing and Remote Access
    ADO.NET Programming : Microsoft SQL Server (part 4) - Working with Typed Data Sets