Digital Terrarium

After getting the flu and watching the Star Trek episode about hyper-procreative tribbles, I suddenly got the urge to model self-replicating organisms in Processing. Download the code on Github.


The smallest dots are autonomous, randomly wandering "bugs" and the green dots are "food," which are placed randomly every few ticks. Whenever a bug encounters food it will produce 10 offspring of the same color, but if a bug never finds food it will eventually shrink in size and disappear. The larger, red dots are wandering predators.  Whenever they eat bugs, they say "nom!" and also have a small chance of spawning another predator.

Key Parameters

  • W, width of canvas [pixels]
  • H, height of canvas [pixels]
  • D_f, diameter of food [pixels]
  • P_f, period of food generation [ticks]
  • D_b, diameter of bug [pixels]
  • V_b, maximum velocity of a "bug" [pixels/tick]
  • S_b, number of bugs "spawned" when food eaten [bugs]
  • H_b, number of ticks a bug can go without food before death [ticks]

Key Behaviors

  • Bug acceleration is "random" in 2D space but velocity never exceeds V_b
  • The canvas is "infinite" (i.e. if bug wanders off left side it will appear on right side, and vice versa)
  • Upon encountering food, a bug will eat it. The polygon representing the bug and the food must intersect.
  • If a bug goes length of time H_b without eating food it will disappear from simulation
  • Upon eating food, the bug will become completely "full" and will instantly generate offspring of amount S_b
  • Every amount of time P_f, a unit of food is placed randomly on the canvas

Special Thanks to Anton de Winter for helping to implement the predators.