Mommy's Best Games, Inc. is an independent game developer founded in 2007. This is a view behind the scenes of our game development and marketing!

Nathan

Wednesday, October 1, 2008

For Flock's Sake

Flocking is the sort of programming that you can lose days experimenting with the subtleties. Some of you old-timers may remember Stanley and Stella video from back in the 80's. Craig Reynold's "boids" taught me my original programming understanding of flocking. Since learning about it, I've programmed flocking systems several times (all for fun) and was excited to come up with an enemy to use it in Weapon of Choice.

The creature in the game is called the Darting Flock. In the prototype, the center (shown as a white rectangle) is the controlling element. The green fliers try to guard it. As the player shoots their way through the flock, the controller creates more fliers that encircle it. Some even dive-bomb you. The fun is trying to cut a hole through the flock and shoot the center while dodging incoming fliers.



Originally I tried simply having the fliers steer around the controller, and constantly home in on its center with an acceleration, sort of like the Earth being slung around the Sun. While the behavior looks decent in the video, it feels a little chaotic (which can be exciting) but generally primitive. I decided to try out a flocking algorithm to spice up the action.

As described by the "boids" system, flocking works by having many objects follow a small set of steering rules. I've simplified the system more but also added some changes to it to keep it interesting. The new actions are: 1. Find the closest neighbor. 2. Follow a target position offset behind the neighbor by a random angle. 3. Recheck for the closest neighbor every few seconds. The exceptions are a few 'leader fliers' which need to know where the flock is going. They are forced to follow the controller at a certain distance, while all other fliers connect from there. When the leaders follow the controller, they are actually following a position just outside the controller that orbits the controller itself. This indirection gives even more motion to the group.

The blue lines in the image are between the flier and their target. The white lines are from the flier that is getting followed by another flier. You can trace the chain of fliers as they follow each other to see the order.

Flocking can be more expensive than homing from a processing standpoint since every flier has to check the distance to every other flier. By staggering and delaying how often this happens we can ease the burden on the processor.

Several neat behaviors come from this. The fliers may fly outwards suddenly but will eventually come back around, usually in a wild spiral. When the player kills fliers, for a few seconds some of them will follow their dead friends down before reacquiring a new neighbor! This makes the fight a lot more exciting and can create some really intelligent looking emergent behavior from relatively simple programming.

3 comments:

Anonymous said...

I love this - as it makes me want to play this game even more. I think my first experience with shooting a core amidst a "guarding mechanism" that contained motion was in the arcade Phoenix (like...a 25 years ago). But that was just the silly band of barrier after you shoot through the big mass at the bottom of the mother ship. There is something about an enemy with this sort of meat sheild that makes it that much more frustrating and rewarding to kill.

Of course, I would have named your new enemy Flocking von Flockington...but what do I know.

Jo8nathan said...

Sounds and looks awesome. I can't wait! Now how do I go about incorporating your flocking logic into a communications server? Life seeming suddenly boring...

Nathan Fouts said...

Communications server? Ha! I can see the data following each other around, going to one server, then another. Not sure if that'd work out so well.

If you're bored, I've got a fun game you could help me on...