Question

I have a bit of an issue (again) with my Perlin Noise generation. I have read multiple articles on how it works. I implemented it with the following code:

package PerlinNoise;
import java.util.Random;

public class PerlinNoise {

private long seed;
private Random rand;
private float f;

public PerlinNoise(long seed, float f) {
    this.seed = seed;
    this.f = f;
    rand = new Random();
}

//interpolates generated noise
public float getInterpolatedNoise(float x, float y) {
    float a = (int) Math.floor((double) x / f);
    float A = a + 1;
    float b = (int) Math.floor((double) y / f);  //<-- define the points around the point
    float B = b + 1;
    return cosineInterpolate(
            cosineInterpolate((float) getNoise(a, b), (float) getNoise(A, b), (float) (x - a * f) / f),
            cosineInterpolate((float) getNoise(a, B), (float) getNoise(A, B), (float) (x - a * f) / f),
            (float) (y - b * f) / f); //<-- interpolates everything 
}

//cosine interpolation
private float cosineInterpolate(float a, float b, float x) {
    float f = (float) ((1f - Math.cos(x * Math.PI)) * .5f);
    return a * (1f - f) + b * f;
}

//generates random noise value between -1.0 and 1.0
private float getNoise(float x, float y) {
    if(y < 0) {
        rand.setSeed((long) (332423 * (Math.sin(Math.cos(x) * x) + Math.cos(Math.sin(y) * y) + Math.tan(seed))));
    }else{
        rand.setSeed((long) (432423 * (Math.sin(x) + Math.cos(y) + Math.tan(seed))));
    }
    float n = (float)(((float)rand.nextInt(255) - (float)rand.nextInt(255)) / 255.0f);
    return n;
}

And here is when I add all my octaves together:

    //performs perlin noise function
public static float[][] getPerlinNoise(int octaves, long seed) {
    float[][] noise = new float[Main.csX][Main.csY];
    for(int z = 0; z < octaves; z++) {
        float f = (float)Math.pow(2, z);
        PerlinNoise oct = new PerlinNoise(seed, f);
        for(int y = 0; y < Main.csY; y++) {
            for(int x = 0; x < Main.csX; x++) {
                noise[x][y] = (noise[x][y] + oct.getInterpolatedNoise(x * f, y * f)) * (float)Math.pow(p, z); //<- pumps out numbers between -1.0 and 1.0
            }
        }
    }
    return noise;
}

Sorry for the giant code dump there. The code all works when I run it, but I don't get Perlin noise. I just get this:

Definitely not Perlin Noise..

This is more of blury, blended noise than anything else. I get very similar results even when I add more octaves and/or ramp up the persistence. I used this article as a reference for building the code (and also this one). So if anyone has any ideas as to why this isn't working, please comment/answer. Thanks!

Was it helpful?

Solution

I've run into this problem, and it's fairly common for those just starting out with Perlin Noise. I believe what's happening is that you're sampling the perlin noise at points that are too far apart. Try multiplying your f by 0.1, and see if that helps. Also, try using just one octave at first, it'll help you debug.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top