Making an Xbox Gamepad Rumble With XNA Game Studio

Requires: XNA Game Studio, C#, corded Xbox gamepad

When I first made an Xbox Xbox gamepad rumble with C# and XNA Game Studio, I was in awe. Imagine all the things you could do with that!

It’s very easy to do.

First, you have to make a windows game:

  1. If you haven’t already, download and install the latest version of XNA Game studio from http://create.msdn.com.  NOTE: You must also have the latest version of C#. Also, you don’t have to get an App Hub membership to run programs on your computer, but you do to run them on your Xbox.
  2. Open C# and click “New Project”.Capture
  3. You will get a popup window. Simply select “Windows Game”, and press “OK”.

Congratulations! Winking smile You have just made a windows game!

You should see a window similar to this.

Capture1 

Now, scroll down in the big window with confusing-looking code you have now until you see this method:

protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit 

if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            base.Update(gameTime);
        }


This code is actually very simple. It is a method called ‘Update’. When it says,

// Allows the game to exit

if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

That means that when you press the back button on your gamepad, it closes your game. You want to try it? Simply get a corded Xbox controller, and plug it into a USB hub on your computer. (If you only have a cordless Xbox controller, than I’m sorry to say that you have to buy an adapter for your computer Sad smile).

Then, go to the top left corner of your screen, and click the green arrow.Capture2

Tada! You have a boring blue screen. Sarcastic smile BORING

However, when you press the back button on your Xbox controller, the blue screen closes.

Neat.

Let’s get to the part about the rumbling. Are you psyched up?? Is your controller still plugged in?? Great! This is the code for making the rumble pad rumble:

GamePad.SetVibration(PlayerIndex.One, 0, 0)

Not very menacing is it? Here’s how it works:

  • You might not know this, but the reason that the gamepad rumbles is because there are two motors inside it that are not equally weighted. Imagine if you went to RadioShack, and bought a motor and a battery pack. When you turn on the motor it doesn’t vibrate that much. Imagine if you tape a water bottle to one side of it. Then, when the motor spins, it would move your hand a lot. That’s how the Xbox gamepad’s rumble feature works (only the water bottle and the motor are a lot smaller and less dangerous.)
  • When you write the code
    GamePad.SetVibration(PlayerIndex.One, 0, 0)

    It works because your calling a method called SetVibration() owned by  the object Gamepad.

  • PlayerIndex works by telling the computer which controller to send the code to (PlayerIndex only goes up to four controllers).
  • 0, 0 tells witch motor to use. If you change the second zero to a one (0, 1), than you’re turning on the low vibration motor. If you change the first zero to a one (1, 0), than you turn on the high vibration motor. If you change both zeros to ones than the controller vibrates very very very fast and makes a noise like a cat purring through a megaphone.

Remember the Update code I was talking about? Copy and paste this code into that method:

GamePad.SetVibration(PlayerIndex.One, 0, 1);


Update now looks like this:

protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit 

if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            GamePad.SetVibration(PlayerIndex.One, 0, 1);

            base.Update(gameTime);
        }

The final thing you have to do is hit F5 on your keyboard or press the green button at the top-left corner of the C# window, and you will see the boring blue screen, with one other thing: your Gamepad will rumble. When you get bored, just press the back button.

Have fun with this new and interesting code!

Tags: , , , ,