5
\$\begingroup\$

I've been working all day at learning perlin and other methods for generating terrain. I have a 2d top-down game and I need to generate random regions of connected colors. I've been playing with existing implementations - some worked very well, some didn't. My main problem was that several implementations were very inconsistent and were not returning the same noise number for a coordinate - something that's important to me.

I finally landed on an implementation of Simplex I like, and with all of my tests it's generating the same noise value for every coordinate properly.

However, the noise values are doubles with unknown ranges, for example:

x:1525 y:1125 Noise: 0.0
x:1525 y:1150 Noise: 0.2764424964813472
x:1525 y:1175 Noise: 0.02497111685822111
x:1525 y:1200 Noise: 0.22943575651784293
x:1525 y:1225 Noise: -0.498659492513381
x:1525 y:1250 Noise: -0.9257368870408553
x:1525 y:1275 Noise: 0.2976243577485172

I have a list of a dozen initial tile colors and am stumped as to best way to ensure that I can safely choose a color from the range based on the noise value.

Secondary question if possible - if anyone knows how I can change the above class to compute the noise values by using a seed (to eventually allow people to generate specific maps with known seeds)

\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

The noise values are between -1 and 1. Simply set specific ranges to be certain colors. You can do this a few different ways:

Method One

if ( noise <= -0.5 ) {
    //color 1
} else if ( noise <= 0 ) {
    //color 2
} else if ( noise <= 0.5 ) {
    //color 3
} else {
    //color 4
}

Method Two

noise = ( noise + 1 ) / 2.0; //noise is now between 0 and 1
if ( noise <= 0.25 ) {
    //color 1
} else if ( noise <= 0.5 ) {
    //color 2
} else if ( noise <= 0.75 ) {
    //color 3
} else {
    //color 4
}

Method Three

//assume colors is an array of colors
noise = ( noise + 1 ) / 2.0; //noise is now between 0 and 1
color = colors[ Math.floor(noise * colors.length) ];

These methods are some ways to turn noise into regions by using the elevation of the noise.

Random Seeds via http://www.cs.geneseo.edu/~baldwin/reference/random.html

Random generator2 = new Random( 19580427 );
\$\endgroup\$
1
\$\begingroup\$

Indexed color

to pick a color based on a noise value, you just have to convert a range into a set of discrete values.

allColors = { Color(0,0,0), Color(255,255,255) ... } // etc
min= -1;
max= 1;
index = (int)(allColors.length() * (noisevalue - min) / (max - min));
color = allColors[index];

Random Seed

The randomness comes from the permutation table defined in private static final int[] p = { 151, 160, ... };. Simply use a random number generator initialized to a particular seed to shuffle all the integers from 0-255 instead of the fixed value you have there.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .