# Generating Continents Procedurally with Distortion Fields

Computer generated terrain usually begins with a base of random noise, or perhaps a fractal. These look nice in small sections, but are unconvincing for a continent: noise has none of the major features it should, like mountain ranges, and fractals are nothing but feature — they’re too regular. Here’s a method for generating continent-level terrain using distortion fields.

### Making the Distortion Field

By a distortion field, I basically mean a vector field of uniform speeds. We can use such a field to warp a base layer of terrain to create both (1) the outline of the continent, and (2) its major features, like mountain ranges. Small and mid-level features can then be added later.

We can begin by creating a random field, with arrows pointing every which-way (image above). This would produce no meaningful features, so we need to smooth it out first. Terrain smoothing can generally be done by averaging each cells with its orthogonal neighbors. (We have to be careful here not to use a simple mean, which would bias results toward 180°. Instead we must decompose the angles into x and y component, sum those, and calculate a new angle. This is the process used for calculating average wind speed, so that everything doesn’t simply cancel out to zero.) Here is the same field after some smoothing:

The above image illustrates the method, but real maps are usually larger and the arrow notation is cumbersome. Below is a color-coded distortion field (red being up) with hundreds of smoothing iterations (the amount should scale with the size of the map).

You can see that some large-scale feature emerge, even while some smaller-scale anomalies remain. It can be nice to add a second, less smoothed, field on top to create that roughening detail.

### The Underlying Layer

Our distortion field must act on something, so we begin again with some random noise — with values representing altitudes.

Again, we smooth this out (but in a more straightforward manner), and we also exaggerate the peaks so that a few mountains stand out against a more uniform background.

### Applying the Distortion Field

How do we distort one layer with another? I wrote a function that basically calculates weighted averages over a grid (a 2D array), with weights for each neighbor. The distortion field is then used as the weighting mechanism, so that each cell is averaged in one direction — but on a grid this becomes some combination of 1 to 2 neighbors’ values. Here is the terrain, plus some padding, with 5 iterations of the distortion:

As we increase the number of iterations, the original (random) features are smeared out over larger distances, including outside the original bounds (hence the padding). Because the distortion works in one direction at a time, long features like mountain ranges can emerge from the initial high points.

With some mid-level terrain added, and some erosion applied, we have a fairly reasonable sort of computer-generated continent:

### Variations

The distortion field itself determines the outline of the continent and the internal similarities, but exactly where mountains exist is highly dependent on the starting terrain layer. We can swap out that layer while keeping the distortion field constant to play around with the big features.

Many other elaborations are possible, with different layers of distortion applied to various degrees. (Swapping out different fields on the same map can, of course, also be done, but because the interaction between them is what counts for mountain position, there is little point.)

This approach is not difficult to implement and can create landforms that are not (readily) possible with simple noise or fractals — but of course it can be combined with those methods, and indeed relies on noise at its base. Distortion fields can thus be one more powerful tool in the world-builder’s kit.

Originally published at ideabyre.com.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.