Sunday, January 31, 2010

Wiimote and XNA Part 2 – Moving the Ship

The Wii Remote itself can only track two axes of rotation without using the Infra-red sensor bar. The CPU inside the little white box calculates these rotations and sends the data to the Wii (or in our case XNA) through Bluetooth so it can be used to calculate the movements of the objects on the screen. It’s because of this that we’re building our project on top of the Chase Camera sample I gave the link to in part 1. The Chase Camera sample is built in such a way that it uses a Vector2 object to hold rotations in the X and Y axes and so is perfect to use with the Wii Remote.


In the Ship class of the game, we’ve already put in the code to create and connect to a Wii Remote, now we’re going to use that Remote to move the ship around our world.
Inside the Update method, there is a list of declarations that allow the player to move the ship using the keyboard, just under here is where we’re going to put our Wii Remote code.
As we’re working with rotations, to move the ship left or right, the Wii Remote has to be “rolled”, and the if statements we’ll use check if this rolling has happened more than a certain degree. For simplicity sake, we’ll re-use the code in the game that moves the ship using the keyboard. It is possible to use the rotation of the Wii Remote itself but for this introduction tutorial I’d like to keep it as simple as possible.
Copy in the following code underneath the keyboard controls:

//Used to make the model go to the plus end of the X axis (->)
if (Wiimote.WiimoteState.AccelState.X > 0.09f)
{
rotationAmount.X = - 1.0f;
}
//Used to make the model go to the minus end of the X axis (<-)
if (Wiimote.WiimoteState.AccelState.X < -0.09f)
{
rotationAmount.X = 1.0f;
}
//Used to make the model go along the minus end of the Y axis (down)
if (Wiimote.WiimoteState.AccelState.Y < -0.09f)
{
rotationAmount.Y = 1.0f;
}
//Used to make the model go along the plus end of the Y axis (up)
if (Wiimote.WiimoteState.AccelState.Y > 0.09f)
{
rotationAmount.Y = - 1.0f;
}






Accessing the buttons of the Wii Remote is as easy as using an Xbox 360 controller in XNA. To show how this is done, find the piece of code that uses the space bar to make the ship fly forward. Here you can see how the KeyboardState object keeps track of changes to the keyboard buttons. The WiimoteLib’s button functions work in the exact same way. We can use this if statement to move the ship using the B button of the Wii Remote (which is the trigger on the back). The following code shows you how to do this:










if (keyboardState.IsKeyDown(Keys.Space)||
Wiimote.WiimoteState.ButtonState.B == true)
thrustAmount = 1.0f;









Now if you build and run the game, you’ll be able to fly around the world using the Wii Remote. Remember though, to move left and right the Remote has to be rolled to the left or right. Try and play around with the variables to see how different sensitivities can affect how the Remote works.








Feel free to leave a comment if you have any problems, I’ll try and help as best I can.

No comments:

Post a Comment