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  
  Noise in 2D  (Read 4094 times)
0 Members and 1 Guest are viewing this topic.
Online matheus23

JGO Wizard


Medals: 72
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.
Online matheus23

JGO Wizard


Medals: 72
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!
Try the Free Demo of Droid Assault
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Online matheus23

JGO Wizard


Medals: 72
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
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
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?
Online Roquen

JGO Ninja


Medals: 66



« Reply #8 - Posted 2012-05-04 19:42:24 »

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

Junior Member


Projects: 1


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!
Play the free demo of Revenge of the Titans!
Online matheus23

JGO Wizard


Medals: 72
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
Online Roquen

JGO Ninja


Medals: 66



« Reply #11 - Posted 2012-05-06 11:50:09 »

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

JGO Wizard


Medals: 72
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

Online matheus23

JGO Wizard


Medals: 72
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.

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!
cubemaster21 (76 views)
2013-05-17 21:29:12

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

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

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

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

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

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

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

kutucuk (178 views)
2013-05-12 15:36:09

UnluckyDevil (186 views)
2013-05-12 05:09:57
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.133 seconds with 21 queries.