summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2013-11-29 00:45:35 +0100
committerPeter Wu <lekensteyn@gmail.com>2013-11-29 00:45:35 +0100
commitc04d638460e9094cdefcafb9c3f003b138ca35c2 (patch)
treee0d89a97275ab91329faf4ce43d0c8d8ffa6869c
parent38564eb6736b820caff345b4adb2bfd8ec1c93c9 (diff)
download2iv60-robots-c04d638460e9094cdefcafb9c3f003b138ca35c2.tar.gz
Refactor: move Camera outside RobotRace
Extra changes include reordering comments around conditions and adding the GlobalState parameter to the constructor.
-rw-r--r--src/Camera.java105
-rw-r--r--src/RobotRace.java104
2 files changed, 106 insertions, 103 deletions
diff --git a/src/Camera.java b/src/Camera.java
new file mode 100644
index 0000000..e865bc7
--- /dev/null
+++ b/src/Camera.java
@@ -0,0 +1,105 @@
+
+import robotrace.Vector;
+import static java.lang.Math.*;
+import robotrace.GlobalState;
+
+/**
+ * Implementation of a camera with a position and orientation.
+ */
+class Camera {
+
+ /** The position of the camera. */
+ public Vector eye = new Vector(3f, 6f, 5f);
+
+ /** The point to which the camera is looking. */
+ public Vector center = Vector.O;
+
+ /** The up vector. */
+ public Vector up = Vector.Z;
+
+ /**
+ * A reference to the global game state from RobotRace.
+ */
+ private final GlobalState gs;
+
+ public Camera(GlobalState gs) {
+ this.gs = gs;
+ }
+
+ /**
+ * Updates the camera viewpoint and direction based on the
+ * selected camera mode.
+ */
+ public void update(int mode) {
+ if (1 == mode) { // Helicopter mode
+ setHelicopterMode();
+ } else if (2 == mode) { // Motor cycle mode
+ setMotorCycleMode();
+ } else if (3 == mode) { // First person mode
+ setFirstPersonMode();
+ } else if (4 == mode) { // Auto mode
+ // code goes here...
+ } else { // Default mode
+ setDefaultMode();
+ }
+ }
+
+ /**
+ * Computes {@code eye}, {@code center}, and {@code up}, based
+ * on the camera's default mode.
+ */
+ private void setDefaultMode() {
+ /* z |
+ * | vDist %
+ * | % * Ez
+ * |%________*________ y
+ * Ex / % *
+ * / s % *
+ * x / - - - - - - - *
+ * Ey
+ * phi is angle between vDist and XY plane (Z direction)
+ * theta is angle between X-axis and s (XY plane)
+ * E = (Ex, Ey, Ez)
+ * sin phi = Ez / vDist => Ez = vDist * sin phi
+ * cos phi = s / vDist => s = vDist * cos phi
+ * Ex = s * sin theta
+ * Ey = s * cos theta
+ */
+ float Ex, Ey, Ez, s;
+ Ez = gs.vDist * (float) sin(gs.phi);
+ s = gs.vDist * (float) cos(gs.phi);
+ Ex = s * (float) sin(gs.theta);
+ Ey = s * (float) cos(gs.theta);
+
+ // change center point with WASD
+ Ex += gs.cnt.x();
+ Ey += gs.cnt.y();
+ Ez += gs.cnt.z();
+
+ eye = new Vector(Ex, Ey, Ez);
+ }
+
+ /**
+ * Computes {@code eye}, {@code center}, and {@code up}, based
+ * on the helicopter mode.
+ */
+ private void setHelicopterMode() {
+ // code goes here ...
+ }
+
+ /**
+ * Computes {@code eye}, {@code center}, and {@code up}, based
+ * on the motorcycle mode.
+ */
+ private void setMotorCycleMode() {
+ // code goes here ...
+ }
+
+ /**
+ * Computes {@code eye}, {@code center}, and {@code up}, based
+ * on the first person mode.
+ */
+ private void setFirstPersonMode() {
+ // code goes here ...
+ }
+}
diff --git a/src/RobotRace.java b/src/RobotRace.java
index 6495285..6919422 100644
--- a/src/RobotRace.java
+++ b/src/RobotRace.java
@@ -106,7 +106,7 @@ public class RobotRace extends Base {
/* add other parameters that characterize this robot */);
// Initialize the camera
- camera = new Camera();
+ camera = new Camera(gs);
// Initialize the race track
raceTrack = new RaceTrack();
@@ -286,108 +286,6 @@ public class RobotRace extends Base {
}
/**
- * Implementation of a camera with a position and orientation.
- */
- private class Camera {
-
- /** The position of the camera. */
- public Vector eye = new Vector(3f, 6f, 5f);
-
- /** The point to which the camera is looking. */
- public Vector center = Vector.O;
-
- /** The up vector. */
- public Vector up = Vector.Z;
-
- /**
- * Updates the camera viewpoint and direction based on the
- * selected camera mode.
- */
- public void update(int mode) {
- // Helicopter mode
- if (1 == mode) {
- setHelicopterMode();
-
- // Motor cycle mode
- } else if (2 == mode) {
- setMotorCycleMode();
-
- // First person mode
- } else if (3 == mode) {
- setFirstPersonMode();
-
- // Auto mode
- } else if (4 == mode) {
- // code goes here...
-
- // Default mode
- } else {
- setDefaultMode();
- }
- }
-
- /**
- * Computes {@code eye}, {@code center}, and {@code up}, based
- * on the camera's default mode.
- */
- private void setDefaultMode() {
- /* z |
- * | vDist %
- * | % * Ez
- * |%________*________ y
- * Ex / % *
- * / s % *
- * x / - - - - - - - *
- * Ey
- * phi is angle between vDist and XY plane (Z direction)
- * theta is angle between X-axis and s (XY plane)
- * E = (Ex, Ey, Ez)
- * sin phi = Ez / vDist => Ez = vDist * sin phi
- * cos phi = s / vDist => s = vDist * cos phi
- * Ex = s * sin theta
- * Ey = s * cos theta
- */
- float Ex, Ey, Ez, s;
- Ez = gs.vDist * (float) sin(gs.phi);
- s = gs.vDist * (float) cos(gs.phi);
- Ex = s * (float) sin(gs.theta);
- Ey = s * (float) cos(gs.theta);
-
- // change center point with WASD
- Ex += gs.cnt.x();
- Ey += gs.cnt.y();
- Ez += gs.cnt.z();
-
- eye = new Vector(Ex, Ey, Ez);
- }
-
- /**
- * Computes {@code eye}, {@code center}, and {@code up}, based
- * on the helicopter mode.
- */
- private void setHelicopterMode() {
- // code goes here ...
- }
-
- /**
- * Computes {@code eye}, {@code center}, and {@code up}, based
- * on the motorcycle mode.
- */
- private void setMotorCycleMode() {
- // code goes here ...
- }
-
- /**
- * Computes {@code eye}, {@code center}, and {@code up}, based
- * on the first person mode.
- */
- private void setFirstPersonMode() {
- // code goes here ...
- }
-
- }
-
- /**
* Main program execution body, delegates to an instance of
* the RobotRace implementation.
*/