1
\$\begingroup\$

I'm trying to create a terraria like terrain with SDL2 and C++. For generating the terrain I'm trying to use the 1D noise function from here: https://github.com/SRombauts/SimplexNoise

It's pretty basic and only takes a x value. I've been messing around with it for a while but can't work out what to pass into the function as the x value. I first tried passing just the x value of the column I'm finding the noise for. This just returned 0 every time. Next I tried passing a value between 0 and 1, so I passed it x / width. This sort of worked, but the wider I made the terrain, the more spaced out it became.

Hope you understand, what I do give the function for the x value? Thanks in advance.

\$\endgroup\$
0
\$\begingroup\$

What Pikalek said is absolutely true about the return value, but the workaround he suggest is not very good.

Instead of adding a random offset, you should divide the current x position with a cobstant value. Let's call this value "smoothness". Higher smoothness means smokther terrain:

float smoothness = 20;
float height = 20; // Max height of terrain
for (float x = 0; x < COLUMN_MAX; x++) {
    float val = SimplexNoise::noise(x / smoothness) * height;
    // Stuff
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks, it works nicely :) Is it possible I could also add the random value to get random results everytime? \$\endgroup\$ – IHazza Oct 6 '16 at 8:14
  • \$\begingroup\$ @IHazza Do you mean to make it more "realistic" or to make it more random? \$\endgroup\$ – Bálint Oct 6 '16 at 8:50
  • \$\begingroup\$ Make it random. The result is the same everytime, and I'm wondering if I add a random offset will it be random everytime? \$\endgroup\$ – IHazza Oct 6 '16 at 9:07
  • \$\begingroup\$ @IHazza you should really read something about pseudo random noises. Take every integer between 0 abd 255 inclusively and insert them in the perm table randomly \$\endgroup\$ – Bálint Oct 6 '16 at 10:01
  • \$\begingroup\$ What specifically is not very good about my solution? The constructor for SimplexNoise already has parameters to control frequency, amplitude, lacunarity & persistence. If OP wants smoother noise, the library already supports that without applying transforms to the input. \$\endgroup\$ – Pikalek Oct 6 '16 at 14:38
0
\$\begingroup\$

The function documentation explicitly says:

@return Noise value in the range[-1; 1], value of 0 on all integer coordinates

Since the column number is an integer coord, passing it as input will always give 0 as output. An easy way around that is to add a constant decimal offset to the column number:

// seed RNG however you need
randomOffset = ((float) rand() / (RAND_MAX));

// do other setup as needed

// iterate over columns getting a noise value for each
for(int a=0; a<COLUMN_MAX; a++){
   float noiseValue = SimplexNoise::noise(a + randomOffset);

   // use noise value for the current column
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Ok I tried it, but the output seems weird, heres the output: postimg.org/image/9ahh9ro2l, and the code: gist.github.com/anonymous/856f2644d08995822392412eeaebf70f \$\endgroup\$ – IHazza Oct 6 '16 at 4:14
  • 1
    \$\begingroup\$ I don't think so. The noise function is static and the constructor/object is for the fractal noise. But correct me if I'm wrong. \$\endgroup\$ – IHazza Oct 6 '16 at 23:26
  • \$\begingroup\$ No, you are correct; I'm the one who didn't read the doc comments closely enough. If want to use the static function, you'll need to adjust for smoothness by scaling the input value as @Bálint suggested. \$\endgroup\$ – Pikalek Oct 7 '16 at 2:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.