summaryrefslogtreecommitdiff
path: root/src/Robot.java
blob: 043cc1ce59716fd5f8273088033c252e81dda744 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369

import java.awt.Color;
import javax.media.opengl.GL;
import static javax.media.opengl.GL2.*;
import javax.media.opengl.fixedfunc.GLLightingFunc;

/**
 * Represents a Robot, to be implemented according to the Assignments.
 */
class Robot extends BetterBase {
    private final Color boneColor = Colors.CHOCOLATE;

    /** The material from which this robot is built. */
    private final Material material;

    /** Relative lengths, widths and heights of robot model. */
    private final float torsoHeight;
    private final float torsoWidth;
    private final float shoulderRadius;
    private final float armLength;
    private final float legLength;
    private final float armWidth;
    private final float legWidth;
    private final float neckHeight;
    private final float headRadius;
    private final float depth;
    private final float footWidth;
    private final float footHeight;
    private final float footLength;
    /** Size of the bone for stick figures. */
    private final float boneSize;

    /**
     * True if a skeleton should be drawn instead of a full body.
     */
    private boolean asStickFigure;

    /**
     * Robot speed (on track) in meters per second.
     */
    private double speed;

    /**
     * Location of the robot on the track (in terms of GlobalState time).
     */
    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, int laneNo) {
        /* Set all parameters of the robot */
        this.material = material;
        this.torsoHeight = 0.6f;
        this.torsoWidth = 0.48f;
        this.shoulderRadius = 0.09f;
        this.armLength = 0.6f;
        this.armWidth = 0.06f;
        this.legLength = 0.78f;
        this.legWidth = 0.06f;
        this.neckHeight = 0.15f;
        this.headRadius = 0.12f;
        this.footWidth = legWidth;
        this.footHeight = legWidth;
        this.footLength = 2 * legWidth;
        this.boneSize = 0.02f;
        this.depth = 0.24f;
        this.laneNo = laneNo;
    }

    /**
     * Draws this robot (as a {@code stickfigure} if specified).
     */
    public void draw(boolean stickFigure) {
        // before drawing body parts, configure whether to draw a full body or
        // just a stick figure with joints and such.
        this.asStickFigure = stickFigure;

        // These materials control the reflected light
        gl.glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, material.diffuse, 0);
        gl.glMaterialfv(GL_FRONT, GL_SPECULAR, material.specular, 0);

        // save positions so it can be restored easily later
        gl.glPushMatrix();
        // position the center of the torso above the Z axis such that the foot
        // stands on the XY plane.
        gl.glTranslatef(0, 0, torsoHeight / 2 + legLength + footHeight);

        // Draw the robot, everything is relative to the center of torso.

        // Static parts (that do not animate):
        drawTorso();
        drawHead();
        // only draw the shoulder parts in normal, full body mode
        if (!asStickFigure) {
            drawShoulderTop();
        }

        // Parts that should be animated:
        // draw left and right legs
        drawLeg(false);
        drawLeg(true);
        // draw left and right arms
        drawArm(false);
        drawArm(true);

        //<editor-fold>
        // The following function call exist to make a point clear. Adding
        // comments for the sake of having comments is silly, pointless and an
        // outdated idea. Self-documenting code (by using appropriate symbol
        // names and logical structures) is much more useful than adding
        // comments for every statement (50% comments...). High-level
        // descriptions of functionality could be given in a design document or
        // added as comments, but something like "draws a leg" for a function
        // named "drawLeg" is useless. If you want to know what exactly happens,
        // look in the method code, it likely contains some comments to
        // describe what happens.
        thisFunctionDoesAbsolutelyNothing();
        //</editor-fold>

        // restore position
        gl.glPopMatrix();
    }

    /**
     * Draws a 3d figure with given dimensions and color. If a stick figure
     * must be drawn, then the figure will become a thin line in the X, Y
     * or Z direction depending on the direction parameter.
     * @param dir Direction of the line segment, relevant for stick figures.
     * @param color If non-null, it becomes the color for this beam. Ignored
     * when drawing a stick figure, in that case the boneColor constant will
     * be used.
     */
    private void drawBeam(float x, float y, float z, Direction dir, Color color) {
        if (asStickFigure) {
            // for a stick figure, draw a thin figure without colors
            switch (dir) {
                case X:
                    assert x != 0;
                    y = z = boneSize;
                    break;
                case Y:
                    assert y != 0;
                    x = z = boneSize;
                    break;
                case Z:
                    assert z != 0;
                    x = y = boneSize;
                    break;
                default:
                    throw new AssertionError(dir.name());
            }
            // stick figures always get a "bone" color
            setColor(boneColor);
        } else {
            assert x != 0;
            assert y != 0;
            assert z != 0;
            if (color != null) {
                setColor(color);
            }
        }
        // turn a cube into a small beam
        gl.glScalef(x, y, z);
        glut.glutSolidCube(1);
        // return to previous scales
        gl.glScalef(1 / x, 1 / y, 1 / z);
    }

    /**
     * Draws a joint for stick figure model.
     */
    private void drawJoint() {
        if (asStickFigure) {
            glut.glutSolidSphere(boneSize * 1.5, 16, 16);
        }
    }

    /**
     * Draws the torso of the robot.
     */
    private void drawTorso() {
        // Scale the torso to specified values
        drawBeam(torsoWidth, depth, torsoHeight, Direction.Z, Color.LIGHT_GRAY);

        if (asStickFigure) {
            // draw the bone connecting the arms (visible for stick figure)
            gl.glTranslatef(0, 0, torsoHeight / 2);
            drawBeam(torsoWidth + armWidth * 3, boneSize, boneSize, Direction.X, null);

            gl.glTranslatef(0, 0, -torsoHeight);
            // draw the bone connecting the legs (visible for stick figure)
            drawBeam(.5f * torsoWidth + legWidth / 2, boneSize, boneSize, Direction.X, null);

            // return to torso center
            gl.glTranslatef(0, 0, torsoHeight / 2);
        }
    }

    /**
     * Draws both shoulders of the robot.
     */
    private void drawShoulderTop() {
        // save position
        gl.glPushMatrix();

        float shoulder_x = torsoWidth / 2 + shoulderRadius / 1.5f;
        // Translate to the left of the robot for left shoulder
        gl.glTranslatef(shoulder_x, 0f, torsoHeight / 2);

        setColor(Colors.BLUEISH);

        // Set the drawing color and draw right shoulder
        glut.glutSolidSphere(shoulderRadius, 32, 32);
        // left shoulder
        gl.glTranslatef(-2 * shoulder_x, 0, 0);
        glut.glutSolidSphere(shoulderRadius, 32, 32);

        // restore position
        gl.glPopMatrix();
    }

    /**
     * Draws the upper and lower legs and feet of the robot.
     * @param isRight True if at the robot's right (from the robot POV).
     */
    private void drawLeg(boolean isRight) {
        // save center position
        gl.glPushMatrix();

        // The legs are located on the first and third quarter
        float leg_top_x = -torsoWidth / 4;
        if (!isRight) {
            leg_top_x += torsoWidth / 2;
        }
        gl.glTranslatef(leg_top_x, 0f, -torsoHeight / 2);
        drawJoint();

        // upper leg half
        gl.glTranslatef(0, 0, -legLength / 4);
        drawBeam(legWidth, legWidth, legLength / 2, Direction.Z, Color.DARK_GRAY);

        // knee
        gl.glTranslatef(0, 0, -legLength / 4);
        drawJoint();

        // lower leg half
        gl.glTranslatef(0, 0, -legLength / 4);
        drawBeam(legWidth, legWidth, legLength / 2, Direction.Z, Color.DARK_GRAY);

        // for the stick figure, the "foot" is just an extension of the leg line
        // and therefore stays at the same y-position
        float foot_y = 0;
        if (!asStickFigure) {
            // go to the heel and then move to the center of the foot
            foot_y = -legWidth / 2 + footLength / 2;
        }
        // draw foot!
        gl.glTranslatef(0, foot_y, -legLength / 4 - footHeight / 2);
        drawBeam(footWidth, footLength, footHeight, Direction.Z, Colors.GRAYISH);

        // Restore position
        gl.glPopMatrix();
    }

   /**
    *  Draw both arms and both hands of the robot.
    * @param isRight True if at the robot's right (from the robot POV).
    */
    private void drawArm(boolean isRight) {
        // Push the translation matrix so we can return to the origin
        gl.glPushMatrix();

        // the arm is located outside the torso
        float arm_x = torsoWidth / 2 + armWidth * 1.5f;
        if (isRight) {
            arm_x *= -1;
        }
        // arm starts next to the shoulder let's add a joint there...
        gl.glTranslatef(arm_x, 0, torsoHeight / 2);
        drawJoint();

        // here is your arm (start drawing from the elbow)
        gl.glTranslatef(0, 0, -armLength / 2);
        drawBeam(armWidth, armWidth, armLength, Direction.Z, Colors.ARM_GRAY_COLOR);

        if (!asStickFigure) {
            // Give me a big hand!
            setColor(Colors.DIRTY_BLUE);
            gl.glTranslatef(0f, 0f, -armLength / 2);
            glut.glutSolidSphere(armWidth * 1.25f, 32, 32);
        }

        gl.glPopMatrix();
    }

    /**
     * Draw the head and the neck of the robot.
     */
    private void drawHead() {
        // Push matrix so we can go to the origin afterwards
        gl.glPushMatrix();

        // position centered above the torso for the neck
        gl.glTranslatef(0f, 0f, torsoHeight / 2 + neckHeight / 2);
        float neckRadius = headRadius / 2.5f;
        drawBeam(neckRadius, neckRadius, neckHeight, Direction.Z, Colors.LAVENDER);

        // continue moving to the center of the head
        gl.glTranslatef(0f, 0f, neckHeight / 2 + headRadius / 2);

        // Set color and draw head
        setColor(asStickFigure ? boneColor : Colors.PALE_TURQOISE);
        glut.glutSolidSphere(headRadius, 32, 32);

        // Pop so we are at the origin again
        gl.glPopMatrix();
    }

    /**
     * This function does nothing, its use is described at the caller in
     * Robot.draw().
     */
    private void thisFunctionDoesAbsolutelyNothing() {
    }

    /**
     * Determine the location of the robot on the track.
     * @return A positive number (time).
     */
    public double getTimePos() {
        return robot_time_pos;
    }

    /**
     * Set the speed (meters per second) for this robot.
     */
    public void setSpeed(double speed) {
        this.speed = speed;
    }

    /**
     * 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;
    }
}