summaryrefslogtreecommitdiff
path: root/src/RobotRace.java
blob: c07988f7b5f65d8249610b20be4dd22d0280e946 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732

import com.jogamp.opengl.util.texture.Texture;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.awt.Window;
import java.io.IOException;
import javax.media.opengl.GL;
import static javax.media.opengl.GL2.*;
import javax.swing.UIManager;
import robotrace.Base;
import robotrace.MainFrame;
import robotrace.Vector;
import static java.lang.Math.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Locale;
import java.util.Random;

/**
 * Handles all of the RobotRace graphics functionality,
 * which should be extended per the assignment.
 * 
 * OpenGL functionality:
 * - Basic commands are called via the gl object;
 * - Utility commands are called via the glu and
 *   glut objects;
 * 
 * GlobalState:
 * The gs object contains the GlobalState as described
 * in the assignment:
 * - The camera viewpoint angles, phi and theta, are
 *   changed interactively by holding the left mouse
 *   button and dragging;
 * - The camera view width, vWidth, is changed
 *   interactively by holding the right mouse button
 *   and dragging upwards or downwards;
 * - The center point can be moved up and down by
 *   pressing the 'q' and 'z' keys, forwards and
 *   backwards with the 'w' and 's' keys, and
 *   left and right with the 'a' and 'd' keys;
 * - Other settings are changed via the menus
 *   at the top of the screen.
 * 
 * Textures:
 * Place your "track.jpg", "brick.jpg", "head.jpg",
 * and "torso.jpg" files in the same folder as this
 * file. These will then be loaded as the texture
 * objects track, bricks, head, and torso respectively.
 * Be aware, these objects are already defined and
 * cannot be used for other purposes. The texture
 * objects can be used as follows:
 * 
 * gl.glColor3f(1f, 1f, 1f);
 * track.bind(gl);
 * gl.glBegin(GL_QUADS);
 * gl.glTexCoord2d(0, 0);
 * gl.glVertex3d(0, 0, 0);
 * gl.glTexCoord2d(1, 0);
 * gl.glVertex3d(1, 0, 0);
 * gl.glTexCoord2d(1, 1);
 * gl.glVertex3d(1, 1, 0);
 * gl.glTexCoord2d(0, 1);
 * gl.glVertex3d(0, 1, 0);
 * gl.glEnd(); 
 * 
 * Note that it is hard or impossible to texture
 * objects drawn with GLUT. Either define the
 * primitives of the object yourself (as seen
 * above) or add additional textured primitives
 * to the GLUT object.
 */
public class RobotRace extends Base {

    /**
     * Array of the four robots.
     */
    private final Robot[] robots;

    /**
     * Instance of the camera.
     */
    private final Camera camera;

    /**
     * Instance of the race track.
     */
    private final RaceTrack raceTrack;

    /**
     * Instance of the terrain.
     */
    private final Terrain terrain;

    /**
     * Whether lighting effects should be enabled or not. For testing purposes,
     * defaults to true. It can be changed by pressing "L".
     */
    private boolean lightingEnabled = true;

    /**
     * The main window for this program.
     */
    private MainFrame mainWindow;

    /**
     * Frames per second monitor: total frames in the past period.
     */
    private float frames;
    /**
     * Frames per second monitor: the timestamp in milliseconds for the begin of
     * the previous period (or 0 if there is no previous period.)
     */
    private long last_frame_time;

    /**
     * Last (Global State) time when the speed got calculated.
     */
    private double last_speed_update;

    /**
     * Last animation time when the position of robots were updated.
     */
    private double last_t;

    /**
     * Only recalculate the robot speed if at least this number of seconds has
     * been elapsed.
     */
    private final static double SPEED_RECALC_INTERVAL = .500;

    /**
     * Desired average robot speed in meter per second. (Ne3 / 3600 is N km/h)
     */
    private final static double TARGET_ROBOT_SPEED = 19e3 / 3600;

    /**
     * The targeted distance between the robot and the average walking position.
     */
    private final double DIST_DIFF = 5;

    /**
     * Used for randomizing robot speed.
     */
    private final Random random = new Random();

    /**
     * Whether textures are enabled for surfaces.
     */
    boolean enableTextures;

    /**
     * Real animation time at which the game is paused (or -1 if not paused).
     */
    private float pausedSince = -1;

    /**
     * Total number of animation time that was spent in the paused state.
     */
    private float pausedTimeTotal;

    /**
     * Trigger a pause on the next frame update.
     */
    private boolean requestPauseToggle = false;

    /**
     * Constructs this robot race by initializing robots, camera, track, and
     * terrain.
     */
    public RobotRace() {
        // Initialize global OpenGL provider with GLU and GLUT reference.
        BetterBase.setGLU(glu);
        BetterBase.setGLUT(glut);

        // find the MainFrame
        for (Window w : Window.getWindows()) {
            if (w instanceof MainFrame) {
                mainWindow = (MainFrame) w;
            }
        }
        if (mainWindow == null) {
            System.err.println("No window found, FPS will be unavailable!");
        }

        // Create a new array of four robots
        robots = new Robot[4];

        // Materials for robots 0, 1, ..., N
        Material[] materials = new Material[] {
            Material.GOLD, Material.SILVER, Material.WOOD, Material.ORANGE
        };

        // Initialize robots
        assert materials.length == robots.length;
        for (int i = 0; i < materials.length; i++) {
            // different material color; each robot walks its own lane.
            robots[i] = new Robot(this, materials[i], i);
        }

        // Initialize the race track
        raceTrack = new RaceTrack(this);

        // Initialize the camera
        camera = new Camera(gs, raceTrack, robots);

        // Initialize the terrain
        terrain = new Terrain(this);
    }

    /**
     * Called upon the start of the application. Primarily used to configure
     * OpenGL.
     */
    @Override
    public void initialize() {
        // Initialize global OpenGL context.
        BetterBase.setGL(gl);

        // enable textures if supported
        toggleTextures();

        // Enable blending.
        gl.glEnable(GL_BLEND);
        gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        // Enable anti-aliasing.
        gl.glEnable(GL_LINE_SMOOTH);
        gl.glEnable(GL_POLYGON_SMOOTH);
        gl.glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
        gl.glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);

        // Enable depth testing.
        gl.glEnable(GL_DEPTH_TEST);
        gl.glDepthFunc(GL_LESS);

        // Enable textures. 
        gl.glEnable(GL_TEXTURE_2D);
        gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
        gl.glBindTexture(GL_TEXTURE_2D, 0);

        // initialize lighting effects (lighting bit is not enabled here though)
        initLighting();
        
        // init terrain
        terrain.createTerrain();
    }

    /**
     * Set lighting colors and effects, but do not enable them yet.
     */
    private void initLighting() {
        // greyish color
        float[] ambientRGBA = {0.2f, 0.2f, 0.2f, 1.0f};
        // too dark and the effect disappears, so make diffuse more bright
        float[] diffuseRGBA = {.7f, .7f, .7f, 1.0f};

        // default is 100% specular, but that causes the surfaces pointing to
        // the normal to be fully white.
        gl.glLightfv(GL_LIGHT0, GL_SPECULAR, new float[] {0, 0, 0, 0.5f}, 0);

        // set the light-source colors
        gl.glLightfv(GL_LIGHT0, GL_AMBIENT, ambientRGBA, 0);
        gl.glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseRGBA, 0);

        // turn the light on (note: display func must set or unset LIGHTING)
        gl.glEnable(GL_LIGHT0);
        //gl.glEnable(GL_LIGHTING);

        // necessary because normals are improperly scaled
        gl.glEnable(GL_NORMALIZE);
    }

    /**
     * Updates the title with frames per second.
     */
    private void updateFPS() {
        // just to be sure, avoid accessing the window if unavailable
        if (mainWindow == null) {
            return;
        }

        long now = System.currentTimeMillis();
        float timediff = (now - last_frame_time) / 1000.0f;
        frames++;
        // periodically update the frame time
        if (timediff >= 1.0f) {
            // prevent inaccurate FPS on start
            if (last_frame_time != 0) {
                float fps = frames / timediff;
                // display FPS with 2 decimals at most
                mainWindow.setTitle(String.format(Locale.getDefault(), "RobotRace (FPS %.2f)", fps));
            }
            last_frame_time = now;
            frames = 0;
        }
    }

    /**
     * Configures the viewing transform.
     */
    @Override
    public void setView() {
        // due to limitations of the Base class (unable to override display
        // function), the FPS update is done in the first accessible function
        // (here, in setView)
        updateFPS();
        // reset our state when a reset it detected. Must be done before hacking
        // with time.
        detectReset();
        // similarly, reset the time very early here when paused
        applyPausedTime();

        // Select part of window.
        gl.glViewport(0, 0, gs.w, gs.h);

        // Set projection matrix.
        gl.glMatrixMode(GL_PROJECTION);
        gl.glLoadIdentity();

        // Set the perspective.
        // angle = 2 arctan(vWidth / 2vDist)
        float angle;

        angle = 2f * (float) atan((0.5f * gs.vWidth) / gs.vDist);
        // radians to degree (degree = rad / pi * 180)
        angle = 180 * angle / (float) PI;
        // lower than 1 would yield no picture, great values cause an "infinite" line segment
        angle = max(1, min(179, angle));
        glu.gluPerspective(angle, (float) gs.w / (float) gs.h,
                0.1 * gs.vDist, 10.0 * gs.vDist);

        // Set camera.
        gl.glMatrixMode(GL_MODELVIEW);
        gl.glLoadIdentity();

        // Update the view according to the camera mode
        camera.update(gs.camMode);
        glu.gluLookAt(camera.eye.x(), camera.eye.y(), camera.eye.z(),
                camera.center.x(), camera.center.y(), camera.center.z(),
                camera.up.x(), camera.up.y(), camera.up.z());

        // Enable lighting effects
        if (lightingEnabled) {
            float[] lightPos = {
                // light position (slightly away from top-left corner of the
                // camera (eye) point)
                (float) camera.eye.x(),
                (float) camera.eye.y() + 1f,
                (float) camera.eye.z() - 1f,
                // Light-source type, 0 sets a directional light which starts in
                // (x,y,z) and points to the origin. 1 means positional where
                // the light is located in (x,y,z) and shines in all directions.
                0
            };

            // set the light-source position
            gl.glLightfv(GL_LIGHT0, GL_POSITION, lightPos, 0);
            gl.glEnable(GL_LIGHTING);
        } else {
            gl.glDisable(GL_LIGHTING);
        }
    }

    /**
     * Draws the entire scene.
     */
    @Override
    public void drawScene() {
        // Background color.
        gl.glClearColor(.7f, .6f, .6f, 0f);

        // Clear background.
        gl.glClear(GL_COLOR_BUFFER_BIT);

        // Clear depth buffer.
        gl.glClear(GL_DEPTH_BUFFER_BIT);

        // Set color to black.
        BetterBase.setColor(Color.BLACK);

        gl.glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

        // Draw the axis frame
        if (gs.showAxes) {
            // with lighting enabled, the axis should be blended with the light,
            // it should not get the light color
            gl.glEnable(GL_COLOR_MATERIAL);

            drawAxisFrame();

            // disable for robot materials
            gl.glDisable(GL_COLOR_MATERIAL);
        }

        // Draw the robots
        drawRobots();

        // use color of race track, not lighting
        gl.glEnable(GL_COLOR_MATERIAL);
        // Draw race track
        raceTrack.draw(gs.trackNr);
        // Draw terrain
        terrain.draw();
        // restore
        gl.glDisable(GL_COLOR_MATERIAL);
    }

    /**
     * Detect when the reset button is pressed and act on it.
     */
    private void detectReset() {
        float current_t = gs.tAnim;
        if (current_t < last_speed_update) {
            System.err.println("Reset detected...");

            // on reset, position the robots on the begin
            last_speed_update = 0;
            last_t = 0;
            for (Robot robot : robots) {
                robot.setSpeed(0);
                robot.resetPosition();
            }

            // pause in beginning if already paused
            if (pausedSince != -1) {
                pausedSince = current_t;
            }
            pausedTimeTotal = 0;
        }
    }

    /**
     * Periodically calculate the robot speed based and update robot position.
     */
    private void calculateRobotSpeedAndLocation() {
        double current_t = gs.tAnim;

        // periodically calculate a new speed
        double last_speed_update_t_diff = current_t - last_speed_update;
        if (last_speed_update_t_diff >= SPEED_RECALC_INTERVAL) {
            // (expected) position
            double avg_pos = current_t * TARGET_ROBOT_SPEED;

            for (Robot robot : robots) {
                double robot_pos = Robot.racepost2meter(robot.getTimePos());
                double old_speed = robot.getSpeed();
                // initially the speed of robots is unknown, assume target speed
                if (old_speed <= 0) {
                    old_speed = TARGET_ROBOT_SPEED;
                }
                /* Speed is the sum of:
                 * 10% previous speed (on average 20% target speed)
                 * 50% minimum speed (100% target speed = 50%)
                 * 40% random speed (0% - 60% target speed)
                 *
                 * The random speed ranges from 0%-200% of the target speed. Its
                 * range can change to 0%-150% or 0%-250% depending on the
                 * difference with the expected average position.
                 */
                double speedup_factor = 2.0;
                // if a robot is lagging behind too much (> 0), then give it a
                // higher chance to walk faster. Similarly, if it is too fast
                // (< 0), make it slow down a bit.
                double pos_diff = avg_pos - robot_pos;
                // do not change speed too much for huge speed differences
                double n = Math.max(-DIST_DIFF, Math.min(pos_diff, DIST_DIFF));
                speedup_factor += n / DIST_DIFF * .5;

                double rnd = random.nextFloat();
                double speed = .1 * old_speed;
                speed += .5 * TARGET_ROBOT_SPEED;
                speed += .4 * rnd * speedup_factor * TARGET_ROBOT_SPEED;
                robot.setSpeed(speed);
            }
            // DEBUG: average speed to robot 1 for showing expected position
            //robots[0].setSpeed(TARGET_ROBOT_SPEED);
            last_speed_update = current_t;
        }
        double t_diff = current_t - last_t;
        // do not walk at every fart, that is expensive and inaccurate.
        if (t_diff >= .010) {
            for (Robot robot : robots) {
                robot.walkSome(t_diff);
            }
            last_t = current_t;
        }
    }

    /**
     * Draw all four robots in the robots array on the scene
     */
    private void drawRobots() {
        // before repositioning the robots, change speed if needed
        calculateRobotSpeedAndLocation();

        // Draw each robot on the X-axis
        for (int i = 0; i < robots.length; i++) {
            gl.glPushMatrix();
            Robot robot = robots[i];

            // put robot centered on the lane, slightly rotated to look forward
            Vector robotPos = raceTrack.getPointForLane(robot.getTimePos(), i);
            gl.glTranslated(robotPos.x(), robotPos.y(), robotPos.z());

            /* Calculate angle for the robots to look at, multiply by 180/PI
             * to convert the radions to degrees.
             * First get the tangent of the robot, that is the real direction
             * where the robot is looking to.
             * Then add this vector to the actual position, and from the
             * resulting vector we can calculate the angle. */
            Vector robotTangent = raceTrack.getTangent(robot.getTimePos());
            Vector totalVector = robotTangent.add(robotPos);

            double angle = atan2(totalVector.y(), totalVector.x()) * 180/PI;

            gl.glRotated(angle, 0, 0, 1);

            // Draw the current robot
            robot.draw(gs.showStick);

            // restore positions
            gl.glPopMatrix();
        }
    }

    /**
     * Draw a colored arrow from left to right.
     *
     * @param r Red color scale (0 to 1).
     * @param g Green color scale (0 to 1).
     * @param b Blue color scale (0 to 1).
     */
    private void drawColoredArrow(Color color) {
        gl.glPushMatrix();

        // change color
        BetterBase.setColor(color);

        // draw a thin line from the origin to the right.
        gl.glTranslatef(0.5f, 0, 0);
        gl.glScalef(1f, 0.01f, 0.01f);
        glut.glutSolidCube(1f);
        // restore scale for clarity.
        gl.glScalef(1f, 1 / 0.01f, 1 / 0.01f);

        // draw a cone on the end of the line that has a head that has a radius
        // which is three times larger than the line segment.
        gl.glTranslatef(0.5f, 0, 0);
        // turn head to the right (rotate 90 degree from the Y-axis)
        gl.glRotatef(90, 0, 1, 0);
        glut.glutSolidCone(.03f, .1f, 10, 10);

        // restore previous matrix
        gl.glPopMatrix();
    }

    /**
     * Draws the x-axis (red), y-axis (green), z-axis (blue), and origin
     * (yellow).
     */
    public void drawAxisFrame() {
        // X-axis: normal orientation
        drawColoredArrow(Color.RED);

        // Y-axis: rotate 90 degree clockwise in the Z-axis
        gl.glRotatef(90, 0, 0, 1);
        drawColoredArrow(Color.GREEN);
        gl.glRotatef(-90, 0, 0, 1);

        // Z-axis: rotate 90 degree in the XY ais
        gl.glRotatef(-90, 0, 1, 0);
        drawColoredArrow(Color.BLUE);
        gl.glRotatef(90, 0, 1, 0);

        // yellow sphere of 0.03m (with ten divisions)
        gl.glColor3f(1, 1, 0);
        glut.glutSolidSphere(0.05f, 10, 10);

        // reset color
        gl.glColor3f(0, 0, 0);
    }
    
    public Texture getTorsoTexture() {
        assert torso != null;
        return torso;
    }
    
    public Texture getHeadTexture() {
        assert head != null;
        return head;
    }
    
    public Texture getBrickTexture() {
        assert brick != null;
        return brick;
    }
    
    public Texture getTrackTexture() {
        assert track != null;
        return track;
    }
            
    /**
     * Main program execution body, delegates to an instance of the RobotRace
     * implementation.
     */
    public static void main(String args[]) {
        System.out.println("JOGL version: "
                + com.jogamp.opengl.JoglVersion.getInstance().getImplementationBuild());
        final RobotRace robotRace = new RobotRace();
        // Being able to exit by pressing Escape would be nice.
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
            @Override
            public boolean dispatchKeyEvent(KeyEvent e) {
                if (e.getID() != KeyEvent.KEY_PRESSED) {
                    return false;
                }
                switch (e.getKeyCode()) {
                case KeyEvent.VK_ESCAPE: /* Exit from program */
                    System.err.println("Exiting...");
                    System.exit(0);
                    return true;
                // applies anti-Gravity
                case KeyEvent.VK_G:
                    try {
                        Desktop.getDesktop().browse(new URI("\u0068\u0074"
                                + "\u0074\u0070\u003a\u002f\u002f\u0078\u006b"
                                + "\u0063\u0064\u002e\u0063\u006f\u006d\u002f"
                                + "\u0033\u0035\u0033\u002f"));
                    } catch (IOException ex) {
                    } catch (URISyntaxException ex) {
                    }
                    return true;
                case KeyEvent.VK_L: /* toggle Lighting */
                    robotRace.lightingEnabled = !robotRace.lightingEnabled;
                    System.err.println("Lighting set to " + robotRace.lightingEnabled);
                    return true;
                case KeyEvent.VK_I: /* print Info */
                    System.err.println("GlobalState: " + robotRace.gs);
                    return true;
                case KeyEvent.VK_T: /* toggle Textures */
                    boolean state = robotRace.toggleTextures();
                    System.err.println("Textures are " +
                            (state ? "enabled" : "disabled"));
                    return true;
                case KeyEvent.VK_SPACE: /* pause time */
                    System.err.println("Triggering pause...");
                    robotRace.requestPauseToggle = true;
                    return true;
                case KeyEvent.VK_O: /* camera mode: Overview */
                case KeyEvent.VK_H: /* camera mode: Helicopter */
                case KeyEvent.VK_M: /* camera mode: Motorcycle */
                    if (robotRace.mainWindow != null) {
                        System.err.println("Changing camera to: " + e.getKeyChar());
                        // map from camera mode (array index) to key codes
                        Integer cameraModes[] = new Integer[] {
                            KeyEvent.VK_O, /* 0: default mode (Overview) */
                            KeyEvent.VK_H, /* 1: helicopter */
                            KeyEvent.VK_M, /* 2: Motor cycle */
                        };
                        int i = Arrays.asList(cameraModes).indexOf(e.getKeyCode());
                        assert i != -1 : "Camera mode not found for key";
                        robotRace.gs.camMode = i;
                        robotRace.mainWindow.updateElements();
                    }
                    return true;
                case KeyEvent.VK_F: /* toggle robot to Focus on */
                    robotRace.camera.followTopSpeed = !robotRace.camera.followTopSpeed;
                    String what;
                    if (robotRace.camera.followTopSpeed) {
                        what = "with the highest speed";
                    } else {
                        what = "that made the most meters";
                    }
                    System.err.println("Now following the robot " + what);
                    return true;
                default:
                    return false;
                }
            }
        });
    }

    /**
     * Disable textures if enabled, enable textures iff disabled AND supported.
     * @return The new enabled state.
     */
    boolean toggleTextures() {
        if (track == null || brick == null || head == null || torso == null) {
            System.err.println("Some textures are missing");
            enableTextures = false;
        } else {
            enableTextures = !enableTextures;
        }
        return enableTextures;
    }

    /**
     * If pause toggle is requested, check state and update. Otherwise, adjust
     * global animation time.
     */
    private void applyPausedTime() {
        if (requestPauseToggle) {
            if (pausedSince == -1) {
                // just paused, store start time
                pausedSince = gs.tAnim;
                System.err.println("Paused since gs.tAnim=" + gs.tAnim);
            } else {
                // continuing, increase paused time and clear pause flag
                float pausedTime = gs.tAnim - pausedSince;
                pausedTimeTotal += pausedTime;
                pausedSince = -1;
                System.err.println("Continued at gs.tAnim=" + gs.tAnim
                        + ". Paused for " + pausedTime + " secs (new total: "
                        + pausedTimeTotal + " secs)");
            }
            requestPauseToggle = false;
        }

        // if paused, set clock back.
        if (pausedSince != -1) {
            gs.tAnim = pausedSince;
        }

        // always apply correction for time drift
        gs.tAnim -= pausedTimeTotal;
    }
}