Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
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  
  Noise in 2D  (Read 5719 times)
0 Members and 1 Guest are viewing this topic.
Offline matheus23

JGO Wizard


Medals: 97
Projects: 3


You think about my Avatar right now!


« Posted 2012-05-04 17:33:26 »

For anyone wondering, how to set something up, here is some code for you Smiley (Recursive algorithm)
I haven't added comments, nor I will add any explanation. If you don't know, what Perlin's Noise is, that code is not for you Tongue Google it Cheesy
Also, I'ts posted in Shared Code, so I think, thats okey Smiley

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  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
package /*TOP-SECRET*/

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

public class PerlinNoise2D {
   
   private float[][] values;
   private float roughness;
   private float varyation;
   private Random rand;

   public PerlinNoise2D(int size, float roughness, float varyation, float seed, Random rand) {
      this.roughness = roughness;
      this.varyation = varyation;
      this.rand = rand;
     
      int realsize = 1;
      while(realsize < size) {
         realsize *= 2;
      }
      realsize += 1;
      values = new float[realsize][realsize];
     
      int firststep = realsize/2;
      values[0][0] = seed;
      values[realsize-1][0] = seed;
      values[0][realsize-1] = seed;
      values[realsize-1][realsize-1] = seed;
      values[0][firststep] = seed;
      values[firststep][0] = seed;
      values[firststep][firststep] = seed;
      values[firststep][realsize-1] = seed;
      values[realsize-1][firststep] = seed;
     
      noise(0, 0, realsize-1);
   }
   
   private void noise(int x, int y, int step) {
      if (step > 1) {
         int halfStep = step/2;
         float vary = (step*varyation)*roughness;
         
         values[x+halfStep][y] = values[x+halfStep][y] == 0f ?
               vary(mid(values[x][y], values[x+step][y]), vary) : values[x+halfStep][y];
               
         values[x][y+halfStep] = values[x][y+halfStep] == 0f ?
               vary(mid(values[x][y], values[x][y+step]), vary) : values[x][y+halfStep];
         
         values[x+step][y+halfStep] = values[x+step][y+halfStep] == 0f ?
               vary(mid(values[x+step][y], values[x+step][y+step]), vary) : values[x+step][y+halfStep];
               
         values[x+halfStep][y+step] = values[x+halfStep][y+step] == 0f ?
               vary(mid(values[x][y+step], values[x+step][y+step]), vary) : values[x+halfStep][y+step];
         
         values[x+halfStep][y+halfStep] = vary(mid(
               values[x][y],
               values[x+halfStep][y],
               values[x+step][y],
               values[x][y+halfStep],
               values[x+step][y+halfStep],
               values[x][y+step],
               values[x+halfStep][y+step],
               values[x+step][y+step]), vary);
         
         noise(x, y, halfStep);
         noise(x+halfStep, y, halfStep);
         noise(x, y+halfStep, halfStep);
         noise(x+halfStep, y+halfStep, halfStep);
      }
   }
   
   public float randRange(float range, Random rand) {
      return rand.nextBoolean() ? -rand.nextFloat()*range : rand.nextFloat()*range;
   }
   
   public float vary(float val, float vary) {
      return val+randRange(vary, rand);
   }
   
   public float mid(float... vals) {
      float sum = 0;
      for (int i = 0; i < vals.length; i++) {
         sum += vals[i];
      }
      return sum / vals.length;
   }
   
   public float[][] get() {
      return values;
   }
   
   public static void main(String[] args) {
      System.out.println("Started.");
      int size = 512;
      PerlinNoise2D pn2d = new PerlinNoise2D(size, 0.4f, 1, 20000f, new Random());
      float[][] vals = pn2d.get();
      BufferedImage img = new BufferedImage(size+1, size+1, BufferedImage.TYPE_INT_ARGB);
      for (int x = 0; x < vals.length; x++) {
         for (int y = 0; y < vals[x].length; y++) {
            img.setRGB(x, y, ((int)vals[x][y]) | 0xFF000000);
         }
      }
      try {
         saveImg(new File("Heightmap.png"), img);
      } catch (IOException e) {
         e.printStackTrace();
      }
      System.out.println("Finished.");
   }
   
   public static void saveImg(File f, BufferedImage img) throws IOException {
      f.createNewFile();
      ImageIO.write(img, "PNG", f);
   }
   
}

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline theagentd
« Reply #1 - Posted 2012-05-04 18:15:34 »

Hmmm. Might come in handy one day. Care to post a screenshot of a produced image? You also misspelled variation. =S

Myomyomyo.
Offline matheus23

JGO Wizard


Medals: 97
Projects: 3


You think about my Avatar right now!


« Reply #2 - Posted 2012-05-04 18:21:04 »

You also misspelled variation. =S

Oops... sorry, english is not my native language Cheesy

Care to post a screenshot of a produced image?

Acctually, these produced images are just for showing, that it works, and to visualize them.

The "beautiness" of these Images TOTALLY depends on your settings in the constructor of PerlinNoise2D.
Also, I just say "int color on that pixel is just the value of the perlin noise at the position of that pixel".

But anyways:
http://i.imgur.com/LCTCe.png

The green and blue colors aren't ment to be like that, that was by accident Cheesy
The hard edges are there due to wrapping of that nice int-generic-type... (wrapping to negative values...)

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2012-05-04 18:22:21 »

This is not PerlinNoise. Clueless

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline matheus23

JGO Wizard


Medals: 97
Projects: 3


You think about my Avatar right now!


« Reply #4 - Posted 2012-05-04 18:23:09 »

This is not PerlinNoise. Clueless

persecutioncomplex

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Online Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2012-05-04 18:24:54 »


This is perlin noise:
http://riven8192.blogspot.com/2009/08/perlinnoise.html

Not everything that looks noisy is actually perlin noise.

Mr. Perlin invented a very specific algorithm, and yours is simply something else. Smiley

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline theagentd
« Reply #6 - Posted 2012-05-04 18:39:43 »

Comparisons! Comparisons!

Myomyomyo.
Offline sproingie
« Reply #7 - Posted 2012-05-04 18:42:18 »

Perlin actually created a few different algorithms that all have his name, and there's certainly a lot out there with his name that aren't his algorithms.  Perhaps that's value noise?
Offline Roquen
« Reply #8 - Posted 2012-05-04 19:42:24 »

Yes indeed: it's a value noise variant.
Offline Geemili

Senior Member


Medals: 9
Projects: 1
Exp: 2 years


No Games Finished


« Reply #9 - Posted 2012-05-05 20:26:33 »

This kind of looks like Diamond square. I didn't look too closely, so I might be wrong.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matheus23

JGO Wizard


Medals: 97
Projects: 3


You think about my Avatar right now!


« Reply #10 - Posted 2012-05-06 09:26:00 »

This kind of looks like Diamond square. I didn't look too closely, so I might be wrong.

That's right.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Roquen
« Reply #11 - Posted 2012-05-06 11:50:09 »

That describes how you're combining the base noise.  The base noise is value noise.
Offline matheus23

JGO Wizard


Medals: 97
Projects: 3


You think about my Avatar right now!


« Reply #12 - Posted 2012-05-06 12:06:53 »

That describes how you're combining the base noise.  The base noise is value noise.

.... okey.. I've just heard of "Diamond-Square"-Algorithms. And guys told me, that would be Perlin's Noise... That's all I know Smiley
Awesome Article: http://www.gameprogrammer.com/fractal.html
He/She in the Article talks about the mid-displacement algorithm.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline DzzD
« Reply #13 - Posted 2012-05-07 19:21:04 »

depending on your need, alternativly you may use this one ^^  :

http://www.java-gaming.org/topics/fastest-perlinnoise-improved-version-bicubic-amp-bilinear-grad-amp-value-noise/23771/view.html

Offline matheus23

JGO Wizard


Medals: 97
Projects: 3


You think about my Avatar right now!


« Reply #14 - Posted 2012-05-07 19:27:19 »

Yeah... realized it after that posting Cheesy

But, I don't need it anyways. It's just a fixed version of the algorithm, I created for my Ludum Dare game... Grin

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (50 views)
2014-04-15 18:08:23

BurntPizza (46 views)
2014-04-15 03:46:01

UprightPath (62 views)
2014-04-14 17:39:50

UprightPath (44 views)
2014-04-14 17:35:47

Porlus (61 views)
2014-04-14 15:48:38

tom_mai78101 (84 views)
2014-04-10 04:04:31

BurntPizza (142 views)
2014-04-08 23:06:04

tom_mai78101 (242 views)
2014-04-05 13:34:39

trollwarrior1 (201 views)
2014-04-04 12:06:45

CJLetsGame (208 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!