Christopher Doel
/
el17cd
Cube Dodger A 3D 'endless runner' game
Cube/Cube.cpp@46:824ec81ff578, 2019-05-08 (annotated)
- Committer:
- el17cd
- Date:
- Wed May 08 18:17:59 2019 +0000
- Revision:
- 46:824ec81ff578
- Parent:
- 39:41dcf1604fdf
Potentiometer now changes screen contrast for use on other devices.; Final Submission.; I have read and agreed with the Statement of Academic Integrity.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17cd | 7:15543cb10a14 | 1 | //#include "mbed.h" |
el17cd | 16:64cd7bc094f9 | 2 | #ifndef CUBE_H |
el17cd | 16:64cd7bc094f9 | 3 | #define CUBE_H |
el17cd | 4:759a5c34e239 | 4 | #include "Cube.h" |
el17cd | 16:64cd7bc094f9 | 5 | #endif |
el17cd | 4:759a5c34e239 | 6 | |
el17cd | 25:3995271e411c | 7 | |
el17cd | 25:3995271e411c | 8 | |
el17cd | 16:64cd7bc094f9 | 9 | Cube::Cube(){ |
el17cd | 22:236319885874 | 10 | xPos = yPos = zPos = 0; //set cube position to origin and size to 5 |
el17cd | 19:ec4cb22accb0 | 11 | int size = 5; |
el17cd | 22:236319885874 | 12 | float v[8][3] = {{-size, size, -size}, //define xyz coordinates for all 8 verticies |
el17cd | 16:64cd7bc094f9 | 13 | {-size, size, size}, |
el17cd | 16:64cd7bc094f9 | 14 | {size, size, size}, |
el17cd | 16:64cd7bc094f9 | 15 | {size, size, -size}, |
el17cd | 16:64cd7bc094f9 | 16 | {-size, -size, -size}, |
el17cd | 16:64cd7bc094f9 | 17 | {-size, -size, size}, |
el17cd | 16:64cd7bc094f9 | 18 | {size, -size, size}, |
el17cd | 16:64cd7bc094f9 | 19 | {size, -size, -size}}; |
el17cd | 16:64cd7bc094f9 | 20 | |
el17cd | 22:236319885874 | 21 | for(int vertex = 0; vertex < 8; vertex++){ //copy temporary array to private array |
el17cd | 16:64cd7bc094f9 | 22 | for(int axis = 0; axis < 3; axis++){ |
el17cd | 16:64cd7bc094f9 | 23 | verticies[vertex][axis] = v[vertex][axis]; |
el17cd | 16:64cd7bc094f9 | 24 | } |
el17cd | 16:64cd7bc094f9 | 25 | } |
el17cd | 22:236319885874 | 26 | updateFacesVerticies(verticies); //populate verticies of 6 faces |
el17cd | 39:41dcf1604fdf | 27 | setPartiallyVisible(); //set front, left and right faces to visible |
el17cd | 16:64cd7bc094f9 | 28 | } |
el17cd | 16:64cd7bc094f9 | 29 | |
el17cd | 39:41dcf1604fdf | 30 | void Cube::setPartiallyVisible() { //set front, left and right faces to be rendered(only these can be seen during gameplay) |
el17cd | 13:f4de03202477 | 31 | faces[1].setVisible(true); |
el17cd | 13:f4de03202477 | 32 | faces[3].setVisible(true); |
el17cd | 14:885915260e25 | 33 | faces[4].setVisible(true); |
el17cd | 4:759a5c34e239 | 34 | } |
el17cd | 4:759a5c34e239 | 35 | |
el17cd | 39:41dcf1604fdf | 36 | void Cube::setVisible() { //set front, left and right faces to be rendered(only these can be seen during gameplay) |
el17cd | 39:41dcf1604fdf | 37 | faces[0].setVisible(true); |
el17cd | 39:41dcf1604fdf | 38 | faces[1].setVisible(true); |
el17cd | 39:41dcf1604fdf | 39 | faces[2].setVisible(true); |
el17cd | 39:41dcf1604fdf | 40 | faces[3].setVisible(true); |
el17cd | 39:41dcf1604fdf | 41 | faces[4].setVisible(true); |
el17cd | 39:41dcf1604fdf | 42 | faces[5].setVisible(true); |
el17cd | 39:41dcf1604fdf | 43 | } |
el17cd | 39:41dcf1604fdf | 44 | |
el17cd | 22:236319885874 | 45 | void Cube::updateFacesVerticies(float (&vert)[8][3]) { //assign xyz coordinates for 6 faces |
el17cd | 33:02c5048b3b3f | 46 | float face0Points[4][3] = {{vert[0][0], vert[0][1], vert[0][2]}, |
el17cd | 33:02c5048b3b3f | 47 | {vert[1][0],vert[1][1], vert[1][2]}, {vert[2][0], vert[2][1], |
el17cd | 33:02c5048b3b3f | 48 | vert[2][2]}, {vert[3][0],vert[3][1], vert[3][2]}}; |
el17cd | 33:02c5048b3b3f | 49 | float face1Points[4][3] = {{vert[0][0], vert[0][1], vert[0][2]}, |
el17cd | 33:02c5048b3b3f | 50 | {vert[1][0], vert[1][1], vert[1][2]}, {vert[5][0], vert[5][1], |
el17cd | 33:02c5048b3b3f | 51 | vert[5][2]}, {vert[4][0], vert[4][1], vert[4][2]}}; |
el17cd | 33:02c5048b3b3f | 52 | float face2Points[4][3] = {{vert[1][0], vert[1][1], vert[1][2]}, |
el17cd | 33:02c5048b3b3f | 53 | {vert[2][0], vert[2][1], vert[2][2]}, {vert[6][0], vert[6][1], |
el17cd | 33:02c5048b3b3f | 54 | vert[6][2]}, {vert[5][0], vert[5][1], vert[5][2]}}; |
el17cd | 33:02c5048b3b3f | 55 | float face3Points[4][3] = {{vert[3][0], vert[3][1], vert[3][2]}, |
el17cd | 33:02c5048b3b3f | 56 | {vert[2][0], vert[2][1], vert[2][2]}, {vert[6][0], vert[6][1], |
el17cd | 33:02c5048b3b3f | 57 | vert[6][2]}, {vert[7][0], vert[7][1], vert[7][2]}}; |
el17cd | 33:02c5048b3b3f | 58 | float face4Points[4][3] = {{vert[0][0], vert[0][1], vert[0][2]}, |
el17cd | 33:02c5048b3b3f | 59 | {vert[3][0], vert[3][1], vert[3][2]}, {vert[7][0], vert[7][1], |
el17cd | 33:02c5048b3b3f | 60 | vert[7][2]}, {vert[4][0], vert[4][1], vert[4][2]}}; |
el17cd | 33:02c5048b3b3f | 61 | float face5Points[4][3] = {{vert[4][0], vert[4][1], vert[4][2]}, |
el17cd | 33:02c5048b3b3f | 62 | {vert[5][0], vert[5][1], vert[5][2]}, {vert[6][0], vert[6][1], |
el17cd | 33:02c5048b3b3f | 63 | vert[6][2]}, {vert[7][0], vert[7][1], vert[7][2]}}; |
el17cd | 33:02c5048b3b3f | 64 | assignFacesVerticies(face0Points, face1Points, face2Points, face3Points, |
el17cd | 33:02c5048b3b3f | 65 | face4Points, face5Points);//copy temporary arrays to all 6 faces private arrays |
el17cd | 22:236319885874 | 66 | } |
el17cd | 22:236319885874 | 67 | |
el17cd | 22:236319885874 | 68 | void Cube::assignFacesVerticies(float (&face0Points)[4][3], |
el17cd | 22:236319885874 | 69 | float (&face1Points)[4][3], float (&face2Points)[4][3], |
el17cd | 22:236319885874 | 70 | float (&face3Points)[4][3], float (&face4Points)[4][3], |
el17cd | 22:236319885874 | 71 | float (&face5Points)[4][3]) { //set each faces verticies based on cubes verticies |
el17cd | 22:236319885874 | 72 | |
el17cd | 22:236319885874 | 73 | faces[0].setVerticies(face0Points); |
el17cd | 22:236319885874 | 74 | faces[1].setVerticies(face1Points); |
el17cd | 22:236319885874 | 75 | faces[2].setVerticies(face2Points); |
el17cd | 22:236319885874 | 76 | faces[3].setVerticies(face3Points); |
el17cd | 22:236319885874 | 77 | faces[4].setVerticies(face4Points); |
el17cd | 22:236319885874 | 78 | faces[5].setVerticies(face5Points); |
el17cd | 22:236319885874 | 79 | } |
el17cd | 22:236319885874 | 80 | |
el17cd | 22:236319885874 | 81 | Face Cube::getFace(int index) { //return face of certain index |
el17cd | 4:759a5c34e239 | 82 | return faces[index]; |
el17cd | 5:54d3b36fec2c | 83 | } |
el17cd | 5:54d3b36fec2c | 84 | |
el17cd | 22:236319885874 | 85 | bool Cube::tooClose() { //return if cube is a distance of 7 to origin |
el17cd | 22:236319885874 | 86 | if (sqrt(pow(xPos, 2) + pow(yPos, 2) + pow(zPos, 2)) < 7) { //use pythagoras to determine distance |
el17cd | 11:2cd6341136ca | 87 | return true; |
el17cd | 11:2cd6341136ca | 88 | } |
el17cd | 11:2cd6341136ca | 89 | return false; |
el17cd | 11:2cd6341136ca | 90 | } |
el17cd | 11:2cd6341136ca | 91 | |
el17cd | 22:236319885874 | 92 | void Cube::rotateY(float angle) { //rotate cube in Y axis |
el17cd | 17:3c9672c6e532 | 93 | float originalX = xPos; |
el17cd | 17:3c9672c6e532 | 94 | float originalZ = zPos; |
el17cd | 17:3c9672c6e532 | 95 | float originalY = yPos; |
el17cd | 22:236319885874 | 96 | translate(-originalX, -originalY, -originalZ); //translate cube to origin |
el17cd | 16:64cd7bc094f9 | 97 | |
el17cd | 10:07a23afd5088 | 98 | |
el17cd | 20:3ca430241df0 | 99 | for( int i = 0; i<8; i++){ |
el17cd | 17:3c9672c6e532 | 100 | float x = verticies[i][0]; |
el17cd | 17:3c9672c6e532 | 101 | float z = verticies[i][2]; |
el17cd | 7:15543cb10a14 | 102 | |
el17cd | 22:236319885874 | 103 | verticies[i][0] = x*cos(angle)-z*sin(angle); //perform rotation on all verticies |
el17cd | 8:a667bc5050c1 | 104 | verticies[i][2] = x*sin(angle)+z*cos(angle); |
el17cd | 8:a667bc5050c1 | 105 | } |
el17cd | 22:236319885874 | 106 | translate(originalX, originalY, originalZ); //translate back to original position |
el17cd | 8:a667bc5050c1 | 107 | } |
el17cd | 8:a667bc5050c1 | 108 | |
el17cd | 11:2cd6341136ca | 109 | |
el17cd | 22:236319885874 | 110 | void Cube::rotateZ(float angle) { //rotate cube in Z axis |
el17cd | 17:3c9672c6e532 | 111 | float originalX = xPos; |
el17cd | 17:3c9672c6e532 | 112 | float originalZ = zPos; |
el17cd | 17:3c9672c6e532 | 113 | float originalY = yPos; |
el17cd | 22:236319885874 | 114 | translate(-originalX, -originalY, -originalZ); //translate cube to origin |
el17cd | 11:2cd6341136ca | 115 | |
el17cd | 22:236319885874 | 116 | for( int i = 0; i<8; i++) { |
el17cd | 17:3c9672c6e532 | 117 | float y = verticies[i][1]; |
el17cd | 17:3c9672c6e532 | 118 | float x = verticies[i][0]; |
el17cd | 11:2cd6341136ca | 119 | |
el17cd | 22:236319885874 | 120 | verticies[i][0] = x*cos(angle)-y*sin(angle); //perform rotation on all verticies |
el17cd | 11:2cd6341136ca | 121 | verticies[i][1] = y*cos(angle)+x*sin(angle); |
el17cd | 11:2cd6341136ca | 122 | } |
el17cd | 22:236319885874 | 123 | translate(originalX, originalY, originalZ); //translate back to original position |
el17cd | 11:2cd6341136ca | 124 | } |
el17cd | 11:2cd6341136ca | 125 | |
el17cd | 22:236319885874 | 126 | void Cube::rotateX(float angle) { //rotate cube in X axis |
el17cd | 17:3c9672c6e532 | 127 | float originalX = xPos; |
el17cd | 17:3c9672c6e532 | 128 | float originalZ = zPos; |
el17cd | 17:3c9672c6e532 | 129 | float originalY = yPos; |
el17cd | 22:236319885874 | 130 | translate(-originalX, -originalY, -originalZ); //translate cube to origin |
el17cd | 10:07a23afd5088 | 131 | |
el17cd | 22:236319885874 | 132 | for( int i = 0; i<8; i++) { |
el17cd | 17:3c9672c6e532 | 133 | float y = verticies[i][1]; |
el17cd | 17:3c9672c6e532 | 134 | float z = verticies[i][2]; |
el17cd | 8:a667bc5050c1 | 135 | |
el17cd | 8:a667bc5050c1 | 136 | |
el17cd | 22:236319885874 | 137 | verticies[i][1] = y*cos(angle)-z*sin(angle); //perform rotation on all verticies |
el17cd | 8:a667bc5050c1 | 138 | verticies[i][2] = z*cos(angle)+y*sin(angle); |
el17cd | 7:15543cb10a14 | 139 | |
el17cd | 6:75031d14fc0d | 140 | } |
el17cd | 22:236319885874 | 141 | translate(originalX, originalY, originalZ); //translate back to original position |
el17cd | 6:75031d14fc0d | 142 | } |
el17cd | 6:75031d14fc0d | 143 | |
el17cd | 6:75031d14fc0d | 144 | |
el17cd | 22:236319885874 | 145 | void Cube::translate(float x, float y, float z) { //translate cube by xyz amount |
el17cd | 22:236319885874 | 146 | xPos += x; //translate cube cooridnates |
el17cd | 10:07a23afd5088 | 147 | yPos += y; |
el17cd | 10:07a23afd5088 | 148 | zPos += z; |
el17cd | 10:07a23afd5088 | 149 | |
el17cd | 22:236319885874 | 150 | for( int i = 0; i<8; i++) { |
el17cd | 22:236319885874 | 151 | verticies[i][0] += x; //translate vertex coorinates |
el17cd | 6:75031d14fc0d | 152 | verticies[i][1] += y; |
el17cd | 6:75031d14fc0d | 153 | verticies[i][2] += z; |
el17cd | 5:54d3b36fec2c | 154 | } |
el17cd | 22:236319885874 | 155 | updateFacesVerticies(verticies); //update all face verticies |
el17cd | 14:885915260e25 | 156 | } |
el17cd | 14:885915260e25 | 157 | |
el17cd | 22:236319885874 | 158 | void Cube::resetPos() { //translate cube to origin |
el17cd | 20:3ca430241df0 | 159 | float x = xPos; |
el17cd | 20:3ca430241df0 | 160 | float y = yPos; |
el17cd | 20:3ca430241df0 | 161 | float z = zPos; |
el17cd | 20:3ca430241df0 | 162 | |
el17cd | 22:236319885874 | 163 | xPos = 0; //set cube position to 0 on all axis' |
el17cd | 20:3ca430241df0 | 164 | yPos = 0; |
el17cd | 20:3ca430241df0 | 165 | zPos = 0; |
el17cd | 20:3ca430241df0 | 166 | |
el17cd | 22:236319885874 | 167 | for( int i = 0; i<8; i++) { |
el17cd | 22:236319885874 | 168 | verticies[i][0] -= x; //translate verticies by negative of current position |
el17cd | 20:3ca430241df0 | 169 | verticies[i][1] -= y; |
el17cd | 20:3ca430241df0 | 170 | verticies[i][2] -= z; |
el17cd | 20:3ca430241df0 | 171 | } |
el17cd | 22:236319885874 | 172 | updateFacesVerticies(verticies); //update all face verticies |
el17cd | 20:3ca430241df0 | 173 | } |
el17cd | 20:3ca430241df0 | 174 | |
el17cd | 24:4e8bdcb74266 | 175 | bool Cube::despawn() { //Accessor to return whether the cube is behind perspective |
el17cd | 14:885915260e25 | 176 | if(zPos < 0){ |
el17cd | 10:07a23afd5088 | 177 | return true; |
el17cd | 10:07a23afd5088 | 178 | } |
el17cd | 10:07a23afd5088 | 179 | return false; |
el17cd | 4:759a5c34e239 | 180 | } |