From 0f4533e3df3a519bbe8c647b8b9acdf926e7da1a Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Mon, 2 Dec 2013 23:20:12 +0100 Subject: drawRobots: reduce push/pop and better comments Now robots are drawn from left to right (gold, silver, brown, orange), the direction is now explicitly documented. --- src/RobotRace.java | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/RobotRace.java b/src/RobotRace.java index a7fc2e7..5d5a99c 100644 --- a/src/RobotRace.java +++ b/src/RobotRace.java @@ -287,17 +287,27 @@ public class RobotRace extends Base { * Draw all four robots in the robots array on the scene */ private void drawRobots() { - // Draw all four robots on the x-axis - for (int i = 0; i < 4; i++) { - gl.glPushMatrix(); + // laziness, save current positions because of translations + gl.glPushMatrix(); - // Place the robots on a line based on the current robot - gl.glTranslatef(-1.5f + i * 1f, 0f, 0f); + // 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); + // Draw each robot on the X-axis + for (Robot robot: robots) { // Draw the current robot - robots[i].draw(gs.showStick); - gl.glPopMatrix(); + robot.draw(gs.showStick); + + // Position the next robot 1 meter away from the current one. + gl.glTranslatef(1f, 0f, 0f); } + + // restore positions + gl.glPopMatrix(); } /** -- cgit v1.2.1