ELEC2645 (2018/19) / Mbed 2 deprecated el17cd

Dependencies:   mbed

Committer:
el17cd
Date:
Sun Mar 31 13:55:51 2019 +0000
Revision:
14:885915260e25
Parent:
13:f4de03202477
Child:
15:8fbbdefbe720
Added score and ensured players cannot hold left/right and win (at current;  speed)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el17cd 1:044238f7bdda 1 #include "mbed.h"
el17cd 1:044238f7bdda 2 #include "Rasturizer.h"
el17cd 2:a5bc7b3779f7 3
el17cd 4:759a5c34e239 4 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
el17cd 3:2e31dfcb712a 5
el17cd 4:759a5c34e239 6 Rasturizer::Rasturizer(){
el17cd 3:2e31dfcb712a 7 lcd.init();
el17cd 14:885915260e25 8 fov = 50;
el17cd 14:885915260e25 9 /*double tempPoints[4][3] = {{43, 2.5, 30}, {43, 2.5, 200}, {43, -2.5, 200}, {43, -2.5, 30}};
el17cd 14:885915260e25 10 for(int vertex = 0; vertex < 8; vertex++){
el17cd 14:885915260e25 11 for(int axis = 0; axis < 3; axis++){
el17cd 14:885915260e25 12 edgePoints[vertex][axis] = tempPoints[vertex][axis];
el17cd 14:885915260e25 13 }
el17cd 14:885915260e25 14 }
el17cd 14:885915260e25 15 edge.setVerticies(edgePoints);
el17cd 14:885915260e25 16 edge.setVisible(true);*/
el17cd 14:885915260e25 17 }
el17cd 14:885915260e25 18
el17cd 14:885915260e25 19 double Rasturizer::xTo2D(double x, double z){
el17cd 14:885915260e25 20 return x * (fov/z) + 42;
el17cd 14:885915260e25 21 }
el17cd 14:885915260e25 22
el17cd 14:885915260e25 23 double Rasturizer::yTo2D(double y, double z){
el17cd 14:885915260e25 24 return y * (fov/z) + 21;
el17cd 4:759a5c34e239 25 }
el17cd 1:044238f7bdda 26
el17cd 11:2cd6341136ca 27 void Rasturizer::drawHorizon(double angle){
el17cd 11:2cd6341136ca 28 lcd.drawLine(0,
el17cd 12:b69657862610 29 21-rint(angle*50),
el17cd 11:2cd6341136ca 30 84,
el17cd 12:b69657862610 31 21+rint(angle*50),
el17cd 11:2cd6341136ca 32 1);/*
el17cd 14:885915260e25 33 for( int i = 0; i<4; i++){
el17cd 14:885915260e25 34 edgePoints[i][0] -= angle*50;
el17cd 14:885915260e25 35 }
el17cd 14:885915260e25 36 edge.setVerticies(edgePoints);
el17cd 14:885915260e25 37 drawFace(edge, angle);*/
el17cd 11:2cd6341136ca 38 }
el17cd 11:2cd6341136ca 39
el17cd 11:2cd6341136ca 40 void Rasturizer::drawFace(Face face, double angle){
el17cd 7:15543cb10a14 41 double points[4][3];
el17cd 4:759a5c34e239 42 for(int vertex = 0; vertex < 4; vertex++){
el17cd 4:759a5c34e239 43 for(int axis = 0; axis < 3; axis++){
el17cd 4:759a5c34e239 44 points[vertex][axis] = face.getVertexValue(vertex, axis);
el17cd 4:759a5c34e239 45 }
el17cd 11:2cd6341136ca 46 double y = points[vertex][1];
el17cd 11:2cd6341136ca 47 double x = points[vertex][0];
el17cd 11:2cd6341136ca 48
el17cd 11:2cd6341136ca 49 points[vertex][0] = x*cos(angle)-y*sin(angle);
el17cd 11:2cd6341136ca 50 points[vertex][1] = y*cos(angle)+x*sin(angle);
el17cd 4:759a5c34e239 51 }
el17cd 6:75031d14fc0d 52
el17cd 14:885915260e25 53 if ((points[0][2] > 12 || points[1][2] > 12 || points[2][2] > 12|| points[3][2] > 12) && face.getVisible()){
el17cd 14:885915260e25 54 int diffX1 = xTo2D(points[0][0], points[0][2])-xTo2D(points[1][0], points[1][2]);
el17cd 14:885915260e25 55 int diffY1 = yTo2D(points[0][1], points[0][2])-yTo2D(points[1][1], points[1][2]);
el17cd 14:885915260e25 56 int diffX2 = xTo2D(points[2][0], points[2][2])-xTo2D(points[3][0], points[3][2]);
el17cd 14:885915260e25 57 int diffY2 = yTo2D(points[2][1], points[2][2])-yTo2D(points[3][1], points[3][2]);
el17cd 8:a667bc5050c1 58
el17cd 9:5915fc800824 59 double step = (double)diffY2/(double)diffX2;
el17cd 9:5915fc800824 60 double stepSmall1 = (double)diffX1/(double)diffX2;
el17cd 9:5915fc800824 61 double stepSmall2 = (double)diffY1/(double)diffX2;
el17cd 9:5915fc800824 62
el17cd 12:b69657862610 63 for(int s = 0; s< abs(diffX2); s++){
el17cd 14:885915260e25 64 if(diffX2 > 0){
el17cd 14:885915260e25 65 lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])-stepSmall1*s),//rint((points[0][0])*(40/points[0][2])-stepSmall1*s)+42,
el17cd 14:885915260e25 66 rint(yTo2D(points[0][1], points[0][2])-stepSmall2*s),
el17cd 14:885915260e25 67 rint(xTo2D(points[3][0], points[3][2])+s),
el17cd 14:885915260e25 68 rint(yTo2D(points[3][1], points[3][2])+step*s),
el17cd 14:885915260e25 69 0);
el17cd 14:885915260e25 70 }
el17cd 14:885915260e25 71 else{
el17cd 14:885915260e25 72 lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])+stepSmall1*s),//rint((points[0][0])*(40/points[0][2])-stepSmall1*s)+42,
el17cd 14:885915260e25 73 rint(yTo2D(points[0][1], points[0][2])+stepSmall2*s),
el17cd 14:885915260e25 74 rint(xTo2D(points[3][0], points[3][2])-s),
el17cd 14:885915260e25 75 rint(yTo2D(points[3][1], points[3][2])-step*s),
el17cd 14:885915260e25 76 0);
el17cd 14:885915260e25 77 }
el17cd 7:15543cb10a14 78 }
el17cd 13:f4de03202477 79 }
el17cd 13:f4de03202477 80 if((points[0][2] > 10 || points[1][2] > 10 || points[2][2] > 10|| points[3][2] > 10)){
el17cd 14:885915260e25 81 for (int i = 0; i < 3; i++){
el17cd 14:885915260e25 82 lcd.drawLine(rint(xTo2D(points[i][0], points[i][2])),
el17cd 14:885915260e25 83 rint(yTo2D(points[i][1], points[i][2])),
el17cd 14:885915260e25 84 rint(xTo2D(points[i+1][0], points[i+1][2])),
el17cd 14:885915260e25 85 rint(yTo2D(points[i+1][1], points[i+1][2])),
el17cd 14:885915260e25 86 1);
el17cd 1:044238f7bdda 87 }
el17cd 14:885915260e25 88 lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])),
el17cd 14:885915260e25 89 rint(yTo2D(points[0][1], points[0][2])),
el17cd 14:885915260e25 90 rint(xTo2D(points[3][0], points[3][2])),
el17cd 14:885915260e25 91 rint(yTo2D(points[3][1], points[3][2])),
el17cd 14:885915260e25 92 1);
el17cd 1:044238f7bdda 93 }
el17cd 4:759a5c34e239 94 }
el17cd 9:5915fc800824 95
el17cd 11:2cd6341136ca 96 void Rasturizer::print(const char *text){
el17cd 11:2cd6341136ca 97 lcd.printString(text, 0, 0);
el17cd 11:2cd6341136ca 98 }
el17cd 11:2cd6341136ca 99
el17cd 4:759a5c34e239 100 void Rasturizer::clear(){
el17cd 4:759a5c34e239 101 lcd.clear();
el17cd 4:759a5c34e239 102 }
el17cd 4:759a5c34e239 103
el17cd 4:759a5c34e239 104 void Rasturizer::refresh(){
el17cd 4:759a5c34e239 105 lcd.refresh();
el17cd 14:885915260e25 106 }
el17cd 14:885915260e25 107
el17cd 14:885915260e25 108 void Rasturizer::invertMode(){
el17cd 14:885915260e25 109 lcd.inverseMode();
el17cd 14:885915260e25 110 }
el17cd 14:885915260e25 111
el17cd 14:885915260e25 112 void Rasturizer::normalMode(){
el17cd 14:885915260e25 113 lcd.normalMode();
el17cd 1:044238f7bdda 114 }