How to make perlin noise return something other than 0.5

Hey there,

This is on v1.9.3 of the api on macOS 12.3.

I'm experimenting with the API for generating perlin noise, but it seems like it always returns 0.5

I've built a small project that tries to generate a 10x10 grid of booleans. When you press :a: , it toggles between using math.random to generate the booleans, and gfx.perlin. But from what I can tell, the way I'm calling the perlin method always generates a value of 0.5, resulting in a really really boring grid of like, no noise.

The full project's attached, but here's how I'm calling it:

function generatePerlinGrid()
    for row = 1, size, 1 do
        for col = 1, size, 1 do
            local value = gfx.perlin(col, row, 1, 0)
            grid[col][row] = (value < 0.5)
        end
    end
end

Do I need to seed this somewhere? Any idea what I'm doing wrong?

Thanks,
Chris

CleanShot 2022-03-23 at 20.30.56

test-random-grid.zip (60.7 KB)

Hello.

Depending on how the noise generation algorithm is implemented, it might occur that you'd get the same value on round coordinates like you do in your snippet.

In your linked project, I got something working more like you'd expect by altering the coordinates before passing them to the perlin function, in my case by dividing col and row by respectively 3.2 and 5.4.

function generatePerlinGrid()
    for row = 1, size, 1 do
        for col = 1, size, 1 do
            local noise_x  = col / 3.2
            local noise_y  = row / 5.4
            local value = gfx.perlin(noise_x, noise_y, 1, 0)
            grid[col][row] = (value < 0.5)
        end
    end
end

Feel free to experiment with any kind of coordinate transformation you can think of to see how they change the result.

Addendum: I don't know yet if gfx.noise can be seeded, I have yet to experiment with the Lua API, but in the meantime you can issue a random translation to make up for the possible lack of seed.

Did you try the Perlin Array function?

I did try the perlin array function. That also gave me 0.5 consistently when I was using integer coordinates.

@eiyron That totally worked. What made you choose those divisors? Or did you just fiddle with things until they looked good.

I updated the example game

  • It now uses dithering so it's not just on/off to show that, yeah, everything is 0.5 with integer x/y
  • Added a third state where I'm using the perlin array generation instead
  • And let you change the x and y divisors with the arrow buttons to see how the response from the perlin function changes.

perlin-noise


I guess one last question: is this how it's supposed to work? Specifically:

  • All integer aligned coordinates have the same value
  • You get "randomness" not from seeding it, but by just changing how you move through the coordinate space?

Thanks again!

test-random-grid-v2.zip (75.4 KB)

1 Like

That totally worked. What made you choose those divisors? Or did you just fiddle with things until they looked good.

You're welcome. I just used random factors just to have noise coordinates that don't fall into integer coordinates, it could have been instead a multiplication or even an offset. This should be the same with the Perlin Array function Matt suggested to use (which I didn't know about until he suggested it, I mostly delved into the C API, my bad). Try to fiddle with both the coordinates and the step values to see how they work.

One other thing you could do, which I tried on your example, was just add 0.5 to you row/col numbers to avoid being integer aligned.

It looks something like this:
Untitled

Which was then easy to pan around:
Untitled

Perlin noise is cool :smiley:

1 Like

I also found this image from the perlin noise wiki:
image
helpful to understand why it's always 0.5 at the integer values.

And this generator useful to see how various params tweak things:
http://kitfox.com/projects/perlinNoiseMaker/
although it'd be interesting to translate this into Playdate code.

1 Like

OK, so, I didn't exactly recreate that noise maker, but I definitely took inspiration from it to make a tool to generate noise using the perlin APIs on Playdate.

Cross linking to that post in case folks find this thread first: Perlin Noise explorer

Thanks again for all your help!

1 Like