Fractal terrain generation in 130 lines of Javascript





That's certainly more elegant than this one:

195 lines of Amiga BASIC.

It's kinda fun to duplicate those BASIC demos in JavaScript and Canvas, and every bit as interesting (imho) to see the differences in coding styles.


How about mountains including sun, clouds, fog and snow in 500 lines of text:-

Needs a WebGL enabled browser.



The terrain is somewhat unbelievable, though. After the generation, I'd suggest running some simulated erosion on it. A few iterations of simulated water flow and material removal. Assumption of homogenous soil will do the job; asking for simulated harder rock intrusions would be too much.


Yeah, I think that's the largest problem with any of the various random terrain generators, they're missing the next step (likely because it's a very complicated step and likely extremely hard to code for FOSS) of errosion simulation. This would also allow you to know where rivers and lakes would be, and thus make it a lot easier to use these sorts of terrain for more in-depth creative works like fiction and gaming.


Maybe I'm being naive here, but couldn't you model erosion fairly easily using some kind of sandpile model? I don't know about javascript, but you could easily do that in under 20 lines of python.


A ways back I was messing with the original simcity source and as part of figuring out how it all worked, re-made the map generation code in javascript:

It's delightfully simple once you figure out what it's doing.. each feature (lakes, rivers, forests) have a 2D array defined that functions like a stamp. For rivers, the generator picks a random x,y coordinate, stamps into the terrain, then moves the x,y in a random direction and stamps again. It repeats until it hits the edge of the map.

Lakes and forests run on a similar concept, but the loop is modified so it behaves differently.


Reticulating splines.


that water simulation is pretty amazing, thanks for sharing!

I was especially impressed once i realized i could lift the sphere out of the water and smash it back down, or have it partially submerged.

the only real flaw i've noticed is that water can't overcome the surface tension and separate, like when you smash the sphere downwards you'd expect some water to become airborn. likely that bit adds a lot of complication to the simulation.


After looking at a few of these terrains, I was thinking that I'd want to pick a good one and 3D print it for table top gaming. smile


POV-Ray can produce much the same thing with a script of about 130 characters.


This topic was automatically closed after 5 days. New replies are no longer allowed.