Perlin Noise

Perlin Noise was developed by Ken Perlin and is used extensively throughout Feralor. More information on Perlin Noise in general can be found at:

http://en.wikipedia.org/wiki/Perlin_noise

In Feralor, Perlin Noise is used to generate lots of things, including:

- Terrain
- Resource Nodes
- Difficulty Levels
- Weather

By using Perlin Noise, we can have an infinite world with minimal storage requirements on the server and also allow much of the processing to be offloaded onto the client’s browser.

One challenge related to Perlin noise has been the need to build two separate yet identical versions of Perlin libraries: one in PHP (for the server) and one in JavaScript (for the client). Perlin Noise makes use of a “random noise function”, which is actually less of a random number generator and more of a hash function – for any given input, it needs to produce the same output. JavaScript does not provide an unsigned integer data type (or any strong data types at all, for that matter) which makes bit-wise operations hard to implement, and as it turns out most hash functions are dependent on bit-wise operations. Of course, PHP does not provide strong data types either, but it seems that PHP’s default is to treat integers as unsigned whereas JavaScript seems to treat them as signed by default.

One option I have not year explored is to use string representations of numbers (in PHP, using the dechex function, and in JavaScript using the value.toString(16) function) to effectively mask the effect of signed integers, but I have not implemented this yet and I have a sneaking suspicion that the resulting code won’t be quite as fast.

Another task that needs to be dealt with is selecting which hash function to use. Initially, I wrote some code that used an SHA1 hash which produced beautifully random-looking Perlin noise. However, SHA1 is not a lightweight function and the JavaScript implementation I found is probably too slow for actual game play. Other random number generators that I found in articles related to Perlin noise produced visually-apparent artifacts (like straight lines in the noise) that are undesirable. More recently, I have come across a hash function from Paul Hsieh that appears promising:

http://www.azillionmonkeys.com/qed/hash.html

But, at present, I have not attempted to implement this in JavaScript and PHP.

For my own reference as much as everyone else’s, here are some other pages with good information about Perlin Noise:

http://mrl.nyu.edu/~perlin/noise/
http://freespace.virgin.net/hugo.elias/ … perlin.htm
http://dev.horemag.net/2008/03/02/perli … or-in-php/

Comments are closed.