ELEC2645 (2018/19) / Mbed 2 deprecated el17cd

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
el17cd
Date:
Wed Apr 03 15:11:12 2019 +0000
Parent:
21:6b5d2d75e083
Child:
23:eb50ab95bb53
Commit message:
Game now runs at a target fps of 60

Changed in this revision

Cube/Cube.cpp Show annotated file Show diff for this revision Revisions of this file
Cube/Cube.h Show annotated file Show diff for this revision Revisions of this file
Face/Face.cpp Show annotated file Show diff for this revision Revisions of this file
Game/Game.cpp Show annotated file Show diff for this revision Revisions of this file
Game/Game.h Show annotated file Show diff for this revision Revisions of this file
Rasturizer/Rasturizer.cpp Show annotated file Show diff for this revision Revisions of this file
Rasturizer/Rasturizer.h Show annotated file Show diff for this revision Revisions of this file
--- a/Cube/Cube.cpp	Tue Apr 02 17:40:59 2019 +0000
+++ b/Cube/Cube.cpp	Wed Apr 03 15:11:12 2019 +0000
@@ -3,15 +3,11 @@
 #define CUBE_H
 #include "Cube.h"
 #endif
-#include "math.h"
 
 Cube::Cube(){
-    xPos = 0;
-    yPos = 0;
-    zPos = 0;
+    xPos = yPos = zPos = 0; //set cube position to origin and size to 5
     int size = 5;
-    
-    float v[8][3] = {{-size, size, -size},
+    float v[8][3] = {{-size, size, -size}, //define xyz coordinates for all 8 verticies
     {-size, size, size},
     {size, size, size},
     {size, size, -size},
@@ -20,161 +16,152 @@
     {size, -size, size},
     {size, -size, -size}};
     
-    for(int vertex = 0; vertex < 8; vertex++){
+    for(int vertex = 0; vertex < 8; vertex++){ //copy temporary array to private array
         for(int axis = 0; axis < 3; axis++){
             verticies[vertex][axis] = v[vertex][axis];
         }
     }
-    updateFaceVerticies(verticies);
-
-    faces[0].setVisible(false);
-    faces[1].setVisible(true);
-    faces[2].setVisible(false);
-    faces[3].setVisible(true);
-    faces[4].setVisible(true);  
-    faces[5].setVisible(false);
+    updateFacesVerticies(verticies); //populate verticies of 6 faces
+    setVisible(); //set front, left and right faces to visible
     }
 
-Cube::Cube(float x, float y, float z, float size){
-     xPos = x;
-     yPos = y;
-     zPos = z;
-     float v[8][3] = {{x-size, y+size, z-size},
-    {x-size, y+size, z+size},
-    {x+size, y+size, z+size},
-    {x+size, y+size, z-size},
-    {x-size, y-size, z-size},
-    {x-size, y-size, z+size},
-    {x+size, y-size, z+size},
-    {x+size, y-size, z-size}};
-    
-    for(int vertex = 0; vertex < 8; vertex++){
-        for(int axis = 0; axis < 3; axis++){
-            verticies[vertex][axis] = v[vertex][axis];
-        }
-    }
-    updateFaceVerticies(verticies);
-
-    faces[0].setVisible(false);
+void Cube::setVisible() {  //set front, left and right faces to be rendered
     faces[1].setVisible(true);
-    faces[2].setVisible(false);
     faces[3].setVisible(true);
     faces[4].setVisible(true);  
-    faces[5].setVisible(false);
 }
 
-Face Cube::getFace(int index){
+void Cube::updateFacesVerticies(float (&vert)[8][3]) { //assign xyz coordinates for 6 faces
+    float face0Points[4][3] = {{vert[0][0], vert[0][1], vert[0][2]}, {vert[1][0],
+    vert[1][1], vert[1][2]}, {vert[2][0], vert[2][1], vert[2][2]}, {vert[3][0],
+    vert[3][1], vert[3][2]}};
+    float face1Points[4][3] = {{vert[0][0], vert[0][1], vert[0][2]}, {vert[1][0],
+    vert[1][1], vert[1][2]}, {vert[5][0], vert[5][1], vert[5][2]}, {vert[4][0],
+    vert[4][1], vert[4][2]}};
+    float face2Points[4][3] = {{vert[1][0], vert[1][1], vert[1][2]}, {vert[2][0],
+    vert[2][1], vert[2][2]}, {vert[6][0], vert[6][1], vert[6][2]}, {vert[5][0],
+    vert[5][1], vert[5][2]}};
+    float face3Points[4][3] = {{vert[3][0], vert[3][1], vert[3][2]}, {vert[2][0],
+    vert[2][1], vert[2][2]}, {vert[6][0], vert[6][1], vert[6][2]}, {vert[7][0],
+    vert[7][1], vert[7][2]}};
+    float face4Points[4][3] = {{vert[0][0], vert[0][1], vert[0][2]}, {vert[3][0],
+    vert[3][1], vert[3][2]}, {vert[7][0], vert[7][1], vert[7][2]}, {vert[4][0],
+    vert[4][1], vert[4][2]}};
+    float face5Points[4][3] = {{vert[4][0], vert[4][1], vert[4][2]}, {vert[5][0],
+    vert[5][1], vert[5][2]}, {vert[6][0], vert[6][1], vert[6][2]}, {vert[7][0],
+    vert[7][1], vert[7][2]}};
+     //copy temporary arrays to all 6 faces private arrays
+    assignFacesVerticies(face0Points, face1Points, face2Points, face3Points, face4Points, face5Points);
+}
+
+void Cube::assignFacesVerticies(float (&face0Points)[4][3], 
+    float (&face1Points)[4][3], float (&face2Points)[4][3],
+    float (&face3Points)[4][3], float (&face4Points)[4][3],
+    float (&face5Points)[4][3]) { //set each faces verticies based on cubes verticies
+
+    faces[0].setVerticies(face0Points);
+    faces[1].setVerticies(face1Points);
+    faces[2].setVerticies(face2Points);
+    faces[3].setVerticies(face3Points);
+    faces[4].setVerticies(face4Points);
+    faces[5].setVerticies(face5Points);
+}
+
+Face Cube::getFace(int index) { //return face of certain index
     return faces[index];
 }
 
-bool Cube::tooClose(){
-    if (sqrt(pow(xPos, 2) + pow(yPos, 2) + pow(zPos, 2)) < 7){
+bool Cube::tooClose() { //return if cube is a distance of 7 to origin
+    if (sqrt(pow(xPos, 2) + pow(yPos, 2) + pow(zPos, 2)) < 7) { //use pythagoras to determine distance
         return true;
     }
     return false;
-
 }
 
-void Cube::rotateY(float angle){
+void Cube::rotateY(float angle) { //rotate cube in Y axis
     float originalX = xPos;
     float originalZ = zPos;
     float originalY = yPos;
-    translate(-originalX, -originalY, -originalZ);
+    translate(-originalX, -originalY, -originalZ); //translate cube to origin
 
     
     for( int i = 0; i<8; i++){
         float x = verticies[i][0];
         float z = verticies[i][2];
         
-        verticies[i][0] = x*cos(angle)-z*sin(angle);
+        verticies[i][0] = x*cos(angle)-z*sin(angle); //perform rotation on all verticies
         verticies[i][2] = x*sin(angle)+z*cos(angle);
     }
-    translate(originalX, originalY, originalZ);
+    translate(originalX, originalY, originalZ); //translate back to original position
 }
 
 
-void Cube::rotateZ(float angle){
+void Cube::rotateZ(float angle) { //rotate cube in Z axis
     float originalX = xPos;
     float originalZ = zPos;
     float originalY = yPos;
-    translate(-originalX, -originalY, -originalZ);
+    translate(-originalX, -originalY, -originalZ); //translate cube to origin
     
-    for( int i = 0; i<8; i++){
+    for( int i = 0; i<8; i++) {
         float y = verticies[i][1];
         float x = verticies[i][0];
         
-        verticies[i][0] = x*cos(angle)-y*sin(angle);
+        verticies[i][0] = x*cos(angle)-y*sin(angle); //perform rotation on all verticies
         verticies[i][1] = y*cos(angle)+x*sin(angle);
     }
-    translate(originalX, originalY, originalZ);
+    translate(originalX, originalY, originalZ); //translate back to original position
 }
 
-void Cube::rotateX(float angle){
+void Cube::rotateX(float angle) { //rotate cube in X axis
     float originalX = xPos;
     float originalZ = zPos;
     float originalY = yPos;
-    translate(-originalX, -originalY, -originalZ);
+    translate(-originalX, -originalY, -originalZ); //translate cube to origin
     
-    for( int i = 0; i<8; i++){
+    for( int i = 0; i<8; i++) {
         float y = verticies[i][1];
         float z = verticies[i][2];
         
         
-        verticies[i][1] = y*cos(angle)-z*sin(angle);
+        verticies[i][1] = y*cos(angle)-z*sin(angle); //perform rotation on all verticies
         verticies[i][2] = z*cos(angle)+y*sin(angle);
         
     }
-    translate(originalX, originalY, originalZ);
+    translate(originalX, originalY, originalZ); //translate back to original position
 }
 
 
-void Cube::translate(float x, float y, float z){
-    
-    xPos += x;
+void Cube::translate(float x, float y, float z) { //translate cube by xyz amount
+    xPos += x; //translate cube cooridnates
     yPos += y;
     zPos += z;
     
-    for( int i = 0; i<8; i++){
-        verticies[i][0] += x;
+    for( int i = 0; i<8; i++) {
+        verticies[i][0] += x; //translate vertex coorinates
         verticies[i][1] += y;
         verticies[i][2] += z;
     }
-    updateFaceVerticies(verticies);
+    updateFacesVerticies(verticies); //update all face verticies
 }
 
-void Cube::resetPos(){
+void Cube::resetPos() { //translate cube to origin
     float x = xPos;
     float y = yPos;
     float z = zPos;
     
-    xPos = 0;
+    xPos = 0; //set cube position to 0 on all axis'
     yPos = 0;
     zPos = 0;
     
-    for( int i = 0; i<8; i++){
-        verticies[i][0] -= x;
+    for( int i = 0; i<8; i++) {
+        verticies[i][0] -= x; //translate verticies by negative of current position
         verticies[i][1] -= y;
         verticies[i][2] -= z;
     }
-    updateFaceVerticies(verticies);
+    updateFacesVerticies(verticies);  //update all face verticies
 }
 
-void Cube::updateFaceVerticies(float (&verticies)[8][3]){
-    float f0Points[4][3] = {{verticies[0][0], verticies[0][1], verticies[0][2]}, {verticies[1][0], verticies[1][1], verticies[1][2]}, {verticies[2][0], verticies[2][1], verticies[2][2]}, {verticies[3][0], verticies[3][1], verticies[3][2]}};
-    float f1Points[4][3] = {{verticies[0][0], verticies[0][1], verticies[0][2]}, {verticies[1][0], verticies[1][1], verticies[1][2]}, {verticies[5][0], verticies[5][1], verticies[5][2]}, {verticies[4][0], verticies[4][1], verticies[4][2]}};
-    float f2Points[4][3] = {{verticies[1][0], verticies[1][1], verticies[1][2]}, {verticies[2][0], verticies[2][1], verticies[2][2]}, {verticies[6][0], verticies[6][1], verticies[6][2]}, {verticies[5][0], verticies[5][1], verticies[5][2]}};
-    float f3Points[4][3] = {{verticies[3][0], verticies[3][1], verticies[3][2]}, {verticies[2][0], verticies[2][1], verticies[2][2]}, {verticies[6][0], verticies[6][1], verticies[6][2]}, {verticies[7][0], verticies[7][1], verticies[7][2]}};
-    float f4Points[4][3] = {{verticies[0][0], verticies[0][1], verticies[0][2]}, {verticies[3][0], verticies[3][1], verticies[3][2]}, {verticies[7][0], verticies[7][1], verticies[7][2]}, {verticies[4][0], verticies[4][1], verticies[4][2]}};
-    float f5Points[4][3] = {{verticies[4][0], verticies[4][1], verticies[4][2]}, {verticies[5][0], verticies[5][1], verticies[5][2]}, {verticies[6][0], verticies[6][1], verticies[6][2]}, {verticies[7][0], verticies[7][1], verticies[7][2]}};
-    faces[0].setVerticies(f0Points);
-    faces[1].setVerticies(f1Points);
-    faces[2].setVerticies(f2Points);
-    faces[3].setVerticies(f3Points);
-    faces[4].setVerticies(f4Points);
-    faces[5].setVerticies(f5Points);
-}
-
-bool Cube::despawn(){
+bool Cube::despawn() { //return whether the cube is behind perspective
     if(zPos < 0){
         return true;
     }
--- a/Cube/Cube.h	Tue Apr 02 17:40:59 2019 +0000
+++ b/Cube/Cube.h	Wed Apr 03 15:11:12 2019 +0000
@@ -11,9 +11,13 @@
     float xPos, yPos, zPos;
   public:
     Cube();
-    Cube(float x, float y, float z, float size);
     Face getFace(int index);
-    void updateFaceVerticies(float (&verticies)[8][3]);
+    void setVisible();
+    void updateFacesVerticies(float (&vert)[8][3]);
+    void assignFacesVerticies(float (&face0Points)[4][3],
+    float (&face1Points)[4][3], float (&face2Points)[4][3],
+    float (&face3Points)[4][3], float (&face4Points)[4][3],
+    float (&face5Points)[4][3]);
     bool tooClose();
     void rotateX(float angle);
     void rotateY(float angle);
--- a/Face/Face.cpp	Tue Apr 02 17:40:59 2019 +0000
+++ b/Face/Face.cpp	Wed Apr 03 15:11:12 2019 +0000
@@ -5,6 +5,7 @@
 #endif
 
 Face::Face(){
+    visible = false;
 }
 
 bool Face::getVisible(){
--- a/Game/Game.cpp	Tue Apr 02 17:40:59 2019 +0000
+++ b/Game/Game.cpp	Wed Apr 03 15:11:12 2019 +0000
@@ -1,14 +1,12 @@
 #include "Game.h"
 
-
-
 Game::Game(){
-    noOfCubes = 18;
+    noOfCubes = 25;
     homeSelection = 0;
     gamepad.init();
     renderer.init();
     for(int i = 0; i < noOfCubes; i++){
-        cubeArray[i].translate(rand()%100-50,0,20+ i*10);
+        cubeArray[i].translate(rand()%250-125,0,20+ i*10);
     }
 }
 
@@ -16,39 +14,39 @@
     score = 0;
     selection = true;
     playing = true;
-    //ticker.attach(this, &Game::addScore, 0.1);
+
     while(1) {
-        Vector2D coord = gamepad.get_coord();
         renderer.clear();
+        coord = gamepad.get_coord();
+        
         renderer.drawHorizon(coord.x/15);
+        
         for (int c = 0; c< noOfCubes; c++){  
             if(playing){
                 if(score < 2000)
-                    cubeArray[c].translate(-coord.x*1.4f,0,-2.5f-(float)score/500);
+                    cubeArray[c].translate(-coord.x*1.4f,0,-1-(float)score/1000);
                 else{
-                    cubeArray[c].translate(-coord.x*1.4f,0,-4.5f);
+                    cubeArray[c].translate(-coord.x*1.4f,0,-3);
                 }
             }
             else{
                 coord.x = 0;
-                coord.y = 0;
             }
             for (int i = 0; i < 6; i++){
                 faceArray[c*6 + i] = cubeArray[c].getFace(i);
             }
             if (cubeArray[c].despawn()){
                 cubeArray[c].resetPos();
-                cubeArray[c].translate(rand()%180-90,0,140);
+                cubeArray[c].translate(rand()%250-125,0,140);
             }
             if (cubeArray[c].tooClose()){
                 playing = false;
-                //ticker.detach();
                 cubeArray[c].resetPos();
-                cubeArray[c].translate(rand()%180-90,0,140);
+                cubeArray[c].translate(rand()%250-125,0,140);
             }
         }
         
-        renderer.drawAllFaces(faceArray, noOfCubes, coord.x);//faceArray, noOfCubes, coord.x);
+        renderer.drawAllFaces(faceArray, noOfCubes, coord.x);
         
         if(!playing){
             deathScreen();
@@ -56,15 +54,17 @@
                 break;
             }
         }
+        else{
+            addScore();
+        }
         
                 
         char buf[5];
-        sprintf(buf, "%d", score);
+        sprintf(buf, "%d", score/3);
         renderer.print(buf, 0, 0);
         memset(buf, 0, sizeof buf);
         renderer.refresh();
-
-        wait_ms(1000/30);
+        wait_ms(1000/60);
     }
 }
 
@@ -75,6 +75,7 @@
 void Game::resetScore(){
     score = 0;
 }
+
 bool Game::deathButtonSelections(){
         if(gamepad.check_event(Gamepad::Y_PRESSED) == true){
             selection = true;
@@ -86,7 +87,6 @@
             playing = true;
             score = 0;
             resetScore();
-            //ticker.attach(this, &Game::addScore, 0.5);
         }
         else if(selection == false && gamepad.check_event(Gamepad::B_PRESSED) == true){
             resetScore();
@@ -120,7 +120,7 @@
         homeButtonSelections();
         renderer.drawHomeScreen(homeSelection);
         renderer.refresh();
-        wait_ms(1000/30);
+        wait_ms(1000/60);
     }
 }
    
\ No newline at end of file
--- a/Game/Game.h	Tue Apr 02 17:40:59 2019 +0000
+++ b/Game/Game.h	Wed Apr 03 15:11:12 2019 +0000
@@ -18,6 +18,7 @@
     bool selection;
     bool playing;
     int score;
+    Vector2D coord;
     Cube cubeArray[30];
     Face faceArray[180];
 
--- a/Rasturizer/Rasturizer.cpp	Tue Apr 02 17:40:59 2019 +0000
+++ b/Rasturizer/Rasturizer.cpp	Wed Apr 03 15:11:12 2019 +0000
@@ -10,7 +10,6 @@
     }
 }
 
-
 void Rasturizer::init(){
     lcd.init();
     fov = 50;
@@ -34,17 +33,10 @@
 
 void Rasturizer::drawFace(Face *face, float angle){
     float points[4][3];
-    float y;
-    float x;
+    float y, x;
+    int diffX1, diffY1, diffX2, diffY2;
+    float step, stepSmall1, stepSmall2;
     
-    int diffX1;
-    int diffY1;
-    int diffX2;
-    int diffY2;
-    
-    float step;
-    float stepSmall1;
-    float stepSmall2;
     for(int vertex = 0; vertex < 4; vertex++){
         for(int axis = 0; axis < 3; axis++){
             points[vertex][axis] = face->getVertexValue(vertex, axis);
@@ -97,10 +89,6 @@
             1);
 
     }
-    //if((points[0][2] > 10 || points[1][2] > 10 || points[2][2] > 10|| points[3][2] > 10)){
-    //}
-    
-
 }
 void Rasturizer::drawAllFaces(Face *faceArray, int noOfCubes, float angle){
     Face temp;
@@ -207,7 +195,6 @@
     lcd.printString("Help",35,3);
     lcd.drawRect(24, 22, 45, 10, FILL_TRANSPARENT);
 
-    
     lcd.drawRect(24, 38, 45, 10, FILL_WHITE);
     lcd.printString("Quit",35,5);
     lcd.drawRect(24, 38, 45, 10, FILL_TRANSPARENT);
@@ -217,8 +204,8 @@
         faces[i] = selectionCube.getFace(i);
     }
     drawAllFaces(faces, 1, 0);
-    selectionCube.rotateX(-0.1);
-    selectionCube.rotateY(-0.05);
-    selectionCube.rotateZ(0.08);
+    selectionCube.rotateX(-0.05);
+    selectionCube.rotateY(-0.025);
+    selectionCube.rotateZ(0.04);
     selectionCube.translate(-x, -y, -z);
 }
\ No newline at end of file
--- a/Rasturizer/Rasturizer.h	Tue Apr 02 17:40:59 2019 +0000
+++ b/Rasturizer/Rasturizer.h	Wed Apr 03 15:11:12 2019 +0000
@@ -10,9 +10,6 @@
 #include "Cube.h"
 #endif
 
-
-
-
 class Rasturizer {
   private:
     float fov;
@@ -25,7 +22,7 @@
     float xTo2D(float x, float z);
     float yTo2D(float y, float z);
     void drawHorizon(float angle);
-    void drawFace(Face *,m face, float angle);
+    void drawFace(Face *face, float angle);
     void drawAllFaces(Face *faceArray, int noOfFaces, float angle);
     bool checkOnScreen(float (&Points)[4][3]);
     void print(const char *text, int x, int y);