Flocks of boids for Erlang and Elixir

Each boid is one process: an agent that senses its neighbors, decides, and acts, on its own clock. Many boids together move as a flock of birds, a school of fish or a crowd of people.

Connecting

A boid is alone

It reads its neighbors, calculates its force, moves, and writes its position. It sends no message to do this.

A boid is yours

A module with the behavior gen_boid tells what a boid perceives, what it decides and what it does. The hawk above hunts while it has energy, then it rests.

A boid has relations

A boid follows a leader, or fears an enemy. The world keeps the relations as a graph.

No dependency

The library is Erlang code. It uses only kernel and stdlib, from Erlang/OTP 29.

A first flock

{ok, World} = boids:start_world(#{
    bounds => {{0, 0}, {100, 100}},
    wrap => true
}),
{ok, Ids} = boids:populate(World, 200, #{}),
boids:snapshot(World).

The 200 boids now move in real time. snapshot/1 reads their positions.

A boid with a behavior

-module(hawk).
-behavior(gen_boid).

init(Pos) ->
    {ok, #{pos => Pos, group => hawks,
           rules => [{wander, 1.0}]}}.

handle_sense(Seen = #{neighbors := Near}, Boid) ->
    Prey = [N || N = #{group := birds} <- Near],
    {ok, Seen#{prey => Prey}, Boid}.

handle_decide(#{prey := [_ | _]}, Boid) ->
    Hunt = [{pursue, 1.0, #{target => {group, birds}}}],
    {ok, boids_boid:update(Boid, #{rules => Hunt})};
handle_decide(_Seen, Boid) ->
    {ok, Boid}.

Play the showcases

Five games made with boids: a ship in a field of rocks, a flock that you lead with rules, puzzles for young programmers, a herd that must not see you, and a land where species evolve.

Make your own boids

Define kinds of boids and their rules, and see them move.

Read the documentation

The rules, the callbacks, the relations, the flocks of a world, and the speed.