How smart does a bee need to be?

Take a look at this video. It shows bees in a hive forming wave patterns to scare off predators:

This is what we’ll be making today. Here‘s the code for impatient ones. See below for explanation.

The Bees

This is how the bees act:

  1. A random bee flaps its wings.
  2. Tells the bees around it to flap their wings.
  3. A bee lowers its wings after some time and rests for a specific period before it can flap its wings again.

That’s the behavior of these bees in a nutshell. If we implement that in code, we should get similar patterns as result.

Apart from the above rules, there’s a random element to each bee. They have different reaction times, different rest period after flapping wings and different probability of actually flapping the wings when they’re told to.

Tweak the values of these parameters to view various patterns visible in the video.

The Hive

The hive is made of a 2D grid of bees:

There’s another collection in hive class which keeps track of which bees have to flap their wings next:

Bees add their neighbors to it when they want them to flap.

Upon each iteration of the game loop, the hive object:

  1. Tells all the bees to update themselves, and
  2. Tells the bees in beesToFlap to flap their wings.

Here’s the final model:

A more aggressive mode, with low forward_latency and high forward_probability:

Note that the Hive class just acts as a container for the bees (as it should, because that’s what a hive is in real world). The patterns result from the mutual interaction between bees in the hive.

 See you later.

Leave a Reply

Your email address will not be published. Required fields are marked *