summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2013-12-20 12:31:14 +0100
committerPeter Wu <lekensteyn@gmail.com>2013-12-20 12:31:14 +0100
commit6b8a19646b51ed3c19e52607a120025ff395abf1 (patch)
treecc9fefc0147d6697609a422a881ddc77874f672a
parent6e1bd7b66143f960aed18a32a91c604d3786a154 (diff)
download2iv60-robots-6b8a19646b51ed3c19e52607a120025ff395abf1.tar.gz
Center on fastest robot instead of the middle of lane
Refactor Robots initialization to remove redundancy and add lane number. getPointForLane now returns the center of a lane, change the callers to reflect that.
-rw-r--r--src/Camera.java49
-rw-r--r--src/RaceTrack.java8
-rw-r--r--src/Robot.java23
-rw-r--r--src/RobotRace.java28
4 files changed, 75 insertions, 33 deletions
diff --git a/src/Camera.java b/src/Camera.java
index a7c2300..57c6d70 100644
--- a/src/Camera.java
+++ b/src/Camera.java
@@ -103,23 +103,16 @@ class Camera {
private void setHelicopterMode() {
/**
* In the Helicopter view, the camera (eye point) is located above the
- * robots. Take the average time of the robots and use that to determine
- * the position and direction.
+ * robots.
*/
- double time_sum = 0;
- for (Robot robot : robots) {
- time_sum += robot.getTimePos();
- }
- // for now stay in the middle of the robots, later we could follow the
- // fastest (or slowest) robot if desired.
- double time_avg = time_sum / robots.length;
+ Robot focus = getFocusedRobot();
- // center at the center lane
- center = track.getPointForLane(time_avg, robots.length / 2 + .5);
+ // center at the chosen robot.
+ center = track.getPointForLane(focus.getTimePos(), focus.getLane());
// look in the direction where the robots walks, namely the tangent
// FIXME: getTangent() is broken, or this snippet is wrong.
- up = track.getTangent(time_avg);
+ up = track.getTangent(focus.getTimePos());
// "above" is 10 meters.
eye = center.add(new Vector(0, 0, 10f));
@@ -130,7 +123,22 @@ class Camera {
* on the motorcycle mode.
*/
private void setMotorCycleMode() {
- // code goes here ...
+ /**
+ * In the Motor Cycle view, the camera is at the side of a track,
+ * following the robots.
+ */
+ Robot focus = getFocusedRobot();
+
+ // Center at the focused robot.
+ center = track.getPointForLane(focus.getTimePos(), focus.getLane());
+
+ // We are looking at the robot from the side.
+ up = Vector.Z;
+
+ // look at a distance of 10 meters from the center of the first lane
+ eye = track.getPointForLane(focus.getTimePos(), 10);
+ // assume that the motor camera is one meter above the race track.
+ eye = eye.add(new Vector(0, 0, 1f));
}
/**
@@ -140,4 +148,19 @@ class Camera {
private void setFirstPersonMode() {
// code goes here ...
}
+
+ /**
+ * Returns the robot on which the camera is focused.
+ */
+ private Robot getFocusedRobot() {
+ Robot selected = robots[0];
+ for (Robot robot : robots) {
+ // Many possibilities here, fastest, slowest, loser, winner...
+ if (selected.getSpeed() < robot.getSpeed()) {
+ // select the fastest accelerating robot.
+ selected = robot;
+ }
+ }
+ return selected;
+ }
}
diff --git a/src/RaceTrack.java b/src/RaceTrack.java
index 630e58a..d2bcaa5 100644
--- a/src/RaceTrack.java
+++ b/src/RaceTrack.java
@@ -82,11 +82,11 @@ class RaceTrack extends BetterBase {
/**
* Returns the position of the curve at 0 &lt;= {@code t} &lt;= 1 and
- * 1 &lt;= laneNo &lt;= (number of robots).
+ * 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);
+ Vector lanes_len = new Vector(p.x(), p.y(), 0).normalized().scale(laneNo + .5);
return p.add(lanes_len);
}
@@ -135,7 +135,9 @@ class RaceTrack extends BetterBase {
for (double i = 0; i <= SEGMENTS; ++i) {
double t = i / SEGMENTS;
Vector point_C = getPoint(t);
- Vector point_D = getPointForLane(t, 4);
+ // 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));
diff --git a/src/Robot.java b/src/Robot.java
index ba90dea..043cc1c 100644
--- a/src/Robot.java
+++ b/src/Robot.java
@@ -46,9 +46,15 @@ class Robot extends BetterBase {
private double robot_time_pos;
/**
+ * The lane number on which this robot is positioned. Must be a positive
+ * number greater or equal to zero.
+ */
+ private final int laneNo;
+
+ /**
* Constructs the robot with initial parameters.
*/
- public Robot(Material material) {
+ public Robot(Material material, int laneNo) {
/* Set all parameters of the robot */
this.material = material;
this.torsoHeight = 0.6f;
@@ -65,6 +71,7 @@ class Robot extends BetterBase {
this.footLength = 2 * legWidth;
this.boneSize = 0.02f;
this.depth = 0.24f;
+ this.laneNo = laneNo;
}
/**
@@ -339,10 +346,24 @@ class Robot extends BetterBase {
}
/**
+ * Gets the speed of this robot.
+ */
+ public double getSpeed() {
+ return speed;
+ }
+
+ /**
* Move the robot with the number of seconds based on its current speed.
*/
public void walkSome(double seconds) {
assert seconds >= 0 : "Robot cannot walk backwards!";
robot_time_pos += speed * seconds;
}
+
+ /**
+ * Returns the lane number on which the robot should walk.
+ */
+ public int getLane() {
+ return laneNo;
+ }
}
diff --git a/src/RobotRace.java b/src/RobotRace.java
index 2aadc63..05789e2 100644
--- a/src/RobotRace.java
+++ b/src/RobotRace.java
@@ -156,21 +156,17 @@ public class RobotRace extends Base {
// Create a new array of four robots
robots = new Robot[4];
- // Initialize robot 0
- robots[0] = new Robot(Material.GOLD
- /* add other parameters that characterize this robot */);
-
- // Initialize robot 1
- robots[1] = new Robot(Material.SILVER
- /* add other parameters that characterize this robot */);
-
- // Initialize robot 2
- robots[2] = new Robot(Material.WOOD
- /* add other parameters that characterize this robot */);
-
- // Initialize robot 3
- robots[3] = new Robot(Material.ORANGE
- /* add other parameters that characterize this robot */);
+ // 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(materials[i], i);
+ }
// Initialize the race track
raceTrack = new RaceTrack();
@@ -410,7 +406,7 @@ public class RobotRace extends Base {
Robot robot = robots[i];
// put robot centered on the lane, slightly rotated to look forward
- Vector robotPos = raceTrack.getPointForLane(robot.getTimePos(), i + .5);
+ 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());