Source metadata

  • Type: Web book (open access, Creative Commons)
  • Author: Daniel Shiffman
  • URL: https://natureofcode.com/
  • Edition: 2nd edition (current; uses p5.js)
  • Code language: JavaScript (p5.js), but all algorithms are language-agnostic

Key takeaways

  • Physics and natural systems can be simulated through code by decomposing phenomena into vectors, forces, and rules applied per frame.
  • All motion follows the same cascading update: acceleration → velocity → position, applied each frame. Forces accumulate, then reset.
  • Realistic behaviour emerges from local rules with no central controller — flocking, cellular automata, and neuroevolution all demonstrate this.
  • Random values are not all the same: uniform, Gaussian, custom (Monte Carlo accept-reject), and Perlin noise serve different simulation needs.
  • Steering behaviours (Reynolds) provide a reusable vocabulary for autonomous agent motion: seek, arrive, wander, follow, separate, align, cohere.
  • Cellular automata (elementary CA and Game of Life) produce complex emergent patterns from binary state + neighbourhood rules.
  • Fractals and L-systems generate organic-looking procedural geometry through recursive rules.
  • Genetic algorithms encode solutions as DNA, select by fitness, breed via crossover and mutation. Three requirements: heredity, variation, selection.
  • Neural networks with supervised learning (perceptron → multilayer → backpropagation) solve classification and regression problems; ml5.js abstracts the implementation.
  • Neuroevolution combines GAs with neural networks: evolve network weights across a population of agents using fitness, crossover, and mutation.

Notable claims

  • Shiffman’s cellular automata chapter argues that very simple local rules can still generate rich, life-like patterns, using domino-like binary systems as the intuitive starting point for that idea.
  • The book treats each draw() cycle as one unit of time — a simplification that trades physical accuracy for implementation clarity. Euler integration is presented as useful and accessible, but explicitly approximate.

Chapter overview

ChapterTopicKey algorithm
0RandomnessRandom walk; Gaussian distribution; Perlin noise; Monte Carlo accept-reject
1VectorsMotion 101; vector operations; normalise + scale
2ForcesF=ma; force accumulation; friction; drag; gravitational attraction
3OscillationAngular motion; Hooke’s Law spring; pendulum; sine wave
4Particle SystemsEmitter/pool pattern; inheritance; polymorphic collections
5Autonomous AgentsReynolds steering: seek, arrive, wander, flow field, path follow, flocking (separation/alignment/cohesion)
6Physics LibrariesMatter.js; Toxiclibs.js; Verlet integration; compound bodies; constraints
7Cellular AutomataElementary 1D CA (Wolfram rules); Conway’s Game of Life; Wolfram classification
8FractalsCantor set; Koch curve; fractal trees; L-systems
9Evolutionary ComputingGenetic algorithm (fitness, selection, crossover, mutation)
10Neural NetworksPerceptron; supervised learning; multilayer nets; backpropagation; ml5.js
11NeuroevolutionGA + neural networks; sensor-based perception; continuous ecosystem model

Relevance

This source grounds the following wiki pages:

Connects to existing pages:

Unity teaching route in this vault

The book itself is written with p5.js, but the underlying simulation ideas translate well to Unity if the rendering is kept simple. The current vault route for that is overview-unity-nature-of-code-examples, which starts with:


Open questions raised

  • How do Reynolds’ steering behaviours map to Unity’s NavMesh and AI systems — where does custom steering add value over the built-in pathfinding?
  • Genetic algorithms are most useful when fitness can be automated. What are the practical limits for game AI (e.g., when is a behaviour tree faster to write)?
  • Perlin noise is used for terrain and texture; what are its limitations compared to domain-warping or Worley noise for game use?

steering-behaviours | genetic-algorithms | cellular-automata | procedural-generation | particle-systems-design | overview-unity-nature-of-code-examples