Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Question random tile map generation

    Hi, I am making a 2d game with a tiled map, and I want to make a random map generator, but I can manage to do it. I have tried google, but have trouble finding what I want. could someone please me give a pointer to somewhere I can learn about this? :) thanks in advance

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default 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;
    	}
    }

  3. #3
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default 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 :)

  4. #4
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default 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);

  5. #5
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default 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! :)

  6. #6
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: random tile map generation

    No problem, glad I could be of some help :)

Similar Threads

  1. Random number generation
    By vastrolorde in forum New To Java
    Replies: 5
    Last Post: 07-05-2013, 03:38 PM
  2. Random shape generation
    By zeeextra in forum New To Java
    Replies: 0
    Last Post: 12-04-2009, 06:57 AM
  3. Trouble with Random number generation
    By SteroidalPsycho in forum New To Java
    Replies: 9
    Last Post: 11-17-2009, 12:15 AM
  4. Random number generation
    By toasty in forum New To Java
    Replies: 1
    Last Post: 09-30-2009, 12:41 PM
  5. random generation
    By carlos123 in forum New To Java
    Replies: 10
    Last Post: 01-09-2008, 04:43 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •