Page 1 of 1

Map generation problem

Posted: Fri Aug 19, 2016 2:44 am
by tigerclaw
I am trying to generate a map using Simplex\Perlin noise. Debugging with the print function, I have discovered that love.math.noise is producing the same number for every x,y, and "seed" combination. Here is a snippet of the code:

Code: Select all

function gen_map()
	seed = love.math.random(0,10000)

	for x=1,map_width do
		table.insert(map,{})
		for y=1,map_height do
			local noise = love.math.noise(x,y,seed) * 4 + 1
			local terrain = terrains[math.floor(noise)]
			table.insert(map[x],Tile(terrain))
			--print(x..", "..y..", "..seed..": "..noise)
		end
	end
end
Am I doing something wrong?

Re: Map generation problem

Posted: Fri Aug 19, 2016 8:53 am
by AnRu
Hi there!
This is because third parameter for noise functions is a Z coordinate, not seed :)
You need to set seed by 'love.math.setRandomSeed()' function.
So, you will recieve something like this:

Code: Select all

function gen_map()
   love.math.setRandomSeed(os.time(os.date('*t')))

   for x=1,map_width do
      table.insert(map,{})
      for y=1,map_height do
         local noise = love.math.noise(x,y) * 4 + 1
         local terrain = terrains[math.floor(noise)]
         table.insert(map[x],Tile(terrain))
         --print(x..", "..y..", "..seed..": "..noise)
      end
   end
end
'

Re: Map generation problem

Posted: Fri Aug 19, 2016 6:39 pm
by Jasoco
Actually it doesn't appear love.math.noise is affected by any random seed. The noise generated will always look the same no matter what seed you set. (Which is my only problem with it really.) I did my own tests and indeed confirm that I can't get it to look different if I give it a different seed.

And the third parameter isn't a Z. It's just a third parameter. The love.math.noise function accepts up to 4 parameters. You can use the first three as an X, Y and Z if you want but they can be anything you want them to be if you want to get technical.

From the Wiki:
Generates a Simplex or Perlin noise value in 1-4 dimensions. The return value will always be the same, given the same arguments.
Simplex noise is closely related to Perlin noise. It is widely used for procedural content generation.
There are many webpages which discuss Perlin and Simplex noise in detail.
Maybe one of the big guys will pop in here and give more info into how it works.

Re: Map generation problem

Posted: Fri Aug 19, 2016 7:37 pm
by AnRu
Jasoco wrote:Actually it doesn't appear love.math.noise is affected by any random seed. The noise generated will always look the same no matter what seed you set. (Which is my only problem with it really.) I did my own tests and indeed confirm that I can't get it to look different if I give it a different seed.

And the third parameter isn't a Z. It's just a third parameter. The love.math.noise function accepts up to 4 parameters. You can use the first three as an X, Y and Z if you want but they can be anything you want them to be if you want to get technical.

From the Wiki:
Generates a Simplex or Perlin noise value in 1-4 dimensions. The return value will always be the same, given the same arguments.
Simplex noise is closely related to Perlin noise. It is widely used for procedural content generation.
There are many webpages which discuss Perlin and Simplex noise in detail.
Maybe one of the big guys will pop in here and give more info into how it works.
Thank you with corrections, I answered only about what i saw :)

Re: Map generation problem

Posted: Fri Aug 19, 2016 10:53 pm
by Positive07
What do you mean by same value? I'm getting these

Code: Select all

1, 1, 9812: 3.0000813007355
1, 2, 9812: 2.9999188184738
1, 3, 9812: 3
1, 4, 9812: 4.5553710460663
1, 5, 9812: 2.7783100605011
1, 6, 9812: 3
2, 1, 9812: 2.9999188184738
2, 2, 9812: 3
2, 3, 9812: 2.9989434480667
2, 4, 9812: 2.9999188184738
2, 5, 9812: 3
2, 6, 9812: 2.7783100605011
3, 1, 9812: 3
3, 2, 9812: 4.5552086830139
3, 3, 9812: 4.5552897453308
3, 4, 9812: 3
3, 5, 9812: 4.7758421897888
3, 6, 9812: 3.0000813007355
...
This is to be expected, you can use the extra 2 dimensions that you don't need as a seed, this is good because when you need to generate the same map again you only need to store the seed value, if you want to generate a different map simply change the seed, you are using [wiki]love.math.random[/wiki] anyway so that should be what you want. What's the problem you are having with this behaviour?

Re: Map generation problem

Posted: Sat Aug 20, 2016 4:52 am
by pgimeno
See the big notice in [wiki]love.math.noise[/wiki].