Results 1 to 6 of 6
Thread: random tile map generation
 09202013, 08:26 PM #1Member
 Join Date
 Jun 2012
 Posts
 52
 Rep Power
 0
 09212013, 07:37 PM #2Senior Member
 Join Date
 Jan 2009
 Location
 CA, USA
 Posts
 271
 Rep Power
 8
Re: random tile map generation
Look into Perlin Noise. It's commonly used for generating random maps that are consistent over large spaces.
I just did some digging around in some old code I wrote, so I'll share this example with you:
(understanding the math behind it isn't as necessary as understanding how to use it)
Java Code:package org.nubcraft.math.random; import java.util.Random; public class PerlinNoise { private final long seed; private final Random rand; private final int octave; public PerlinNoise(final long seed, final int octave) { this.seed = seed; this.octave = octave; rand = new Random(); } public double getNoiseLevelAt(final int x, final int z) { final int xmin = (int)((double)x / octave); final int xmax = xmin + 1; final int zmin = (int)((double)z / octave); final int zmax = zmin + 1; final double ra = getRandomAt(xmin, zmin); final double rb = getRandomAt(xmax, zmin); final double rc = getRandomAt(xmax, zmax); final double rd = getRandomAt(xmin, zmax); final double ret = cosineInterpolate( cosineInterpolate((float)ra, (float)rb, (float)(x  xmin * octave) / octave), cosineInterpolate((float)rd, (float)rc, (float)(x  xmin * octave) / octave), ((float)z  (float)zmin * (float)octave) / (float)octave); return ret; } private final double getRandomAt(final int x, final int z) { final double s = 10000 * (Math.sin(x) + Math.cos(z) + Math.tan(seed)); rand.setSeed((long)s); final double ret = rand.nextDouble(); return ret; } private final float cosineInterpolate(final float a, final float b, final float x) { final float ft = (float)(x * Math.PI); final float f = (float)((1f  Math.cos(ft)) * .5f); final float ret = a * (1f  f) + b * f; return ret; } }
 09222013, 12:01 PM #3Member
 Join Date
 Jun 2012
 Posts
 52
 Rep Power
 0
Re: random tile map generation
I get decent results, but I read somewhere that perlin noise uses 7 different octaves that creates in total 7 different noises and combines them to create one final noise. this example only uses octave, do you know how to use all of them? and thx for the help :)
 09222013, 03:39 PM #4Senior Member
 Join Date
 Jan 2009
 Location
 CA, USA
 Posts
 271
 Rep Power
 8
Re: random tile map generation
I may be wrong, but I believe you can simply add multiple noises together. I remember what I use to do is have a general shape for the landscape (larger octave) and then add detail by adding noise from a smaller octave. so it might be something like this:
noise1 = new PerlinNoise(seed, 64);
noise2 = new PerlinNoise(seed, 8);
int height = 10 + 64*noise1.get(x, y) + 8*noise2.get(x, y);
 09222013, 04:09 PM #5Member
 Join Date
 Jun 2012
 Posts
 52
 Rep Power
 0
Re: random tile map generation
I think that made it a bit better, I'll just have to tweak it a little bit. Thanks for the help! :)
 09222013, 05:06 PM #6Senior Member
 Join Date
 Jan 2009
 Location
 CA, USA
 Posts
 271
 Rep Power
 8
Similar Threads

Random number generation
By vastrolorde in forum New To JavaReplies: 5Last Post: 07052013, 02:38 PM 
Random shape generation
By zeeextra in forum New To JavaReplies: 0Last Post: 12042009, 06:57 AM 
Trouble with Random number generation
By SteroidalPsycho in forum New To JavaReplies: 9Last Post: 11172009, 12:15 AM 
Random number generation
By toasty in forum New To JavaReplies: 1Last Post: 09302009, 11:41 AM 
random generation
By carlos123 in forum New To JavaReplies: 10Last Post: 01092008, 04:43 AM
Bookmarks