Henry Triff / Mbed 2 deprecated ELEC2645_Project_el18ht

Dependencies:   mbed

Committer:
HenryWTriff
Date:
Sat Mar 28 14:40:04 2020 +0000
Revision:
20:f8d7b04471b8
Parent:
11:7b12992156de
Child:
25:31761087a83f
Map.h Added to files

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