diff options
author | Frank v/d Haterd <f.h.a.v.d.haterd@student.tue.nl> | 2013-11-27 16:58:03 +0100 |
---|---|---|
committer | Peter Wu <lekensteyn@gmail.com> | 2013-11-27 17:04:12 +0100 |
commit | 144b3336423dc6ce9986eff40346460466c123eb (patch) | |
tree | 61e25256fdae93447ec740d1f5679a843f35e998 | |
parent | 304a0baa5b74ed949216e6e41bb01bcba584546c (diff) | |
download | 2iv60-robots-144b3336423dc6ce9986eff40346460466c123eb.tar.gz |
Robot draw methods finished, comments added.
Robot stickfigure methods finished, comments added.
-rw-r--r-- | src/RobotRace.java | 262 |
1 files changed, 229 insertions, 33 deletions
diff --git a/src/RobotRace.java b/src/RobotRace.java index 6b9cc44..8e26f1f 100644 --- a/src/RobotRace.java +++ b/src/RobotRace.java @@ -356,136 +356,332 @@ public class RobotRace extends Base { * Draws this robot (as a {@code stickfigure} if specified). */ public void draw(boolean stickFigure) { - // code goes here ... - if(!stickFigure) { - drawTorso(); - drawShoulders(); - drawLegs(); - drawArms(); - drawHead(); + if (stickFigure) { + // Draw the robot + drawTorso(); // Torso + drawShoulders(); // Shoulders + drawLegs(); // Legs and feet + drawArms(); // Arms and hands + drawHead(); // Neck and head + } else { + // Draw the stick figure + drawStickFigure(); } } + /** + * Draws the stickfigure of the robot. + */ + private void drawStickFigure() { + // Create all vectors based on the real position and the + // translations of the real robot. + Vector torsoLeftTop = new Vector(torsoWidth /2, 0f, torsoHeight /2); + Vector torsoRightTop = new Vector(-torsoWidth/2, 0f, torsoHeight/2); + Vector torsoLeftBottom = new Vector(torsoWidth / 2, 0f, + -torsoHeight /2); + Vector torsoRightBottom = new Vector(-torsoWidth /2 , 0f, + -torsoHeight /2); + + Vector leftShoulder = new Vector(torsoWidth / 2 + armWidth * 1.5f, + 0f, torsoHeight / 2 - 0.05f); + Vector rightShoulder = new Vector(-torsoWidth / 2 - armWidth * 1.5f, + 0f, torsoHeight / 2 - 0.05f); + + Vector leftHand = new Vector(torsoWidth / 2 + armWidth * 1.5f, + 0f, torsoHeight / 2 - 0.05f - armLength); + Vector rightHand = new Vector(-torsoWidth / 2 - armWidth * 1.5f, + 0f, torsoHeight / 2 - 0.05f - armLength); + + Vector neckStart = new Vector(0f, 0f, torsoHeight/2); + Vector neckEnd = new Vector(0f, 0f, torsoHeight/2 + neckHeight); + + Vector leftLegTop = new Vector(torsoWidth / 2 - legWidth * 2, 0f, + -torsoHeight / 2); + Vector leftLegBottom = new Vector(torsoWidth / 2 - legWidth * 2, + 0f, -torsoHeight / 2 - legLength); + + Vector rightLegTop = new Vector(-torsoWidth / 2 + legWidth * 2, 0f, + -torsoHeight / 2); + Vector rightLegBottom = new Vector(-torsoWidth / 2 + legWidth * 2, + 0f, -torsoHeight / 2 - legLength); + + /* + * For future, translate all vectors so they are positioned + * correctly and that they are rotated correctly. + * + * Arms and feet should be translated afterwards to represent + * the angle to simulate movement. + */ + + // Set the color to black + setColorRGB(0, 0, 0); + + // Connect all torso points with a line stip + gl.glBegin(GL_LINE_STRIP); + stickPoint(torsoLeftTop); + stickPoint(torsoRightTop); + stickPoint(torsoRightBottom); + stickPoint(torsoLeftBottom); + stickPoint(torsoLeftTop); + gl.glEnd(); + + // Draw head lines based on neck start and end + gl.glBegin(GL_LINES); + stickPoint(neckStart); + stickPoint(neckEnd); + gl.glEnd(); + + // Draw arm and leg lines, and connect arms with torso (shoulders) + gl.glBegin(GL_LINES); + stickPoint(leftLegTop); + stickPoint(leftLegBottom); + stickPoint(rightLegTop); + stickPoint(rightLegBottom); + stickPoint(leftShoulder); + stickPoint(leftHand); + stickPoint(rightShoulder); + stickPoint(rightHand); + stickPoint(rightShoulder); + stickPoint(torsoRightTop); + stickPoint(leftShoulder); + stickPoint(torsoLeftTop); + gl.glEnd(); + + // Set point size of the stickfigure + gl.glPointSize(7.5f); + + // Begin drawing all points + gl.glBegin(GL_POINTS); + + // First the torso points + stickPoint(torsoLeftTop); + stickPoint(torsoRightTop); + stickPoint(torsoRightBottom); + stickPoint(torsoLeftBottom); + + // Shoulders and the hands + stickPoint(leftShoulder); + stickPoint(rightShoulder); + stickPoint(leftHand); + stickPoint(rightHand); + + // The neck + stickPoint(neckStart); + stickPoint(neckEnd); + + // And finally the legs + stickPoint(leftLegTop); + stickPoint(leftLegBottom); + stickPoint(rightLegTop); + stickPoint(rightLegBottom); + + // Stop drawing points + gl.glEnd(); + } + + /** + * Draws a point with a given vector p + * @param p Point in space + */ + private void stickPoint(Vector p) { + gl.glVertex3f((float) p.x(), (float) p.y(), (float) p.z()); + } + + + /** + * Draws the torso of the robot. + */ private void drawTorso() { + // Push matrix for returning to origin later gl.glPushMatrix(); + // Scale the torso to specified values gl.glScalef(torsoWidth, depth, torsoHeight); - glut.glutWireCube(1f); + // Draw the torso itself with given color + setColorRGB(230, 230, 230); + glut.glutSolidCube(1f); + // Pop matrix so we return to the origin gl.glPopMatrix(); } + /** + * Draws both shoulders of the robot. + */ private void drawShoulders() { + // Push the translation matrix so we can return to the origin gl.glPushMatrix(); - gl.glTranslatef(torsoWidth / 2 + shoulderRadius / 1.5f, 0f, torsoHeight / 2); + // Translate to the left of the robot for left shoulder + gl.glTranslatef(torsoWidth / 2 + shoulderRadius / 1.5f, 0f, + torsoHeight / 2); - gl.glColor3f(1.0f / 100f, 1.0f/130f, 1.0f/255f); - glut.glutWireSphere(shoulderRadius, 10, 10); + // Set the drawing color and draw left shoulder + setColorRGB(100, 130, 255); + glut.glutSolidSphere(shoulderRadius, 10, 10); + // Pop the matrix and then push it, so we are at the origin gl.glPopMatrix(); gl.glPushMatrix(); - gl.glTranslatef(- torsoWidth / 2 - shoulderRadius / 1.5f, 0f, torsoHeight / 2); + // Translate to the right of the robot for right shoulder + gl.glTranslatef(- torsoWidth / 2 - shoulderRadius / 1.5f, 0f, + torsoHeight / 2); - gl.glColor3f(1.0f / 100f, 1.0f/130f, 1.0f/255f); - glut.glutWireSphere(shoulderRadius, 10, 10); + // Set the drawing color and draw right shoulder + setColorRGB(100, 130, 255); + glut.glutSolidSphere(shoulderRadius, 10, 10); + // Pop the translation matrix so we are at the origin gl.glPopMatrix(); } + /** + * Draws the legs and feet of the robot. + */ private void drawLegs() { + // Push the matrix for returning to origin gl.glPushMatrix(); + // Translate and scale for the left leg gl.glTranslatef(torsoWidth / 2 - legWidth * 2, 0f, - legLength / 2 - torsoHeight / 2); gl.glScalef(legWidth, legWidth, legLength); - gl.glColor3f(1.0f / 150f, 1.0f/150f, 1.0f/150f); + // Set the color and draw the left leg + setColorRGB(150, 150, 150); glut.glutSolidCube(1f); + // First scale and translate back, + // then new transformation for left foot gl.glScalef(1/legWidth, 1/legWidth, 1/legLength); gl.glTranslatef(0f, 0f, - legLength / 2); - gl.glTranslatef(legWidth / 2, legWidth, 0f); - gl.glScalef(legWidth, legWidth * 2, 0.1f); + gl.glTranslatef(0f, legWidth / 2, -legWidth / 2); + gl.glScalef(legWidth, legWidth * 2, legWidth); - gl.glColor3f(1.0f / 100f, 1.0f/100f, 1.0f/100f); + // Set the color and draw left foot + setColorRGB(100, 100, 100); glut.glutSolidCube(1f); + // Pop the matrix and then push, so we are at the origin gl.glPopMatrix(); gl.glPushMatrix(); + // Translate and scale for the right leg gl.glTranslatef(-torsoWidth / 2 + legWidth * 2, 0f, - legLength / 2 - torsoHeight / 2); gl.glScalef(legWidth, legWidth, legLength); - gl.glColor3f(1.0f / 150f, 1.0f/150f, 1.0f/150f); + // Set the color and draw right leg + setColorRGB(150, 150, 150); glut.glutSolidCube(1f); + // Scale and translate back, + // then translate and scale for right foot gl.glScalef(1/legWidth, 1/legWidth, 1/legLength); gl.glTranslatef(0f, 0f, - legLength / 2); - gl.glTranslatef(legWidth / 2, legWidth, 0f); - gl.glScalef(legWidth, legWidth * 2, 0.1f); + gl.glTranslatef(0f, legWidth / 2, -legWidth / 2); + gl.glScalef(legWidth, legWidth * 2, legWidth); - gl.glColor3f(1.0f / 100f, 1.0f/100f, 1.0f/100f); + // Set color and draw right foot + setColorRGB(100, 100, 100); glut.glutSolidCube(1f); + // Pop so we are at the origin gl.glPopMatrix(); } + /** + * Draw both arms and both hands of the robot. + */ private void drawArms() { + // Push the translation matrix so we can return to the origin gl.glPushMatrix(); + // Translate and scale for the left arm gl.glTranslatef(torsoWidth / 2 + armWidth * 1.5f, 0f, torsoHeight / 2 - armLength / 2 - 0.05f); gl.glScalef(armWidth, armWidth, armLength); - gl.glColor3f(1.0f / 218f, 1.0f/218f, 1.0f/218f); - glut.glutWireCube(1f); + // Set the color and draw the arm itself + setColorRGB(200, 200, 200); + glut.glutSolidCube(1f); + // Translate and scale for the left hand gl.glScalef(1/armWidth, 1/armWidth, 1/armLength); gl.glTranslatef(0f, 0f, - armLength / 2); - gl.glColor3f(1.0f / 100f, 1.0f/130f, 1.0f/255f); - glut.glutWireSphere(armWidth * 1.5f, 10, 10); + // Set the color and draw the left hand + setColorRGB(130, 158, 174); + glut.glutSolidSphere(armWidth * 1.25f, 10, 10); + // Pop the translation matrix and return to the origin gl.glPopMatrix(); gl.glPushMatrix(); + // Translate and scale for the right arm gl.glTranslatef(-(torsoWidth / 2 + armWidth * 1.5f), 0f, torsoHeight / 2 - armLength / 2 - 0.05f); gl.glScalef(armWidth, armWidth, armLength); - gl.glColor3f(1.0f / 218f, 1.0f/218f, 1.0f/218f); - glut.glutWireCube(1f); + // Set the color and draw the right arm + setColorRGB(200, 200, 200); + glut.glutSolidCube(1f); + // Translate and scale for right hand gl.glScalef(1/armWidth, 1/armWidth, 1/armLength); gl.glTranslatef(0f, 0f, - armLength / 2); - gl.glColor3f(1.0f / 100f, 1.0f/130f, 1.0f/255f); - glut.glutWireSphere(armWidth * 1.5f, 10, 10); + // Set color and draw the right hand + setColorRGB(130, 158, 174); + glut.glutSolidSphere(armWidth * 1.25f, 10, 10); + // Pop the translation matrix so we are at the origin again gl.glPopMatrix(); } + /** + * Set the color for drawing specified with RGB values. + * + * @param r Red parameter (0 - 255) + * @param g Green parameter (0 - 255) + * @param b Blue parameter (0 - 255) + */ + private void setColorRGB(int r, int g, int b) { + // Set the color by dividing the given value by 255.0f + gl.glColor3f(r / 255.0f, g / 255.0f, b / 255.0f); + } + + /** + * Draw the head and the neck of the robot. + */ private void drawHead() { + // Push matrix so we can go to the origin afterwards gl.glPushMatrix(); + // Translate and scale for the neck gl.glTranslatef(0f, 0f, torsoHeight / 2 + neckHeight / 2); gl.glScalef(headRadius / 2.5f, headRadius / 2.5f, neckHeight); - glut.glutWireCube(1f); + // Set color and draw neck + setColorRGB(230, 230, 230); + glut.glutSolidCube(1f); + // Pop the matrix and push so we are at the origin again gl.glPopMatrix(); gl.glPushMatrix(); + // Translate so we are above the neck for the head gl.glTranslatef(0f, 0f, torsoHeight / 2 + neckHeight + headRadius / 2); - glut.glutWireSphere(headRadius, 10, 10); - + // Set color and draw head + setColorRGB(190, 210, 220); + glut.glutSolidSphere(headRadius, 10, 10); + + // Pop so we are at the origin again gl.glPopMatrix(); } |