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.
It reads its neighbors, calculates its force, moves, and writes its position. It sends no message to do this.
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 follows a leader, or fears an enemy. The world keeps the relations as a graph.
The library is Erlang code. It uses only kernel and stdlib, from Erlang/OTP 29.
{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.
-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}.
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.
Define kinds of boids and their rules, and see them move.
The rules, the callbacks, the relations, the flocks of a world, and the speed.