I'm trying to use 1D Perlin Noise to generate "random" terrain for a side-view scrolling game (Think Terraria). I've gotten it mostly working, however strange things start to happen when generating terrain for negative x values. This is a sample of the terrain:
As you can see, the left side, where the x values are negative, is generated in a much more jagged and sharp way than the positive side. I would much prefer it to be more smooth like the positive side. This is the noise generator I am using:
private long seed;
public PerlinNoise(long seed) {
this.seed = seed;
}
public int getNoise(int x, int range) {
int selectionSize = 16 * 16;
float noise = 0;
range /= 2;
while (selectionSize > 0) {
int selectionIndex = x / selectionSize;
float distanceToIndex = (x % selectionSize) / (float) (selectionSize);
float leftRandom = random(selectionIndex, range);
float rightRandom = random(selectionIndex + 1, range);
noise += (1 - distanceToIndex) * leftRandom + distanceToIndex * rightRandom;
selectionSize /= 2;
range /= 2;
range = Math.max(1, range);
}
return Math.round(noise * (noise < 0 ? -1 : 1));
}
private int random(int x, int range) {
return (int) ((x + seed) ^ 10) % range;
}