Bait Ball

I think by now, we have all seen sardine or anchovy bait balls. The swirling vortex of fish bodies was a favorite part of the Planet Earth and Planet Earth: Life series. I had done a couple flocking/swarming simulations in the past but after seeing a presentation by collective animal behavior researcher, Iain Couzin from Princeton University, I retackled the problem with renewed interest.

One of my original flocking sims featured birds flying around in a 3D environment. I took the easy way out and ignored any collision detection. The birds were pushed about by a combination of gravitational attraction (to keep them fairly centered on the screen) and Perlin noise (to give the illusion that the birds were moving around in a fairly organic manner). The follow-up to that version introduced an extra force in the form of magnetic repulsion. This gave me collision avoidance but it was not very graceful.

The Iain Couzin presentation, featured on the WNYC RadioLab blog, presented the problem of flocking in a beautifully simple 3 rule system.

1) Don’t get too crowded.
2) Don’t get too isolated.
3) Swim with neighbors.

(An optional 4th rule is that you should do what it takes to flee predators)

This is a bit of an oversimplification of what Professor Couzin outlined but it gives a nice insight into the simplicity of these complex-looking systems. Based on these three rules, I pretty much had the first two down. The repulsive force kept the fish/birds from getting too crowded. The attractive force kept the fish/birds from getting too isolated. But I never paid much attention the rule 3 which is why my early studies often looked like chaotic writhing balls instead of what is experienced in nature.

The code is pretty straightforward. Find the distance between me and each one of my neighbors. If this distance is really large, move towards the neighbor. If this distance is really low, move away from the neighbor. If the distance is neither too close nor too far, then allow my velocity to become influenced by the neighbor. The amount of influence each fish has on each other is a curve. If the threshold of attraction is a distance of 50, then a distance of 51 would involve a minimum of influence compared to a distance of 200 which would be a stronger influence. I should probably limit this distance so that a fish 5000 units away would have no influence at all, but I need to do more research to see what is being observed in nature.

Future versions of this project will incorporate the dynamically resizing zones, as mentioned in the Ian Couzin presentation. For now, the zone thresholds are hardcoded. Also, there is no sense of leadership (though I don’t know if such a thing exists in the real world equivalent) and each fish is being influenced by every other fish in the simulation (which is probably not how these things actually work). What I will likely try next is to have each fish only being influenced by its 7 or so nearest neighbors.

Here is a video of the simulation. This version features 2500 fish and 2 predators. It runs in real-time.

This version uses 25000 fish and 2 predators and does not run in real-time.