Hi! My name is Nathan Fouts. I've worked on Resistance: Fall of Man, Ratchet and Clank: Tools of Destruction, and POSTAL 2. I'm currently working full time for my own company, Mommy's Best Games, Inc. This blog is to give the gaming community a glimpse of the highs and lows my team faces as we create Xbox Live Community Games. It also shares insights into XNA development in order for other developers to hopefully learn from our mistakes.
Flocking is the sort of programming that you can lose days experimenting with the subtleties. Some of you old-timers may remember the Stanley and Stella video from back in the 80's. Craig Reynolds "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.
Destructoid is an open discussion community. You don't need to "audition" to post a comment - just speak your mind. We respect differing opinions on the site, so have at it. Be smart, funny, insightful, clueless, or cute -- but back it up with substance. Keep your cool, keep it fun. We only ask that you act respectfully and above all: don't be a troll and ruin it for everyone else. Don't bring down gamers or we'll, you know, gently shoot you in the face and stuff you into a flaming mailbox. Each comment is your opportuntity to make this community awesomer. Is that even a word?
Avoiding the banhammer only requires common sense: spamming, trolling, racism, NSFW stuff, and other forms of sucking will not be tolerated. If anyone is griefing please report abuse. Be good. Don't suck!