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 ControlsW - Forward
S - Backward
D - Right
A - Left
Camera Looking ControlsHold 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 HEREDOWNLOAD StarlingSwarmEXE HERELast Update: 8/13/09