summaryrefslogtreecommitdiff
path: root/src/RobotRace.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/RobotRace.java')
-rw-r--r--src/RobotRace.java84
1 files changed, 66 insertions, 18 deletions
diff --git a/src/RobotRace.java b/src/RobotRace.java
index fb4ab1e..aeddfba 100644
--- a/src/RobotRace.java
+++ b/src/RobotRace.java
@@ -114,6 +114,27 @@ public class RobotRace extends Base {
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.
+ */
+ private final static double TARGET_ROBOT_SPEED = .03;
+
+ /**
* Constructs this robot race by initializing robots, camera, track, and
* terrain.
*/
@@ -151,12 +172,12 @@ public class RobotRace extends Base {
robots[3] = new Robot(Material.ORANGE
/* add other parameters that characterize this robot */);
- // Initialize the camera
- camera = new Camera(gs);
-
// Initialize the race track
raceTrack = new RaceTrack();
+ // Initialize the camera
+ camera = new Camera(gs, raceTrack, robots);
+
// Initialize the terrain
terrain = new Terrain();
}
@@ -350,30 +371,57 @@ public class RobotRace extends Base {
}
/**
+ * Periodically calculate the robot speed based and update robot position.
+ */
+ private void calculateRobotSpeedAndLocation() {
+ double current_t = gs.tAnim;
+ double last_speed_update_t_diff = current_t - last_speed_update;
+ if (last_speed_update_t_diff >= SPEED_RECALC_INTERVAL) {
+ for (Robot robot : robots) {
+ // smaller than 0 means that robot is faster than usual, equal
+ // to 0 means robot is matching expected and great means that
+ // we have a turtle.
+ double jitter = current_t - robot.getTimePos();
+ // TODO: do not use static speed
+ robot.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() {
- // laziness, save current positions because of translations
- gl.glPushMatrix();
-
- // Place the robots on a line based on the current robot. Robots are
- // drawn with a distance of 1 meter between the middle of each robot,
- // from left to right (seen from the robot's POV). First, set the
- // starting point in a way such that the robot is drawn left of the
- // axis (for an even number of robots).
- gl.glTranslatef(.5f - robots.length / 2, 0f, 0f);
+ // before repositioning the robots, change speed if needed
+ calculateRobotSpeedAndLocation();
// Draw each robot on the X-axis
- for (Robot robot: robots) {
+ for (int i = 0; i < robots.length; i++) {
+ gl.glPushMatrix();
+ Robot robot = robots[i];
+
+ // put robot on the lane, slightly rotated to look "forward"
+ Vector robotPos = raceTrack.getPointForLane(robot.getTimePos(), i);
+ gl.glTranslated(robotPos.x(), robotPos.y(), robotPos.z());
+ // FIXME: robot looks in wrong direction.
+ double angle = atan2(robotPos.y(), robotPos.x());
+ gl.glRotated(angle, 0, 0, 1);
+
// Draw the current robot
robot.draw(gs.showStick);
- // Position the next robot 1 meter away from the current one.
- gl.glTranslatef(1f, 0f, 0f);
+ // restore positions
+ gl.glPopMatrix();
}
-
- // restore positions
- gl.glPopMatrix();
}
/**