Henry Triff / Mbed 2 deprecated ELEC2645_Project_el18ht

Dependencies:   mbed

Committer:
HenryWTriff
Date:
Wed Apr 29 10:53:21 2020 +0000
Revision:
25:31761087a83f
Parent:
20:f8d7b04471b8
Added Horizon effect to graphics and fixed clipping out of bounds glitch;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HenryWTriff 5:2d9f3c36bcb9 1 #include "Mechanics.h"
HenryWTriff 5:2d9f3c36bcb9 2
HenryWTriff 20:f8d7b04471b8 3 //--------------------------------
HenryWTriff 20:f8d7b04471b8 4 // CONSTRUCTORS AND DESTRUCTORS
HenryWTriff 20:f8d7b04471b8 5 //--------------------------------
HenryWTriff 20:f8d7b04471b8 6
HenryWTriff 20:f8d7b04471b8 7 Mechanics::Mechanics()
HenryWTriff 20:f8d7b04471b8 8 {
HenryWTriff 20:f8d7b04471b8 9 }
HenryWTriff 20:f8d7b04471b8 10
HenryWTriff 20:f8d7b04471b8 11 Mechanics::~Mechanics()
HenryWTriff 20:f8d7b04471b8 12 {
HenryWTriff 20:f8d7b04471b8 13 }
HenryWTriff 20:f8d7b04471b8 14
HenryWTriff 20:f8d7b04471b8 15 //---------
HenryWTriff 7:2ce6e90f6d47 16 // SPEED
HenryWTriff 20:f8d7b04471b8 17 //---------
HenryWTriff 7:2ce6e90f6d47 18
HenryWTriff 9:7b1093d3f03a 19 float Mechanics::Get_Speed(float speed, float max_speed, float acceleration, float deceleration, float off_track_speed, Point_2D position, const Square_2D *offtrack_square, const Triangle_2D *offtrack_triangle, const Square_2D *out_of_bounds_square, const Triangle_2D *out_of_bounds_triangle, const Triangle_2D *plates, const Map_Data map_info, Gamepad &Device)
HenryWTriff 7:2ce6e90f6d47 20 {
HenryWTriff 7:2ce6e90f6d47 21 bool offtrack = Is_Offtrack(position, offtrack_square, offtrack_triangle, map_info);
HenryWTriff 7:2ce6e90f6d47 22 bool out_of_bounds = Is_Out_Of_Bounds(position, out_of_bounds_square, out_of_bounds_triangle, map_info);
HenryWTriff 7:2ce6e90f6d47 23
HenryWTriff 11:7b12992156de 24 if(Device.X_held() == true && Device.A_held() == false) { //If accelerator is pressed and break is not
HenryWTriff 11:7b12992156de 25 if(speed >= 0 && speed < max_speed) { //If the speed is greater than 0 and less than max speed
HenryWTriff 11:7b12992156de 26 speed += acceleration; //Increase the speed
HenryWTriff 11:7b12992156de 27 } else if(speed < 0) { //If the speed is less than 0
HenryWTriff 11:7b12992156de 28 speed = 0; //Set speed to 0
HenryWTriff 7:2ce6e90f6d47 29 }
HenryWTriff 11:7b12992156de 30 } else if(Device.A_held() == true && Device.X_held() == false) { //If the break is pressed and accelerator is not
HenryWTriff 11:7b12992156de 31 if(speed > 0) { //If speed is greater than 0
HenryWTriff 11:7b12992156de 32 speed -= deceleration; //Decrease the speed
HenryWTriff 11:7b12992156de 33 } else if(speed <= 0) { //If speed is less than 0
HenryWTriff 11:7b12992156de 34 speed = -1; //Set speed to -1
HenryWTriff 7:2ce6e90f6d47 35 }
HenryWTriff 11:7b12992156de 36 } else if(Device.A_held() == false && Device.X_held() == false) { //If no button is pressed
HenryWTriff 11:7b12992156de 37 if(speed > 0) { //If speed is greater than 0
HenryWTriff 11:7b12992156de 38 speed -= 0.05; //Decrease speed gradually
HenryWTriff 11:7b12992156de 39 } else { //If speed is less than zero
HenryWTriff 11:7b12992156de 40 speed = 0; //Set speed to 0
HenryWTriff 7:2ce6e90f6d47 41 }
HenryWTriff 7:2ce6e90f6d47 42 }
HenryWTriff 11:7b12992156de 43 if(offtrack == true) { //If the car is offtrack
HenryWTriff 11:7b12992156de 44 if(speed > off_track_speed) { //If speed is greater than off track speed
HenryWTriff 11:7b12992156de 45 speed -= 0.10; //Speed is rapidly reduced
HenryWTriff 7:2ce6e90f6d47 46 }
HenryWTriff 7:2ce6e90f6d47 47 }
HenryWTriff 7:2ce6e90f6d47 48
HenryWTriff 11:7b12992156de 49 speed = Get_Boost_Speed(plates, map_info.number_of_boost_plates, position, speed); //If the car is on a boost plate change the speed
HenryWTriff 20:f8d7b04471b8 50
HenryWTriff 25:31761087a83f 51 #ifdef DEBUG_SPEED
HenryWTriff 25:31761087a83f 52 printf("%f \n", speed);
HenryWTriff 25:31761087a83f 53 #endif
HenryWTriff 20:f8d7b04471b8 54
HenryWTriff 11:7b12992156de 55 return speed; //Return the speed
HenryWTriff 7:2ce6e90f6d47 56 }
HenryWTriff 7:2ce6e90f6d47 57
HenryWTriff 20:f8d7b04471b8 58 //-----------------------------------
HenryWTriff 20:f8d7b04471b8 59 // OFF TRACK / OUT OF BOUNDS CHECK
HenryWTriff 20:f8d7b04471b8 60 //-----------------------------------
HenryWTriff 7:2ce6e90f6d47 61
HenryWTriff 20:f8d7b04471b8 62 //COMBINING
HenryWTriff 11:7b12992156de 63 bool Mechanics::Is_Offtrack(Point_2D position, const Square_2D offtrack_square[], const Triangle_2D offtrack_triangle[], const Map_Data map_info) //Check to see if the player is off track
HenryWTriff 5:2d9f3c36bcb9 64 {
HenryWTriff 25:31761087a83f 65 #ifdef DEBUG_OFF_TRACK
HenryWTriff 25:31761087a83f 66 printf("%i\n",(Is_Offtrack_Square(offtrack_square, map_info.number_of_off_track_squares, position) || Is_Offtrack_Triangle(offtrack_triangle, map_info.number_of_off_track_triangles, position)));
HenryWTriff 25:31761087a83f 67 #endif
HenryWTriff 11:7b12992156de 68 return (Is_Offtrack_Square(offtrack_square, map_info.number_of_off_track_squares, position) || Is_Offtrack_Triangle(offtrack_triangle, map_info.number_of_off_track_triangles, position));//Return the result of checking squares and triangles
HenryWTriff 5:2d9f3c36bcb9 69 }
HenryWTriff 5:2d9f3c36bcb9 70
HenryWTriff 11:7b12992156de 71 bool Mechanics::Is_Out_Of_Bounds(Point_2D position, const Square_2D out_of_bounds_square[], const Triangle_2D out_of_bounds_triangle[], const Map_Data map_info) //Check to see if the player is out of bounds
HenryWTriff 5:2d9f3c36bcb9 72 {
HenryWTriff 25:31761087a83f 73 #ifdef DEBUG_OUT_OF_BOUNDS
HenryWTriff 25:31761087a83f 74 printf("%i\n",(Is_Out_Of_Bounds_Square(out_of_bounds_square, map_info.number_of_out_of_bounds_squares, position) || Is_Out_Of_Bounds_Triangle(out_of_bounds_triangle, map_info.number_of_out_of_bounds_triangles, position)));
HenryWTriff 25:31761087a83f 75 #endif
HenryWTriff 11:7b12992156de 76 return (Is_Out_Of_Bounds_Square(out_of_bounds_square, map_info.number_of_out_of_bounds_squares, position) || Is_Out_Of_Bounds_Triangle(out_of_bounds_triangle, map_info.number_of_out_of_bounds_triangles, position)); //Return the result of checking squares and triangles
HenryWTriff 5:2d9f3c36bcb9 77 }
HenryWTriff 5:2d9f3c36bcb9 78
HenryWTriff 20:f8d7b04471b8 79
HenryWTriff 5:2d9f3c36bcb9 80 // OFF TRACK
HenryWTriff 9:7b1093d3f03a 81 bool Mechanics::Is_Offtrack_Square(const Square_2D offtrack[], int size, Point_2D position)
HenryWTriff 5:2d9f3c36bcb9 82 {
HenryWTriff 11:7b12992156de 83 for(int i = 0; i < size; i++) { //Iterate through the offtrack array
HenryWTriff 11:7b12992156de 84 if(position.x > offtrack[i].TL.x && position.x < offtrack[i].BR.x && position.y < offtrack[i].TL.y && position.y > offtrack[i].BR.y ) { //If the position of the car is in the out_of_bounds square
HenryWTriff 11:7b12992156de 85 return true; //return that the car is off track
HenryWTriff 5:2d9f3c36bcb9 86 }
HenryWTriff 5:2d9f3c36bcb9 87 }
HenryWTriff 11:7b12992156de 88 return false; //return that the car is NOT off track
HenryWTriff 5:2d9f3c36bcb9 89 }
HenryWTriff 5:2d9f3c36bcb9 90
HenryWTriff 9:7b1093d3f03a 91 bool Mechanics::Is_Offtrack_Triangle(const Triangle_2D offtrack[], int size, Point_2D position)
HenryWTriff 5:2d9f3c36bcb9 92 {
HenryWTriff 11:7b12992156de 93 for(int i = 0; i < size; i++) { //Iterate through the offtrack array
HenryWTriff 11:7b12992156de 94 if(position.x > offtrack[i].TL.x && position.x < offtrack[i].BR.x && position.y < offtrack[i].TL.y && position.y > offtrack[i].BR.y ) { //If the player is in the square area of the off track triangle
HenryWTriff 11:7b12992156de 95 if(offtrack[i].Type == 1) { //If the direction of the triangle is type 1 = Hypotenuse is on the top right
HenryWTriff 11:7b12992156de 96 Point_2D Translated_position = {position.x - offtrack[i].TL.x, position.y - offtrack[i].TL.y}; //Compare the position of the car to the top left position of the triangle
HenryWTriff 11:7b12992156de 97 float position_limit = (offtrack[i].BR.y - offtrack[i].TL.y) / (offtrack[i].BR.x - offtrack[i].TL.x) * Translated_position.x; //Calculate the y coordinate of the hypotenuse of the triangle at the given x position
HenryWTriff 11:7b12992156de 98 if(Translated_position.y < position_limit) { //If the y position of the car is less than the y position limit
HenryWTriff 11:7b12992156de 99 return true; //Return that the car is off track
HenryWTriff 5:2d9f3c36bcb9 100 }
HenryWTriff 11:7b12992156de 101 } else if(offtrack[i].Type == 2) { //If the direction of the triangle is type 1 = Hypotenuse is on the bottom right
HenryWTriff 11:7b12992156de 102 Point_2D Translated_position = {position.x - offtrack[i].TL.x, position.y - offtrack[i].BR.y}; //Compare the position of the car to the top bottom position of the triangle
HenryWTriff 11:7b12992156de 103 float position_limit = (offtrack[i].TL.y - offtrack[i].BR.y) / (offtrack[i].BR.x - offtrack[i].TL.x) * Translated_position.x; //Calculate the y coordinate of the hypotenuse of the triangle at the given x position
HenryWTriff 11:7b12992156de 104 if(Translated_position.y > position_limit) { //If the y position of the car is greater than the y position limit
HenryWTriff 11:7b12992156de 105 return true; //Return that the car is off track
HenryWTriff 5:2d9f3c36bcb9 106 }
HenryWTriff 11:7b12992156de 107 } else if(offtrack[i].Type == 3) { //If the direction of the triangle is type 1 = Hypotenuse is on the bottom left
HenryWTriff 11:7b12992156de 108 Point_2D Translated_position = {position.x - offtrack[i].TL.x, position.y - offtrack[i].TL.y}; //Compare the position of the car to the top bottom position of the triangle
HenryWTriff 11:7b12992156de 109 float position_limit = (offtrack[i].BR.y - offtrack[i].TL.y) / (offtrack[i].BR.x - offtrack[i].TL.x) * Translated_position.x; //Calculate the y coordinate of the hypotenuse of the triangle at the given x position
HenryWTriff 11:7b12992156de 110 if(Translated_position.y > position_limit) { //If the y position of the car is greater than the y position limit
HenryWTriff 11:7b12992156de 111 return true; //Return that the car is off track
HenryWTriff 5:2d9f3c36bcb9 112 }
HenryWTriff 11:7b12992156de 113 } else if(offtrack[i].Type == 4) { //If the direction of the triangle is type 1 = Hypotenuse is on the top left
HenryWTriff 11:7b12992156de 114 Point_2D Translated_position = {position.x - offtrack[i].TL.x, position.y - offtrack[i].BR.y}; //Compare the position of the car to the top left position of the triangle
HenryWTriff 11:7b12992156de 115 float position_limit = (offtrack[i].TL.y - offtrack[i].BR.y) / (offtrack[i].BR.x - offtrack[i].TL.x) * Translated_position.x; //Calculate the y coordinate of the hypotenuse of the triangle at the given x position
HenryWTriff 11:7b12992156de 116 if(Translated_position.y < position_limit) { //If the y position of the car is less than the y position limit
HenryWTriff 11:7b12992156de 117 return true; //Return that the car is off track
HenryWTriff 5:2d9f3c36bcb9 118 }
HenryWTriff 5:2d9f3c36bcb9 119 }
HenryWTriff 5:2d9f3c36bcb9 120 }
HenryWTriff 5:2d9f3c36bcb9 121 }
HenryWTriff 11:7b12992156de 122 return false; //Return that the car is NOT off track
HenryWTriff 5:2d9f3c36bcb9 123 }
HenryWTriff 5:2d9f3c36bcb9 124
HenryWTriff 5:2d9f3c36bcb9 125 // OUT OF BOUNDS
HenryWTriff 9:7b1093d3f03a 126 bool Mechanics::Is_Out_Of_Bounds_Square(const Square_2D out_of_bounds[], int size, Point_2D position)
HenryWTriff 5:2d9f3c36bcb9 127 {
HenryWTriff 11:7b12992156de 128 for(int i = 0; i < size; i++) { //Iterate through the out_of_bounds array
HenryWTriff 11:7b12992156de 129 if(position.x > out_of_bounds[i].TL.x && position.x < out_of_bounds[i].BR.x && position.y < out_of_bounds[i].TL.y && position.y > out_of_bounds[i].BR.y ) { //If the position of the car is in the out_of_bounds square
HenryWTriff 11:7b12992156de 130 return true; //return that the car is out of bounds
HenryWTriff 5:2d9f3c36bcb9 131 }
HenryWTriff 5:2d9f3c36bcb9 132 }
HenryWTriff 11:7b12992156de 133 return false; //return that the car is NOT out of bounds
HenryWTriff 5:2d9f3c36bcb9 134 }
HenryWTriff 5:2d9f3c36bcb9 135
HenryWTriff 11:7b12992156de 136 bool Mechanics::Is_Out_Of_Bounds_Triangle(const Triangle_2D out_of_bounds[], int size, Point_2D position) //Calculate if the player is out of bounds when compared to triangular out of bounds
HenryWTriff 5:2d9f3c36bcb9 137 {
HenryWTriff 11:7b12992156de 138 for(int i = 0; i < size; i++) { //Iterate through the out_of_bounds array
HenryWTriff 11:7b12992156de 139 if(position.x > out_of_bounds[i].TL.x && position.x < out_of_bounds[i].BR.x && position.y < out_of_bounds[i].TL.y && position.y > out_of_bounds[i].BR.y ) { //If the player is in the square area of the out of bounds triangle
HenryWTriff 11:7b12992156de 140 if(out_of_bounds[i].Type == 1) { //If the direction of the triangle is type 1 = Hypotenuse is on the top right
HenryWTriff 11:7b12992156de 141 Point_2D Translated_position = {position.x - out_of_bounds[i].TL.x, position.y - out_of_bounds[i].TL.y}; //Compare the position of the car to the top left position of the triangle
HenryWTriff 11:7b12992156de 142 float position_limit = (out_of_bounds[i].BR.y - out_of_bounds[i].TL.y) / (out_of_bounds[i].BR.x - out_of_bounds[i].TL.x) * Translated_position.x; //Calculate the y coordinate of the hypotenuse of the triangle at the given x position
HenryWTriff 11:7b12992156de 143 if(Translated_position.y < position_limit) { //If the y position of the car is less than the y position limit
HenryWTriff 11:7b12992156de 144 return true; //Return that the car is out of bounds
HenryWTriff 5:2d9f3c36bcb9 145 }
HenryWTriff 11:7b12992156de 146 } else if(out_of_bounds[i].Type == 2) { //If the direction of the triangle is type 2 = Hypotenuse is on the bottom right
HenryWTriff 11:7b12992156de 147 Point_2D Translated_position = {position.x - out_of_bounds[i].TL.x, position.y - out_of_bounds[i].BR.y}; //Compare the position of the car to the bottom left position of the triangle
HenryWTriff 11:7b12992156de 148 float position_limit = (out_of_bounds[i].TL.y - out_of_bounds[i].BR.y) / (out_of_bounds[i].BR.x - out_of_bounds[i].TL.x) * Translated_position.x; //Calculate the y coordinate of the hypotenuse of the triangle at the given x position
HenryWTriff 11:7b12992156de 149 if(Translated_position.y > position_limit) { //If the y position of the car is greater than the y position limit
HenryWTriff 11:7b12992156de 150 return true; //Return that the car is out of bounds
HenryWTriff 5:2d9f3c36bcb9 151 }
HenryWTriff 11:7b12992156de 152 } else if(out_of_bounds[i].Type == 3) { //If the direction of the triangle is type 3 = Hypotenuse is on the bottom left
HenryWTriff 11:7b12992156de 153 Point_2D Translated_position = {position.x - out_of_bounds[i].TL.x, position.y - out_of_bounds[i].TL.y}; //Compare the position of the car to the top left position of the triangle
HenryWTriff 11:7b12992156de 154 float position_limit = (out_of_bounds[i].BR.y - out_of_bounds[i].TL.y) / (out_of_bounds[i].BR.x - out_of_bounds[i].TL.x) * Translated_position.x; //Calculate the y coordinate of the hypotenuse of the triangle at the given x position
HenryWTriff 11:7b12992156de 155 if(Translated_position.y > position_limit) { //If the y position of the car is greater than the y position limit
HenryWTriff 11:7b12992156de 156 return true; //Return that the car is out of bounds
HenryWTriff 5:2d9f3c36bcb9 157 }
HenryWTriff 11:7b12992156de 158 } else if(out_of_bounds[i].Type == 4) { //If the direction of the triangle is type 1 = Hypotenuse is on the top left
HenryWTriff 11:7b12992156de 159 Point_2D Translated_position = {position.x - out_of_bounds[i].TL.x, position.y - out_of_bounds[i].BR.y}; //Compare the position of the car to the bottom left position of the triangle
HenryWTriff 11:7b12992156de 160 float position_limit = (out_of_bounds[i].TL.y - out_of_bounds[i].BR.y) / (out_of_bounds[i].BR.x - out_of_bounds[i].TL.x) * Translated_position.x; //Calculate the y coordinate of the hypotenuse of the triangle at the given x position
HenryWTriff 11:7b12992156de 161 if(Translated_position.y < position_limit) { //If the y position of the car is less than the y position limit
HenryWTriff 11:7b12992156de 162 return true; //Return that the car is out of bounds
HenryWTriff 5:2d9f3c36bcb9 163 }
HenryWTriff 5:2d9f3c36bcb9 164 }
HenryWTriff 5:2d9f3c36bcb9 165 }
HenryWTriff 5:2d9f3c36bcb9 166 }
HenryWTriff 11:7b12992156de 167 return false; //Return that the car is NOT out of bounds
HenryWTriff 5:2d9f3c36bcb9 168 }
HenryWTriff 5:2d9f3c36bcb9 169
HenryWTriff 20:f8d7b04471b8 170 //---------------
HenryWTriff 7:2ce6e90f6d47 171 // BOOST PLATE
HenryWTriff 20:f8d7b04471b8 172 //---------------
HenryWTriff 7:2ce6e90f6d47 173
HenryWTriff 11:7b12992156de 174 float Mechanics::Get_Boost_Speed(const Triangle_2D plates[], int number_of_plates, Point_2D position, float speed) //If the player is on a boost plate
HenryWTriff 7:2ce6e90f6d47 175 {
HenryWTriff 11:7b12992156de 176 for(int i = 0; i < number_of_plates; i++) { //Iterate through all boost plates
HenryWTriff 11:7b12992156de 177 if(position.x > plates[i].TL.x && position.x < plates[i].BR.x && position.y < plates[i].TL.y && position.y > plates[i].BR.y) { //If the player is in a boost plate
HenryWTriff 11:7b12992156de 178 speed = 8; //Increase the players speed to 8
HenryWTriff 7:2ce6e90f6d47 179 }
HenryWTriff 7:2ce6e90f6d47 180 }
HenryWTriff 11:7b12992156de 181 return speed; //Return the speed
HenryWTriff 7:2ce6e90f6d47 182 }
HenryWTriff 7:2ce6e90f6d47 183
HenryWTriff 20:f8d7b04471b8 184 //------------------
HenryWTriff 7:2ce6e90f6d47 185 // GATES AND LAPS
HenryWTriff 20:f8d7b04471b8 186 //------------------
HenryWTriff 5:2d9f3c36bcb9 187
HenryWTriff 9:7b1093d3f03a 188 int Mechanics::Get_Gate(const Square_2D gates[], int number_of_gates, Point_2D position, int current_gate)
HenryWTriff 5:2d9f3c36bcb9 189 {
HenryWTriff 5:2d9f3c36bcb9 190 int next_gate;
HenryWTriff 5:2d9f3c36bcb9 191
HenryWTriff 11:7b12992156de 192 if(current_gate + 1 <= (number_of_gates - 1)) { //If the current gate is not the last in the array
HenryWTriff 11:7b12992156de 193 next_gate = current_gate + 1; //The next gate is the current gate + 1
HenryWTriff 11:7b12992156de 194 } else { //Else if it is the last gate in the array
HenryWTriff 11:7b12992156de 195 next_gate = 0; //Then the next gate is 0
HenryWTriff 5:2d9f3c36bcb9 196 }
HenryWTriff 5:2d9f3c36bcb9 197
HenryWTriff 11:7b12992156de 198 if(position.x >= gates[next_gate].TL.x && position.x <= gates[next_gate].BR.x && position.y <= gates[next_gate].TL.y && position.y >= gates[next_gate].BR.y) { //Check if the players position is in the next gate
HenryWTriff 11:7b12992156de 199 return next_gate; //Return the current gate as being the next one
HenryWTriff 5:2d9f3c36bcb9 200 }
HenryWTriff 5:2d9f3c36bcb9 201
HenryWTriff 25:31761087a83f 202 #ifdef DEBUG_GATE
HenryWTriff 25:31761087a83f 203 printf("%i\n", current_gate);
HenryWTriff 25:31761087a83f 204 #endif
HenryWTriff 25:31761087a83f 205
HenryWTriff 11:7b12992156de 206 return current_gate; //Else if the player has not yet reached the next gate, then the current gate remains the same
HenryWTriff 5:2d9f3c36bcb9 207 }
HenryWTriff 5:2d9f3c36bcb9 208
HenryWTriff 11:7b12992156de 209 int Mechanics::Get_Laps(int laps, const Square_2D gates[], int number_of_gates, Point_2D position, int current_gate) //Calculate the number of laps
HenryWTriff 5:2d9f3c36bcb9 210 {
HenryWTriff 5:2d9f3c36bcb9 211 int next_gate;
HenryWTriff 5:2d9f3c36bcb9 212
HenryWTriff 11:7b12992156de 213 if(current_gate + 1 <= (number_of_gates - 1)) { //If the current gate is not the last in the array
HenryWTriff 11:7b12992156de 214 next_gate = current_gate + 1; //The next gate is the current gate + 1
HenryWTriff 11:7b12992156de 215 } else { //Else if it is the last gate in the array
HenryWTriff 11:7b12992156de 216 next_gate = 0; //Then the next gate is 0
HenryWTriff 5:2d9f3c36bcb9 217 }
HenryWTriff 5:2d9f3c36bcb9 218 if(position.x >= gates[next_gate].TL.x && position.x <= gates[next_gate].BR.x && position.y <= gates[next_gate].TL.y && position.y >= gates[next_gate].BR.y) {
HenryWTriff 11:7b12992156de 219 if(next_gate == 0) { //If the car is in the area of the next gate and the next gate is 0
HenryWTriff 11:7b12992156de 220 laps++; //Increase the number of laps
HenryWTriff 5:2d9f3c36bcb9 221 }
HenryWTriff 5:2d9f3c36bcb9 222 }
HenryWTriff 11:7b12992156de 223 return laps; //Return the number of laps
HenryWTriff 6:5f76dd718dc3 224 }
HenryWTriff 6:5f76dd718dc3 225
HenryWTriff 20:f8d7b04471b8 226 //--------
HenryWTriff 7:2ce6e90f6d47 227 // TIME
HenryWTriff 20:f8d7b04471b8 228 //--------
HenryWTriff 7:2ce6e90f6d47 229
HenryWTriff 11:7b12992156de 230 Time Mechanics::Convert_To_Time(int game_fps, int number_of_frames) //Convert The number of frames elapsed to minutes, seconds and miliseconds
HenryWTriff 6:5f76dd718dc3 231 {
HenryWTriff 11:7b12992156de 232 int total_seconds = float(number_of_frames) / float(game_fps); //Calculate the total number of seconds elapsed
HenryWTriff 11:7b12992156de 233 int seconds = total_seconds % 60; //The remainder after 60 is the seconds
HenryWTriff 11:7b12992156de 234 float miliseconds_decimal = (float(number_of_frames) / float(game_fps)) - seconds; //Calculating the decimal number of miliseconds
HenryWTriff 11:7b12992156de 235 int miliseconds = float(miliseconds_decimal * 1000); //Multiplying by 1000 to get the number of miliseconds as an integer
HenryWTriff 11:7b12992156de 236 int minuites = (total_seconds - seconds) / 60; //The minutes is the total number of seconds subtract the seconds on the clock divided by 60
HenryWTriff 11:7b12992156de 237 return {minuites, seconds, miliseconds}; //Return the time
HenryWTriff 6:5f76dd718dc3 238 }
HenryWTriff 6:5f76dd718dc3 239
HenryWTriff 20:f8d7b04471b8 240 //-------------
HenryWTriff 7:2ce6e90f6d47 241 // GET ANGLE
HenryWTriff 20:f8d7b04471b8 242 //-------------
HenryWTriff 6:5f76dd718dc3 243
HenryWTriff 7:2ce6e90f6d47 244 int Mechanics::Get_Angle(int angle, int handling, bool gyro_enabled, FXOS8700CQ &Gyro, Gamepad &Device)
HenryWTriff 7:2ce6e90f6d47 245 {
HenryWTriff 25:31761087a83f 246 #ifdef DEBUG_ANGLE
HenryWTriff 25:31761087a83f 247 printf("%i\n", angle);
HenryWTriff 25:31761087a83f 248 #endif
HenryWTriff 25:31761087a83f 249
HenryWTriff 10:29126a41b1da 250 if(gyro_enabled == false) { //If we are using stick control
HenryWTriff 20:f8d7b04471b8 251 int Stick_Position = Device.get_direction(); //The stick position is calculated
HenryWTriff 11:7b12992156de 252 float Stick_Magnitude = Device.get_mag(); //The magnitude of the stick position is calculated
HenryWTriff 11:7b12992156de 253 if(Stick_Magnitude > 0.95) { //Rounds up if almost at full magnitude
HenryWTriff 9:7b1093d3f03a 254 Stick_Magnitude = 1;
HenryWTriff 9:7b1093d3f03a 255 }
HenryWTriff 11:7b12992156de 256 if(Stick_Position == E || Stick_Position == NE || Stick_Position == SE) { //Allows the stick to be pushed in any right direction
HenryWTriff 11:7b12992156de 257 angle += handling * Stick_Magnitude; //The angle is increased by the handling of the car × stick magntude
HenryWTriff 11:7b12992156de 258 } else if(Stick_Position == W || Stick_Position == NW || Stick_Position == SW) { //Allows the stick to be pushed in any left direction
HenryWTriff 11:7b12992156de 259 angle -= handling * Stick_Magnitude; //The angle is decreased by the handling of the car × stick magntude
HenryWTriff 7:2ce6e90f6d47 260 }
HenryWTriff 11:7b12992156de 261 return angle; //Return the new angle of rotation of the track
HenryWTriff 7:2ce6e90f6d47 262 } else {
HenryWTriff 11:7b12992156de 263 Gyro_Data Gyro_Tilt = Gyro.get_values(); //Calculate the amount of tilt of the device
HenryWTriff 11:7b12992156de 264 float tilt = Gyro_Tilt.ay * 90; //Turn the tild into an angle from -90° to +90°
HenryWTriff 20:f8d7b04471b8 265 if(tilt > 5) { //±5° deadzone
HenryWTriff 11:7b12992156de 266 if(tilt < 40) { //Max angle is 40°
HenryWTriff 11:7b12992156de 267 angle += handling * (tilt/40); //Increase the angle
HenryWTriff 7:2ce6e90f6d47 268 } else {
HenryWTriff 11:7b12992156de 269 angle += handling; //Increase the angle
HenryWTriff 7:2ce6e90f6d47 270 }
HenryWTriff 20:f8d7b04471b8 271 } else if(tilt < -5) {//±5° deadzone
HenryWTriff 11:7b12992156de 272 if(tilt > -40) { //Max angle is -40°
HenryWTriff 11:7b12992156de 273 angle -= handling * (tilt/-40); //Decrease the angle
HenryWTriff 7:2ce6e90f6d47 274 } else {
HenryWTriff 11:7b12992156de 275 angle -= handling; //Decrease the angle
HenryWTriff 7:2ce6e90f6d47 276 }
HenryWTriff 7:2ce6e90f6d47 277 }
HenryWTriff 25:31761087a83f 278
HenryWTriff 11:7b12992156de 279 return angle; //Return the new angle of roation of the track
HenryWTriff 7:2ce6e90f6d47 280 }
HenryWTriff 7:2ce6e90f6d47 281 }
HenryWTriff 7:2ce6e90f6d47 282
HenryWTriff 20:f8d7b04471b8 283 //-------------------
HenryWTriff 7:2ce6e90f6d47 284 // GET TRANSLATION
HenryWTriff 20:f8d7b04471b8 285 //-------------------
HenryWTriff 7:2ce6e90f6d47 286
HenryWTriff 9:7b1093d3f03a 287 Point_2D Mechanics::Get_Translation(Point_2D in, float angle, float speed, const Square_2D *out_of_bounds_square, const Triangle_2D *out_of_bounds_triangle, const Map_Data map_info, Gamepad &Device)
HenryWTriff 10:29126a41b1da 288 //Calculates the change in the translation of the map, depending on the speed and direction of the cars movement
HenryWTriff 7:2ce6e90f6d47 289 {
HenryWTriff 10:29126a41b1da 290 float temp_speed;
HenryWTriff 10:29126a41b1da 291
HenryWTriff 10:29126a41b1da 292 //Converting to floats
HenryWTriff 7:2ce6e90f6d47 293 float x = in.x;
HenryWTriff 7:2ce6e90f6d47 294 float y = in.y;
HenryWTriff 10:29126a41b1da 295
HenryWTriff 25:31761087a83f 296 //printf("%f\n", temp_speed);
HenryWTriff 25:31761087a83f 297 y += speed * cos(-angle * PI / 180); //The change in the y position is then claculated
HenryWTriff 25:31761087a83f 298 x -= speed * sin(-angle * PI / 180); //The change in the x position is then claculated
HenryWTriff 25:31761087a83f 299
HenryWTriff 25:31761087a83f 300 Point_2D next_translation = {x,y};
HenryWTriff 25:31761087a83f 301
HenryWTriff 10:29126a41b1da 302 //Checking to see if the car is out of bounds
HenryWTriff 25:31761087a83f 303 bool will_be_out_of_bounds = Is_Out_Of_Bounds(next_translation, out_of_bounds_square, out_of_bounds_triangle, map_info);
HenryWTriff 25:31761087a83f 304 if(will_be_out_of_bounds == true) {
HenryWTriff 25:31761087a83f 305 #ifdef DEBUG_POSITION
HenryWTriff 25:31761087a83f 306 printf("%f,%f\n", x,y);
HenryWTriff 25:31761087a83f 307 #endif
HenryWTriff 25:31761087a83f 308 return {in.x,in.y};
HenryWTriff 25:31761087a83f 309
HenryWTriff 25:31761087a83f 310 } else {
HenryWTriff 25:31761087a83f 311 #ifdef DEBUG_POSITION
HenryWTriff 25:31761087a83f 312 printf("%f,%f\n", x,y);
HenryWTriff 25:31761087a83f 313 #endif
HenryWTriff 25:31761087a83f 314 return {x,y};
HenryWTriff 7:2ce6e90f6d47 315 }
HenryWTriff 25:31761087a83f 316
HenryWTriff 25:31761087a83f 317
HenryWTriff 25:31761087a83f 318 #ifdef DEBUG_POSITION
HenryWTriff 25:31761087a83f 319 printf("%f,%f\n", x,y);
HenryWTriff 25:31761087a83f 320 #endif
HenryWTriff 7:2ce6e90f6d47 321
HenryWTriff 7:2ce6e90f6d47 322 return {x,y};
HenryWTriff 7:2ce6e90f6d47 323 }
HenryWTriff 7:2ce6e90f6d47 324
HenryWTriff 20:f8d7b04471b8 325 //-----------------------------
HenryWTriff 20:f8d7b04471b8 326 // CAR MODELS AND PARAMETERS
HenryWTriff 20:f8d7b04471b8 327 //-----------------------------
HenryWTriff 7:2ce6e90f6d47 328
HenryWTriff 10:29126a41b1da 329 float Mechanics::Get_Max_Speed(int car_model) //Returns the maximum speed of each vehicle
HenryWTriff 6:5f76dd718dc3 330 {
HenryWTriff 11:7b12992156de 331 //For each car, Return the maximum speed when on track
HenryWTriff 11:7b12992156de 332 //This is the number of integer coordinates in any direction moved by the car per frame
HenryWTriff 7:2ce6e90f6d47 333 if(car_model == Stupid) {
HenryWTriff 7:2ce6e90f6d47 334 return 8;
HenryWTriff 7:2ce6e90f6d47 335 } else if(car_model == Racecar) {
HenryWTriff 7:2ce6e90f6d47 336 return 6;
HenryWTriff 7:2ce6e90f6d47 337 } else if(car_model == Sportscar) {
HenryWTriff 7:2ce6e90f6d47 338 return 4;
HenryWTriff 7:2ce6e90f6d47 339 } else if(car_model == Drifter) {
HenryWTriff 7:2ce6e90f6d47 340 return 4;
HenryWTriff 7:2ce6e90f6d47 341 } else if(car_model == Offroad) {
HenryWTriff 7:2ce6e90f6d47 342 return 2;
HenryWTriff 7:2ce6e90f6d47 343 } else if(car_model == Basic) {
HenryWTriff 7:2ce6e90f6d47 344 return 3;
HenryWTriff 7:2ce6e90f6d47 345 }
HenryWTriff 7:2ce6e90f6d47 346 }
HenryWTriff 7:2ce6e90f6d47 347
HenryWTriff 10:29126a41b1da 348 float Mechanics::Get_Acceleration(int car_model) //Returns the acceleration of each vehicle
HenryWTriff 7:2ce6e90f6d47 349 {
HenryWTriff 11:7b12992156de 350 //For each car, Return the acceleration when accelerating
HenryWTriff 11:7b12992156de 351 //This is the rate of change in the number of integer coordinates in any direction moved by the car per frame
HenryWTriff 7:2ce6e90f6d47 352 if(car_model == Stupid) {
HenryWTriff 7:2ce6e90f6d47 353 return 0.15;
HenryWTriff 7:2ce6e90f6d47 354 } else if(car_model == Racecar) {
HenryWTriff 7:2ce6e90f6d47 355 return 0.1;
HenryWTriff 7:2ce6e90f6d47 356 } else if(car_model == Sportscar) {
HenryWTriff 7:2ce6e90f6d47 357 return 0.08;
HenryWTriff 7:2ce6e90f6d47 358 } else if(car_model == Drifter) {
HenryWTriff 7:2ce6e90f6d47 359 return 0.05;
HenryWTriff 7:2ce6e90f6d47 360 } else if(car_model == Offroad) {
HenryWTriff 7:2ce6e90f6d47 361 return 0.02;
HenryWTriff 7:2ce6e90f6d47 362 } else if(car_model == Basic) {
HenryWTriff 7:2ce6e90f6d47 363 return 0.03;
HenryWTriff 6:5f76dd718dc3 364 }
HenryWTriff 7:2ce6e90f6d47 365
HenryWTriff 7:2ce6e90f6d47 366 }
HenryWTriff 7:2ce6e90f6d47 367
HenryWTriff 10:29126a41b1da 368 float Mechanics::Get_Deceleration(int car_model) //Returns the deceleration when the brakes are pressed for each vehicle
HenryWTriff 7:2ce6e90f6d47 369 {
HenryWTriff 11:7b12992156de 370 //For each car, Return the deceleration when breaking
HenryWTriff 11:7b12992156de 371 //This is the rate of change in the number of integer coordinates in any direction moved by the car per frame
HenryWTriff 7:2ce6e90f6d47 372 if(car_model == Stupid) {
HenryWTriff 7:2ce6e90f6d47 373 return 0.3;
HenryWTriff 7:2ce6e90f6d47 374 } else if(car_model == Racecar) {
HenryWTriff 7:2ce6e90f6d47 375 return 0.15;
HenryWTriff 7:2ce6e90f6d47 376 } else if(car_model == Sportscar) {
HenryWTriff 7:2ce6e90f6d47 377 return 0.1;
HenryWTriff 7:2ce6e90f6d47 378 } else if(car_model == Drifter) {
HenryWTriff 7:2ce6e90f6d47 379 return 0.03;
HenryWTriff 7:2ce6e90f6d47 380 } else if(car_model == Offroad) {
HenryWTriff 7:2ce6e90f6d47 381 return 0.03;
HenryWTriff 7:2ce6e90f6d47 382 } else if(car_model == Basic) {
HenryWTriff 7:2ce6e90f6d47 383 return 0.05;
HenryWTriff 7:2ce6e90f6d47 384 }
HenryWTriff 7:2ce6e90f6d47 385 }
HenryWTriff 7:2ce6e90f6d47 386
HenryWTriff 10:29126a41b1da 387 float Mechanics::Get_Off_Road_Speed(int car_model) //Returns the maximum speed that the vehicle can travel off road
HenryWTriff 7:2ce6e90f6d47 388 {
HenryWTriff 11:7b12992156de 389 //For each car, Return the max speed when off road
HenryWTriff 11:7b12992156de 390 //This is the number of integer coordinates in any direction moved by the car per frame
HenryWTriff 7:2ce6e90f6d47 391 if(car_model == Stupid) {
HenryWTriff 7:2ce6e90f6d47 392 return 2;
HenryWTriff 7:2ce6e90f6d47 393 } else if(car_model == Racecar) {
HenryWTriff 7:2ce6e90f6d47 394 return 0.5;
HenryWTriff 7:2ce6e90f6d47 395 } else if(car_model == Sportscar) {
HenryWTriff 7:2ce6e90f6d47 396 return 0.8;
HenryWTriff 7:2ce6e90f6d47 397 } else if(car_model == Drifter) {
HenryWTriff 7:2ce6e90f6d47 398 return 0.3;
HenryWTriff 7:2ce6e90f6d47 399 } else if(car_model == Offroad) {
HenryWTriff 7:2ce6e90f6d47 400 return 2;
HenryWTriff 7:2ce6e90f6d47 401 } else if(car_model == Basic) {
HenryWTriff 7:2ce6e90f6d47 402 return 0.8;
HenryWTriff 7:2ce6e90f6d47 403 }
HenryWTriff 7:2ce6e90f6d47 404 }
HenryWTriff 7:2ce6e90f6d47 405
HenryWTriff 10:29126a41b1da 406 int Mechanics::Get_Handling(int car_model) //Returns the value for the handling of each vehicle
HenryWTriff 7:2ce6e90f6d47 407 {
HenryWTriff 7:2ce6e90f6d47 408
HenryWTriff 11:7b12992156de 409 //For each car, return the handling
HenryWTriff 11:7b12992156de 410 //Handling is the change is the max degrees of rotation of track per frame
HenryWTriff 7:2ce6e90f6d47 411 if(car_model == Stupid) {
HenryWTriff 7:2ce6e90f6d47 412 return 3;
HenryWTriff 7:2ce6e90f6d47 413 } else if(car_model == Racecar) {
HenryWTriff 7:2ce6e90f6d47 414 return 3;
HenryWTriff 7:2ce6e90f6d47 415 } else if(car_model == Sportscar) {
HenryWTriff 7:2ce6e90f6d47 416 return 2;
HenryWTriff 7:2ce6e90f6d47 417 } else if(car_model == Drifter) {
HenryWTriff 7:2ce6e90f6d47 418 return 2;
HenryWTriff 7:2ce6e90f6d47 419 } else if(car_model == Offroad) {
HenryWTriff 7:2ce6e90f6d47 420 return 1;
HenryWTriff 7:2ce6e90f6d47 421 } else if(car_model == Basic) {
HenryWTriff 7:2ce6e90f6d47 422 return 2;
HenryWTriff 7:2ce6e90f6d47 423 }
HenryWTriff 6:5f76dd718dc3 424 }
HenryWTriff 6:5f76dd718dc3 425
HenryWTriff 6:5f76dd718dc3 426
HenryWTriff 6:5f76dd718dc3 427
HenryWTriff 6:5f76dd718dc3 428
HenryWTriff 6:5f76dd718dc3 429
HenryWTriff 6:5f76dd718dc3 430
HenryWTriff 6:5f76dd718dc3 431