Concept
Code Highlight
One part of the code I’m particularly proud of is the updateGrid
function, which handles how each cell evolves based on its current state and neighbors. Here’s the snippet:
function updateGrid() { let newGrid = grid.map((col) => col.slice()); // Copy the current grid for (let x = 0; x < cols; x++) { for (let y = 0; y < rows; y++) { let state = grid[x][y]; let neighbors = getNeighbors(x, y); // Get neighboring cells if (state === 3) { // Fire burns out into barren land newGrid[x][y] = 0; } else if (state === 2) { // Tree catches fire if near fire if (neighbors.includes(3)) { if (random(1) < 0.6 + 0.2 * windDirection) newGrid[x][y] = 3; } } else if (state === 1) { // Grass grows into a tree if surrounded by trees if (neighbors.filter((n) => n === 2).length > 2) { newGrid[x][y] = 2; } } } } grid = newGrid; // Update the grid }
This function applies the rules of the automaton in a straightforward way:
- Fire turns into barren land after burning.
- Trees can catch fire if they’re near a burning cell.
- Grass grows into trees when surrounded by enough trees.
Embedded Sketch
Reflection and Future Ideas
I’m happy with how the sketch turned out, as it creates an engaging way to explore the balance between growth and destruction in a forest. Watching the fire spread and the forest recover with water and trees is satisfying and makes the simulation feel alive. However, there is room for improvement. The interface could be designed to look more visually appealing to attract users and make the experience more immersive. Adding better visuals, like smoother transitions between states or animated effects for fire and water, could enhance the overall presentation. Another idea is to include sound effects for the different features, such as a crackling sound for fire or a soft splash when using water. These small additions could make the simulation more engaging and enjoyable for users, turning it into a more polished and interactive experience.
References
https://editor.p5js.org/yadlra/sketches/B3TTmi_3F
https://ahmadhamze.github.io/posts/cellular-automata/cellular-automata/