online

Thứ Tư, 1 tháng 10, 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.

Không có nhận xét nào:

Đăng nhận xét