Question

I am using a perlin noise function to generate data for a game I am making, but for some reason it keeps returning the exact same results for different inputs. I spent like 4 hours trying to debug this on my FBM function and couldn't figure it out so I tried Ken Perlin's improved noise function and the same thing happens.

Does anyone know why this is? What is a good way to fix it? The only thing I have been able to do is add a decimal value like .6473 to the x and y coordinates, which helped, but introduced other problems such as the values repeating themselves inside the arrays.

Here is some test code. I am trying to create two different 2D arrays filled with noise values. The x and y inputs are coordinates from my game. On the lines with '**' after them, if I don't increase those values, both arrays will be filled with all zeros. In the example, the coordinates (0.0, -768.0) and (-1024.0, -768.0) return the exact same noise values. In my game, 9 different coordinates return the same values.

The Perlin Noise function I am using for this test is here

public class TestPerlinMain
{
    public static void main(String[] args) 
    {
        int seed = 532434;

        //create first noise array
        double x = 0.0;    //x-coordinate
        double y = -768.0; //y-coordinate
        float z = 10.0f;

        double[][] test = new double[15][15];

        System.out.println("Noise Array 1: ");

        for(int i = 0; i < test.length; i++)
        {
            for(int j = 0; j < test[i].length; j++)
            {
                test[i][j] = ImprovedNoise.noise(x + (j * 64.0), y + (i * 64.0), 10.0);
                x += .314f;//************

                System.out.print(test[i][j] + " ");
            }
            y += .314f;//***********

        }
        System.out.println();

        //create 2nd noise array
        double x2 = -1024.0; //x coordinate
        double y2 = -768.0;  //y coordinate
        float z2 = 10.0f;    

        System.out.println();

        double[][] test2 = new double[15][15];

        System.out.println("Noise Array 2: ");

        for(int i = 0; i < test2.length; i++)
        {
            for(int j = 0; j < test2[i].length; j++)
            {
                test2[i][j] = ImprovedNoise.noise(x2 + (j * 64.0), y2 + (i * 64.0), 10.0);
                x2 += .314f;//*************

                System.out.print(test2[i][j] + " ");
            }
            y2 += .314f;//************

        }
        System.out.println();
    }
Était-ce utile?

La solution

Perlin noise is defined to be 0 at all grid locations (integer x, y, z). You can prove this to yourself by hand-simulating it in the code you linked. Since x, y, and z all become 0 when their floors are subtracted, the grad() values are all 0, so the lerp() values are all 0.

There are several ways to get the noise you want. First, if you use a non-integer value of z, then you should get random values of noise. However, since your grid spacing of 64 is much larger than the noise basis, this will look like static, not Perlin noise. A better approach would be to scale up the noise by doing something like noise(j/4., i/4., z). Sampling 4 points across each noise cell will allow some of the smoothness of the noise to be seen.

Note also that your noise implementation is designed to repeat with tiles of size 256 in each direction (see the first line of noise(). This is why you get repeating values every 4 in your array.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top