Flock Simulation v2 (Blender Geometry-Nodes)
A Blender file with Geometry-Node (GN) setup and utils to run flock simulation.
Requires Blender >=4.3.
Have questions or want to share your results? Reach out on Twitter/X or Threads.
The setup enables the simulation of flocking dynamics observed 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 file you can find a flock_obj with three modifiers slots: "Initialize Flock" to define the initial points, "Flock Behavior" for the base simulation and "Render Flock" for the rendering and instancing aspect. You can start playing with these and get results right away!
flock_init initializes the flock by defining the number and position of entities. We provide few template initialization (e.g. circle, sphere surface) to play with, but one can adapt this setup as preferred.
"Flock Behavior" contains the actual logic that governs the flock. It simply requires a point cloud as input geometry. We provide two variants node-trees to use here: "Flock Simplified", with minimal parameters, if you just want to get some quick results, and "Flock Full Control", where all parameters are exposed. The premium version includes multiple presets to choose from via the "Preset" menu.
flock_render is used to instance aligned objects for the flock entities and render trails.
Simulation Parameters
A flock behavior is defined by three rules: cohesion, alignment and separation. Each is 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 controls 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).
"Sim Substeps" controls how many steps are run in a frame. The higher, the more precise the results, especially important when using obstacles or attractors, but the simulation will require more computation time.
"Friction" controls how much of the previous iteration velocity is lost for the current update. No friction can result in speed build-up that quickly pushes entities far away in the scene. We suggest to start with a small friction value like 0.01. Any amount of friction without external forces (e.g. attractor) can result in the flock to completely stop. Tweaking factors to find interesting results is the fun part.
"Velocity Update Factor" controls the momentum: is the mixing factor between the current velocity and the new velocity based on present forces. A factor of 1 means entities will make sharper turns and change speed more quickly.
A flock can optionally be attracted to 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 for Premium Version
In the premium version you will find additional functionalities like trails and branching generation, flock-on-ground behavior as well as multiple simulation parameters presets (access them in "Flock Simplified").
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 simulation.
"Generate Branches" allows for branching and randomized generation of new entities. Use parsimoniously, as exponential growth can cause Blender to slow down or even crash.
If you provide a "Ground Object" the flock will stick-to-it, like vehicles sticking to the ground. The other logic will be the same (like attraction or obstacles avoidance), but the entities movements will be limited to the ground.
Technical Considerations
To compute rules results, each entity detects its neighbors within the "Visibility Radius". We currently implement this using 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.
Also, we don't use a visibility angle, so any entity has a full visibility of all neighbors inside the sphere centered at its current position and of radius "Visibility Radius".
If using the same "Collision" method, cohesion and separation forces will simply cancel each other out (depending on the respective factors). Emergent behavior causes interesting results nevertheless, but if really caring about avoiding collision (especially when running "stick-to-ground") edit the "flock_behavior" tree to use "Self Collision" for separation (this accounts only for the closest entity).
Changelog
v2
- Added "flock-on-ground" behavior, requiring a target "Ground Object" (plus align-to-ground option in render)
- Fixed alignment logic to impact only direction and not speed
- Fixed "Mean Neighbors", cause "Merge by Distance" behaved differently for points than for vertices
- Add "Velocity Update Factor" to control how fast an entity changes its velocity vector in one update step
- Refactor of flock_behavior. Made the input for attractor and obstacles minimal, and instead ported logic to the "Flock Simplified" + "Flock Full Control" wrappers. Results in flock_behavior being more flexible (e.g. pass directly points or mesh).
- Added some sim presets to "Flock Simplified"
- Set position only for "extrude" points (for when trails are enabled)
- Instance collection instead of object in the renderer.
- Add optional "Self Collision" for separation rule