diff options
Diffstat (limited to 'src/RaceTrack.java')
-rw-r--r-- | src/RaceTrack.java | 70 |
1 files changed, 61 insertions, 9 deletions
diff --git a/src/RaceTrack.java b/src/RaceTrack.java index 78e6e89..d40c529 100644 --- a/src/RaceTrack.java +++ b/src/RaceTrack.java @@ -1,20 +1,26 @@ import robotrace.Vector; import static java.lang.Math.*; +import static javax.media.opengl.GL2.*; /** * Implementation of a race track that is made from Bezier segments. */ -class RaceTrack { +class RaceTrack extends BetterBase { /** - * Halfwidth of the ellipse. + * Half-width of the ellipse. */ - protected static final int ELLIPSE_A = 10; + protected static final double ELLIPSE_A = 10; /** - * Halfheight of the ellipse. + * Half-height of the ellipse. */ - protected static final int ELLIPSE_B = 14; + protected static final double ELLIPSE_B = 14; + + /** + * Number of segments for the race track. + */ + private final double SEGMENTS = 180; /** * Array with control points for the O-track. @@ -52,7 +58,7 @@ class RaceTrack { // The test track is selected if (0 == trackNr) { - // code goes here ... + drawTestTrack(); } else if (1 == trackNr) { // The O-track is selected // code goes here ... } else if (2 == trackNr) { // The L-track is selected @@ -94,9 +100,55 @@ class RaceTrack { * describes the tangent vector of point p. */ Vector p = getPoint(t); - - return new Vector(-p.y()/(ELLIPSE_A * ELLIPSE_A), - p.x()/(ELLIPSE_B * ELLIPSE_B), + return new Vector(-p.y() / (ELLIPSE_A * ELLIPSE_A), + p.x() / (ELLIPSE_B * ELLIPSE_B), 0).normalized(); } + + private void drawTestTrack() { + /* A track segment looks like: + * B----------------------------D + * / : /| + * / G- - - - - - - - - - - - -/--H + * / / + * A----------------------------C + * | | + * E----------------------------F + * ^-- t = t0 ^-- t = t0 + 1 + * Assume point A the inner point of the race track. Draw quads from + * EF (starting point) to AC, BD, GH. + */ + // previous points + Vector point_A = null, point_B = null, point_E = null, point_G = null; + + for (double i = 0; i < SEGMENTS; ++i) { + double t = i * 2 * PI / SEGMENTS; + Vector point_C = getPoint(t); + Vector lanes_len = getTangent(t).cross(point_C).normalized().scale(4); + Vector point_D = point_C.add(lanes_len); + // Z=1 to Z=-1 + Vector point_F = point_C.subtract(new Vector(0, 0, 2)); + Vector point_H = point_D.subtract(new Vector(0, 0, 2)); + + // initially, there are no "previous" vectors to use as start. + if (i > 0) { + gl.glBegin(GL_QUAD_STRIP); + glVertex(point_E); + glVertex(point_F); + glVertex(point_A); + glVertex(point_C); + glVertex(point_B); + glVertex(point_D); + glVertex(point_G); + glVertex(point_H); + gl.glEnd(); + } + + // save points for next draw round + point_E = point_F; + point_A = point_C; + point_B = point_D; + point_G = point_H; + } + } } |