Boids Demo in Bevy

Jay Bond

I went into Dinacon 2022 with the notion that I would make an animal-themed videogame of some sort. It was a very vague goal which quickly became overshadowed by my desire to learn new tools, as I abandoned the fully-featured game engine and programming language I knew well in favour of an engine in its infancy and a language utterly new to me.

The result is a simple tech demo rather than a game, but I learned a great deal in the process and do not regret the choice.

The House Crow

Sri Lanka is home to a wide variety of fascinating wildlife, so the choice of a common house crow as the subject for my project may seem odd. However, I’ve always been attracted to creatures with unique relationships with human populations, and the crows certainly feature prominently in everyday Batticaloa. Their population size may be linked to human waste management problems. We regularly witnessed them stealing food from the endangered sea eagles who were nesting nearby, suggesting that in turn that population size can have an impact on other wildlife as well.

So I began my project with a simple 3D model of a crow, based roughly on photographs I took myself or collected from generous colleagues. I’m not an experienced modeller or animator, so it’s a little rough but suited my purposes.

Boids

As I discussed my interests with colleagues at Dinacon many ambitious ideas arose, such as modelling the social hierarchy of the crows, their interactions with other species, and how their population tracks with human waste management patterns.

However, I settled on modelling flocking behaviour, in part because it is something I always wanted to model in a game engine. I was also surprised to learn from biologists in attendance that the algorithms they used to model flocking behaviours were very similar to those I had heard of used in videogames. It should be said, however, that although we witnessed crows in large groups often I don’t believe this demo to be very accurate to the way those groups move about.

The Boids algorithm was developed in 1986 and has applications in simulations and videogames. It’s also very commonly the subject of tech demos such as this one! The gist of the algorithm is the balancing of three goals for each “boid”: separation, alignment and cohesion. All are based on each boid knowing the nearest bunch of neighbours in the flock. Separation is the goal in which each boid wishes to push away from any other boid that is too close. That is, it keeps them from bumping into each other. Alignment is the goal in which each boid attempts to face the same direction as its neighbours. Cohesion is the goal in which each boid attempts to get near the average position of its neighbours. This condenses larger groups into smaller ones, while separation functions as a counter-balance to keep a minimum distance between them. At each timestep in the simulation, every boid recalculates where it wants to go according to these three goals, and steers accordingly. The results of this very simple algorithm are surprisingly natural and varied.

The basic goals of the Boids algorithm, swiped shamelessly from Wikipedia.

In the demo, each of these goals is exposed as a weight with a slider. You can adjust them individually and observe the resulting change in the flocking behaviour. It sometimes takes a minute for the effects of changes to become obvious, but by varying these weights you can create larger or smaller flocks in tight or loose formations.

Bevy & Rust

The game engine I chose for this project, aptly named Bevy, is still quite early in its development. What attracted me to it was its open-source nature, cross-platform support, and its use of modern programming paradigms. It’s also programmed in Rust, a very new, low-level language which has rapidly gained popularity in the FOSS community.

It stands in stark contrast with Unity, the game engine I have used both as a hobbyist and professionally for more than a decade. Unity is rich with features, but is owned by a large private company and is married to paradigms and a language (C#) which are falling out of fashion. Moreover, Unity’s business practises include a cozy relationship with the US military — something that doesn’t sit well with me, personally. While I cannot avoid it at my day job, I was determined to learn something new at Dinacon, and have enjoyed my experience so far with Bevy.

Downloads

The demo, including full source code and crow asset, can be found at:

https://github.com/HulloImJay/bevy_boids_demo