Need help for a Perlin Noise implementation in Corona

Hi,

I’m developing an isometric turn-based strategy game and I need to create a tile map generation algorithm.

I searched some methods for doing that and I found that the most common is Perlin Noise. I also found that corona has already a Perlin Noise generation and can be used to create tile maps as well… The problem is that the Perlin Noise generation in corona is not an actual generator, because it gives the same result every time.

I also searched some code on the Internet but it wasn’t very helpful.

The question is, can somebody give me a suggestion on how to implement a Perlin Noise algorithm in Lua?

Hi @m.melorio,

Do you want it to be an actual shader-based generator? Or do you want to do a “pretend” perlin noise kind of thing? If the first option, then you’re free to write your own custom shaders for Corona, but it won’t be in Lua, it will need to be done with GLSL ES. This guide explains it all in more detail:

https://docs.coronalabs.com/guide/graphics/customEffects.html
 

Take care,

Brent

I want a perlin noise algorithm for mathematical purpose, because I want to create a tile map based on the color of the perlin noise image. Something like: if the color is black, the tile is water, if the color is gray tile is sand, ext.
If possible it would be better to generate a table with values instead of an image.

I also found that corona has already a Perlin Noise generation and can be used to create tile maps as well

What are you referring to?

The problem is that the Perlin Noise generation in corona is not an actual generator, because it gives the same result every time.

This is by design. Noise algorithms, in the computer graphics sense, produce coherent randomness: if you compare the values yielded by some pixel and any of its neighbors, they will be close. (As opposed to white noise, where the values are all over the place.) Generally this will mean the values themselves must be fixed.

What you want to be doing is sending unique inputs to the algorithm for each tile, say the row and column. If your concern is then that you always get the same map, you just have to offset these (this is basically like seeding a random number generator), probably with some wide step so you don’t land near the values from your previous map. Something like:

function CreateLevel (cols, rows) local col\_offset = math.random(-150000, 150000) -- spread the numbers out a bit local row\_offset = math.random(-150000, 150000) for row = 1, rows do for col = 1, cols do local sample = PerlinNoise(col + col\_offset, row + row\_offset) DoSomethingWithIt(sample) end end end

If you do want to treat it like a generator, you could just maintain a counter and march in one direction:

local Index = 0 function CreateLevel (cols, rows) for row = 1, rows do for col = 1, cols do local sample = PerlinNoise(Index, 1) -- assuming 2D noise DoSomethingWithIt(sample) Index = Index + 1 end end end

I’m not sure our Perlin Noise generator is going to help you much. Since it’s basically going to be an effect applied to an image. I’m assuming you want to generate land masses or caves that you can then use to place tiles on a map, correct?

You might want to look at these posts:

https://gamedev.stackexchange.com/questions/79049/generating-tile-map

https://forums.coronalabs.com/topic/14101-perlin-noise-module-need-help-to-convert-to-lua/

https://gamedev.stackexchange.com/questions/58664/2d-and-3d-perlin-noise-terrain-generation

https://stackoverflow.com/questions/33425333/lua-perlin-noise-generation-getting-bars-rather-than-squares

https://github.com/OpenPrograms/Sangar-Programs/blob/master/noise.lua

Rob

I have written a proper perlin noise generator that generates random landscapes.  I returns a value between 0 and 1.  In my game values < 0.45 are water, between 0.45 and 6 contain different tree combinations and all above is plain land.

It wasn’t easy though and only seals with 2D generation - although you could have 0 as water and 1 as top of mountain.  It would do caves or anything as that requires a 3D implementation.

Hi @m.melorio,

Do you want it to be an actual shader-based generator? Or do you want to do a “pretend” perlin noise kind of thing? If the first option, then you’re free to write your own custom shaders for Corona, but it won’t be in Lua, it will need to be done with GLSL ES. This guide explains it all in more detail:

https://docs.coronalabs.com/guide/graphics/customEffects.html
 

Take care,

Brent

I want a perlin noise algorithm for mathematical purpose, because I want to create a tile map based on the color of the perlin noise image. Something like: if the color is black, the tile is water, if the color is gray tile is sand, ext.
If possible it would be better to generate a table with values instead of an image.

I also found that corona has already a Perlin Noise generation and can be used to create tile maps as well

What are you referring to?

The problem is that the Perlin Noise generation in corona is not an actual generator, because it gives the same result every time.

This is by design. Noise algorithms, in the computer graphics sense, produce coherent randomness: if you compare the values yielded by some pixel and any of its neighbors, they will be close. (As opposed to white noise, where the values are all over the place.) Generally this will mean the values themselves must be fixed.

What you want to be doing is sending unique inputs to the algorithm for each tile, say the row and column. If your concern is then that you always get the same map, you just have to offset these (this is basically like seeding a random number generator), probably with some wide step so you don’t land near the values from your previous map. Something like:

function CreateLevel (cols, rows) local col\_offset = math.random(-150000, 150000) -- spread the numbers out a bit local row\_offset = math.random(-150000, 150000) for row = 1, rows do for col = 1, cols do local sample = PerlinNoise(col + col\_offset, row + row\_offset) DoSomethingWithIt(sample) end end end

If you do want to treat it like a generator, you could just maintain a counter and march in one direction:

local Index = 0 function CreateLevel (cols, rows) for row = 1, rows do for col = 1, cols do local sample = PerlinNoise(Index, 1) -- assuming 2D noise DoSomethingWithIt(sample) Index = Index + 1 end end end

I’m not sure our Perlin Noise generator is going to help you much. Since it’s basically going to be an effect applied to an image. I’m assuming you want to generate land masses or caves that you can then use to place tiles on a map, correct?

You might want to look at these posts:

https://gamedev.stackexchange.com/questions/79049/generating-tile-map

https://forums.coronalabs.com/topic/14101-perlin-noise-module-need-help-to-convert-to-lua/

https://gamedev.stackexchange.com/questions/58664/2d-and-3d-perlin-noise-terrain-generation

https://stackoverflow.com/questions/33425333/lua-perlin-noise-generation-getting-bars-rather-than-squares

https://github.com/OpenPrograms/Sangar-Programs/blob/master/noise.lua

Rob

I have written a proper perlin noise generator that generates random landscapes.  I returns a value between 0 and 1.  In my game values < 0.45 are water, between 0.45 and 6 contain different tree combinations and all above is plain land.

It wasn’t easy though and only seals with 2D generation - although you could have 0 as water and 1 as top of mountain.  It would do caves or anything as that requires a 3D implementation.