Monday, June 4, 2018

tiles - Random seed function for map generation?



I am looking for a function to generate a random tile-based map as the visual boundaries of the map change (by going through the map). I want the map to be infinitely large, and have maze-like structure.


However, if the world is infinite, going back to where a player has already been before raises a problem. The game must remember how everything back there actually looked like.


So, I was thinking - "How does Minecraft solve this issue?" and I thought to myself that they must be using some kind of random-number function with a seed, that can both go forward but also backwards, and in that way, re-generate old tiles exactly as they were, but in new instances.


What are your thoughts on this?



Answer



What you've noticed is the difference between a random number generator and a noise function. A random number generator spits out a different number each time you call it. A noise function takes some arguments - say, a map x and y - and spits out numbers with random-like statistical properties, but the same value for the same arguments every time, i.e. it is a proper mathematical function.


The two are very closely related. A noise function can simulate a random number generator, by passing in a different value each time - e.g. noise(1), noise(2), and so on. And a random number generator, dumped into a giant table, can act as a noise function. In both cases though, you're using the wrong tool for the job.


Minecraft in particular uses Perlin noise, a type of noise which is cheap to compute, and has a desirable property of being continuous in as many dimensions as you need - if you graph f(x) to f(x + 1), there won't be any sudden jumps. This makes it very useful for many things like texture modulation, volumetric clouds and gases, and terrain generation.


If you are looking for an implementation to start playing with, Ken Perlin's improved Perlin noise generator is one of the simplest implementations.


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...