Procedual Map Generation (Perlin Noise)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ohmi
:warning: Old Version Published before Godot 3 was released.

Hi,

I recently tried to randomly generate a hex-tile map through GD-script.
I took a script i found on github to generate noise. But what ever I try, I always get the same (not very random looking) result.

My script:

func _map_gen():
    noise_map = perlin.SoftNoise.new()

func _tile_type(pos):
   var noise_var = noise_map.perlin_noise2d(pos.x/mapsize.x, pos.y/mapsize.x)
   if noise_var < -0.5:
	   return 1
   elif noise_var < -0.1:
	   return 2
   elif noise_var < 0.4:
	   return 3
   elif noise_var < 0.8:
	   return 4
   elif noise_var < 1.1:
	   return 5

The SoftNoise.new() function should generate a random noise-map. I also tried using different seeds, but the results are always the same. Am I missing something very obvious here?

Why are you reading the noise like this?

var noise_var = noise_map.perlin_noise2d(pos.x/mapsize.x, pos.y/mapsize.x)

I cannot test it right now but I guess you should call it like this:

var noise_var = noise_map.perlin_noise2d(pos.x, pos.y)

davidoc | 2017-11-05 17:15

perlin noise is usually read this way, because it returns 0 on an integer value. Meaning that the way you wrote it all tiles would be the same. Also in this case (maybe thats normal too) it would just repeat. So for example if the map is 100x100 and you read pos / 50, you just get the same pattern in the four quarters of the map.

ohmi | 2017-11-05 19:34

:bust_in_silhouette: Reply From: mollusca

The gradient table generating function in softnoise.gd seems a bit strange. This part starting at line 160:

for i in range(256):
    gx.append(float(randf())/(32767/2) - 1.0)
    gy.append(float(randf())/(32767/2) - 1.0)

Change it to:

for i in range(256):
    gx.append(2.0 * randf() - 1.0)
    gy.append(2.0 * randf() - 1.0)

This should give a better result.

Thank you! That fixed it.

ohmi | 2017-11-06 12:08

:bust_in_silhouette: Reply From: RedLizard

use randomize() function