summaryrefslogtreecommitdiff
path: root/src/RaceTrack.java
blob: 01a28c2bce137e964e610b35918ea160166989a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

import java.awt.Color;
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 extends BetterBase {
    /**
     * Half-width of the ellipse.
     */
    protected static final double ELLIPSE_A = 10;

    /**
     * Half-height of the ellipse.
     */
    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.
     */
    private Vector[] controlPointsOTrack;

    /**
     * Array with control points for the L-track.
     */
    private Vector[] controlPointsLTrack;

    /**
     * Array with control points for the C-track.
     */
    private Vector[] controlPointsCTrack;

    /**
     * Array with control points for the custom track.
     */
    private Vector[] controlPointsCustomTrack;
    private final RobotRace race;

    /**
     * Constructs the race track, sets up display lists.
     */
    public RaceTrack(RobotRace race) {
        this.race = race;
    }

    /**
     * Draws this track, based on the selected track number.
     */
    public void draw(int trackNr) {

        // The test track is selected
        if (0 == trackNr) {
            drawTestTrack();
        } else if (1 == trackNr) { // The O-track is selected
            // code goes here ...
        } else if (2 == trackNr) { // The L-track is selected
            // code goes here ...
        } else if (3 == trackNr) { // The C-track is selected
            // code goes here ...
        } else if (4 == trackNr) { // The custom track is selected
            // code goes here ...
        }
    }

    /**
     * Returns the position of the curve at 0 <= {@code t} <= 1.
     */
    public Vector getPoint(double t) {
        return new Vector(ELLIPSE_A * cos(2 * PI * t),
                          ELLIPSE_B * sin(2 * PI * t),
                          1);
    }

    /**
     * Returns the position of the curve at 0 &lt;= {@code t} &lt;= 1 and
     * the center of a lane at lane 1 &lt;= laneNo &lt;= (number of robots).
     */
    public Vector getPointForLane(double t, double laneNo) {
        Vector p = getPoint(t);
        Vector lanes_len = new Vector(p.x(), p.y(), 0).normalized().scale(laneNo + .5);
        return p.add(lanes_len);
    }

    /**
     * Returns the tangent of the curve at 0 <= {@code t} <= 1.
     */
    public Vector getTangent(double t) {
        /* 
         * Given a vector (-Y/B^2, X/A^2, 0) where X and Y are the coordinates 
         * of the point p on the ellipse. A is the HALFWIDTH of the ellipse and 
         * B is the HALFHEIGHT of the ellipse. 
         * 
         * Vector (X/A^2, Y/B^2, 0) is the normal vector through point p
         * and the center of the ellipse. Because the X and Y coordinates
         * are divided by the width and height of the ellipse, everything is
         * "normalized" to a circle. Hence a line through the origin and a point
         * p describes a normal vector for a point p on the ellipse. 
         * 
         * Since the dot product of the latter vector and the first (tangent) 
         * vector results in zero, we can say the normal vector is perpendicular
         * to the tangent vector. And because of that, the first vector 
         * 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),
                          0).normalized();
    }

    private void drawTestTrack() {
        /* A track segment looks like:
         *         B----------------------------D   "outside top"
         *       / :                           /|
         *     /   G- - - - - - - - - - - - -/--H   "outside bottom"
         *   /                            /
         * A----------------------------C  "inside top"
         * |                            |
         * E----------------------------F  "inside bottom"
         * ^-- 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 / SEGMENTS;
            Vector point_C = getPoint(t);
            // the outer side is located on the number of lanes (4) shifted from
            // the center to the side (minus 0.5).
            Vector point_D = getPointForLane(t, 3.5);
            // 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) {
                Vector norm_outside = new Vector(point_E.x(), point_E.y(), 0).normalized();
                Vector norm_inside = norm_outside.scale(-1).normalized();
                Vector norm_up = Vector.Z;

                // Set brick texture
                if (race.enableTextures) {
                    race.getBrickTexture().bind(gl);
                }

                // Draw track walls
                gl.glBegin(GL_QUADS);
                setColor(Color.RED);
                // inside bottom
                glNormal(norm_inside);
                gl.glTexCoord2f(0, 0);
                glVertex(point_E);
                gl.glTexCoord2f(1, 0);
                glVertex(point_F);
                setColor(Colors.PALE_TURQOISE);
                // inside top
                glNormal(norm_up.add(norm_inside).normalized());
                gl.glTexCoord2f(1, 1);
                glVertex(point_C);
                gl.glTexCoord2f(0, 1);
                glVertex(point_A);

                // outside bottom
                glNormal(norm_outside);
                gl.glTexCoord2f(0, 0);
                glVertex(point_G);
                gl.glTexCoord2f(1, 0);
                glVertex(point_H);
                // outside top
                glNormal(norm_up.add(norm_outside).normalized());
                gl.glTexCoord2f(1, 1);
                glVertex(point_D);
                gl.glTexCoord2f(0, 1);
                glVertex(point_B);
                gl.glEnd();

                if (race.enableTextures) {
                    race.getTrackTexture().bind(gl);
                }

                // Draw track itself
                gl.glBegin(GL_QUADS);
                glNormal(Vector.Z);
                gl.glTexCoord2f(0, 0);
                glVertex(point_A);
                gl.glTexCoord2f(1, 0);
                glVertex(point_C);
                gl.glTexCoord2f(1, 1);
                glVertex(point_D);
                gl.glTexCoord2f(0, 1);
                glVertex(point_B);
                gl.glEnd();
            }

            unbindTextures();

            // save points for next draw round
            point_E = point_F;
            point_A = point_C;
            point_B = point_D;
            point_G = point_H;
        }
    }
}