diff options
author | Frank v/d Haterd <f.h.a.v.d.haterd@student.tue.nl> | 2014-01-16 23:32:00 +0100 |
---|---|---|
committer | Frank v/d Haterd <f.h.a.v.d.haterd@student.tue.nl> | 2014-01-16 23:32:00 +0100 |
commit | c5bb0d2aa600d838b301461fe6968c5e6ff66037 (patch) | |
tree | de1b3e3db109907ce67c65dbaa5a63c2198890f2 | |
parent | 39f1b2e104d1180e0444f0873982d5e71ab0cb8a (diff) | |
download | 2iv60-robots-c5bb0d2aa600d838b301461fe6968c5e6ff66037.tar.gz |
Trees are now positioned away from the track.
Small edit in robotrace.java for racetrack funtionallity in terrain.
-rw-r--r-- | src/Terrain.java | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/src/Terrain.java b/src/Terrain.java index 6a26e17..54c6d2a 100644 --- a/src/Terrain.java +++ b/src/Terrain.java @@ -5,6 +5,7 @@ import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.util.Arrays; import java.util.Random; import java.util.logging.Level; import java.util.logging.Logger; @@ -17,7 +18,7 @@ import robotrace.Vector; /** * Implementation of the terrain. */ -class Terrain extends BetterBase { +public class Terrain extends BetterBase { /** * The display list containing the terrain. @@ -48,10 +49,12 @@ class Terrain extends BetterBase { private boolean heightMapFileMode; + private RaceTrack track; + /** * Can be used to set up a display list. */ - public Terrain(RobotRace race) { + public Terrain(RobotRace race, RaceTrack track) { // Setup terrain variables this.race = race; this.terrainDisplayList = 0; @@ -61,7 +64,8 @@ class Terrain extends BetterBase { this.terrainTrees = new Tree[10]; this.heightMapFile = "heightmap.bmp"; this.heightMapFileMode = true; - + this.track = track; + // Fill the height map array File f = new File("src/" + heightMapFile); if(f.exists()) { @@ -131,8 +135,17 @@ class Terrain extends BetterBase { */ private Vector getRandomPosition() { Random r = new Random(); - int x = r.nextInt(37) - 18; - int y = r.nextInt(37) - 18; + double distance; + int x, y; + + // Make sure all trees are moved away from the track + do { + x = 19 - r.nextInt(39); + y = 19 - r.nextInt(39); + + distance = Math.sqrt(x * x + y * y); + } while (distance < 19); + return new Vector(x, y, heightAt(x + 20, y + 20)); } @@ -157,6 +170,10 @@ class Terrain extends BetterBase { if (Math.abs(tree.getPosition().y() - y) < marge) { return false; } + + if(x < - 19 || x > 19 || y < - 19 || y > 19) { + return false; + } // No trees allowed in water if (z < 0.25f) { @@ -268,7 +285,7 @@ class Terrain extends BetterBase { System.out.println("Terrain created"); } - + /** * Fills the array with new valid trees. */ |