I am working on a small summer project and this project got me into procedurally generated terrain for the first time. So I found this code online and it worked pretty well.
Code: `
import java.util.Random;
public class SimplexNoise {
/** Source of entropy */
private Random rand_;
/** Amount of roughness */
float roughness_;
/** Plasma fractal grid */
private float[][] grid_;
private int Xoffset_, Yoffset_;
/**
* Generate a noise source based upon the midpoint displacement fractal.
*
* @param rand
* The random number generator
* @param roughness
* a roughness parameter
* @param width
* the width of the grid
* @param height
* the height of the grid
*/
public SimplexNoise(Random rand, float roughness, int width, int height) {
roughness_ = roughness / width;
grid_ = new float[width][height];
rand_ = (rand == null) ? new Random() : rand;
}
public void initialise() {
int xh = grid_.length - 1;
int yh = grid_[0].length - 1;
// set the corner points
grid_[0][0] = rand_.nextFloat() - 0.5f;
grid_[0][yh] = rand_.nextFloat() - 0.5f;
grid_[xh][0] = rand_.nextFloat() - 0.5f;
grid_[xh][yh] = rand_.nextFloat() - 0.5f;
// generate the fractal
generate(0, 0, xh, yh);
}
// Add a suitable amount of random displacement to a point
private float roughen(float v, int l, int h) {
return v + roughness_ * (float) (rand_.nextGaussian() * (h - l));
}
// generate the fractal
private void generate(int xl, int yl, int xh, int yh) {
int xm = (xl + xh) / 2;
int ym = (yl + yh) / 2;
if ((xl == xm) && (yl == ym))
return;
grid_[xm][yl] = 0.5f * (grid_[xl][yl] + grid_[xh][yl] );
grid_[xm][yh] = 0.5f * (grid_[xl][yh] + grid_[xh][yh] );
grid_[xl][ym] = 0.5f * (grid_[xl][yl] + grid_[xl][yh]) ;
grid_[xh][ym] = 0.5f * (grid_[xh][yl] + grid_[xh][yh]) ;
float v = roughen(0.5f * (grid_[xm][yl] + grid_[xm][yh]), xl + yl, yh + xh);
grid_[xm][ym] = v;
grid_[xm][yl] = roughen(grid_[xm][yl], xl, xh);
grid_[xm][yh] = roughen(grid_[xm][yh], xl, xh);
grid_[xl][ym] = roughen(grid_[xl][ym], yl, yh);
grid_[xh][ym] = roughen(grid_[xh][ym], yl, yh);
generate(xl, yl, xm, ym);
generate(xm, yl, xh, ym);
generate(xl, ym, xm, yh);
generate(xm, ym, xh, yh);
}
/**
* Dump out as a CSV
*/
public void printAsCSV() {
for (int i = 0; i < grid_.length; i++) {
for (int j = 0; j < grid_[0].length; j++) {
System.out.print(grid_[i][j]);
System.out.print(",");
}
System.out.println();
}
}
/**
* Convert to a Boolean array
*
* @return the boolean array
*/
public boolean[][] toBooleans() {
int w = grid_.length;
int h = grid_[0].length;
boolean[][] ret = new boolean[w][h];
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
ret[i][j] = grid_[i][j] < 0;
}
}
return ret;
}
public float[][] getGrid_() {
return grid_;
}
}`
But then I wanted to generate the next chunk so I thought I will have to add some kind of offset somewhere in the noise generation to create the float[][] that would be next to my initial Chunk. Each Chunk has an xID and a yID those tell the renderer where to draw the tiles that are in the chunk. So when the xID and the yID are 0 the tiles will draw from X = 0,Y = 0 till X = 3200, Y = 3200. (Each chunk is 100*100 tiles and each tile is 32*32 pixel). I think the Offset would be the location of the chunk I want to generate so when I want to generate Chunk(1, 0) it would be offsetX = (1 * 3200) and offsetY = (0 * 3200). But where should I add this values? I thought here at first but after re thinking it I saw that it would not work cause that wil cause an ArrayOutOfBounds Exception.
here: int xm = (xl + xh) / 2 + offsetX; int ym = (yl + yh) / 2 + offsetY;
Here are some of my notes (I know my handwriting is awful)ABCD is my initial chunk and BEDF is the chunk that I want to generate.
So can anyone explain to me how I am going to do this?
(English is not my main language).
Aucun commentaire:
Enregistrer un commentaire