protected void addFault(float[][] tempBuffer, Random random) { float faultHeight = minFaultHeight + random.nextFloat() * (maxFaultHeight - minFaultHeight); float range = minRange + random.nextFloat() * (maxRange - minRange); switch (faultShape) { case FAULTSHAPE_LINE: addLineFault(tempBuffer, random, faultHeight, range); break; case FAULTSHAPE_CIRCLE: addCircleFault(tempBuffer, random, faultHeight, range); break; } }
@Override public boolean load() { // clean up data if needed. if (null != heightData) { unloadHeightMap(); } heightData = new float[size * size]; float[][] tempBuffer = new float[size][size]; Random random = new Random(seed); for (int i = 0; i < iterations; i++) { addFault(tempBuffer, random); } for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { setHeightAtPoint(tempBuffer[i][j], i, j); } } normalizeTerrain(NORMALIZE_RANGE); logger.log(Level.FINE, "Fault heightmap generated"); return true; }
protected void addLineFault(float[][] tempBuffer, Random random, float faultHeight, float range) { int x1 = random.nextInt(size); int x2 = random.nextInt(size); int y1 = random.nextInt(size); int y2 = random.nextInt(size); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { float dist = ((x2 - x1) * (j - y1) - (y2 - y1) * (i - x1)) / (FastMath.sqrt(FastMath.sqr(x2 - x1) + FastMath.sqr(y2 - y1))); tempBuffer[i][j] += calcHeight(dist, random, faultHeight, range); } } }
this.minRadius = size / 10; this.maxRadius = size / 4; load();
this.minRadius = size / 10; this.maxRadius = size / 4; load();
@Override public boolean load() { // clean up data if needed. if (null != heightData) { unloadHeightMap(); } heightData = new float[size * size]; float[][] tempBuffer = new float[size][size]; Random random = new Random(seed); for (int i = 0; i < iterations; i++) { addFault(tempBuffer, random); } for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { setHeightAtPoint(tempBuffer[i][j], i, j); } } normalizeTerrain(NORMALIZE_RANGE); logger.log(Level.FINE, "Fault heightmap generated"); return true; }
protected void addFault(float[][] tempBuffer, Random random) { float faultHeight = minFaultHeight + random.nextFloat() * (maxFaultHeight - minFaultHeight); float range = minRange + random.nextFloat() * (maxRange - minRange); switch (faultShape) { case FAULTSHAPE_LINE: addLineFault(tempBuffer, random, faultHeight, range); break; case FAULTSHAPE_CIRCLE: addCircleFault(tempBuffer, random, faultHeight, range); break; } }
protected void addCircleFault(float[][] tempBuffer, Random random, float faultHeight, float range) { float radius = random.nextFloat() * (maxRadius - minRadius) + minRadius; int intRadius = (int) FastMath.floor(radius); // Allox circle center to be out of map if not by more than radius. // Unlucky cases will put them in the far corner, with the circle // entirely outside heightmap int x = random.nextInt(size + 2 * intRadius) - intRadius; int y = random.nextInt(size + 2 * intRadius) - intRadius; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { float dist; if (i != x || j != y) { int dx = i - x; int dy = j - y; float dmag = FastMath.sqrt(FastMath.sqr(dx) + FastMath.sqr(dy)); float rx = x + dx / dmag * radius; float ry = y + dy / dmag * radius; dist = FastMath.sign(dmag - radius) * FastMath.sqrt(FastMath.sqr(i - rx) + FastMath.sqr(j - ry)); } else { dist = 0; } tempBuffer[i][j] += calcHeight(dist, random, faultHeight, range); } } }
protected void addLineFault(float[][] tempBuffer, Random random, float faultHeight, float range) { int x1 = random.nextInt(size); int x2 = random.nextInt(size); int y1 = random.nextInt(size); int y2 = random.nextInt(size); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { float dist = ((x2 - x1) * (j - y1) - (y2 - y1) * (i - x1)) / (FastMath.sqrt(FastMath.sqr(x2 - x1) + FastMath.sqr(y2 - y1))); tempBuffer[i][j] += calcHeight(dist, random, faultHeight, range); } } }
protected void addCircleFault(float[][] tempBuffer, Random random, float faultHeight, float range) { float radius = random.nextFloat() * (maxRadius - minRadius) + minRadius; int intRadius = (int) FastMath.floor(radius); // Allox circle center to be out of map if not by more than radius. // Unlucky cases will put them in the far corner, with the circle // entirely outside heightmap int x = random.nextInt(size + 2 * intRadius) - intRadius; int y = random.nextInt(size + 2 * intRadius) - intRadius; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { float dist; if (i != x || j != y) { int dx = i - x; int dy = j - y; float dmag = FastMath.sqrt(FastMath.sqr(dx) + FastMath.sqr(dy)); float rx = x + dx / dmag * radius; float ry = y + dy / dmag * radius; dist = FastMath.sign(dmag - radius) * FastMath.sqrt(FastMath.sqr(i - rx) + FastMath.sqr(j - ry)); } else { dist = 0; } tempBuffer[i][j] += calcHeight(dist, random, faultHeight, range); } } }