Task 2 of 6

Which Way Is Down

Rain has landed. Where does it go? Downhill — but "downhill" on a grid needs spelling out, and two details decide whether this simulation ever grows a river.

One: the surface is rock plus water. A puddle sitting in a hollow raises the level the next drop has to climb; keep filling it and the puddle spills over the rim by itself. Route on height alone and hollows become bottomless traps. So every comparison below is between height + water values — call that H.

Two: square the drops. This pass adds up over the neighbours that are below this cell, and the next task will hand each of them d²/total of the water. Weighting by the plain drop smears the flow across every neighbour that is even slightly lower and no channel ever forms; squaring makes a cell commit to the steep side. That one exponent is the difference between a damp hillside and a river.

Goal: complete dropTotal so each cell returns the sum of over its four neighbours, counting only the ones the water can actually fall to.

Requirements

Hint 1 — one neighbour at a time

The starter writes the left neighbour out in full. The other three are the same two lines with different indexes:

d = here - (height[y][xr] + water[y][xr]);
if (d > 0) total += d * d;
Hint 2 — why the if matters

d * d is positive whether the neighbour is above or below, so the square destroys the very information you are selecting on. The if (d > 0) has to come first, or an uphill neighbour ends up "receiving" water.

Same idea elsewhere

Deciding a flow direction per cell and then normalising by a per-cell total is the multiple-flow-direction (MFD) router every terrain and hydrology package ships — GRASS, TauDEM, Houdini's erosion nodes. The exponent this task fixes at 2 is a tuning knob in all of them; 1.1 gives braided sheets, ∞ gives single-thread rivers.

All tasks in Hydraulic Erosion: Carving Terrain by Accumulation

  1. Seeing the Ground
  2. Which Way Is Down
  3. Everybody Downhill At Once
  4. Capacity: What the Water Can Carry
  5. Ping-Pong the Whole Landscape
  6. Two Hundred Steps, and a River Appears

This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.