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
| Chapter | Topic | Key algorithm |
|---|---|---|
| 0 | Randomness | Random walk; Gaussian distribution; Perlin noise; Monte Carlo accept-reject |
| 1 | Vectors | Motion 101; vector operations; normalise + scale |
| 2 | Forces | F=ma; force accumulation; friction; drag; gravitational attraction |
| 3 | Oscillation | Angular motion; Hooke’s Law spring; pendulum; sine wave |
| 4 | Particle Systems | Emitter/pool pattern; inheritance; polymorphic collections |
| 5 | Autonomous Agents | Reynolds steering: seek, arrive, wander, flow field, path follow, flocking (separation/alignment/cohesion) |
| 6 | Physics Libraries | Matter.js; Toxiclibs.js; Verlet integration; compound bodies; constraints |
| 7 | Cellular Automata | Elementary 1D CA (Wolfram rules); Conway’s Game of Life; Wolfram classification |
| 8 | Fractals | Cantor set; Koch curve; fractal trees; L-systems |
| 9 | Evolutionary Computing | Genetic algorithm (fitness, selection, crossover, mutation) |
| 10 | Neural Networks | Perceptron; supervised learning; multilayer nets; backpropagation; ml5.js |
| 11 | Neuroevolution | GA + neural networks; sensor-based perception; continuous ecosystem model |
Relevance
This source grounds the following wiki pages:
- steering-behaviours — Reynolds Ch. 5
- genetic-algorithms — Ch. 9 and Ch. 11
- cellular-automata — Ch. 7
- procedural-generation — Ch. 0, 8 (Perlin noise, L-systems, fractals)
- particle-systems-design — Ch. 4
- overview-unity-nature-of-code-examples — Unity/C# teaching route using simple 2D sprites, dots, and grid cells
Connects to existing pages:
- unity-rigidbody2d and unity-collider2d-and-triggers — conceptual foundation for the force/physics model Unity abstracts
- game-analytics — reinforcement learning mention in Ch. 10–11
- reward-systems and bartle-taxonomy — agent behaviour design context
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:
- random walkers: RandomWalker2D.cs
- steering: Vehicle2D.cs
- particles: ParticleEmitter2D.cs
- cellular automata: GameOfLifeGrid.cs
- genetic algorithms: TextPopulationManager.cs
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?
Links
steering-behaviours | genetic-algorithms | cellular-automata | procedural-generation | particle-systems-design | overview-unity-nature-of-code-examples