This work elegantly resolves the tension between agent density and computational overhead through sophisticated spatial heuristics and temporal subsampling. It is a compelling demonstration of how iterative optimization can elevate a standard simulation into a high-performance masterpiece.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Solving my Simulator's Biggest Problem
Added:Overall, I am quite satisfied with the current state of my ant simulator.
It runs smoothly and allows to observe some interesting group behaviors.
>> [music] >> Performance is good and the animations are mostly fine. However, there is one aspect that bothers me a lot >> [music] >> and for which I had no solution until today.
The collisions between ants are a major [music] improvement over the previous version, but it brought its fair share of problems.
The biggest issue is that the ants constantly bump into each other as if they were completely blind and stubborn.
This problem seriously degrades the quality of the simulation as it affects [music] the very core of how the ants interact.
In this video, I will present the approach I used to solve this tricky issue.
Just as ants leave markers to guide others to points of interest, we leave a trail of personal data behind us on the web.
This information can then be harvested by malicious actors for a wide range of harmful activities from simple scams to identity [music] theft. Unfortunately, massive data breaches are becoming increasingly [music] common. And if your information is exposed in one of them, the consequences can be truly devastating.
Incogni, the sponsor of this video, helps prevent these incidents by automatically erasing these digital footprints [music] for you. To do this, it continuously scans trackers for your information and then requests its removal.
Their custom removal feature even allows you to request the deletion of your data from specific websites, ensuring you maintain full control over your online presence.
You can track all of this in real time directly from the dashboard >> [music] >> and the numbers are honestly quite impressive.
A huge thanks to Incogni for sponsoring this video.
>> [music] >> Use my code or click the link in the description to get 60% off an Incogni subscription.
Ideally, we need a solution that enables the ants to look ahead and adjust their paths based on obstacles.
There are numerous methods for this kind of problem, >> [music] >> which aim to simulate the behavior of agents moving in groups using avoidance strategies. [music] While these approaches tackle the issue from very different angles, one in particular caught my attention.
>> [music] >> PBMAD which stands for position-based multi-agent dynamics.
What makes this algorithm especially relevant to this [music] project is that it operates at the exact same level as the physics solver I use for collisions.
Both systems are working in the same position-based framework, making the integration very natural.
Let's explore how this algorithm works.
Despite its intimidating name, the underlying mechanism is actually [music] quite simple.
And fortunately, the paper describing the procedure is very well written.
Let's consider two agents with [music] their respective velocities.
Using this function, we can determine if and when they [music] will collide.
Once the point of collision is found, we look at their positions just before >> [music] >> and just after impact.
We then adjust [music] the post-collision positions to fix the overlap just as we would for a standard non-penetration constraint.
Next, we calculate the displacement necessary to avoid the collision entirely.
By subtracting these two values, we obtain the relative displacement needed [music] to prevent contact.
If we project this displacement onto the collision axis, we isolate [music] the specific component of movement that brings the agents together.
We can then offset the agents' [music] positions with this correction to cancel the collision before it happens. The correction is scaled based on how imminent the collision is.
Let's see how this plays out with two groups facing each other.
As we can see, this isn't quite the expected result.
The issue is that [music] the applied correction reduces the approach speed, which causes the agents to slow down drastically when facing each other in this scenario. What we actually want >> [music] >> is for the ants to actively avoid one another instead of simply braking.
To achieve this, rather than projecting the displacement onto the collision axis, >> [music] >> we will retain the tangential component.
This adjustment should encourage the agents to favor trajectories >> [music] >> that allow them to smoothly brush past each other rather than just decelerating. Let's see the result with this approach.
This is much better.
The ants now try to avoid one another while maintaining their speed as much [music] as possible.
Let's see what happens in a slightly more complex situation where the two [music] groups intersect at a 90° angle.
In this scenario, too, the ants manage to avoid each other quite effectively, albeit a bit more chaotically. Out of curiosity, let's compare this with the correction applied along the normal axis.
We can also compare it to having the avoidance system completely disabled.
[music] >> I find this result very satisfying.
As always, it isn't perfect, but things rarely are in nature anyway.
>> [music] >> Before activating the system in the actual simulation, let's run a quick performance test.
I will then instantiate 20,000 ants instead of 200, >> [music] >> which is fairly representative of a heavy simulation load.
To say the least, the performance isn't great.
The reason for this is that whenever I implement a new feature, I always start by writing a naive, yet reliable version.
Currently, identifying agents that might collide is done through a brute-force approach, meaning every single agent checks >> [music] >> against all the others.
It works, but it is incredibly slow.
Fortunately, the physics solver has already generated a spatial grid containing the positions of all objects, which we can now leverage [music] to efficiently find the colliders. To detect potential collisions within a given time horizon, we might assume it is enough to search for agents within a radius equal to the agent's speed multiplied by the maximum time.
However, if another agent is moving toward it at that same speed, >> [music] >> it wouldn't be detected, even though it could still cause an impact.
Therefore, >> [music] >> we must double the search radius to account for the relative speeds.
We can then restrict our search to the grid cells that intersect the time horizon's radius.
Using the spatial grid >> [music] >> already yields significant improvements.
The avoidance system's execution time drops from over 300 milliseconds to around 100.
The simulation is now usable, running at a much better frame rate.
However, we can improve performance even further by considering that the agents cannot see behind them.
This allows us to process only the cells within their field of view, saving a few extra milliseconds.
The relationship between the ants' perception time horizon >> [music] >> and the number of cells to check is quadratic. Therefore, an extremely simple way to save time is to reduce this radius.
Until now, I had been using a maximum [music] time to collision of 5 seconds, but it turns out that 2.5 seconds is more than enough for this simulation.
[music] This small adjustment has a massive impact, cutting the system's execution [music] time in half, down to just 45 milliseconds. While this does affect the ants' behavior by causing them to form smaller groups, [music] it does not affect the avoidance mechanism capabilities.
A key aspect of this avoidance mechanism [music] is that it deals with future interactions, potentially 100 ticks away.
Because of this, it is not strictly necessary for every agent to re-evaluate its future collisions on every single frame. We can easily reduce [music] the computational load by updating only half of the agents on any given tick.
This instantly divides the execution time >> [music] >> by two, while maintaining good simulation quality. To compensate for the lower update rate, [music] the avoidance corrections are scaled by a factor of two.
This brings us down to 24 milliseconds, >> [music] >> which is not bad, but still too high for a simulation aiming for 60 frames per second.
The main bottleneck remains the search for potential collisions, [music] which requires iterating over a massive number of cells.
Fortunately, [music] this search step has one specific characteristic. There are no dependencies between the agents.
This means [music] we can heavily parallelize it. Each thread will be responsible for a subset of the agents and will record the collisions it detects into its own [music] array.
Once the collisions are gathered, we still resolve them sequentially.
>> [music] >> As doing so concurrently could introduce data races.
It is possible to parallelize this step as well, >> [music] >> but we will leave that for later.
By utilizing 12 threads, >> [music] >> the processing time drops to just 4 milliseconds, which is immediately much more reasonable.
Thanks to all these optimizations, [music] the system is finally ready to be used in the actual simulation.
I have also reworked the user interface to make it less intrusive and easier to extend.
The various features are now accessible via a menu at the bottom of the screen [music] and I have added a Zen mode to minimize the UI.
I am now going to add a colony with a large food source nearby.
Placing an obstacle right in the middle will really put the system to the test.
As its edges will create situations with extremely high ant density.
Right from the moment the ants spawn, we can see that they are quite organized.
Their movements are more uniform [music] and they do not get in each other's way.
The ants have found the food source, so the real test can now begin as the first trails form and the ant population starts growing.
Let's speed things up a bit to see the trails emerge.
We can already see that adding the avoidance system does not prevent the formation of roots at all, which is a great start.
The ants seem to navigate well and no longer collide head-on.
However, the density isn't very high yet.
I find the ants' trajectories very satisfying.
The navigation is much more organized than before. [music] >> [music] >> In crowded areas, navigation is trickier but remains much smoother than in the previous version.
>> In the hotspot near the obstacle, >> [music] >> the density has increased significantly.
A few ants struggle slightly to make their way through.
But, overall, the swarm remains quite disciplined.
The population has now grown substantially, approaching 1,200 ants.
We are far from the 20,000 entities used in the performance test, >> [music] >> so the computing cost of the avoidance system here is negligible.
There is one detail I haven't mentioned.
Ants carrying food are heavier, so they delegate most of the avoidance correction >> [music] >> to the unburdened ants.
This is why we can often see the latter clearing the path to let the carriers [music] pass.
Everything seems to be going well.
I will let the simulation run longer to test the whole system with many more ants.
It seems to me that with this new mechanic, the paths are straighter.
Perhaps [music] having far less chaos on the trails helps optimize the trajectories.
>> Another change I made to the simulation is that the colony physically grows as the population increases.
In addition to providing a visual [music] indicator of the colony's size, this helps absorb a much larger flow of arrivals and departures, rather than creating massive bottlenecks around the nest.
After an hour of simulation and nearly 6,000 ants, I am very satisfied with the result.
The trails remain highly organized and the whole system feels much more cohesive.
Performance remains solid, even though more could potentially be parallelized.
As always, the project and its source code are available on my Patreon.
I'd like to take this opportunity to thank all the members who support my work. And of course, Incognit >> [music] >> for sponsoring this video. You can find all the relevant links in the description. Thanks for watching.
>> Mhm.
Related Videos

Setting up a curved screen with Immersive Calibration Pro 4 and multiple cameras (P3D v4)
FlyerOneZero
23K views•2019-07-21

Robot Learning with Sparsity and Scarcity
allenai
379 views•2025-10-14

Jorge Mendez-Mendez: Unlocking Lifelong Robot Learning With Modularity (2023-10-05)
umassmlfl
237 views•2024-01-06

Northwestern’s MS in Robotics: Student Robotics Projects, 2023
NorthwesternEngineering
1K views•2024-05-31

"Perfect" Turns: Turning by the Gyro - FIRST LEGO League (FLL) SPIKE Prime + EV3 RePlay Programming
ZacharyTrautwein
94K views•2020-10-02

Gorkem Secer: TSLIP-based Deadbeat Running Control of Bipedal Robot ATRIAS
DynamicWalking-wv6qm
298 views•2018-06-22

Self-Driving Cars Need Lessons On Human Drivers | Maddie About Science
skunkbear
26K views•2018-08-21

Milrem Robotics’ THeMIS UGVs used in a live-fire manned-unmanned teaming exercise
MilremRobotics
99K views•2021-05-20
Trending

we're almost finished the house (ep.125)
JennaPhipps
347K views•2026-07-22

We Finally Know Where Saturn’s Rings Came From
astrumspace
79K views•2026-07-22

BIG BET: Cathie Wood goes ALL IN on Elon Musk
FoxBusiness
89K views•2026-07-22

MIC DROP: Smithsonian Director Called Out For Woke Propaganda
TheAmalaEkpunobi
37K views•2026-07-23