Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Mechanics/Mechanics.cpp@10:29126a41b1da, 2020-03-25 (annotated)
- Committer:
- HenryWTriff
- Date:
- Wed Mar 25 15:36:01 2020 +0000
- Revision:
- 10:29126a41b1da
- Parent:
- 9:7b1093d3f03a
- Child:
- 11:7b12992156de
Better ghost mode using SD card
Who changed what in which revision?
User | Revision | Line number | New 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 | 10:29126a41b1da | 12 | if(Device.X_held() == true && Device.A_held() == false) { |
HenryWTriff | 7:2ce6e90f6d47 | 13 | if(speed >= 0 && speed < max_speed) { |
HenryWTriff | 7:2ce6e90f6d47 | 14 | speed += acceleration; |
HenryWTriff | 7:2ce6e90f6d47 | 15 | } else if(speed < 0) { |
HenryWTriff | 7:2ce6e90f6d47 | 16 | speed = 0; |
HenryWTriff | 7:2ce6e90f6d47 | 17 | } |
HenryWTriff | 10:29126a41b1da | 18 | } else if(Device.A_held() == true && Device.X_held() == false) { |
HenryWTriff | 7:2ce6e90f6d47 | 19 | if(speed >= deceleration) { |
HenryWTriff | 7:2ce6e90f6d47 | 20 | speed -= deceleration; |
HenryWTriff | 7:2ce6e90f6d47 | 21 | } else if(speed <= 0) { |
HenryWTriff | 7:2ce6e90f6d47 | 22 | speed = -1; |
HenryWTriff | 7:2ce6e90f6d47 | 23 | } |
HenryWTriff | 10:29126a41b1da | 24 | } else if(Device.A_held() == false && Device.X_held() == false) { |
HenryWTriff | 7:2ce6e90f6d47 | 25 | if(speed > 0) { |
HenryWTriff | 7:2ce6e90f6d47 | 26 | speed -= 0.05; |
HenryWTriff | 7:2ce6e90f6d47 | 27 | } else { |
HenryWTriff | 7:2ce6e90f6d47 | 28 | speed = 0; |
HenryWTriff | 7:2ce6e90f6d47 | 29 | } |
HenryWTriff | 7:2ce6e90f6d47 | 30 | } |
HenryWTriff | 7:2ce6e90f6d47 | 31 | if(offtrack == true) { |
HenryWTriff | 7:2ce6e90f6d47 | 32 | if(speed > off_track_speed) { |
HenryWTriff | 7:2ce6e90f6d47 | 33 | speed -= 0.10; |
HenryWTriff | 7:2ce6e90f6d47 | 34 | } |
HenryWTriff | 7:2ce6e90f6d47 | 35 | } |
HenryWTriff | 7:2ce6e90f6d47 | 36 | |
HenryWTriff | 7:2ce6e90f6d47 | 37 | if(out_of_bounds == true) { |
HenryWTriff | 7:2ce6e90f6d47 | 38 | speed = 0; |
HenryWTriff | 7:2ce6e90f6d47 | 39 | } |
HenryWTriff | 7:2ce6e90f6d47 | 40 | |
HenryWTriff | 7:2ce6e90f6d47 | 41 | speed = Get_Boost_Speed(plates, map_info.number_of_boost_plates, position, speed); |
HenryWTriff | 7:2ce6e90f6d47 | 42 | |
HenryWTriff | 7:2ce6e90f6d47 | 43 | return speed; |
HenryWTriff | 7:2ce6e90f6d47 | 44 | } |
HenryWTriff | 7:2ce6e90f6d47 | 45 | |
HenryWTriff | 7:2ce6e90f6d47 | 46 | // COMBINING |
HenryWTriff | 7:2ce6e90f6d47 | 47 | |
HenryWTriff | 9:7b1093d3f03a | 48 | bool Mechanics::Is_Offtrack(Point_2D position, const Square_2D offtrack_square[], const Triangle_2D offtrack_triangle[], const Map_Data map_info) |
HenryWTriff | 5:2d9f3c36bcb9 | 49 | { |
HenryWTriff | 8:4503c92acaf6 | 50 | 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)); |
HenryWTriff | 5:2d9f3c36bcb9 | 51 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 52 | |
HenryWTriff | 9:7b1093d3f03a | 53 | 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) |
HenryWTriff | 5:2d9f3c36bcb9 | 54 | { |
HenryWTriff | 7:2ce6e90f6d47 | 55 | 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)); |
HenryWTriff | 5:2d9f3c36bcb9 | 56 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 57 | |
HenryWTriff | 5:2d9f3c36bcb9 | 58 | // OFF TRACK |
HenryWTriff | 5:2d9f3c36bcb9 | 59 | |
HenryWTriff | 9:7b1093d3f03a | 60 | bool Mechanics::Is_Offtrack_Square(const Square_2D offtrack[], int size, Point_2D position) |
HenryWTriff | 5:2d9f3c36bcb9 | 61 | { |
HenryWTriff | 5:2d9f3c36bcb9 | 62 | for(int i = 0; i < size; i++) { |
HenryWTriff | 5:2d9f3c36bcb9 | 63 | 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 ) { |
HenryWTriff | 5:2d9f3c36bcb9 | 64 | return true; |
HenryWTriff | 5:2d9f3c36bcb9 | 65 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 66 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 67 | return false; |
HenryWTriff | 5:2d9f3c36bcb9 | 68 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 69 | |
HenryWTriff | 9:7b1093d3f03a | 70 | bool Mechanics::Is_Offtrack_Triangle(const Triangle_2D offtrack[], int size, Point_2D position) |
HenryWTriff | 5:2d9f3c36bcb9 | 71 | { |
HenryWTriff | 5:2d9f3c36bcb9 | 72 | for(int i = 0; i < size; i++) { |
HenryWTriff | 5:2d9f3c36bcb9 | 73 | 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 ) { |
HenryWTriff | 5:2d9f3c36bcb9 | 74 | if(offtrack[i].Type == 1) { |
HenryWTriff | 5:2d9f3c36bcb9 | 75 | Point_2D Translated_position = {position.x - offtrack[i].TL.x, position.y - offtrack[i].TL.y}; |
HenryWTriff | 5:2d9f3c36bcb9 | 76 | float position_limit = (offtrack[i].BR.y - offtrack[i].TL.y) / (offtrack[i].BR.x - offtrack[i].TL.x) * Translated_position.x; |
HenryWTriff | 5:2d9f3c36bcb9 | 77 | if(Translated_position.y < position_limit) { |
HenryWTriff | 5:2d9f3c36bcb9 | 78 | return true; |
HenryWTriff | 5:2d9f3c36bcb9 | 79 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 80 | } else if(offtrack[i].Type == 2) { |
HenryWTriff | 5:2d9f3c36bcb9 | 81 | Point_2D Translated_position = {position.x - offtrack[i].TL.x, position.y - offtrack[i].BR.y}; |
HenryWTriff | 5:2d9f3c36bcb9 | 82 | float position_limit = (offtrack[i].TL.y - offtrack[i].BR.y) / (offtrack[i].BR.x - offtrack[i].TL.x) * Translated_position.x; |
HenryWTriff | 5:2d9f3c36bcb9 | 83 | if(Translated_position.y > position_limit) { |
HenryWTriff | 5:2d9f3c36bcb9 | 84 | return true; |
HenryWTriff | 5:2d9f3c36bcb9 | 85 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 86 | } else if(offtrack[i].Type == 3) { |
HenryWTriff | 5:2d9f3c36bcb9 | 87 | Point_2D Translated_position = {position.x - offtrack[i].TL.x, position.y - offtrack[i].TL.y}; |
HenryWTriff | 5:2d9f3c36bcb9 | 88 | float position_limit = (offtrack[i].BR.y - offtrack[i].TL.y) / (offtrack[i].BR.x - offtrack[i].TL.x) * Translated_position.x; |
HenryWTriff | 5:2d9f3c36bcb9 | 89 | if(Translated_position.y > position_limit) { |
HenryWTriff | 5:2d9f3c36bcb9 | 90 | return true; |
HenryWTriff | 5:2d9f3c36bcb9 | 91 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 92 | } else if(offtrack[i].Type == 4) { |
HenryWTriff | 5:2d9f3c36bcb9 | 93 | Point_2D Translated_position = {position.x - offtrack[i].TL.x, position.y - offtrack[i].BR.y}; |
HenryWTriff | 5:2d9f3c36bcb9 | 94 | float position_limit = (offtrack[i].TL.y - offtrack[i].BR.y) / (offtrack[i].BR.x - offtrack[i].TL.x) * Translated_position.x; |
HenryWTriff | 5:2d9f3c36bcb9 | 95 | if(Translated_position.y < position_limit) { |
HenryWTriff | 5:2d9f3c36bcb9 | 96 | return true; |
HenryWTriff | 5:2d9f3c36bcb9 | 97 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 98 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 99 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 100 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 101 | return false; |
HenryWTriff | 5:2d9f3c36bcb9 | 102 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 103 | |
HenryWTriff | 5:2d9f3c36bcb9 | 104 | // OUT OF BOUNDS |
HenryWTriff | 5:2d9f3c36bcb9 | 105 | |
HenryWTriff | 9:7b1093d3f03a | 106 | bool Mechanics::Is_Out_Of_Bounds_Square(const Square_2D out_of_bounds[], int size, Point_2D position) |
HenryWTriff | 5:2d9f3c36bcb9 | 107 | { |
HenryWTriff | 5:2d9f3c36bcb9 | 108 | for(int i = 0; i < size; i++) { |
HenryWTriff | 5:2d9f3c36bcb9 | 109 | 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 ) { |
HenryWTriff | 5:2d9f3c36bcb9 | 110 | return true; |
HenryWTriff | 5:2d9f3c36bcb9 | 111 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 112 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 113 | return false; |
HenryWTriff | 5:2d9f3c36bcb9 | 114 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 115 | |
HenryWTriff | 9:7b1093d3f03a | 116 | bool Mechanics::Is_Out_Of_Bounds_Triangle(const Triangle_2D out_of_bounds[], int size, Point_2D position) |
HenryWTriff | 5:2d9f3c36bcb9 | 117 | { |
HenryWTriff | 5:2d9f3c36bcb9 | 118 | for(int i = 0; i < size; i++) { |
HenryWTriff | 5:2d9f3c36bcb9 | 119 | 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 ) { |
HenryWTriff | 5:2d9f3c36bcb9 | 120 | if(out_of_bounds[i].Type == 1) { |
HenryWTriff | 5:2d9f3c36bcb9 | 121 | Point_2D Translated_position = {position.x - out_of_bounds[i].TL.x, position.y - out_of_bounds[i].TL.y}; |
HenryWTriff | 5:2d9f3c36bcb9 | 122 | 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; |
HenryWTriff | 5:2d9f3c36bcb9 | 123 | if(Translated_position.y < position_limit) { |
HenryWTriff | 5:2d9f3c36bcb9 | 124 | return true; |
HenryWTriff | 5:2d9f3c36bcb9 | 125 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 126 | } else if(out_of_bounds[i].Type == 2) { |
HenryWTriff | 5:2d9f3c36bcb9 | 127 | Point_2D Translated_position = {position.x - out_of_bounds[i].TL.x, position.y - out_of_bounds[i].BR.y}; |
HenryWTriff | 5:2d9f3c36bcb9 | 128 | 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; |
HenryWTriff | 5:2d9f3c36bcb9 | 129 | if(Translated_position.y > position_limit) { |
HenryWTriff | 5:2d9f3c36bcb9 | 130 | return true; |
HenryWTriff | 5:2d9f3c36bcb9 | 131 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 132 | } else if(out_of_bounds[i].Type == 3) { |
HenryWTriff | 5:2d9f3c36bcb9 | 133 | Point_2D Translated_position = {position.x - out_of_bounds[i].TL.x, position.y - out_of_bounds[i].TL.y}; |
HenryWTriff | 5:2d9f3c36bcb9 | 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; |
HenryWTriff | 5:2d9f3c36bcb9 | 135 | if(Translated_position.y > position_limit) { |
HenryWTriff | 5:2d9f3c36bcb9 | 136 | return true; |
HenryWTriff | 5:2d9f3c36bcb9 | 137 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 138 | } else if(out_of_bounds[i].Type == 4) { |
HenryWTriff | 5:2d9f3c36bcb9 | 139 | Point_2D Translated_position = {position.x - out_of_bounds[i].TL.x, position.y - out_of_bounds[i].BR.y}; |
HenryWTriff | 5:2d9f3c36bcb9 | 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; |
HenryWTriff | 5:2d9f3c36bcb9 | 141 | if(Translated_position.y < position_limit) { |
HenryWTriff | 5:2d9f3c36bcb9 | 142 | return true; |
HenryWTriff | 5:2d9f3c36bcb9 | 143 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 144 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 145 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 146 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 147 | return false; |
HenryWTriff | 5:2d9f3c36bcb9 | 148 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 149 | |
HenryWTriff | 7:2ce6e90f6d47 | 150 | // BOOST PLATE |
HenryWTriff | 7:2ce6e90f6d47 | 151 | |
HenryWTriff | 9:7b1093d3f03a | 152 | float Mechanics::Get_Boost_Speed(const Triangle_2D plates[], int number_of_plates, Point_2D position, float speed) |
HenryWTriff | 7:2ce6e90f6d47 | 153 | { |
HenryWTriff | 7:2ce6e90f6d47 | 154 | for(int i = 0; i < number_of_plates; i++) { |
HenryWTriff | 7:2ce6e90f6d47 | 155 | 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) { |
HenryWTriff | 7:2ce6e90f6d47 | 156 | speed = 6; |
HenryWTriff | 7:2ce6e90f6d47 | 157 | } |
HenryWTriff | 7:2ce6e90f6d47 | 158 | } |
HenryWTriff | 7:2ce6e90f6d47 | 159 | return speed; |
HenryWTriff | 7:2ce6e90f6d47 | 160 | } |
HenryWTriff | 7:2ce6e90f6d47 | 161 | |
HenryWTriff | 7:2ce6e90f6d47 | 162 | //****************** |
HenryWTriff | 7:2ce6e90f6d47 | 163 | // GATES AND LAPS |
HenryWTriff | 7:2ce6e90f6d47 | 164 | //****************** |
HenryWTriff | 5:2d9f3c36bcb9 | 165 | |
HenryWTriff | 9:7b1093d3f03a | 166 | int Mechanics::Get_Gate(const Square_2D gates[], int number_of_gates, Point_2D position, int current_gate) |
HenryWTriff | 5:2d9f3c36bcb9 | 167 | { |
HenryWTriff | 5:2d9f3c36bcb9 | 168 | int next_gate; |
HenryWTriff | 5:2d9f3c36bcb9 | 169 | |
HenryWTriff | 5:2d9f3c36bcb9 | 170 | if(current_gate + 1 <= (number_of_gates - 1)) { |
HenryWTriff | 5:2d9f3c36bcb9 | 171 | next_gate = current_gate + 1; |
HenryWTriff | 5:2d9f3c36bcb9 | 172 | } else { |
HenryWTriff | 5:2d9f3c36bcb9 | 173 | next_gate = 0; |
HenryWTriff | 5:2d9f3c36bcb9 | 174 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 175 | |
HenryWTriff | 5:2d9f3c36bcb9 | 176 | 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 | 5:2d9f3c36bcb9 | 177 | return next_gate; |
HenryWTriff | 5:2d9f3c36bcb9 | 178 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 179 | |
HenryWTriff | 5:2d9f3c36bcb9 | 180 | return current_gate; |
HenryWTriff | 5:2d9f3c36bcb9 | 181 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 182 | |
HenryWTriff | 9:7b1093d3f03a | 183 | int Mechanics::Get_Laps(int laps, const Square_2D gates[], int number_of_gates, Point_2D position, int current_gate) |
HenryWTriff | 5:2d9f3c36bcb9 | 184 | { |
HenryWTriff | 5:2d9f3c36bcb9 | 185 | int next_gate; |
HenryWTriff | 5:2d9f3c36bcb9 | 186 | |
HenryWTriff | 5:2d9f3c36bcb9 | 187 | if(current_gate + 1 <= (number_of_gates - 1)) { |
HenryWTriff | 5:2d9f3c36bcb9 | 188 | next_gate = current_gate + 1; |
HenryWTriff | 5:2d9f3c36bcb9 | 189 | } else { |
HenryWTriff | 5:2d9f3c36bcb9 | 190 | next_gate = 0; |
HenryWTriff | 5:2d9f3c36bcb9 | 191 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 192 | 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 | 5:2d9f3c36bcb9 | 193 | if(next_gate == 0) { |
HenryWTriff | 5:2d9f3c36bcb9 | 194 | laps++; |
HenryWTriff | 5:2d9f3c36bcb9 | 195 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 196 | } |
HenryWTriff | 5:2d9f3c36bcb9 | 197 | return laps; |
HenryWTriff | 6:5f76dd718dc3 | 198 | } |
HenryWTriff | 6:5f76dd718dc3 | 199 | |
HenryWTriff | 7:2ce6e90f6d47 | 200 | //******** |
HenryWTriff | 7:2ce6e90f6d47 | 201 | // TIME |
HenryWTriff | 7:2ce6e90f6d47 | 202 | //******** |
HenryWTriff | 7:2ce6e90f6d47 | 203 | |
HenryWTriff | 6:5f76dd718dc3 | 204 | Time Mechanics::Convert_To_Time(int game_fps, int number_of_frames) |
HenryWTriff | 6:5f76dd718dc3 | 205 | { |
HenryWTriff | 6:5f76dd718dc3 | 206 | int total_seconds = float(number_of_frames) / float(game_fps); |
HenryWTriff | 6:5f76dd718dc3 | 207 | int seconds = total_seconds % 60; |
HenryWTriff | 6:5f76dd718dc3 | 208 | float miliseconds_decimal = (float(number_of_frames) / float(game_fps)) - seconds; |
HenryWTriff | 6:5f76dd718dc3 | 209 | int miliseconds = float(miliseconds_decimal * 1000); |
HenryWTriff | 6:5f76dd718dc3 | 210 | int minuites = (total_seconds - seconds) / 60; |
HenryWTriff | 6:5f76dd718dc3 | 211 | return {minuites, seconds, miliseconds}; |
HenryWTriff | 6:5f76dd718dc3 | 212 | } |
HenryWTriff | 6:5f76dd718dc3 | 213 | |
HenryWTriff | 7:2ce6e90f6d47 | 214 | //************* |
HenryWTriff | 7:2ce6e90f6d47 | 215 | // GET ANGLE |
HenryWTriff | 7:2ce6e90f6d47 | 216 | //************* |
HenryWTriff | 6:5f76dd718dc3 | 217 | |
HenryWTriff | 7:2ce6e90f6d47 | 218 | int Mechanics::Get_Angle(int angle, int handling, bool gyro_enabled, FXOS8700CQ &Gyro, Gamepad &Device) |
HenryWTriff | 7:2ce6e90f6d47 | 219 | { |
HenryWTriff | 10:29126a41b1da | 220 | if(gyro_enabled == false) { //If we are using stick control |
HenryWTriff | 10:29126a41b1da | 221 | int Stick_Position = Device.get_direction(); //The stick position is calculated |
HenryWTriff | 9:7b1093d3f03a | 222 | float Stick_Magnitude = Device.get_mag(); |
HenryWTriff | 9:7b1093d3f03a | 223 | if(Stick_Magnitude > 0.95) { |
HenryWTriff | 9:7b1093d3f03a | 224 | Stick_Magnitude = 1; |
HenryWTriff | 9:7b1093d3f03a | 225 | } |
HenryWTriff | 9:7b1093d3f03a | 226 | if(Stick_Position == E || Stick_Position == NE || Stick_Position == SE) { |
HenryWTriff | 9:7b1093d3f03a | 227 | angle += handling * Stick_Magnitude; |
HenryWTriff | 9:7b1093d3f03a | 228 | } else if(Stick_Position == W || Stick_Position == NW || Stick_Position == SW) { |
HenryWTriff | 9:7b1093d3f03a | 229 | angle -= handling * Stick_Magnitude; |
HenryWTriff | 7:2ce6e90f6d47 | 230 | } |
HenryWTriff | 7:2ce6e90f6d47 | 231 | return angle; |
HenryWTriff | 7:2ce6e90f6d47 | 232 | } else { |
HenryWTriff | 7:2ce6e90f6d47 | 233 | Gyro_Data Gyro_Tilt = Gyro.get_values(); |
HenryWTriff | 7:2ce6e90f6d47 | 234 | float tilt = Gyro_Tilt.ay * 90; |
HenryWTriff | 7:2ce6e90f6d47 | 235 | if(tilt > 5) { |
HenryWTriff | 7:2ce6e90f6d47 | 236 | if(tilt < 40) { |
HenryWTriff | 7:2ce6e90f6d47 | 237 | angle += handling * (tilt/40); |
HenryWTriff | 7:2ce6e90f6d47 | 238 | } else { |
HenryWTriff | 7:2ce6e90f6d47 | 239 | angle += handling; |
HenryWTriff | 7:2ce6e90f6d47 | 240 | } |
HenryWTriff | 7:2ce6e90f6d47 | 241 | } else if(tilt < -5) { |
HenryWTriff | 7:2ce6e90f6d47 | 242 | if(tilt > -40) { |
HenryWTriff | 7:2ce6e90f6d47 | 243 | angle -= handling * (tilt/-40); |
HenryWTriff | 7:2ce6e90f6d47 | 244 | } else { |
HenryWTriff | 7:2ce6e90f6d47 | 245 | angle -= handling; |
HenryWTriff | 7:2ce6e90f6d47 | 246 | } |
HenryWTriff | 7:2ce6e90f6d47 | 247 | } |
HenryWTriff | 7:2ce6e90f6d47 | 248 | return angle; |
HenryWTriff | 7:2ce6e90f6d47 | 249 | } |
HenryWTriff | 7:2ce6e90f6d47 | 250 | } |
HenryWTriff | 7:2ce6e90f6d47 | 251 | |
HenryWTriff | 7:2ce6e90f6d47 | 252 | //******************* |
HenryWTriff | 7:2ce6e90f6d47 | 253 | // GET TRANSLATION |
HenryWTriff | 7:2ce6e90f6d47 | 254 | //******************* |
HenryWTriff | 7:2ce6e90f6d47 | 255 | |
HenryWTriff | 9:7b1093d3f03a | 256 | 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 | 257 | //Calculates the change in the translation of the map, depending on the speed and direction of the cars movement |
HenryWTriff | 7:2ce6e90f6d47 | 258 | { |
HenryWTriff | 10:29126a41b1da | 259 | float temp_speed; |
HenryWTriff | 10:29126a41b1da | 260 | |
HenryWTriff | 10:29126a41b1da | 261 | //Converting to floats |
HenryWTriff | 7:2ce6e90f6d47 | 262 | float x = in.x; |
HenryWTriff | 7:2ce6e90f6d47 | 263 | float y = in.y; |
HenryWTriff | 10:29126a41b1da | 264 | |
HenryWTriff | 10:29126a41b1da | 265 | //Checking to see if the car is out of bounds |
HenryWTriff | 7:2ce6e90f6d47 | 266 | bool out_of_bounds = Is_Out_Of_Bounds(in, out_of_bounds_square, out_of_bounds_triangle, map_info); |
HenryWTriff | 7:2ce6e90f6d47 | 267 | if(out_of_bounds == true) { |
HenryWTriff | 10:29126a41b1da | 268 | if(speed > 0) { //If the car is driving forward |
HenryWTriff | 10:29126a41b1da | 269 | temp_speed = -4; //The speed is temporarily reversed |
HenryWTriff | 10:29126a41b1da | 270 | } else if(speed < 0) { //If the car is driving backward |
HenryWTriff | 10:29126a41b1da | 271 | temp_speed = 4; //The speed is temporarily forward |
HenryWTriff | 10:29126a41b1da | 272 | } else { //Else if the car is still |
HenryWTriff | 10:29126a41b1da | 273 | if(Device.X_held() == true){ //If the car is acceleratng forward |
HenryWTriff | 10:29126a41b1da | 274 | temp_speed = -4; //The car is temporarily reversed |
HenryWTriff | 10:29126a41b1da | 275 | } else if(Device.A_held() == true){ //If the car is accelerating backward |
HenryWTriff | 10:29126a41b1da | 276 | temp_speed = 4; //The car is temporarily forward |
HenryWTriff | 10:29126a41b1da | 277 | } |
HenryWTriff | 10:29126a41b1da | 278 | } |
HenryWTriff | 10:29126a41b1da | 279 | y += temp_speed * cos(-angle * PI / 180); //The change in the y position is then claculated |
HenryWTriff | 10:29126a41b1da | 280 | x -= temp_speed * sin(-angle * PI / 180); //The change in the x position is then calculated |
HenryWTriff | 7:2ce6e90f6d47 | 281 | } |
HenryWTriff | 10:29126a41b1da | 282 | y += speed * cos(-angle * PI / 180); //The change in the x position is then claculated |
HenryWTriff | 10:29126a41b1da | 283 | x -= speed * sin(-angle * PI / 180); //The change in the y position is then claculated |
HenryWTriff | 7:2ce6e90f6d47 | 284 | |
HenryWTriff | 7:2ce6e90f6d47 | 285 | return {x,y}; |
HenryWTriff | 7:2ce6e90f6d47 | 286 | } |
HenryWTriff | 7:2ce6e90f6d47 | 287 | |
HenryWTriff | 7:2ce6e90f6d47 | 288 | //************** |
HenryWTriff | 7:2ce6e90f6d47 | 289 | // CAR MODELS |
HenryWTriff | 7:2ce6e90f6d47 | 290 | //************** |
HenryWTriff | 7:2ce6e90f6d47 | 291 | |
HenryWTriff | 10:29126a41b1da | 292 | float Mechanics::Get_Max_Speed(int car_model) //Returns the maximum speed of each vehicle |
HenryWTriff | 6:5f76dd718dc3 | 293 | { |
HenryWTriff | 7:2ce6e90f6d47 | 294 | enum cars {Basic, Offroad, Drifter, Sportscar, Racecar, Stupid}; |
HenryWTriff | 7:2ce6e90f6d47 | 295 | |
HenryWTriff | 7:2ce6e90f6d47 | 296 | if(car_model == Stupid) { |
HenryWTriff | 7:2ce6e90f6d47 | 297 | return 8; |
HenryWTriff | 7:2ce6e90f6d47 | 298 | } else if(car_model == Racecar) { |
HenryWTriff | 7:2ce6e90f6d47 | 299 | return 6; |
HenryWTriff | 7:2ce6e90f6d47 | 300 | } else if(car_model == Sportscar) { |
HenryWTriff | 7:2ce6e90f6d47 | 301 | return 4; |
HenryWTriff | 7:2ce6e90f6d47 | 302 | } else if(car_model == Drifter) { |
HenryWTriff | 7:2ce6e90f6d47 | 303 | return 4; |
HenryWTriff | 7:2ce6e90f6d47 | 304 | } else if(car_model == Offroad) { |
HenryWTriff | 7:2ce6e90f6d47 | 305 | return 2; |
HenryWTriff | 7:2ce6e90f6d47 | 306 | } else if(car_model == Basic) { |
HenryWTriff | 7:2ce6e90f6d47 | 307 | return 3; |
HenryWTriff | 7:2ce6e90f6d47 | 308 | } |
HenryWTriff | 7:2ce6e90f6d47 | 309 | } |
HenryWTriff | 7:2ce6e90f6d47 | 310 | |
HenryWTriff | 10:29126a41b1da | 311 | float Mechanics::Get_Acceleration(int car_model) //Returns the acceleration of each vehicle |
HenryWTriff | 7:2ce6e90f6d47 | 312 | { |
HenryWTriff | 7:2ce6e90f6d47 | 313 | enum cars {Basic, Offroad, Drifter, Sportscar, Racecar, Stupid}; |
HenryWTriff | 7:2ce6e90f6d47 | 314 | |
HenryWTriff | 7:2ce6e90f6d47 | 315 | if(car_model == Stupid) { |
HenryWTriff | 7:2ce6e90f6d47 | 316 | return 0.15; |
HenryWTriff | 7:2ce6e90f6d47 | 317 | } else if(car_model == Racecar) { |
HenryWTriff | 7:2ce6e90f6d47 | 318 | return 0.1; |
HenryWTriff | 7:2ce6e90f6d47 | 319 | } else if(car_model == Sportscar) { |
HenryWTriff | 7:2ce6e90f6d47 | 320 | return 0.08; |
HenryWTriff | 7:2ce6e90f6d47 | 321 | } else if(car_model == Drifter) { |
HenryWTriff | 7:2ce6e90f6d47 | 322 | return 0.05; |
HenryWTriff | 7:2ce6e90f6d47 | 323 | } else if(car_model == Offroad) { |
HenryWTriff | 7:2ce6e90f6d47 | 324 | return 0.02; |
HenryWTriff | 7:2ce6e90f6d47 | 325 | } else if(car_model == Basic) { |
HenryWTriff | 7:2ce6e90f6d47 | 326 | return 0.03; |
HenryWTriff | 6:5f76dd718dc3 | 327 | } |
HenryWTriff | 7:2ce6e90f6d47 | 328 | |
HenryWTriff | 7:2ce6e90f6d47 | 329 | } |
HenryWTriff | 7:2ce6e90f6d47 | 330 | |
HenryWTriff | 10:29126a41b1da | 331 | float Mechanics::Get_Deceleration(int car_model) //Returns the deceleration when the brakes are pressed for each vehicle |
HenryWTriff | 7:2ce6e90f6d47 | 332 | { |
HenryWTriff | 7:2ce6e90f6d47 | 333 | enum cars {Basic, Offroad, Drifter, Sportscar, Racecar, Stupid}; |
HenryWTriff | 7:2ce6e90f6d47 | 334 | |
HenryWTriff | 7:2ce6e90f6d47 | 335 | if(car_model == Stupid) { |
HenryWTriff | 7:2ce6e90f6d47 | 336 | return 0.3; |
HenryWTriff | 7:2ce6e90f6d47 | 337 | } else if(car_model == Racecar) { |
HenryWTriff | 7:2ce6e90f6d47 | 338 | return 0.15; |
HenryWTriff | 7:2ce6e90f6d47 | 339 | } else if(car_model == Sportscar) { |
HenryWTriff | 7:2ce6e90f6d47 | 340 | return 0.1; |
HenryWTriff | 7:2ce6e90f6d47 | 341 | } else if(car_model == Drifter) { |
HenryWTriff | 7:2ce6e90f6d47 | 342 | return 0.03; |
HenryWTriff | 7:2ce6e90f6d47 | 343 | } else if(car_model == Offroad) { |
HenryWTriff | 7:2ce6e90f6d47 | 344 | return 0.03; |
HenryWTriff | 7:2ce6e90f6d47 | 345 | } else if(car_model == Basic) { |
HenryWTriff | 7:2ce6e90f6d47 | 346 | return 0.05; |
HenryWTriff | 7:2ce6e90f6d47 | 347 | } |
HenryWTriff | 7:2ce6e90f6d47 | 348 | } |
HenryWTriff | 7:2ce6e90f6d47 | 349 | |
HenryWTriff | 10:29126a41b1da | 350 | float Mechanics::Get_Off_Road_Speed(int car_model) //Returns the maximum speed that the vehicle can travel off road |
HenryWTriff | 7:2ce6e90f6d47 | 351 | { |
HenryWTriff | 7:2ce6e90f6d47 | 352 | enum cars {Basic, Offroad, Drifter, Sportscar, Racecar, Stupid}; |
HenryWTriff | 7:2ce6e90f6d47 | 353 | |
HenryWTriff | 7:2ce6e90f6d47 | 354 | if(car_model == Stupid) { |
HenryWTriff | 7:2ce6e90f6d47 | 355 | return 2; |
HenryWTriff | 7:2ce6e90f6d47 | 356 | } else if(car_model == Racecar) { |
HenryWTriff | 7:2ce6e90f6d47 | 357 | return 0.5; |
HenryWTriff | 7:2ce6e90f6d47 | 358 | } else if(car_model == Sportscar) { |
HenryWTriff | 7:2ce6e90f6d47 | 359 | return 0.8; |
HenryWTriff | 7:2ce6e90f6d47 | 360 | } else if(car_model == Drifter) { |
HenryWTriff | 7:2ce6e90f6d47 | 361 | return 0.3; |
HenryWTriff | 7:2ce6e90f6d47 | 362 | } else if(car_model == Offroad) { |
HenryWTriff | 7:2ce6e90f6d47 | 363 | return 2; |
HenryWTriff | 7:2ce6e90f6d47 | 364 | } else if(car_model == Basic) { |
HenryWTriff | 7:2ce6e90f6d47 | 365 | return 0.8; |
HenryWTriff | 7:2ce6e90f6d47 | 366 | } |
HenryWTriff | 7:2ce6e90f6d47 | 367 | } |
HenryWTriff | 7:2ce6e90f6d47 | 368 | |
HenryWTriff | 10:29126a41b1da | 369 | int Mechanics::Get_Handling(int car_model) //Returns the value for the handling of each vehicle |
HenryWTriff | 7:2ce6e90f6d47 | 370 | { |
HenryWTriff | 7:2ce6e90f6d47 | 371 | enum cars {Basic, Offroad, Drifter, Sportscar, Racecar, Stupid}; |
HenryWTriff | 7:2ce6e90f6d47 | 372 | |
HenryWTriff | 7:2ce6e90f6d47 | 373 | if(car_model == Stupid) { |
HenryWTriff | 7:2ce6e90f6d47 | 374 | return 3; |
HenryWTriff | 7:2ce6e90f6d47 | 375 | } else if(car_model == Racecar) { |
HenryWTriff | 7:2ce6e90f6d47 | 376 | return 3; |
HenryWTriff | 7:2ce6e90f6d47 | 377 | } else if(car_model == Sportscar) { |
HenryWTriff | 7:2ce6e90f6d47 | 378 | return 2; |
HenryWTriff | 7:2ce6e90f6d47 | 379 | } else if(car_model == Drifter) { |
HenryWTriff | 7:2ce6e90f6d47 | 380 | return 2; |
HenryWTriff | 7:2ce6e90f6d47 | 381 | } else if(car_model == Offroad) { |
HenryWTriff | 7:2ce6e90f6d47 | 382 | return 1; |
HenryWTriff | 7:2ce6e90f6d47 | 383 | } else if(car_model == Basic) { |
HenryWTriff | 7:2ce6e90f6d47 | 384 | return 2; |
HenryWTriff | 7:2ce6e90f6d47 | 385 | } |
HenryWTriff | 6:5f76dd718dc3 | 386 | } |
HenryWTriff | 6:5f76dd718dc3 | 387 | |
HenryWTriff | 6:5f76dd718dc3 | 388 | |
HenryWTriff | 6:5f76dd718dc3 | 389 | |
HenryWTriff | 6:5f76dd718dc3 | 390 | |
HenryWTriff | 6:5f76dd718dc3 | 391 | |
HenryWTriff | 6:5f76dd718dc3 | 392 | |
HenryWTriff | 6:5f76dd718dc3 | 393 |