Wave Function Collapse (WFC) is an algorithm for generating structured randomness, useful for procedural world maps, city layouts, dungeon mazes, and more. The post walks through building a road-generation simulation in JavaScript, explaining core WFC concepts: cells with multiple possible states (superposition), entropy-based cell selection (choosing the cell with fewest possible states), constraint propagation (neighbors inform each other of their collapsed state), and weighted randomness to reduce isolated road islands. Code snippets illustrate the Grid and Cell classes, the main loop, and a weighted observe() method.
Table of contents
What is Wave Function Collapse?The Setup (feat. JavaScript)The Algorithm (feat. Quantum Mechanics)Innovating FurtherWFC City, Here We ComeSort: