Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp@11:2cd6341136ca, 2019-03-24 (annotated)
- Committer:
- el17cd
- Date:
- Sun Mar 24 12:31:38 2019 +0000
- Revision:
- 11:2cd6341136ca
- Parent:
- 10:07a23afd5088
- Child:
- 12:b69657862610
added horizon, rotating field of view when moving and collision detection
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el17cd | 0:efb5eec6b8ea | 1 | /* |
| el17cd | 0:efb5eec6b8ea | 2 | ELEC2645 Embedded Systems Project |
| el17cd | 0:efb5eec6b8ea | 3 | School of Electronic & Electrical Engineering |
| el17cd | 0:efb5eec6b8ea | 4 | University of Leeds |
| el17cd | 0:efb5eec6b8ea | 5 | Name: Christopher Doel |
| el17cd | 0:efb5eec6b8ea | 6 | Username: el17cd |
| el17cd | 0:efb5eec6b8ea | 7 | Student ID Number: 201146223 |
| el17cd | 0:efb5eec6b8ea | 8 | Date: 22/02/19 |
| el17cd | 0:efb5eec6b8ea | 9 | */ |
| el17cd | 0:efb5eec6b8ea | 10 | |
| el17cd | 0:efb5eec6b8ea | 11 | #include "mbed.h" |
| el17cd | 1:044238f7bdda | 12 | #include <vector> |
| el17cd | 4:759a5c34e239 | 13 | #ifndef FACE_H |
| el17cd | 4:759a5c34e239 | 14 | #define FACE_H |
| el17cd | 3:2e31dfcb712a | 15 | #include "Face.h" |
| el17cd | 4:759a5c34e239 | 16 | #endif |
| el17cd | 4:759a5c34e239 | 17 | #include "Rasturizer.h" |
| el17cd | 4:759a5c34e239 | 18 | #include "Cube.h" |
| el17cd | 6:75031d14fc0d | 19 | #include "Gamepad.h" |
| el17cd | 3:2e31dfcb712a | 20 | |
| el17cd | 8:a667bc5050c1 | 21 | Serial pc(USBTX, USBRX); // tx, rx |
| el17cd | 8:a667bc5050c1 | 22 | Ticker ticker; |
| el17cd | 8:a667bc5050c1 | 23 | Gamepad gamepad; |
| el17cd | 9:5915fc800824 | 24 | std::vector<Cube> cubeVector; |
| el17cd | 9:5915fc800824 | 25 | std::vector<Face> faceVector; |
| el17cd | 0:efb5eec6b8ea | 26 | |
| el17cd | 8:a667bc5050c1 | 27 | |
| el17cd | 0:efb5eec6b8ea | 28 | int main() |
| el17cd | 0:efb5eec6b8ea | 29 | { |
| el17cd | 6:75031d14fc0d | 30 | |
| el17cd | 6:75031d14fc0d | 31 | gamepad.init(); |
| el17cd | 4:759a5c34e239 | 32 | Rasturizer renderer; |
| el17cd | 10:07a23afd5088 | 33 | for(int i = 0; i < 10; i++){ |
| el17cd | 10:07a23afd5088 | 34 | Cube cube(rand()%80-40,0,30+ i*20,5); |
| el17cd | 8:a667bc5050c1 | 35 | cubeVector.push_back(cube); |
| el17cd | 8:a667bc5050c1 | 36 | } |
| el17cd | 1:044238f7bdda | 37 | while(1) { |
| el17cd | 4:759a5c34e239 | 38 | renderer.clear(); |
| el17cd | 11:2cd6341136ca | 39 | renderer.drawHorizon(coord.x/10); |
| el17cd | 8:a667bc5050c1 | 40 | Vector2D coord = gamepad.get_coord(); |
| el17cd | 9:5915fc800824 | 41 | bool rotate1 = false; |
| el17cd | 9:5915fc800824 | 42 | if(gamepad.check_event(Gamepad::B_PRESSED) == true){ |
| el17cd | 9:5915fc800824 | 43 | rotate1 = true; |
| el17cd | 6:75031d14fc0d | 44 | } |
| el17cd | 9:5915fc800824 | 45 | bool rotate2 = false; |
| el17cd | 9:5915fc800824 | 46 | if(gamepad.check_event(Gamepad::X_PRESSED) == true){ |
| el17cd | 9:5915fc800824 | 47 | rotate2 = true; |
| el17cd | 7:15543cb10a14 | 48 | } |
| el17cd | 10:07a23afd5088 | 49 | |
| el17cd | 8:a667bc5050c1 | 50 | for (int c = 0; c< cubeVector.size(); c++) |
| el17cd | 8:a667bc5050c1 | 51 | { |
| el17cd | 9:5915fc800824 | 52 | if(rotate1){ |
| el17cd | 11:2cd6341136ca | 53 | cubeVector[c].rotateZ(0.2); |
| el17cd | 9:5915fc800824 | 54 | } |
| el17cd | 9:5915fc800824 | 55 | if(rotate2){ |
| el17cd | 11:2cd6341136ca | 56 | cubeVector[c].rotateZ(-0.2); |
| el17cd | 9:5915fc800824 | 57 | } |
| el17cd | 8:a667bc5050c1 | 58 | for (int i = 0; i < 6; i++){ |
| el17cd | 9:5915fc800824 | 59 | faceVector.push_back(cubeVector[c].getFace(i)); |
| el17cd | 10:07a23afd5088 | 60 | } |
| el17cd | 11:2cd6341136ca | 61 | cubeVector[c].translate(-coord.x*2,0,-4); |
| el17cd | 11:2cd6341136ca | 62 | |
| el17cd | 10:07a23afd5088 | 63 | if (cubeVector[c].despawn()){ |
| el17cd | 10:07a23afd5088 | 64 | cubeVector.erase(cubeVector.begin() + c); |
| el17cd | 10:07a23afd5088 | 65 | Cube cube(rand()%80-40,0,90,5); |
| el17cd | 10:07a23afd5088 | 66 | cubeVector.push_back(cube); |
| el17cd | 8:a667bc5050c1 | 67 | } |
| el17cd | 11:2cd6341136ca | 68 | if (cubeVector[c].tooClose()){ |
| el17cd | 11:2cd6341136ca | 69 | renderer.print("dead"); |
| el17cd | 11:2cd6341136ca | 70 | } |
| el17cd | 9:5915fc800824 | 71 | } |
| el17cd | 10:07a23afd5088 | 72 | |
| el17cd | 10:07a23afd5088 | 73 | |
| el17cd | 10:07a23afd5088 | 74 | |
| el17cd | 10:07a23afd5088 | 75 | for (int f = 0; f< faceVector.size(); f++){ |
| el17cd | 10:07a23afd5088 | 76 | for (int f2 = 0; f2< faceVector.size(); f2++){ |
| el17cd | 10:07a23afd5088 | 77 | if(faceVector[f2].getAvgZ() < faceVector[f2+1].getAvgZ()){ |
| el17cd | 10:07a23afd5088 | 78 | Face temp = faceVector[f2+1]; |
| el17cd | 10:07a23afd5088 | 79 | faceVector[f2+1] = faceVector[f2]; |
| el17cd | 10:07a23afd5088 | 80 | faceVector[f2] = temp; |
| el17cd | 9:5915fc800824 | 81 | } |
| el17cd | 8:a667bc5050c1 | 82 | } |
| el17cd | 8:a667bc5050c1 | 83 | } |
| el17cd | 10:07a23afd5088 | 84 | |
| el17cd | 9:5915fc800824 | 85 | for (int f = 0; f< faceVector.size() ; f++){ |
| el17cd | 10:07a23afd5088 | 86 | //pc.printf("%f\n", faceVector[f].getAvgZ()); |
| el17cd | 11:2cd6341136ca | 87 | renderer.drawFace(faceVector[f], coord.x/10); |
| el17cd | 10:07a23afd5088 | 88 | |
| el17cd | 9:5915fc800824 | 89 | } |
| el17cd | 10:07a23afd5088 | 90 | faceVector.clear(); |
| el17cd | 10:07a23afd5088 | 91 | // refresh the LCD so the pixels appear |
| el17cd | 10:07a23afd5088 | 92 | // this gives a refresh rate of 10 frames per second |
| el17cd | 10:07a23afd5088 | 93 | renderer.refresh(); |
| el17cd | 10:07a23afd5088 | 94 | wait_ms(1000/30); |
| el17cd | 0:efb5eec6b8ea | 95 | } |
| el17cd | 8:a667bc5050c1 | 96 | } |