Question

I'm trying to make a map generator for a 2d tilemap game (similar to games like Civilization - I'd like the ability to create small islands, or large continents, etc). I found an article that does pretty much what I'd like, but it's written in flash. Looks like there's a convenient function to generate a bitmap from perlin noise:

class BitmapData
function perlinNoise(
    baseX:Number, 
    baseY:Number, 
    numOctaves:uint, 
    randomSeed:int, 
    stitch:Boolean, 
    fractalNoise:Boolean, 
    channelOptions:uint = 7, 
    grayScale:Boolean = false, 
    offsets:Array = null):

This is being used in the article (but I'm using java). So now I'm stuck at trying to implement perlin noise myself (basically finding a pure java implementation and copy/pasting it).

I'm just trying to understand the basic principle behind using perlin noise - my basic understanding:

  1. Define the size of map you want (in my case about 100 x 50 tiles).
  2. Initialize a perlin noise object (setting params like frequency etc - not quite sure how these params affect things like island size etc, but putting aside for the moment).
  3. Iterate over each tile in my map, and ask the perlin noise object for a value. The returned value can be interpreted as a height value I'm guessing.
  4. Now assign a tile type based on the height value.

The iteration code might look something like:

for (int x = 0; x < 100; x++) {
    for (int y = 0; y < 50; y++) {
        int height = perlin.getValue(x, y);
        if (height < 5); // this tile is water
        else if (height < 20); // this tile is shore
        else if (height < 40); // this tile is hill
        else // this tile is mountain
    }
}

Is that generally correct? That would at least be a starting point.

The article (written in flash) that does what I'm looking for: http://www.nolithius.com/game-development/world-generation-breakdown

Flash BitmapData class which conveniently has a perlin noise function built in: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#perlinNoise()

Thanks

Was it helpful?

Solution

Given appropriate initialization, your iteration code should work as far as I can see. That is: if you want to threshold 2D Perlin noise to water, shore, hill and mountain.

This page has some useful explanation of Perlin noise and pseudocode examples. Essentially, the algorithm adds together noise functions with increasing frequency and decreasing amplitude.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top