Digit 7

Your source for XNA tutorials and 3D programming ideas.

Home     Shader Tutorials     XNA/C# Downloads     Links      
Blackjack Simulation
Heightmap Generator
Physics Simulator
Procedural Tree Generator
QuadTree
Swarm Intelligence
Matrix Solver

Swarm Intelligence Version 2





Improvements

     I went through all of my code from the StarlingSwarm application (see below) and completely reworked all of it to make this much improved general swarm intelligence application.
  • I rewrote the AI code so each boid looks to the nearest 8 boids instead of the entire swarm. This has allowed me to increase the counts dramatically from 1000 to 15000 (and my machine isn't even that powerful, so I'm sure many of you could get more boids on the screen.) The boids are initialized in an X,Y grid, so each one looks in all the 8 intermediate cardinal directions (N,S,W,E,NW,SW,NE,SE). As they move along, they try to keep each of their neighbors within a certain distance. If their neighbor is past that distance, they move towards it. If the boid is too close, they repel away from it. This combined with the predator avoidance makes up the entirety of the behavior. The code is much simpler than the first incarnation of StarlingSwarm
  • Everything is now completely adjustable while the application is running. You don't have to stop the application, change the values it initializes to, and restart it. You now press a key mapped to a certain parameter of the swarm and use the plus or minus keys to adjust it.
  • I've implemented "instanced" models to allow me to draw several thousands objects on the screen with little performance cost.
  • I've taken "Starling" off of the title because I've noticed you can simulate a lot of different types of swarm intelligence using this new algorithm. Play around the with the values and you can see what I mean. I've been able to create realistic simulations of a school of tuna, a swarm of insects, a line of ants, etc.

How to Use It

     I've provided both the XNA project and executable below in case some of you visiting the site don't have XNA installed. All of the key binds are displayed for you so it should all be pretty self explanatory. If you have any questions, email me at dan@digitseven.com.

8/29/09 - Updated the Instancing so it now runs with cards that can run VS2.0
DOWNLOAD SwarmIntelligence XNA Project HERE
DOWNLOAD SwarmIntelligence Executable HERE

Last Update: 8/29/09


Swarm Intelligence: Starling Swarm




Introduction

     This was a program I first programmed in DirectX several months ago before I got into XNA. I'm not sure exactly how it started, but I thought it would be interesting to try and model a flock of starlings. If you don't know exactly what a flock of starlings looks like, watch this video. The organization and fluidity the entire flock has despite a lack of any central intelligence is mind boggling. Each bird pays attention to the nearest 6 or 7 birds around it and adjusts its position based on those neighbors. I've added some references to my Links section if you'd like to research it futher. Anyway, I've decided to finally port it over to XNA so I could share it with the community.
     I've also discovered that on the XNA Creator's Club website, they already have an example of 2D Flocking (along with many other cool AI examples. Check them out!). Although similar to what I did, my StarlingSwarm application can handle hundreds of starlings, exhibits more complex behavior (which you can tweak however you like; see below) and is done in 3D, unlike the CC example which uses sprites. One thing I liked better about their example is that they split of the different components of movement into "behaviors." For example, the repulsion between starlings is one behavior, the force to follow the general direction of the flock is another behavior, etc. I recommend changing my code to use that approach for a good programming exercise. It makes things much easier to debug and is easily extendable if you want to add more behaviors later. You could also create other AI patterns using different combinations from your library of behaviors. It's really a brilliant, well-thought out approach.
     When moving it over to XNA, I also implemented a tree. If you don't know what a quad-tree is, check out my QuadTree class I've provided above. I also suggest searching Google or reading this Wikipedia article. To keep a long story short, the quad tree allowed me to double the amount of starlings I had on the screen. Space partitioning is an amazing thing and allows you to greatly increase the complexity of your scene. Anywho, I just thought I'd mention this so you aren't wondering what that class is doing in my project.

How to Use It

     Camera Movement Controls
W - Forward
S - Backward
D - Right
A - Left

     Camera Looking Controls
Hold down the left mouse button and move the mouse around to look.

     You're going to see a bunch of balls flying around. If you don't at first, move the camera around. I don't have the modeling skills to make an actual animated starling. If someone did that I think it would be pretty cool. The smaller red balls represent the starlings and the bigger blue balls represent the falcons.
    
How to Tweak It

     There's three classes which you can tweak: the Flock class, the Starling class and the Falcon class. If you want to change how many falcons and starlings there are, go to Game1.cs and change the constants in there. The Flock class is a DrawableGameComponent which I use to manage all of the starlings and falcons. All of the AI code is kept in the update() method. I won't totally explain everything here since it'd take me a couple pages. All you need to concern yourself with is the constants which are defined in each class. The Starling class has a bunch of different constants controlling how attracted the starlings are to eachother, how closely they follow the flock, how repulsed they are from eachother, how repulsed they are from the falcons, etc. I'd suggest just going through the code for all of the classes so you get an idea of how things fit together. Have some fun and try and create new behavior!

How to Make It Better

  • Replace the sphere models with actual models of starlings and falcons.
  • Optimize the algorithms to allow for even more starlings. On my machine, I can get about 2000 at a decent frame-rate with 3 or 4 falcons to keep things interesting. You could optimize the actual routines calculating forces, or maybe work on a more efficient QuadTree.


DOWNLOAD StarlingSwarm HERE
DOWNLOAD StarlingSwarmEXE HERE
Last Update: 8/13/09