Perlin noise: The Gaming Algorithm

#HackEatofDay

Hi, Guys again Learner is here with some amazing facts of gaming algorithms.
Games are just big bags of algorithms. There’s art, music, writing, direction, design and playtesting too, but a lot of algorithms.

What is Perlin noise?

Perlin Noise is an extremely powerful algorithm that is used often in a procedural content generation. It is especially useful for games and other visual media such as movies. The man who created it, Ken Perlin, won an academy award for the original implementation.
Perlin noise is most commonly implemented as a two-, three- or four-dimensional function, but can be defined for any number of dimensions. An implementation typically involves three steps: grid definition with random gradient vectors, computation of the dot product between the distance-gradient vectors and interpolation between these values.

How can I implement it?

// Function to linearly interpolate between a0 and a1

// Weight w should be in the range [0.0, 1.0]


function lerp(float a0, float a1, float w) {


return (1.0 – w)*a0 + w*a1;


}


// Computes the dot product of the distance and gradient vectors.


function dotGridGradient(int ix, int iy, float x, float y) {

// Precomputed (or otherwise) gradient vectors at each grid node
extern float Gradient[IYMAX][IXMAX][2];

// Compute the distance vector
float dx = x – (float)ix;
float dy = y – (float)iy;

// Compute the dot-product
return (dx*Gradient[iy][ix][0] + dy*Gradient[iy][ix][1]);
}

// Compute Perlin noise at coordinates x, y
function perlin(float x, float y) {

// Determine grid cell coordinates
int x0 = floor(x);
int x1 = x0 + 1;
int y0 = floor(y);
int y1 = y0 + 1;

// Determine interpolation weights
// Could also use higher order polynomial/s-curve here
float sx = x – (float)x0;
float sy = y – (float)y0;

// Interpolate between grid point gradients
float n0, n1, ix0, ix1, value;
n0 = dotGridGradient(x0, y0, x, y);
n1 = dotGridGradient(x1, y0, x, y);
ix0 = lerp(n0, n1, sx);
n0 = dotGridGradient(x0, y1, x, y);
n1 = dotGridGradient(x1, y1, x, y);
ix1 = lerp(n0, n1, sx);
value = lerp(ix0, ix1, sy);

return value;
}

What about Complexity?

For each evaluation of the noise function, the dot product of the position and gradient vectors must be evaluated at each node of the containing grid cell. Perlin noise therefore scales with complexity {\displaystyle O(2^{n})} O(2^{n}) for {\displaystyle n} n dimensions

Alternatives?

Can I implement it in games with different Surface forms?

Noise

A common way to generate 2D maps is to use a bandwidth-limited noise function, such as Perlin or Simplex noise, as a building block. This is what the noise function looks like:


for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
double nx = x/width – 0.5, ny = y/height – 0.5;
value[y][x] = noise(nx, ny);
}
}


Elevation


Noise by itself is just a bunch of numbers. We need to assign meaning to it. The first thing we might think of is to make the noise correspond to elevation (also called a “height map”).


elevation[y][x] = noise(nx, ny);

Do you wanna know more?

Come here
{Code with Code@ter}

Give Your Reviews

Leave a Reply