Flockin’ Droid

While ago, I was looking for some informations about the film Batman Returns and stumbled upon the flocking algorithm, developed by Craig Reynolds in the eighties.

It has been used for many applications, other than movie making: optimization tasks, data visualization, UAV flight strategies, modeling of humans’ opinion flow in social networks and many more.

There / are / several / sources that explain the algorithm, other than Reynolds’ site, and in a few words the algorithm is based upon the combination of three forces:

  1. Separation: each entity of the flock, called boid, must try to not collide with one another, maintaining some space between each other.
  2. Alignment: each boid moves in the same direction of the other boids.
  3. Cohesion: each boid try to remain near the other boids.

Each force has a magnitude and a range (centered on the boid) in which it takes effect.
In my simulation, the six values of forces and ranges are the same for all boids.

I thought it could be interesting to visually see the effect of each force in a flock of boids so I made a simple app for Android where you can change the magnitude / range of each force and see the effect on the whole flock.
It is called Flock Lab and the complete source code is in my Github and the app is in the Google Play.

Flock Lab - QRCode

Flock Lab

Please, bear in mind that this is a simple test app, so you won’t find any special UI / UX design. :-)

In this post I will talk about the algorithm using videos of the application as guides. In a future post I will dive in the code, facing more Android – related aspects (It seems that the code is easy to understand without further explanation).

Tri Force

To describe what kind of behavior the forces generate, I made three videos and put them on Youtube. One for weights, one for ranges, one for targets.
I am sorry for the quality: they are quick and dirty ones.


Weights parameters describe the intensity of each force. Low intensity means that we remove that force from calculations.

On start, the app uses default values that I hard coded to get a resonable flocking behavior.

As we can see in the video, if we maximize separation and minimize the other two, a scatter behavior emerge and the boids avoid each other, moving around the field.

As soon as we put alignment force in the engine, the boids still try to remain separated but will tends to follow the directions of other boids, starting to show a tendency to form groups.

If we maximize cohesion and alignment but minimize separation, the boids will form very narrow flocks, as they won’t maintain any distances from each other. They still move around, as they will try to follow a direction.

Finally if we consider only cohesion force, the boids will form tight and stationary flocks. These groups won’t move around because there is no alignment involved: they don’t know which direction to take.


Each force has a range in which it takes effect.

We can regard the range as the ability of a boid to “perceive” or to “see” other boids.

In the video, I started forcing the boids to scatter around, changing weights accordingly.

As soon as we remove all ranges, we can see that the boids ignore each other and continue to move with their last velocity vector which won’t change. It’s like the scatter behavior we have seen before, the difference is that now a boid don’t “bounce” against another boid: it simply overlap.

Maximizing separation range, we get that the boids remain separated but don’t move in straight lines anymore: they will tend to remain at the same spot after a while. That’s because they have to remain separated from each other and they can see even faraway companions.

Of course, if we maximize the range of a force with no weight, then we don’t have any effect.

If we maximize alignment range and weight and we minimize everything else, then the agents will follow a unique direction, ignoring the fact that they are near or far from each other. Now we can clearly see a more distinct direction, as a wider range is set.

If we consider only cohesion force, both for weight and range, then we have again tight and stationary flocks. This time, we have a smaller number of groups, with more boids for each group.

Finally if we reset weights, then separation force is calculated and we’ve got loosen groups. Separation range is still at 1 but it is sufficient to have some effects, as the boids are very close together.


Tapping in the flocking area, will add a target. Continue to tap to cycle between types of targets: seek or evade.

We can also change the radius of each type of target and all emergent behaviors will be bounded inside or outside of the area, depending if it is a seek or an evade target.

Sometimes, the flocks will tend to move near the inner side of the circonference. I think that’s because at the perimeter of the circle is where most of the forces sum up, as the boids are all crushed against a wall, so is where we get most effects and separation force at default is the weakest.

That’s it for the flocking algorithm! Please, check out the other links for more. What I found really interesting is that there are a lot of applications, in lots of different fields.

In the next post, I will dive in the code of the app (It seems that the code is easy to understand without further explanation).

About these ads

About Luong

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.