Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Using Perlin Noise to generate an infinite world  (Read 3694 times)
0 Members and 1 Guest are viewing this topic.
Offline ahottev

Senior Newbie





« Posted 2010-12-13 16:09:55 »

So i have this function :

1  
2  
public PerlinNoise(int width, int height, int freq, float persistency,
            int density, float sharpness, int detail, float amplitude, long seed)


It generates me perfectly good Perlin Noise. I'm using it to create a 2d height map.

I just can't seem to figure out how to get an infinite world out of it. Say i generate a 1024x1024 map with it. That's cool, i have a 1024x1024 grid. Now, how do i get past that? If every pixel is a 3d block in-game, how can i calculate past the initial first 1024 ?

Offline SimonH
« Reply #1 - Posted 2010-12-13 16:26:03 »

Adapt the perlin noise function to take x & z values instead of a seed value, then you can get height data for any x,z location. If you use doubles for x & z you'll get a very, very big (near-infinite) landscape.

Heroes' Keep is in development.
Meanwhile try Bloodridge
Offline ahottev

Senior Newbie





« Reply #2 - Posted 2010-12-13 16:47:31 »

Can't seem to get it to work perfectly.

I have this: (not the complete Perlin code here)

1  
2  
public PerlinNoise(int width, int height, int startFreqX, int endFreqX, int startFreqY, int endFreqY, float persistency,
            int density, float cloudSharpness, int detail, float amplitude, long seed)


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
public void run()
      {
                        Random r = new Random();
                        r.setSeed(seed);
         BufferedImage temp = new BufferedImage(endFreqX, endFreqY ,
               BufferedImage.TYPE_4BYTE_ABGR);
         Graphics2D g = temp.createGraphics();
         // generate a low-res random image
        for (int i = 0; i < endFreqX ; i++)
         {
            for (int j = 0; j < endFreqY; j++)
            {
                                        int val = r.nextInt(255);
                               
               g.setColor(new Color(val, val, val, (int) (alpha * 0xFF)));
               g.fillRect(i, j, 1, 1);
            }
         }
         g.dispose();
         // re-scale the image up using interpolation (in this case, linear)
        image = new BufferedImage(width, height,
               BufferedImage.TYPE_4BYTE_ABGR);
         g = image.createGraphics();
         g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
               RenderingHints.VALUE_INTERPOLATION_BILINEAR);

         g.drawImage(temp, 0, 0, width, height, startFreqX, startFreqY, endFreqX , endFreqY , null);
         g.dispose();
      }
   }


With these parameters
(256, 128, 0, 24, 0, 12, 8.0f, 120, 0.10f, 5, 0.99f, 1l) i get a 256x128 b&w map.

Now, i want to work in 128x128 blocks. To get the first block, i enter:
(128,128, 0, 12, 0, 12, 8.0f, 120, 0.10f, 5, 0.99f, 1l)

And to get the second block to the right, i enter:
(128,128, 12, 24, 0, 12, 8.0f, 120, 0.10f, 5, 0.99f, 1l)

I almost works, you can clearly see the resemblance, but there's always a few pixels different near the edge. It's not acceptable for map generation.

What is wrong ?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen

JGO Ninja


Medals: 66



« Reply #3 - Posted 2010-12-13 19:13:21 »

I'll repeat what SimonH said.  You have to feed coordinates, otherwise "edges" of your precomputed regions will not match.  Since you're combining multiple "frequencies" (or zoom/scale levels) of noise, the coordinates must match at all levels.
Offline ahottev

Senior Newbie





« Reply #4 - Posted 2010-12-14 14:15:55 »

I'll repeat what SimonH said.  You have to feed coordinates, otherwise "edges" of your precomputed regions will not match.  Since you're combining multiple "frequencies" (or zoom/scale levels) of noise, the coordinates must match at all levels.

Ok, I see what was wrong. I was not using a real Perlin noise algorithm, but a function that creates noise by re-scaling a low-res noise bitmap... It's working fine now.
Offline namrog84

JGO Knight


Medals: 23
Projects: 2


Keep programming!


« Reply #5 - Posted 2011-03-31 17:58:54 »

Could you post your Perlin noise function please?

"Experience is what you get when you did not get what you wanted"
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
hobbles (7 views)
2013-05-22 00:54:55

cubemaster21 (74 views)
2013-05-17 21:29:12

alaslipknot (86 views)
2013-05-16 21:24:48

gouessej (115 views)
2013-05-16 00:53:38

gouessej (110 views)
2013-05-16 00:17:58

theagentd (121 views)
2013-05-15 15:01:13

theagentd (110 views)
2013-05-15 15:00:54

StreetDoggy (155 views)
2013-05-14 15:56:26

kutucuk (177 views)
2013-05-12 17:10:36

kutucuk (175 views)
2013-05-12 15:36:09
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.152 seconds with 21 queries.