Henry Triff / Mbed 2 deprecated ELEC2645_Project_el18ht

Dependencies:   mbed

Committer:
HenryWTriff
Date:
Sat Mar 28 10:31:41 2020 +0000
Revision:
11:7b12992156de
Parent:
10:29126a41b1da
Child:
20:f8d7b04471b8
Comments Added

Who changed what in which revision?

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