So I'm pretty happy with my terrain, and the progress on my game. I just want to figure out why the terrain is so steep sometimes. Right now to generate my random seed I use this:
seed = (long)(Math.random() * 10000000 + 12345670);
It works, but some of the seeds just make a really steep point hill, while I'm looking for more bumpy-flat maps (similar to Terraria's).
Here's my create terrain loop:
for (int i = minX; i < maxX; i++) {
int columnHeight = p.getNoise(i, maxY);
for (int j = 0; j < columnHeight; j++) {
System.out.println(columnHeight);
map[i][j] = new Tile(i * 8, j * 8, 8, 8, TileType.Grass);
}
}
Here's the class I'm using to try to generate terrain. I tried tweaking parameters but nothing works, and I just keep getting crazy random hills.
public class Perlin {
private long seed;
// Constructor to create a seed
public Perlin(long seed){
this.seed = seed;
}
// Fetches a random value
private int random(int x, int range){
return (int) ((x + seed) * (x + seed) * 8) % range;
// o = (int) (((x + seed)*(x + seed))*5) % range
//return o;
}
// Calculates the noise
public int getNoise(int x, int range){
int chunkSize = 64;
int chunkIndex = x / chunkSize;
float prog = (x % chunkSize) / (chunkSize * 1f);
float left_random = random(chunkIndex, range);
float right_random = random(chunkIndex + 1, range);
float noise = (1 - prog) * left_random + prog * right_random;
return Math.round(noise);
}
}
Here's a picture of the terrain compared to what I want (what I want is in red):
EDIT So I understand what you guys mean about the previous Perlin class not actually being perlin noise. I fixed it with some tweaking and using actual perlin noise, the only thing I'm partially confused about is the seeds, and how do they work..
for example in Java this is how they do it
x = (x<<13) ^ x;
return (float)( 1.0 - (( (x * (x * x * 1376312589 + 789221) + 1376312589) + 3517) & Integer.MAX_VALUE) / 2543768824f);
if someone could explain what all that really means it'd be great, and why does it use prime numbers?