-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcube_floor.c
526 lines (497 loc) · 13.7 KB
/
cube_floor.c
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Project: Manhattan Massacre
// University of Lethbridge - CPSC3710
// Project Deadline: April 1, 2019
// Gideon Richter, Brett Dziedzic, Michelle Le, Sean Herridge-Berry
/*
Move a robot around with QAZS
z - move forward
q - turn left
a - turn right
s - turn around
The robot can only move along the street (line) it
is on. It can always turn around and can turn right or
left it is at an intersection (in place), otherwise,
nothing happens.
Also, the camera follows the robot, by default from an elevated
position directly behind - looking at its position.
Ex. Intersections every 2 units
| | | | |
--|--|--|--|--|--
| | | | |
--|--|--|--|--|--
| | | | |
*/
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include "cube_floor.h"
#include "draw_robot.h"
int BLOCK_LENGTH = 6; // Defines the size of a city block
int X_POS = 0;
int Y_POS = 0;
int Z_POS = 0;
int FACING_STATE = FACE_FORWARD;
int ALT_CAMERA_STATE = DEFAULT;
// Method to calculate the new location of the camera after an F-key has been
// selected
float *get_camera_offset()
{
// Return the offset (array of x, y, z) from the
// robot center to the camera 'eye' coordinates.
static float xyz[3];
float camera_height = Y_POS + 4.0;
xyz[1] = camera_height;
// Equal XYZ distance to move camera for F9, F10, F11, and F12 angles
float far_camera_distance = 20.0;
// Distance left/right of the robot for F5, F6, F7, and F8 angles
float close_camera_lateral_offset = 3.0;
// Distance to follow the robot from behind
float close_camera_follow_distance = 5.0;
// Distance to move camera such that it is facing the front of the robot
float close_camera_front_offset = close_camera_follow_distance * 2.0;
// Here begins the,
// The Unholy Relative Camera Movement Switch Statement of Doom.
switch (FACING_STATE)
{
case FACE_FORWARD:
xyz[0] = X_POS;
xyz[2] = Z_POS - close_camera_follow_distance;
switch (ALT_CAMERA_STATE)
{
case BACK_LEFT:
xyz[0] += close_camera_lateral_offset;
break;
case BACK_LEFT_FAR:
xyz[0] += far_camera_distance;
xyz[1] += far_camera_distance;
xyz[2] -= far_camera_distance;
break;
case BACK_RIGHT:
xyz[0] -= close_camera_lateral_offset;
break;
case BACK_RIGHT_FAR:
xyz[0] -= far_camera_distance;
xyz[1] += far_camera_distance;
xyz[2] -= far_camera_distance;
break;
case FRONT_LEFT:
xyz[0] += close_camera_lateral_offset;
xyz[2] += close_camera_front_offset;
break;
case FRONT_LEFT_FAR:
xyz[0] += far_camera_distance;
xyz[1] += far_camera_distance;
xyz[2] += far_camera_distance;
break;
case FRONT_RIGHT:
xyz[0] -= close_camera_lateral_offset;
xyz[2] += 10;
break;
case FRONT_RIGHT_FAR:
xyz[0] -= far_camera_distance;
xyz[1] += far_camera_distance;
xyz[2] += far_camera_distance;
break;
default:
break;
}
break;
case FACE_BACK:
xyz[0] = X_POS;
xyz[2] = Z_POS + close_camera_follow_distance;
switch (ALT_CAMERA_STATE)
{
case BACK_LEFT:
xyz[0] -= close_camera_lateral_offset;
break;
case BACK_LEFT_FAR:
xyz[0] -= far_camera_distance;
xyz[1] += far_camera_distance;
xyz[2] += far_camera_distance;
break;
case BACK_RIGHT:
xyz[0] += close_camera_lateral_offset;
break;
case BACK_RIGHT_FAR:
xyz[0] += far_camera_distance;
xyz[1] += far_camera_distance;
xyz[2] += far_camera_distance;
break;
case FRONT_LEFT:
xyz[0] -= close_camera_lateral_offset;
xyz[2] -= close_camera_front_offset;
break;
case FRONT_LEFT_FAR:
xyz[0] -= far_camera_distance;
xyz[1] += far_camera_distance;
xyz[2] -= far_camera_distance;
break;
case FRONT_RIGHT:
xyz[0] += close_camera_lateral_offset;
xyz[2] -= close_camera_front_offset;
break;
case FRONT_RIGHT_FAR:
xyz[0] += far_camera_distance;
xyz[1] += far_camera_distance;
xyz[2] -= far_camera_distance;
break;
default:
break;
}
break;
case FACE_RIGHT:
xyz[0] = X_POS + close_camera_follow_distance;
xyz[2] = Z_POS;
switch (ALT_CAMERA_STATE)
{
case BACK_LEFT:
xyz[2] += close_camera_lateral_offset;
break;
case BACK_LEFT_FAR:
xyz[0] += far_camera_distance;
xyz[1] += far_camera_distance;
xyz[2] += far_camera_distance;
break;
case BACK_RIGHT:
xyz[2] -= close_camera_lateral_offset;
break;
case BACK_RIGHT_FAR:
xyz[0] += far_camera_distance;
xyz[1] += far_camera_distance;
xyz[2] -= far_camera_distance;
break;
case FRONT_LEFT:
xyz[0] -= close_camera_front_offset;
xyz[2] += close_camera_lateral_offset;
break;
case FRONT_LEFT_FAR:
xyz[0] -= far_camera_distance;
xyz[1] += far_camera_distance;
xyz[2] += far_camera_distance;
break;
case FRONT_RIGHT:
xyz[0] -= close_camera_front_offset;
xyz[2] -= close_camera_lateral_offset;
break;
case FRONT_RIGHT_FAR:
xyz[0] -= far_camera_distance;
xyz[1] += far_camera_distance;
xyz[2] -= far_camera_distance;
break;
default:
break;
}
break;
case FACE_LEFT:
xyz[0] = X_POS - close_camera_follow_distance;
xyz[2] = Z_POS;
switch (ALT_CAMERA_STATE)
{
case BACK_LEFT:
xyz[2] -= close_camera_lateral_offset;
break;
case BACK_LEFT_FAR:
xyz[0] -= far_camera_distance;
xyz[1] += far_camera_distance;
xyz[2] -= far_camera_distance;
break;
case BACK_RIGHT:
xyz[2] += close_camera_lateral_offset;
break;
case BACK_RIGHT_FAR:
xyz[0] -= far_camera_distance;
xyz[1] += far_camera_distance;
xyz[2] += far_camera_distance;
break;
case FRONT_LEFT:
xyz[0] += close_camera_front_offset;
xyz[2] -= close_camera_lateral_offset;
break;
case FRONT_LEFT_FAR:
xyz[0] += far_camera_distance;
xyz[1] += far_camera_distance;
xyz[2] -= far_camera_distance;
break;
case FRONT_RIGHT:
xyz[0] += close_camera_front_offset;
xyz[2] += close_camera_lateral_offset;
break;
case FRONT_RIGHT_FAR:
xyz[0] += far_camera_distance;
xyz[1] += far_camera_distance;
xyz[2] += far_camera_distance;
break;
default:
break;
}
break;
default:
break;
}
return xyz;
}
// Method to test if the robot is capable of moving in the x direction
//
int can_move_x()
{
return !(Z_POS % BLOCK_LENGTH);
}
// Method to test if the robot is capable of moving in the z direction
//
int can_move_z()
{
return !(X_POS % BLOCK_LENGTH);
}
// Method to implement the movement of the robot to the right
// Calls both can_move_x() and can_move_z() to ensure movement is possible
//
void turn_right_if_possible()
{
// If we are at an intersection, turn the robot right.
if (can_move_x() && can_move_z())
{
switch (FACING_STATE)
{
case FACE_FORWARD:
FACING_STATE = FACE_RIGHT;
break;
case FACE_BACK:
FACING_STATE = FACE_LEFT;
break;
case FACE_RIGHT:
FACING_STATE = FACE_BACK;
break;
case FACE_LEFT:
FACING_STATE = FACE_FORWARD;
break;
}
}
}
// Method to implement the movement of the robot to the left
// Calls both can_move_x() and can_move_z() to ensure movement is possible
//
void turn_left_if_possible()
{
// If we are at an intersection, turn the robot left.
if (can_move_x() && can_move_z())
{
switch (FACING_STATE)
{
case FACE_BACK:
FACING_STATE = FACE_RIGHT;
break;
case FACE_FORWARD:
FACING_STATE = FACE_LEFT;
break;
case FACE_RIGHT:
FACING_STATE = FACE_FORWARD;
break;
case FACE_LEFT:
FACING_STATE = FACE_BACK;
break;
}
}
}
// Method to implement the movement of the robot in the opposite direction
//
void turn_around()
{
// Turn the robot in the opposite direction it is facing.
switch (FACING_STATE)
{
case FACE_BACK:
FACING_STATE = FACE_FORWARD;
break;
case FACE_FORWARD:
FACING_STATE = FACE_BACK;
break;
case FACE_RIGHT:
FACING_STATE = FACE_LEFT;
break;
case FACE_LEFT:
FACING_STATE = FACE_RIGHT;
break;
}
}
void prevent_out_of_bounds()
{
// The bounds here are based on the BLOCKS_COLUMNS in `world.c`,
// but I can't use those DEFINE's so I'll write a comment instead.
// If BLOCKS_COLUMNS and BLOCKS_ROWS are both say, 14, then 7 blocks
// extend in each direction. Multiply 10 by the size of the block (6)
// and you get 42, 42 - 1 is 41, which is the limit to the number of
// times we can move in that direction from the origin.
// Instead of trying to restrict movement ahead of time,
// let it happen and correct it. This leads to fewer (0)
// problems. Magic!
if (Z_POS > 41)
{
Z_POS = 41;
}
if (Z_POS < -41)
{
Z_POS = -41;
}
if (X_POS > 41)
{
X_POS = 41;
}
if (X_POS < -41)
{
X_POS = -41;
}
}
void move_forward()
{
// Move the robot forward in the direction it is facing.
if (can_move_x())
{
// Move right or left.
switch (FACING_STATE)
{
case FACE_RIGHT:
X_POS -= 1;
break;
case FACE_LEFT:
X_POS += 1;
break;
default:
break;
}
}
if (can_move_z())
{
// Move up or down.
switch (FACING_STATE)
{
case FACE_BACK:
Z_POS -= 1;
break;
case FACE_FORWARD:
Z_POS += 1;
break;
default:
break;
}
}
prevent_out_of_bounds();
}
void reset_robot_to_origin()
{
X_POS = 0;
Y_POS = 0;
Z_POS = 0;
}
// Keyboard callback function for movement of the robot in the 4 cardinal
// directions
//
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 114: // "r"
reset_robot_to_origin();
break;
case 122: // "z"
move_forward();
break;
case 97: // "a"
turn_right_if_possible();
break;
case 113: // "q"
turn_left_if_possible();
break;
case 115: // "s"
turn_around();
break;
// TODO: implement the "pause" function with "p"
// description
default:
printf("Not a movement key: %d\n", key);
fflush(stdout);
break;
}
printf("X_POS %d Y_POS %d Z_POS %d\n", X_POS, Y_POS, Z_POS);
}
void special_keyboard_up(int key, int x, int y)
{
switch (key)
{
case 1: // "F1"
case 2: // "F2"
case 3: // "F3"
ROBOT_HEAD_DIRECTION = FORWARD;
break;
default:
printf("Not a special control key up: %d\n", key);
fflush(stdout);
break;
}
}
// Keyboard callback function for movement of the observing camera in various
// directions
//
void special_keyboard(int key, int x, int y)
{
switch (key)
{
case 1: // "F1"
ROBOT_HEAD_DIRECTION = FORWARD;
break;
case 2: // "F2"
ROBOT_HEAD_DIRECTION = LEFT;
break;
case 3: // "F3"
ROBOT_HEAD_DIRECTION = RIGHT;
break;
case 4: // "F4"
ALT_CAMERA_STATE = DEFAULT;
printf("Camera state: default\n");
fflush(stdout);
break;
case 5: // "F5"
ALT_CAMERA_STATE = BACK_LEFT;
printf("Camera state: Back left\n");
fflush(stdout);
break;
case 6: // "F6"
ALT_CAMERA_STATE = BACK_RIGHT;
printf("Camera state: Back right\n");
fflush(stdout);
break;
case 7: // "F7"
ALT_CAMERA_STATE = FRONT_RIGHT;
printf("Camera state: Front right\n");
fflush(stdout);
break;
case 8: // "F8"
ALT_CAMERA_STATE = FRONT_LEFT;
printf("Camera state: Front left\n");
fflush(stdout);
break;
case 9: // "F9"
ALT_CAMERA_STATE = BACK_LEFT_FAR;
printf("Camera state: Back left far\n");
fflush(stdout);
break;
case 10: // "F10"
ALT_CAMERA_STATE = BACK_RIGHT_FAR;
printf("Camera state: Back right far\n");
fflush(stdout);
break;
case 11: // "F11"
ALT_CAMERA_STATE = FRONT_RIGHT_FAR;
printf("Camera state: Front right far\n");
fflush(stdout);
break;
case 12: // "F12"
ALT_CAMERA_STATE = FRONT_LEFT_FAR;
printf("Camera state: Front left far\n");
fflush(stdout);
break;
default:
printf("Not a special control key: %d\n", key);
fflush(stdout);
break;
}
}