O'Reilly logo

Programming Game AI by Example by Mat Buckland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Flocking
Flocking is the behavior I mentioned at the beginning of this chapter —
the one I saw on the BBC documentary. It’s a beautiful demonstration of
what has become known as emergent behavior. Emergent behavior is
behavior that looks complex and/or purposeful to the observer but is actu
-
ally derived spontaneously from fairly simple rules. The lower-level
entities following the rules have no idea of the bigger picture; they are only
aware of themselves and maybe a few of their neighbors.
One good example of emergence is an experiment undertaken by Chris
Melhuish and Owen Holland at the University of the West of England.
Melhuish and Holland are interested in stigmergy, the field of science
partly occupied with emergent behavior in social insects like ants, termites,
and bees. They became interested in the way ants gather their dead, eggs,
and other material into piles, and specifically the ant Leptothorax, because
it lives among the cracks in rocks and operates, for all intents and purposes,
in 2D… just like a wheeled robot. When observing Leptothorax in the lab-
oratory, bustling about in their simulated crack — two sheets of glass —
they noticed the ants had a tendency to push small granules of rock mate-
rial together into clusters and wondered if they could design robots capable
of doing the same.
After a little sweat and toil they managed to create robots operating on
very simple rules, capable of gathering randomly scattered Frisbees into
clusters. The robots had no knowledge of each other and didn’t know what
a cluster, or even a Frisbee, was. They couldn’t even see the Frisbees. They
could only push Frisbees using a U-shaped arm situated in front of them.
So how does the clustering behavior happen? Well, when the robots are
switched on, they wander about until they bump into a Frisbee. A single
Frisbee doesn’t change a robot’s behavior. However, when a Frisbee-
pushing robot bumps into a second Frisbee, it immediately leaves the two
Frisbees where they are, backs up a little, rotates by a random amount, and
then wanders off again. Using just these simple rules and a little time, a
few robots will push all the Frisbees into a few large clusters. Just like the
ants.
Anyhow, let me abandon all this talk of Frisbees and get back to the
flocking. Flocking, as originally described by Reynolds, is a combination
of the three previously described group behaviors: separation, alignment,
and cohesion. This works okay but, because of the limited view distance of
a vehicle, it’s possible for an agent to become isolated from its flock. If this
happens, it will just sit still and do nothing. To prevent this from happen
-
ing, I prefer to add in the wander behavior too. This way, all the agents
keep moving all the time.
Tweaking the magnitudes of each of the contributing behaviors will give
you different effects such as shoals of fish, loose swirling flocks of birds,
118 | Chapter 3
Group Behaviors

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required