Flock Simulation (Geometry-Nodes)

by Alex Martinelli in Modifier Setups


Geometry-nodes setup that allows to simulate flocks.

Requires Blender >=4.1.

For any inquiries or feedback, feel free to reach out on Twitter.

The setup allows to simulate flocking dynamics as seen in the animal kingdom (e.g. group of birds or fish), or more general interaction of large crowds of autonomous entities. Simple rules result in emergent behavior, generating complex structures and patterns. The provided implementation follow the Boids interpretation by Craig Reynolds.

Summary of the Setup

In the provided file you can find three main GN node-trees: flock_init, flock_behavior and flock_render.

flock_init is used to initialize the flock: initial number and position of entities. We provide a couple of template initialization (e.g. circle, sphere surface) to play with, but one should adapt this setup as preferred.

flock_behavior contains the actual logic that governs the flock. It simply requires a point cloud as input geometry to use for the simulation. If a value exist for the "vel" attribute, this will be used as initial velocity for the point, otherwise a random one will be assigned.

flock_render is used to instance aligned objects for the flock entities and render trails.

A flock behavior is defined by three rules: cohesion, alignment and separation. Each are dependent on the "Visibility Radius" which defines how close an entity must be to influence the behavior of other entities. For each of the three rules there is a factor value that control its strength. Setting it to 0 is equivalent to disabling the rule.

"Velocity Factor" controls how far an entity can move in a single frame. Higher values result in a faster but less accurate simulation, as entities will do "bigger jumps", and rules computations will be less precision (e.g. an entity can go through a wall/face it should have avoided).

"Friction" controls how much of the previous iteration velocity is lost for the current update. No friction results in momentum build-up that can quickly push entities to high speed and far away in the scene. We suggest to start with a small friction value like 0.05. Any amount of friction without external forces (e.g. attractor) can result in the flock to completely stop. Tweaking factors to search for interesting results is the fun part, but remember that equal values for cohesion and separation can cancel out, so always try to have them slightly different.

A flock can optionally be attracted by or avoid a target collection. For both "Attractor" and "Obstacles" you can find the dedicated panel in the node-tree, with three parameters: the collection that should contain the target objects, the max distance for the objects to have an effect on the flock and the effect factor (aka strength). You enable attraction or obstacles by providing a valid collection. For attraction, points will be distributed on the faces of all the objects in the collection. For obstacles, raycast will be used to compute avoidance logic. As such, valid collection will require objects with faces.

Extra content

In this version we also offer sim performance optimization and additional functionalities like trails and branching. If "Generate Trails" is enabled, each entity will generate a curve of its movements, allowing to visualize paths and create intricate structures out of the full simulation run.

"Generate Branches" allows for branching and randomized generation of new entities. Use parsimoniously, as exponential growth can cause Blender to get slow or even crash.

Regarding optimizations, we added a "Sim Substeps" param to run multiple steps in a single simulation frame. This allows for more precise results, especially important when using obstacles or attractors. This results in heavier computation (as all operations are run "Sim Substeps" times), however we tweaked some nodes and reduce duplication to save some computation time. We are open to feedback regarding this, and will work on further optimizing the accuracy and speed of the method.

Technical Considerations

To compute rules results, each entity should look at its valid neighbors, meaning the other entities that are at "Visibility Radius" distance. For now we implemented this via a "Merge by Distance" node, for then sampling the nearest. This is a rough estimate of the correct value, and we plan to update the setup once more utilities will be added to GN.

$6

Have questions before purchasing?
Login to message

Details
Published 4 months ago
Software Version 4.2, 4.1
License Creative Commons
Discover more products like this

birds flocks Blender geometry nodes flock simulation