Sailing with your Nintendo Wii controller

The Nintendo Wii controller is obviously a lot of fun for controlling games on a console, but have you ever thought that it's also an excellent way to interact with your own Processing programs? If so you'll be happy to know that there are a number of libraries and programs on the internet that allow you to do just that. Here we'll use the wrj4P5 library that will interface Processing code to the Wii controller using Bluetooth (so you'll obviously need a Bluetooth adaptor connected to, or built into, your computer). However, this article assumes you already have the libraries and controller set up, so if not you could head along to Instructibles.com for a useful explanation of how to do it (http://www.instructables.com/id/virtual-graffiti/). A screenshot of the virtual sailing program is shown in Figure 1.

Figure 1. A screenshot of the virtual sailing program.

To demonstrate how to incorporate the Wii controller into Processing code we'll use the GeoComputing virtual sailing program. That means the inner workings of the 3D processing code won't be discussed here. Also, to use the code ( which you can get in our source code section ), you will need to make a background image to suit your needs, like a nice sunkissed beach, a lighthouse, or whatever takes your fancy. When you run the code in Processing (remember to have the controller and Bluetooth installed first) you need to press the '1' and '2' buttons simultaneously on the WiiMote. The four led's at the bottom of the controller will then flash blue and, when they go out, you should be connected and ready to go.

If that all works, simply tilt the WiiMote from side to side to change the heading of the boat, and press the 'B' trigger button to accelerate forwards, or the 'A' button to slow down or move backwards. Pressing and releasing the '+' and '-' buttons will change the height of the waves, and similarly the Home button will restore the programs settings. It's all pretty simple really, and all the controls are on the WiiMote so you needn't be anywhere close to your computer to do a spot of virtual sailing. If you read our previous article on virtual sailing with keyboard control, you may also spot that this new code uses an improved bounding control method, so you can sail anywhere within the visible blue sea, and not fall off the edge of the world outside of the programs window!

So, how do we make our Processing code work with the WiiMote? Well, the first step is to import the relevant libraries at the top of our code, and make a variable to store the WiiMote object:

import lll.wrj4P5.*;

import lll.Loc.*;

Wrj4P5 wii;

Having done that, we need to initialise the WiiMote object, and then try to connect to the WiiMote via Bluetooth. We can do that by adding the two lines below to the end of our setup() function. While wrj4P5 is trying to connect you'll see a message telling you so in the text console at the bottom of the Processing IDE. If all goes well it will find the WiiMote and tell you that around the same time that the blue led's stop flashing.

wii=new Wrj4P5(this);

wii.connect();

Using the WiiMote is really quite easy. For instance, to use the built in accelerometer to control the heading of the boat all we need is the following two lines, which you'll see in the draw() function. The first line reads the value of the accelerometer, which has a useful range for our purposes of around -1 to 1 (presumably in g-values). What we do then is decide that we want to rotate the boat each frame by a maximum of a fiftieth of one PI radians. So, we can use Processings inbuilt map() function to calculate an angle to add to the existing boat heading. Not bad for two lines of code!

bang=wii.rimokon.senced.x;

boatangle=boatangle+map(bang,-1,1,PI/50,-PI/50);

At the bottom of the code you'll find two functions, one called buttonPressed() and one called buttonReleased(), and hopefully you'll work out that they are automatically called when a button is pressed or released on the WiiMote. For the '+', '-' and Home buttons we include code only in the buttonPressed() function, because we don't want it to auto-repeat. However, for accelerating forwards and backwards it's much better just to hold the buttons down until we get to our desired speed. To do that the code uses two Boolean (i.e. true or false) variables, which are set to true if the button is pressed, and false when it's released. That means the draw routine can continually know the status of the 'A' and 'B' buttons and act accordingly. For instance, below is an extract from our draw() function which controls forward acceleration based on whether the 'B' button is being pressed.

if(buttonBpressed) {

boatvelocity+=0.3;

if(boatvelocity>30) boatvelocity=30; }