Fish

The very first AI added to the game controlled the behavior of the schools of fish. I created a simple flocking algorithm, and added a few subtle features to liven things up. Here is a video of the fish in action:

As you can see, the school of fish sticks together, yet each individual fish moves somewhat independently. Here's the basic idea: each school of fish has a central point which moves around the level. All the fish in the school are drawn toward that point, but each fish is offset in a random direction. That way they don't all clump up in the same spot. Sometimes a fish will slow down or speed up, and sometimes a fish's random offset will change. Below is a diagram:

The fish have additional instructions which make them more realistic:

  • Water Aware: Fish know if they are out of the water, and will respond to gravity rather than 'swimming' through air.
  • Motion Oriented: The fish always face in the direction that they're moving.
  • Velocity Damping: The fish are always being slowed down to mimic water resistance.
  • Speed Cap: The fish are not allowed to go faster than a certain speed. It's fairly realistic, and it also prevents a certain issue where low framerate makes velocity damping ineffective.