Tile 0x0 in a procedurally generated world always the same

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Tile 0x0 in a procedurally generated world always the same

Post by veethree »

Hi, So I've been working on a game that uses procedural generation to procedurally generate procedural worlds. For a while, The game had some code that would find a tile for the player to spawn on by looping through whatever tiles had already spawned until it found a suitable one. But that code was written stupidly so i decided to rewrite it. In that process i set it up to always spawn the player at tile 0x0, And proceeded to try a bunch of different world seeds to find one where 0x0 is not suitable for the player to spawn. To my surprise, After trying about a 100 different seeds i wasn't able to find one. The 0x0 tile always seems to be a ground tile.

Is there any reason this should be the case? I looked over my world generation code and couldn't come up with any reason why that should be the case.

The game decides where ground tiles go via this function

Code: Select all

function generateNoise(x, y, scaleBase, scaleDetail, thresh, ratio1, ratio2, seedOffset)
    scaleBase = scaleBase * noiseScale
    scaleDetail = scaleDetail * noiseScale
    return noise(x * scaleBase, y * scaleBase, seed + seedOffset) * ratio1 + noise(x * scaleDetail, y * scaleDetail, seed + seedOffset) * ratio2 > thresh and true or false
end
where x and y are the tiles grid coordinates, scaleBase and scaleDetail scale the two layers of noise, thresh is the threshold for it returning true, ratio1 and ratio2 are the ratios of the two noise layers and seedOffset is a value added to the base seed, Which is used as the third argument in love.math.noise.

The full world gen code is here(github). Along with all the other code.
The code on github still does the whole finding suitable tile dance. The .love below does not.
Attachments
mining_game.love
(74.73 KiB) Downloaded 88 times
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: Tile 0x0 in a procedurally generated world always the same

Post by milon »

You're using Love's simplex noise function, which has the following notable constraints:
1. Given the same inputs it will ALWAYS produce the same output (it's a stateless generator)
2. Return values tend to be constant given integer only inputs

What do the inputs evaluate to for cell 0,0? I'm guessing a seed-independent constant, or else an integer that results in a constant output. (I don't have time just now to dig into that, but you should be able to work it out quickly.)
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: Tile 0x0 in a procedurally generated world always the same

Post by veethree »

The first 2 arguments for 0x0 will always be 0, but the seed, which is used as the third argument, will be different in each world. And the seed is always an integer. I don't have time right this moment to test it, but this makes some sense. Thank you!
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: Tile 0x0 in a procedurally generated world always the same

Post by milon »

Yeah, makes sense then. Coords 0,0,[INT] seems to always give 0.5 as an output, which your map generator must interpret as a ground tile.

Here's a quick example:

Code: Select all

for i = 1, 5000 do
    print(love.math.noise(0,0,i)) -- always gives 0.5
end
Changing the 0 to 0.5 results in "better" noise values, but you still get 0.5 for many of the values. Try to use non-integer values for better noise values. You can divide x and y by 10, or add pi to them, or something else that will (usually) give a non-integer result.

But hey, if you're happy with what you have, even better! :)
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: Tile 0x0 in a procedurally generated world always the same

Post by veethree »

milon wrote: Thu Feb 10, 2022 8:48 pm Yeah, makes sense then. Coords 0,0,[INT] seems to always give 0.5 as an output, which your map generator must interpret as a ground tile.

Here's a quick example:

Code: Select all

for i = 1, 5000 do
    print(love.math.noise(0,0,i)) -- always gives 0.5
end
Changing the 0 to 0.5 results in "better" noise values, but you still get 0.5 for many of the values. Try to use non-integer values for better noise values. You can divide x and y by 10, or add pi to them, or something else that will (usually) give a non-integer result.

But hey, if you're happy with what you have, even better! :)
The x and y are usually floats, Because they're multiplied by some value between 0 and 1. I did try making the seed a non integer and it seems to result in slightly more interesting worlds. It's kinda hard to tell.
Thank you for your help!
User avatar
zorg
Party member
Posts: 3449
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Tile 0x0 in a procedurally generated world always the same

Post by zorg »

Basically, integer coords are grid points, so their values will be nearly, if not completely the same; also, even if you multiply by some float, 0 times any value is still 0 you know :3

adding an offset of .5 should be a simple solution that mitigates this:

Code: Select all

function generateNoise(x, y, scaleBase, scaleDetail, thresh, ratio1, ratio2, seedOffset)
    scaleBase = scaleBase * noiseScale
    scaleDetail = scaleDetail * noiseScale
    return noise(.5 + x * scaleBase, .5 + y * scaleBase, .5 + seed + seedOffset) * ratio1 + noise(.5 + x * scaleDetail, .5 + y * scaleDetail,.5 +  seed + seedOffset) * ratio2 > thresh and true or false
end
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests