Friday, January 29, 2010

Wiimote and XNA Part 1 – Setting everything up

Today I’m going to write a fairly simple tutorial that shows you how to get a Wii Remote working with Microsoft XNA Game Studio. Before we dive into the code though, we have to run through some things that need to be set up.

Before we begin:

To get a Wiimote working with XNA, the computer you’re working from must be Bluetooth enabled. Most new laptops and PCs have this built in but if yours doesn’t, a cheap Bluetooth adapter (like this one) works just as well. Next is the Wiimote library we’re going to use. This one was developed by Brian Peek and is pretty sophisticated. The game we’ll be using to build on are available on this XNA Creators Club page.

Pairing the Wiimote and PC

The easiest way to pair a Wiimote and your PC is to download and install the BlueSoliel Bluetooth stack. A trial version of this software is available at bluesoliel.com. The BlueCove Bluetooth stack is also known to work and can be found here. If your using BlueSoliel, this site offers a fairly detailed tutorial on how to pair the controller to the PC. For BlueCove, a more general tutorial can be found here.

The Game itself

For the purposes of this tutorial I’m using the Going Beyond: XNA Game Studio in 3D, Tutorial 2. I’ve chosen this to base this tutorial on as it’s a fairly easy guide to follow if you’ve never done 3D or you can download the complete sample. When you’ve opened up the sample game, it’s time to add a reference to the WiimoteLib. This adds a dll file that contains all the functions of the library.

To add a reference to the WiimoteLib:

  1. Click on “Solution Explorer”
  2. Right-click on your solution
  3. Click “Add Reference”
  4. In the Add Reference window, click on the “Browse” tab and navigate to where you’ve downloaded and extracted the WiimoteLib folder.
  5. Click OK and the WiimoteLib should be visible under the “References” folder of the Solution Explorer.

Adding the dll file to the solution

With the library referenced in our project, it’s time to import the classes we need into our game. Under the pre-made using statements add the following line of code: using WiimoteLib; Now its time to initialize our Wii Remote!

Initializing the Wii Remote

In the main constructor of the Ship class pop in the following three lines of code:

public static Boolean useWiimote1 = true; public static WiimoteLib.Wiimote Wiimote = new WiimoteLib.Wiimote(); public static WiimoteLib.WiimoteState wiimoteState = new WiimoteLib.WiimoteState();

The above lines of code a fairly straight forward, a boolean is used to detect whether we want to use the Remote or not (if your game needs alternate game control methods etc), a new Wiimote object is created and a WiimoteState object is used to track changes. In the initialize method, an if statement is used to pass the Wiimote object to an initializeWiimote method we’ll write in a minute. Copy the following code into the Ship method(just under the Initialization region):

if (useWiimote1 == true){ InitWiimote(Wiimote); }

Next we’ll write the initializeWiimote method. This is the method that will actually communicate with the Wii Remote first when they’re trying to connect. The code for this method is shown below:

private void InitWiimote(WiimoteLib.Wiimote wm){




    wm.Connect();
wm.SetReportType(InputReport.ButtonsAccel, true);
wm.SetLEDs(true, false, false, false);
}



Using the Wiimote object we created earlier, this calls the connect method of the WiimoteLib. Then, the InputReport type is set to ButtonsAccel, meaning we’re taking accelerometer and button data. There are a couple of InputReport types (such as one that tracks Infra-red data or extensions such as a Nunchuck attachment) but the button and accelerometer data is all we need for the purpose of this tutorial. If you run the game with a Wii Remote connected, the screen will open with a spaceship model in the middle. Your Wii Remote should have the LED light on the far left lit up to show that it’s connected. With no Remote connected, you’ll see an error like the one below:Running the game with no Wii Remote attached



This is a good error to get as it means the game is trying to connect to a Wii Remote! Now it’s time to move on to part 2 and actually use the Wii Remote to have our ship fly around.

No comments:

Post a Comment