Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Java Perlin Noise 1D terrain generation problem

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Java Perlin Noise 1D terrain generation problem

    I'm using 1D Perlin noise to generate a tile-based world. But the hills that generate are too small. I tried changing the values of the parameters of the PerlinNoise1D method (see below), but that not only changed the maximum height, it also changed the gradient, which I was trying to avoid.

    (Sorry for the attached images "Insert Image" didn't work)

    This is how it generates now:

    <Attached as TerrainNow.jpg>

    This is how I want it (of course it's not possible to get it that smooth with tiles this is just a sketch):

    <Attached as HowIWantIt.jpg>

    This is how it looks like when I try to do it myself:

    <Attached as Terrain.jpg>

    And here's the code I'm using.

    Generation Code:

    Tile[][] tiles = new Tile[][];
     
    int offset = 2048;
     
    for(int i = 0;i<tiles[0].length;i++)
    {
        int y = (int)ClassNoise.PerlinNoise1D(i, 0.5f, 8)*4;
        for(int j = offset+y;j<tiles.length;j++)
        {
            tiles[i,j] = new Tile(i,j);
        }
    }

    ClassNoise class:

    public class ClassNoise
    {
    	public static List<Integer[]> primes = new ArrayList<Integer[]>();
     
    	static
    	{
     
    		int prime1 = 0;
    		int prime2 = 0;
    		int prime3 = 0;
    		for (int i = 1; i < 10000000; i+=2)
    		{
     
    			if (isPrime(i))
    			{
    				//primes.add(i);
    				prime1 = i;
    				for (; i < 10000000; i+=2)
    				{
    					if (isPrime(i))
    					{
    						//primes.add(i);
    						prime2 = i;
    						for (; i < 10000000; i+=2)
    						{
    							if (isPrime(i))
    							{
    								prime3 = i;
     
    								primes.add(new Integer[]{prime1,prime2,prime3});
    							}
    						}
    					}
    				}
    			}
    		}
    	}
     
    	public static float Noise(int x, int octave)
    	{
     
    		x = (x << 13) ^ x;
    		return (float) (1.0 - ((x
    				* (x * x * primes.get(octave)[0] + primes.get(octave)[1]) + primes.get(octave)[2]) & Integer.MAX_VALUE) / 1073741824f);
    	}
     
    	public static float PerlinNoise1D(float x, float persistence, int octaves)
    	{
    		float total = 0;
    		float p = persistence;
    		int n = octaves - 1;
     
    		float[] octavePostMutliply = { 1, 1, 1, 1, 1, 1, 1, 1 };
     
    		for (int i = 0; i <= n; i++)
    		{
     
    			float frequency = (float) Math.pow(2, i);
    			double amplitude = Math.pow(p, octaves - i);
    			total += InterpolatedNoise(x * frequency, i) * amplitude
    					* octavePostMutliply[i];
    		}
     
    		return total;
     
    	}
     
    	private static float InterpolatedNoise(float x, int octave)
    	{
    		int integer_X = (int) x;
    		float fractional_X = x - integer_X;
     
    		float v1 = SmoothNoise1D(integer_X, octave);
    		float v2 = SmoothNoise1D(integer_X + 1, octave);
     
    		return CosineInterpolate(v1, v2, fractional_X);
     
    	}
     
    	public static float CosineInterpolate(float a, float b, float x)
    	{
    		float ft = (float) (x * Math.PI);
    		float f = (float) ((1 - Math.cos(ft)) * 0.5);
     
    		return a * (1 - f) + b * f;
    	}
     
    	public static float SmoothNoise1D(int x, int octave)
    	{
    		return Noise(x, octave) / 2 + Noise(x - 1, octave) / 4 + Noise(x + 1, octave) / 4;
    	}
     
    	private static boolean isPrime(int n)
    	{
    		if (n % 2 == 0)
    			return false;
    		for (int i = 3; i * i <= n; i += 2)
    		{
    			if (n % i == 0)
    				return false;
    		}
    		return true;
    	}
     
    }
    Attached Images Attached Images


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Perlin Noise 1D terrain generation problem

    If you change the difference in height, you're going to change the gradient. You have to adjust your x-scale as well to smooth the gradient out a bit.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java Perlin Noise 1D terrain generation problem

    Okay what should I change to do this?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Perlin Noise 1D terrain generation problem

    You have to think about your problem a little more. You haven't posted an SSCCE including the logic that takes these numbers and uses them as drawing coordinates, so it's rather hard to be more specific. The general idea is that if you want to smooth out the gradient but keep the peak heights, you have to stretch out the x axis. How you do that is up to you. Drawing out a few examples on paper might help you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java Perlin Noise 1D terrain generation problem

    I can't simply scale it because the tiles are stored in an array. I want to change the generation code to generate better hills and drawing shouldn't change.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Perlin Noise 1D terrain generation problem

    When I say scale, I mean you need to scale the data, not the image. The idea is the same though. You're going to have to write some code that takes the noise as input and outputs an array with whatever smoothing you want.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java Perlin Noise 1D terrain generation problem

    When scaling it it would double the x-size of the "hills" but the gradient would still be too rough.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Perlin Noise 1D terrain generation problem

    That's why you also have to fill in the blanks. Say your noise creates this:


    **
    **
    ** **
    **********


    You'd have to fill in the blanks to create something more like this:



    **
    ****
    ** ******
    **********


    --- Update ---

    Ugh, the edit button is broken and that didn't work out how I wanted, but what I mean is you can't just stretch your x axis. You also have to fill in the blanks to create a smoother surface.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java Perlin Noise 1D terrain generation problem

    So it's absoloutely impossible to change the maximum height without changing the gradient?

    --- Update ---

    Okay what changes should I make to my code that is works like I want? How do I check if the blank is a blank that should be filled?

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Perlin Noise 1D terrain generation problem

    There isn't any particular single exact correct way to accomplish your goal, which is why it's difficult to help you with the questions you're asking. We can't give you code like you're asking for. You're going to have to play around and try stuff on your own. I've given you the basics of the approach I would take, but it's up to you to come up with the algorithm based on your exact needs. Try something out and post an updated SSCCE when you get stuck again, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Help With Diamond Square Terrain
    By hobbles in forum Algorithms & Recursion
    Replies: 5
    Last Post: May 6th, 2013, 03:10 PM
  2. Jaxb-- java file generation for DTD
    By tcstcs in forum Java Theory & Questions
    Replies: 0
    Last Post: November 22nd, 2011, 02:21 AM
  3. Problem in generation of custom classes at web service client
    By vinod.pandey in forum Member Introductions
    Replies: 0
    Last Post: January 10th, 2011, 07:12 AM
  4. Perlin Noise (and other Java 2d graphics questions)
    By helloworld922 in forum AWT / Java Swing
    Replies: 3
    Last Post: June 11th, 2010, 07:42 PM
  5. 3D Terrain & Sphere
    By quddusaliquddus in forum Java SE APIs
    Replies: 3
    Last Post: March 20th, 2010, 12:59 PM