A Wii Flock of Boids: Integrating the Nintendo Wii Controller with XNA

A wee flock of boidsAs part of my Last Stand demo, I showed how to integrate Nintendo’s Wiimote controller into XNA, first to navigate a butterfly around an environment by tilting and rolling the controller, and then to walk a creature across a tightrope wire by holding the Wiimote like a balancing pole.

I won’t be posting the code to this demo, but here is a description of how this works and how you can integrate the Wiimote controller with XNA, possibly to make your own Wiimote-controlled applications on the PC.

WiiMote Control:

I integrated the Wii controller using Brian Peek’s Managed Library for Nintendo’s Wiimote, which I found through Coding4Fun.  Brian includes a test app which you can use to help connect and test your Wiimote.  It took less than 20 minutes for me to integrate this library into XNA and map the accelerometers to the butterfly’s motion, but it had previously taken me 4 days and 8 (yes, eight) different attempts to find a Bluetooth adaptor that would work on Vista and speak with the Wiimote. 

Having Windows talk Bluetooth to the Wiimote is the hardest part:

The Wiibrew Wiki offers a list of Bluetooth devices which can communicate with the Wiimote.  Bear in mind that Vista seems to complicate things tremendously.  Many of the drivers for Bluetooth dongles listed as Wiimote-compatible are incompatible with Vista, and when I forced two of them to install, they sent me from Bluetooth to Bluescreen.

I used the EPoX BT-DG05A Bluetooth USB Dongle.  The Microsoft Bluetooth stack included with Vista never spoke to the WiiMote at all, regardless of dongle model on the PC.  Instead, I am using the Toshiba Bluetooth stack (5.10.12), which Toshiba supports being installed onto Vista.  Be forewarned that Toshiba’s stack, when installed on a non-Toshiba machine, has a built-in 30-day timebombed expiry.  As of today, there is no way I could find to acquire a longer license to it… so Toshiba, please offer us a way we can license your stack.

Wiimote input is limited to Windows-based XNA games:

Once you get the Wiimote talking to my PC, having accelerometer input is very, very, very cool.  Both the Wiimote itself and the Nunchuk that can be optionally attached contain 3 accelerometers each, with output normalized to gravity.

Although this provides Wiimote input for Windows games written with XNA, when I publish my XNA game to the Xbox360, I am unable to use the Wiimote.  #ifdef statements in the code make the Wiimote bits conditionally compile for the Windows version.

Animation:

My butterflies are animated with the XNA Animation Components library, which you can download from Codeplex.  Thanks to David Astle (who leads the project) for sorting out a little bug in the InterpolationController.  It’s been fun making a (modest) contribution to that project.

Now I can significantly slow down the animation, and the engine smoothly blends between frames of Phil’s animations, giving the butterflies a really natural look, even when they’re at rest.

Flocking butterflies:

The autonomous butterflies follow the rules of Craig Reynolds’ classic Boids flocking algorithm, which encourage separation, alignment and cohesion of members of the flock.  They avoid obstacles, and tend towards a target.  Here’s the Boids pseudocode that I worked from – and it’s really cool how a high-level language like C# lets you work almost directly from that pseudocode!

Mawg on Tightrope

Art Assets:

The butterfly and Mawg‘s models and animations, as well as the environment, were created by Phil McDarby and originally appeared in Still Life and Mind Balance. The skydome also appears in my Guitar Hero X-Plorer demo.

p.s. If you liked this, you’ll also like the Lego NXT Wii Bowling Robot!