Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  Generating Perlin Noise  (Read 1489 times)
0 Members and 1 Guest are viewing this topic.
Offline trollwarrior1
« Posted 2013-12-27 18:08:21 »

Someone has been saying that this is not Perlin Noise. If this is not Perlin Noise, tell me how it is not. Here is a link to wiki page. It describes Perlin Noise the same way I implemented it.
http://en.wikipedia.org/wiki/Perlin_noise


This is code I wrote to generate Perlin Noise.

It uses java.Random, and has a restriction of the width, height you specify. Also, it stores data in an array. It has some drawbacks, but should be enough for most uses.

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  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
import java.util.Random;

public class PerlinNoise {

   // Just a Random class object so I can fill my noise map with random directions.
   public static final Random random = new Random();

   // Width and Height of the map.
   public int width, height;

   // Random directions of length 1.
   private vec2[] values;

   // Contrast value.
   public float contrast = 1;
   
   private int canvasWidth = 0, canvasHeight = 0;
   
   /**
    * Creates a noise map with specified dimensions.
    * @param width of the noise map.
    * @param height of the noise map.
    */

   public PerlinNoise(int width, int height) {
      this.width = width;
      this.height = height;

      values = new vec2[(width + 1) * (height + 1)]; // Create an array to store random directions.

      for (int y = 0; y < height + 1; y++) {
         for (int x = 0; x < width + 1; x++) {
            int rot = random.nextInt(359); // Random direction.

            // Store random direction of length 1 to our directions array.
            values[x + y * width] = Rotation.point(new vec2(0, 0), new vec2(0, -1), rot);
         }
      }

      // If you're wondering why "width + 1" "height + 1", it is because map looks blurry
      // at right and bottom edges of the image without it. Try removing it, you will see.
     
   }
   
   public void setCanvas(int w, int h) {
      this.canvasWidth = w;
      this.canvasHeight = h;
   }

   public float noise(float x, float y) {

      if(canvasWidth != 0 && canvasHeight != 0) {
         x /= canvasWidth * width;
         y /= canvasHeight * height;
      }
     
      if(x >= this.width) throw new IllegalArgumentException("X specified should be between 0 and (width-1)");
      if(y >= this.height) throw new IllegalArgumentException("Y specified should be between 0 and (width-1)");
     
     
      // Grid cell coordinates in integer values.
      int gx0 = (int) (Math.floor(x)); // Top-Left
      int gy0 = (int) (Math.floor(y)); // Top-Left
      int gx1 = gx0 + 1; // Down-Right
      int gy1 = gy0 + 1; // Down-Right

      // Random directions.
      vec2 g00 = g(gx0, gy0); // Top-Left
      vec2 g10 = g(gx1, gy0); // Top-Right
      vec2 g11 = g(gx1, gy1); // Down-Right
      vec2 g01 = g(gx0, gy1); // Down-Left

      // Subtract grid cells values from the point specified.
      vec2 delta00 = new vec2(x - gx0, y - gy0); // Top-Left
      vec2 delta10 = new vec2(x - gx1, y - gy0); // Top-Right
      vec2 delta11 = new vec2(x - gx1, y - gy1); // Down-Right
      vec2 delta01 = new vec2(x - gx0, y - gy1); // Down-Left

      // Compute a dot product between random directions and corresponding delta values.
      float s = dot(g00, new vec2(delta00.x, delta00.y)); // Top-Left
      float t = dot(g10, new vec2(delta10.x, delta10.y)); // Top-Right
      float u = dot(g11, new vec2(delta11.x, delta11.y)); // Down-Right
      float v = dot(g01, new vec2(delta01.x, delta01.y)); // Down-Left

      // Compute the weights for x and y axis.
      float sx = weigh(delta00.x);
      float sy = weigh(delta00.y);
     
      // Interpolate between values.
      float a = lerp(sy, s, v); // Interpolate Top-Left(s) and Down-Left(v). We can also call this LEFT
      float b = lerp(sy, t, u); // Interpolate Top-Right(t) and Down-Right(u) We can also call this RIGHT
      float h = lerp(sx, a, b); // Interpolate LEFT(a) and RIGHT(b). We can call this height(h)

      h *= contrast; // Multiply here so adjust contrast.
     
      // Make sure it is -1 to 1. If you don't change contrast, you don't have to do this.
      if(h > 1) h = 1;
      if(h < -1) h = -1;
     
      return h;
   }

   /**
    * Computes a weight using S-curve function f(x) = 3 * (x * x) - 2 * (x * x * x).
    * @param x NOT as in axis, but as a variable.
    */

   private float weigh(float x) {
      return 3 * (x * x) - 2 * (x * x * x);
   }
   
   /**
    * Interpolate between 2 values, using weight.
    */

   private float lerp(float weight, float a, float b) {
      float result = a + weight * (b - a);
      return result;
   }
   
   /**
    * Compute a dot product.
    * Example: dot product between (a, b) and (c, d) is:
    * a * c + b * d
    */

   private float dot(vec2 v0, vec2 v1) {
      return (v0.x * v1.x) + (v0.y * v1.y);
   }

   /**
    * Get the random direction.
    */

   private vec2 g(int x, int y) {
      if (x < 0) x = 0;
      if (y < 0) y = 0;
      if (x >= width) x = width;
      if (y >= height) y = height;
      return values[x + y * width];
   }

   public static class vec2 {

      public float x, y;

      /**
       * Just holds some float values.
       * @param x
       * @param y
       */

      public vec2(float x, float y) {
         this.x = x;
         this.y = y;
      }
     
      public int getX() {
         return (int) x;
      }
      public int getY() {
         return (int) y;
      }
     
   }
   
   public static class Rotation {

      /**
       * Rotates specified point around pivot.
       * @param pivot to rotate around.
       * @param point to rotate around pivot.
       * @param rotation - how many degrees to rotate.
       * @return a new point, which was created by rotating given point around pivot by some degrees.
       */

      public static vec2 point(vec2 pivot, vec2 point, float rotation) {
         
         float rot = (float)(1f / 180 * rotation * Math.PI);
         
         float x = point.x - pivot.x;
         float y = point.y - pivot.y;
         
         float newx = (float)(x * Math.cos(rot) - y * Math.sin(rot));
         float newy = (float)(x * Math.sin(rot) + y * Math.cos(rot));
         
         
         newx += pivot.x;
         newy += pivot.y;
         
         return new vec2(newx, newy);
      }
               
   }
   
}
Offline opiop65

JGO Kernel


Medals: 156
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #1 - Posted 2013-12-27 18:43:11 »

This isn't Perlin Noise though?

Offline trollwarrior1
« Reply #2 - Posted 2013-12-27 20:23:34 »

This isn't Perlin Noise though?

Then what is Perlin Noise? Explain.

Since you know how Perlin Noise works and how to implement it, could you post it here? I would be willing to learn.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #3 - Posted 2013-12-27 22:55:58 »

Looks pretty much like simple 2d perlin noise to me. Perlin noise is just an algorithm.

Offline Longarmx
« Reply #4 - Posted 2013-12-27 22:58:22 »

I agree with jonjava. Looks pretty much like this article teaches:webstaff

Offline trollwarrior1
« Reply #5 - Posted 2013-12-28 07:32:11 »

I believe what opiop meant was that I'm storing values in an array. But this is still perlin noise.
Offline opiop65

JGO Kernel


Medals: 156
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #6 - Posted 2013-12-28 12:58:03 »

Sorry, no I didn't realize that was perlin noise.

Where else would you store your values? You need to have an array of some sort to at least throw your newly generated points using perlin noise into your terrain renderer so it can create a mesh. 

Offline trollwarrior1
« Reply #7 - Posted 2013-12-28 19:20:12 »

Sorry, no I didn't realize that was perlin noise.

Where else would you store your values? You need to have an array of some sort to at least throw your newly generated points using perlin noise into your terrain renderer so it can create a mesh. 

The title is "Generating Perlin Noise" not "Generated Perlin Noise array". This code will allow you to calculate the output array to your liking. Perlin Noise is an algorithm to generate smooth looking noise. It is not the generated product. IT IS ALGORITHM.
Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #8 - Posted 2013-12-29 14:44:08 »

Sorry, no I didn't realize that was perlin noise.

Where else would you store your values? You need to have an array of some sort to at least throw your newly generated points using perlin noise into your terrain renderer so it can create a mesh. 

The nice thing about actual Perlin Noise, or better actual noise algorithms is that they're deterministic.

Perlin noise is also often called a Noise Function.
So what does a function actually look like?
f(x) = whateveryoudowithyourx
For example
f(x) = 6x5 - 15x4 + 10x3
The nice thing about this function is you can call f(10) a million times and it will always give the same result.
Or generally speaking: A function that always gives the same result for the same input is called deterministic.
What's nice about this? In minecraft the noise function looks like this: n(x, y, z, seed) = ...
So at each position the minecraft world looks the same, as long as the seed is the same one.

Another property a function has is it's infinity.
You can call f(x) with x = 1, x = 10, x = 100, x = 101000, as long as your number implementation allows it (with that I mean it wouldn't work for
int
s, i.e.). It's out of the question that the function doesn't stop at some kind of x value.
What's nice about that? You can walk in one direction in minecraft almost infinitely. For generating the terrain minecraft only needs the position at which to generate whatever. It's not liminted by any width bounds or similar.

So an actual noise function (in 2D) would look like that: n(x, y, seed) = ..., or in programming terms (and if you leave out the seed):
1  
public float noise(float x, float y) { ... }


Wink

So how would you generate an array of values out of this? Simply call it a couple of times with the array index as input.

That answer was kind of bloated but it's intention was not answering a question but giving some trivia and trying to create some 'ahaaaa' moments Smiley

PS: Ironically, with "actual" perlin noise I am talking about perlin noise that's not dependent on any canvas size and is deterministic Grin
Still perlin noise, though! Smiley

EDIT:
Having fully read through the source code now, it looks like he is only a small step away from a purely deterministic implementation that isn't dependent on a specific size.

What he is doing is precomputing an array of unit vectors pointing in a random direction sequentially.
What he would need to change (if he even wanted such an implementation) is get a hashing function that takes two int arguments and produces a unit vector pointing in a random direction. Thats somehow what his method
vec2 g(int x, int y)
is, but it can only return random unit vectors from inside the width-height-bounds. The next step (if even wanted) would be to code some hashing magic into that function to return a random unit vector from any (x, y).

Again, his implementation is perlin noise, I'm just explaining what I mean by 'noise function' and how it would be implemented.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Longarmx
« Reply #9 - Posted 2013-12-29 19:03:00 »

You can walk in one direction in minecraft almost infinitely.

Not true. As long as you're working with finite numbers, you will never get close to infinity.  Wink

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #10 - Posted 2013-12-29 19:25:29 »

You can walk in one direction in minecraft almost infinitely.

Not true. As long as you're working with finite numbers, you will never get close to infinity.  Wink

Yup. That's what I was talking about when I said
Quote
as long as your number implementation allows it (with that I mean it wouldn't work for
int
s, i.e.)

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 2013-12-30 09:07:36 »

This isn't a reasonable way to generate noise.

On infinity.  It's a mathematical abstract.  I can generate more unique images/terrains/whatever than you could ever look at...so it's effectively infinite even though I'd only be working with finite numbers.  Look at the magnitude local wiki page. 
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.

Longarmx (38 views)
2014-10-17 03:59:02

Norakomi (29 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (55 views)
2014-10-14 00:39:48

TehJavaDev (55 views)
2014-10-14 00:35:47

TehJavaDev (44 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!