Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.syscy.engine.world;
- import de.syscy.engine.components.GameComponent;
- import de.syscy.engine.core.Vector2f;
- import de.syscy.engine.core.Vector3f;
- public class HeightMap extends GameComponent {
- public static final int WIDTH = Chunk.WIDTH;
- public static final int LENGTH = Chunk.LENGTH;
- float[][] height;
- private Noise noise;
- private float noiseFactor = 60;
- public HeightMap(Vector3f pos, Noise noise, Vector2f cID, float noiseFactor) {
- setName("heightMap" + cID.getX() + "," + cID.getY());
- this.noise = noise;
- this.noiseFactor = noiseFactor;
- try {
- height = new float[WIDTH][LENGTH];
- for (int x = (int) pos.getX(); x < WIDTH + pos.getX(); x++) {
- float noiseX = (float) x / noiseFactor;
- for (int z = (int) pos.getZ(); z < LENGTH + pos.getZ(); z++) {
- float noiseZ = (float) z / noiseFactor;
- float noiseValue = (float) noise.noise(noiseX, noiseZ);
- height[(int) (x - pos.getX())][(int) (z - pos.getZ())] = noiseValue;
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public float getHeightAt(int x, int z) {
- try {
- return height[x][z];
- } catch (Exception e) {
- float noiseX = (float) x / noiseFactor;
- float noiseZ = (float) z / noiseFactor;
- float noiseValue = (float) noise.noise(noiseX, noiseZ);
- return noiseValue;
- }
- }
- public float calculateHeight(float x, float z) {
- float x1, x2, y1, y2, Q11, Q12, Q21, Q22;
- x1 = (int) Math.floor(x);
- x2 = (int) Math.floor(x + 1);
- y1 = (int) Math.floor(z);
- y2 = (int) Math.floor(z + 1);
- Q11 = getHeightAt((int) x1, (int) y1);
- Q12 = getHeightAt((int) x1, (int) y2);
- Q21 = getHeightAt((int) x2, (int) y1);
- Q22 = getHeightAt((int) x2, (int) y2);
- return (1f / ((x2 - x1) * (y2 - y1)))
- * (Q11 * (x2 - x) * (y2 - z) + Q21 * (x - x1) * (y2 - z) + Q12
- * (x2 - x) * (z - y1) + Q22 * (x - x1) * (z - y1));
- }
- public float calculateHeightRounded(float x, float z) {
- return getHeightAt(Math.round(x), Math.round(z));
- }
- public void dispose() {
- height = new float[1][1];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment