menu Menu

Finite State Machines and Emergent Behavior: Developing a Simulated Ant Colony

 

 

I’ve spent the last month attempting to develop a simulated ant colony with the help of Antigravity IDE. My goal is to watch a colony mind emerge from the noise of hundreds of individual ants, each hard-coded with simple, stimulus-driven behaviors that would, within the context of hundreds of other similarly coded ants, result in the emergence of a complex, hive intelligence.

 

You can find the simulation here: https://xenius.org/clickandbuilds/Simulants/index.html

 

Notes:
1. Upon entering, it will stutter while assets load. Give it a moment.
2. The digging mechanic is still under development! Once completed, I’ll update the code on the site. 
3. On start, create a few Foragers by dragging the slider to the right. This will get the food flow going. You’ll also need Attendants if you want the Queen to be fed. 
4. The sim is rife with issues… I’m working on them!
5. This app is not optimized for mobile devices, but it will definitely make an attempt to work.



Things that work: Trophollaxis, Necrophoresis, various pheremone channels, nurse behavior (last I checked), attendant behavior, and forager behavior.

 

 SimulAnts is a 3D real-time ant colony simulation that runs in the browser using Three.js and Vite.

Core Concept

Individual ants follow simple behavioral rules (state machines, pheromone sensing, path integration) that produce emergent colony-level organization — foraging, construction, brood
care, and corpse management all arise without top-down coordination.

Key Systems

– Voxel World — A destructible 3D terrain (640x240x640 blocks) that ants can excavate to build tunnels and chambers
– Pheromone Grid — A 4-channel chemical communication layer (home, work, food, death) that decays over time and guides ant behavior
– Ant Colony — Up to 5,000 ants across 8 castes (Queen, Nurses, Attendants, Foragers, Engineers, Workers, Soldiers, Phragmotics), each with distinct roles, colors, and behaviors
– Brood Cycle — Queen lays eggs based on food intake; eggs progress through larvae and pupae stages before becoming adults
– Food System — Clustered food sources that foragers discover, harvest, and share via trophallaxis (mouth-to-mouth feeding)

Ant Behaviors

Ants use state machines, pheromone gradient sensing, climbing physics, energy management, and necrophoresis (corpse burial). An ExcavatorBehavior module implements a behavior tree for complex multi-step tasks.

UI/UX

– Splash screen and logo animation intro
– Orbital 3D camera controls
– dat.GUI panel for tweaking simulation parameters in real time
– Live colony metrics graphs (population by caste, efficiency)
– Click-to-follow individual ants
– Manual dig/fill tools

Tech Stack

Vite + Three.js + dat.GUI + stats.js, with GLB 3D models for each caste and instanced mesh rendering for performance.

1. The Agents: Meet the Colony

The colony simulation is driven by thousands of individual agents, each running a finite state machine and represented by a 3d-ant model. They have their chemoreceptors and an array of simple programs driven by RNG, environmental stimuli, and chemical signal triggers (simulated pheromones). These pheromones have strengths and decay rates, and whether or not an ant responds to stimuli depends on its distance from the pheromones and the angles of its sensory array (antennae), which is configurable by the user in a control panel.

For example, real ants exhibit a behavior called “necrophoresis,” when they sense the presence of oleic acid. Oleic acid is a common monounsaturated fatty acid found in many oils (olive, avocado) and meats, and it functions as both a marker of decay in nature, and ironically, a common preservative in food products. 

Caste System

The colony is divided into specialized roles, each with unique behaviors:

  • The Queen: The heart of the colony. She wanders to find a suitable nest site, digs a “Claustral Chamber” deep underground, and lays eggs. She is governed by strict biological imperatives: survival and reproduction.
  • Engineers: The architects. They are the heavier diggers. They use Perlin Noise to find “naturally weak” spots on the surface to start entrances, ensuring the hill looks organic rather than perfectly geometric. Underground, they “prospect” for new chamber locations and dig connecting shafts.
  • Workers: The labor force. They follow the “Dig” pheromones left by Engineers to widen tunnels and clear debris.
  • Foragers: Speedsters who scour the surface for food, using Path Integration (essentially an internal compass counting steps) to find their way home after grabbing a bite.
  • Nurses: They detect misplaced eggs or larvae and carry them to the designated Nursery chamber.
  • Attendants: Their sole job is to find the Queen and feed her via Trophallaxis (regurgitating food), ensuring she has the energy to lay more eggs.

2. Pheromones: The Internet of Scents

Communication happens entirely through a Pheromone Grid—a 1024×1024 data texture draped over the world. Ants “paint” this grid with different scents, and others “read” it using three sensors (antennae) positioned Left, Center, and Right of their head. This allows for gradient following (sniffing out the strongest trail).

The Channels:

  • 🔴 Red (Channel 0 – Home): A permanent “breadcrumb” trail leading back to the nest.
  • 🟢 Green (Channel 1 – Food): Dropped by successful Foragers returning with loot. Other foragers follow this to finding the food source.
  • 🔵 Blue (Channel 2 – Dig/Recruit): Used by Engineers to mark excavation sites. “Dig Here!”
  • 🟣 Purple (Channel 3 – Death): Emitted by corpses. Used for Necrophoresis (see below).
  • 🟡 Yellow (Channel 4 – Construction): Marks areas designated for massive chamber excavation.

Pheromones evaporate (decay) over time. If a trail isn’t reinforced by more ants, it vanishes. This ensures old, useless paths are forgotten.

3. The World: Voxel Excavation

The ground is a Voxel World (volumetric pixels). It’s not just a heightmap; it’s a fully 3D block environment.

  • Digging Rules: Ants don’t just delete blocks. Engineers perform a Structural Integrity Check before digging. They scan a 2-block radius to ensure they aren’t digging into thin air, leaving pillars and supports naturaly.
  • Chambers: When a chamber needs to be built, ants carve out a specific geometric shape (an ellipsoid), checking the volume until it matches the blueprint.

4. Emergent Behaviors

Complex behaviors emerge from these simple interactions:

Necrophoresis (The Cemetery Effect) Ants hate the smell of death.

  1. Pick Up: If a Worker smells a faint death scent (an isolated corpse), they pick it up.
  2. Carry: They carry it around, effectively “looking” for a stronger death scent.
  3. Drop: If they smell a strong death scent (a pile of corpses), they drop their burden. Result: Without being told where the “cemetery” is, ants naturally tidy up the colony by clustering all dead bodies into huge organized piles.

Spiral Tunnels Because Engineers use localized noise and structural checks, they rarely dig straight lines. They follow the path of least resistance, curving around obstacles and following existing gradients, resulting in organic, spiraling tunnel networks that look remarkably like real ant casts.

Trophallaxis (Sharing) Hungry ants will stop valid food-carrying ants to ask for a meal. If they touch, they lock into an “Interaction” state, transferring energy values. This distributes food from the surface foragers deep down to the Queen without every ant needing to go outside.


Previous Next

Leave a Reply

Your email address will not be published. Required fields are marked *

Cancel Post Comment

keyboard_arrow_up