Java 2D Perlin Noise for random landscape creation

My name is Chris, and I'm working on my first game in Java. So far I have created a tile-based 2D game, but my level is done in such a way that if I create an image and all of it is green, then this green color will stand behind the grass tile. If I put a blue pixel, the game will assign it as a tile of water.

However, this limits the game to the way I design the level; I would rather have an endless terrain of tiles.

As a newbie, I was looking for different ways to do this. An especially sharp method was something called Perlinsky noise.

I looked through it, but it seemed very complicated.

Can anyone think of this easier?

Also, if you have any tutorials that “dumb” it a bit and give a brief overview, that would be fantastic!

Sorry, I was not too specific, I actually avoid this.

+5
source share
3 answers

The Perlin noise was brilliantly covered by Daniel Schiffman on the Nature Code . This is an online book that has an example Javascript / ProcessingJS code example to demonstrate some important concepts:

A good random number generator produces numbers that are irrelevant and do not show a distinguishable pattern. As we begin to see, a bit of randomness can be a good thing when programming organic, realistic behaviors. However, chance, as a single guiding principle, is not necessarily natural. This algorithm, known as “Perlin noise,” named after its inventor Ken Perlin , takes this concept into account. Perlin developed the noise function while working on the original Tron film in the early 1980s; It was designed to create procedural textures for computer-generated effects. In 1997, Perlin received an Academy Award for technical achievements in this work. Perlin noises can be used to create various effects with natural qualities such as clouds, landscapes, and textured textures such as marble.

Perlin noise has a more organic appearance , since it creates an ordered ("smooth") sequence of pseudorandom numbers. The graph at the bottom left shows Perlin noise with time, with an x ​​axis representing time; pay attention to the smoothness of the curve. The graph on the right shows pure random numbers over time.

enter image description here (The code for creating these graphs is available in the accompanying book downloads.)

Khan Academy has dedicated all advanced Javascript lessons to analyze some of the things Shiffman showed in his book. They have excellent , and, of course, one noise for Perlin .

+6
source

I suggest skipping Perlin Noise and taking a look at something called OpenSimplex Noise .

This is useful for almost all of the same things as Perlin Noise, but it has significantly fewer visible directional artifacts. Basically, noise takes an input coordinate (in 2D, 3D, or 4D) and returns a value between -1 and 1. The output values ​​constantly change as the input coordinates change.

Here are three 256x256 images created using noise (x / 24.0, y / 24.0):

  • The first is raw noise
  • The second is green, where the values ​​are greater than zero, and blue - otherwise
  • The third is blue, where values ​​are greater than -0.2 and less than 0.2, and green otherwise.

Please note that there is also Simplex Noise (another algorithm from OpenSimplex) that reduced directional artifacts compared to Perlin Noise, but 3D and higher implementations of Simplex Noise (if you want to use 3D noise to change something in 2D over time) are encumbered with a patent.

OpenSimplex Noise is actually an algorithm that I developed for my own game, so this is a shameless version that I know, but I think this is the best for you of what is available.

+5
source

You are not required to immediately get a complete picture of the perlin or simplex implementation. You can slowly learn while playing with the parameters of the various methods that you find. Just use it by sending x, y, possibly z or more dimension arguments with grid coordinates, for example. To keep it simple, you basically mix / layer multiple layers (octaves) of interpolated random images at different scales.

You can also evaluate and keep your noise offline due to processing fees that it might imply when used at runtime (although depending on the resolution / octave and processing budget or testing goals, you can achieve a pretty decent real frame rate as well )

0
source

Source: https://habr.com/ru/post/1202448/


All Articles