Friday, June 23, 2017

xna - C# Perlin noise - generating “endless” terrain chunks?



I'm currently writing a little side scroller in C#, to both learn C# and have fun. Right now I have a simple random number generator generating the world but it isn't exactly all that great - so with some research, I've discovered that Perlin Noise generation may help me out quite a bit. Problem is, I want to have an "endless" landscape made up of several chunks.


Basically my questions / concerns are:



  1. Using minecraft as an example (Ignoring the 3rd dimension), how is Notch getting each chunk to connect to each other perfectly? Tunnels, caves, ore veins, mountains, flat lands, biomes, etc. are all connected to each other even though each chunk is generated separately, and sometimes at a much later date. This is key for me, I want the player to be able to walk to the right and as they are walking, generate more landscape that connects to previous landscape, including underground tunnels and cave systems.

  2. Going off of #1, how would this be accomplished under the assumption that each chunk is a square, and the world is 10 squares high, and infinite squares wide? I.e. Each "chunk" is 128x128 tiles and the world is 1,280 tiles tall total. (This is so that I can make an infinitely deep map if I choose to - and also to show that all 4 sides of a chunk / square need to be able to connect and continue on what the previous square/chunk was doing).



Answer



Find a C# implementation of Simplex Noise, it's like Perlin but better behaved and faster.


The thing about continuous noise functions like Perlin and Simplex is that they are deterministic, noise is not random. What most noise implementations use is a Seed value that offsets into the noise generated which makes Seed=1 different from Seed=2.


Because of the above observation you can keep reusing the Seed and all the new terrain generated as you move through the noise function will match up because it just picks up where you left off.



This part of it is pretty simple and quite nice, what you do with the generated noise is where the magic happens.


To James, that's possible but my personal speed test with a 3D noise showed simplex to be faster at 3, could be any number of factors in my personal test but it matched the general idea in literature. Even if it were slower I would recommend simplex because it doesn't show the "square" artifacts that perlin does.


To Jon, Noise is a math function and it just spits out results from parameters and given the same parameters you will get the same number back. I see where the confusion might come from.


This noise function is sampled like: number=noise(x,y,z); for 3 dimensional noise. As an example, your first map may sample from X=0.0 to X=1.9, the map just to the right of it continues and samples from X=2.0 to 3.9


Part of the art of using noise is finding good ranges to sample, deciding on how many octaves, filtering and processing etc... Take a look at a handy noise library to see a few examples and some source code of Perlin in action.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...