_-Katsu-_

HeightMap.java

Sep 2nd, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1. package de.syscy.engine.world;
  2.  
  3. import de.syscy.engine.components.GameComponent;
  4. import de.syscy.engine.core.Vector2f;
  5. import de.syscy.engine.core.Vector3f;
  6.  
  7. public class HeightMap extends GameComponent {
  8.     public static final int WIDTH = Chunk.WIDTH;
  9.     public static final int LENGTH = Chunk.LENGTH;
  10.     float[][] height;
  11.    
  12.     private Noise noise;   
  13.     private float noiseFactor = 60;
  14.  
  15.     public HeightMap(Vector3f pos, Noise noise, Vector2f cID, float noiseFactor) {
  16.         setName("heightMap" + cID.getX() + "," + cID.getY());
  17.        
  18.         this.noise = noise;
  19.         this.noiseFactor = noiseFactor;
  20.        
  21.         try {
  22.             height = new float[WIDTH][LENGTH];
  23.             for (int x = (int) pos.getX(); x < WIDTH + pos.getX(); x++) {
  24.                 float noiseX = (float) x / noiseFactor;
  25.                 for (int z = (int) pos.getZ(); z < LENGTH + pos.getZ(); z++) {
  26.                     float noiseZ = (float) z / noiseFactor;
  27.                     float noiseValue = (float) noise.noise(noiseX, noiseZ);
  28.                     height[(int) (x - pos.getX())][(int) (z - pos.getZ())] = noiseValue;
  29.                 }
  30.             }
  31.         } catch (Exception e) {
  32.             e.printStackTrace();
  33.         }
  34.     }
  35.  
  36.     public float getHeightAt(int x, int z) {
  37.         try {
  38.             return height[x][z];
  39.         } catch (Exception e) {
  40.             float noiseX = (float) x / noiseFactor;
  41.             float noiseZ = (float) z / noiseFactor;
  42.             float noiseValue = (float) noise.noise(noiseX, noiseZ);
  43.             return noiseValue;
  44.         }
  45.     }
  46.  
  47.     public float calculateHeight(float x, float z) {
  48.         float x1, x2, y1, y2, Q11, Q12, Q21, Q22;
  49.         x1 = (int) Math.floor(x);
  50.         x2 = (int) Math.floor(x + 1);
  51.         y1 = (int) Math.floor(z);
  52.         y2 = (int) Math.floor(z + 1);
  53.  
  54.         Q11 = getHeightAt((int) x1, (int) y1);
  55.         Q12 = getHeightAt((int) x1, (int) y2);
  56.         Q21 = getHeightAt((int) x2, (int) y1);
  57.         Q22 = getHeightAt((int) x2, (int) y2);
  58.  
  59.         return (1f / ((x2 - x1) * (y2 - y1)))
  60.                 * (Q11 * (x2 - x) * (y2 - z) + Q21 * (x - x1) * (y2 - z) + Q12
  61.                         * (x2 - x) * (z - y1) + Q22 * (x - x1) * (z - y1));
  62.     }
  63.  
  64.     public float calculateHeightRounded(float x, float z) {
  65.         return getHeightAt(Math.round(x), Math.round(z));
  66.     }
  67.  
  68.     public void dispose() {
  69.         height = new float[1][1];
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment