Wednesday, March 5, 2014

Continent Masks

In many cases it is desireable to be able to generate the shape of a continent or island for a game. Many methods for this exist, but few of them produce results that look similar to the quasi-randomness of real life coastlines. In this post I will introduce my favorite algorithm from DungeonLeague.com that produces very nice results.




The steps are relatively simple:
1. Generate a normalized advanced Voronoi diagram from a list of random lines originating from the edges of the array along with a small number in the center.
2. Generate a normalized fractal noise function
3. Multiply the two arrays together and fade the edges to zero.
4. Values > .2 are land, < .2 is ocean.



In more detail:
For the line generation here is psuedo-code:
for(each side)
{
    start = getRandomEdgePoint(currentEdge);
    for(rand(minBranches, maxBranches))
    {
        end.x = start.x + rand(-maxPartOffset, maxPartOffset);
        end.y = start.y + rand(-maxPartOffset, maxPartOffset);
        end.clip(noise.bounds());
        add(new Line(start, end));
        start = end;
    }
}

for(rand(0, maxInternalLines))
{
    start = getRandCenterPoint();
    end.x = start.x + rand(-maxPartOffset, maxPartOffset);
    end.y = start.y + rand(-maxPartOffset, maxPartOffset);
    end.clip(noise.bounds());
    add(new Line(start, end));
}

For the fractal noise function, I like to use fractal Perlin noise, because it is faster than fractal interpolated, and midpoint displacement seems to produce insufficient quality.
For the fading of the edges, I simply get the smallest distance to an edge, clip(0, gradientWidth), linearly interpolate between 0 and 1, and multiply by the noise value.

Here is a applet for you:
Controls:
L = toggle show lines
Q and E = change lines per edge
A and D = change max internal lines
Z and C = change average branches per line
R and T = change branch offset
F and G = change edge gradient width
Enter (downloadable version only) = save screenshot


If the applet doesn't load, trying refreshing the webpage. May not work on mobiles.

Demos: https://drive.google.com/folderview?id=0B_jXYEquMamINGVGQTM0cTJzdmM&usp=sharing
Source Code: https://github.com/f4113nb34st/Println/

No comments:

Post a Comment