Adventures in XNA 6: A Wii Flock of Boids
By tilting and rolling the Wiimote, you can control the flight of the lead butterfly.
The rest of the flock sticks together, chasing you wherever your accellerometer-fuelled flights of fancy take you.
All the butterflies, including the one controlled by the Wiimote, avoid collisions with eachother, as well as with the features of the environment.
[Update: After showing this demo at my Last Stand in Dublin, I posted an updated version of this blog entry on my new blog.]
How it's done
WiiMote Control: I integrated the Wii controller using the Managed Library for Nintendo's Wiimote which I found through Coding4Fun. 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 different attempts to find a Bluetooth adaptor that would work on Vista and speak with the Wiimote.
I am now using the EPoX BT-DG05A Bluetooth USB Dongle. Just so you know, the Vista bluetooth stack doesn't seem to talk to the WiiMote at all, so instead, I am using the Toshiba stack installed onto Vista. The Wiibrew Wiki offers a list of working Bluetooth devices - although Vista seems to complicate things tremendously. Many of the drivers for the Bluetooth dongles listed as Wiimote-compatible are simply incompatible with Vista, and when I forced two of them to install, they sent me from Bluetooth to Bluescreen. And the fantastic Toshiba stack will expire in 30 days. Sigh.
Once I got the Wiimote talking to my PC, having Wii input is very, very, very, very cool. Note that although this gives Wiimote input for PC games written with XNA, if I published my XNA game to the Xbox360, I wouldn't be able to use the Wiimote.
Flocking: The autonomous butterflies follow the rules of Craig Reynold's 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 pseudocode that I worked from.
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. 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.
I have some ideas for my butterflies that require a particle system, and rather than doing a cheap 'n' cheerful one, I'd like to implement one using HLSL, the High-Level Shader Language, which will give me hardware acceleration. If anyone wants to give me a starting point for that, I'd be really grateful :)
Also, I have some really fun Wiimote ideas in mind, and it's, as always, a matter of hours in the evenings!
XNA Refresh Available
With the XNA Game Studio Refresh, which was announced yesterday, developers can now package up your XNA games as binaries and distribute them.
I'd love to publish this game so that you can check it out yourself. Before I publish the Boids butterfly demo, I need to understand more about how "dissectable" the deployed format is. I'll get back to you on that one, but for now, happy coding!