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.
Diff: Mechanics/Mechanics.cpp
- Revision:
- 10:29126a41b1da
- Parent:
- 9:7b1093d3f03a
- Child:
- 11:7b12992156de
--- a/Mechanics/Mechanics.cpp Thu Mar 05 15:40:21 2020 +0000 +++ b/Mechanics/Mechanics.cpp Wed Mar 25 15:36:01 2020 +0000 @@ -9,19 +9,19 @@ bool offtrack = Is_Offtrack(position, offtrack_square, offtrack_triangle, map_info); bool out_of_bounds = Is_Out_Of_Bounds(position, out_of_bounds_square, out_of_bounds_triangle, map_info); - if(Device.X_held() == true && Device.B_held() == false) { + if(Device.X_held() == true && Device.A_held() == false) { if(speed >= 0 && speed < max_speed) { speed += acceleration; } else if(speed < 0) { speed = 0; } - } else if(Device.B_held() == true && Device.X_held() == false) { + } else if(Device.A_held() == true && Device.X_held() == false) { if(speed >= deceleration) { speed -= deceleration; } else if(speed <= 0) { speed = -1; } - } else if(Device.B_held() == false && Device.X_held() == false) { + } else if(Device.A_held() == false && Device.X_held() == false) { if(speed > 0) { speed -= 0.05; } else { @@ -217,10 +217,9 @@ int Mechanics::Get_Angle(int angle, int handling, bool gyro_enabled, FXOS8700CQ &Gyro, Gamepad &Device) { - if(gyro_enabled == false) { - int Stick_Position = Device.get_direction(); + if(gyro_enabled == false) { //If we are using stick control + int Stick_Position = Device.get_direction(); //The stick position is calculated float Stick_Magnitude = Device.get_mag(); - printf("%f\n", Stick_Magnitude); if(Stick_Magnitude > 0.95) { Stick_Magnitude = 1; } @@ -255,18 +254,33 @@ //******************* 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) +//Calculates the change in the translation of the map, depending on the speed and direction of the cars movement { + float temp_speed; + + //Converting to floats float x = in.x; float y = in.y; + + //Checking to see if the car is out of bounds bool out_of_bounds = Is_Out_Of_Bounds(in, out_of_bounds_square, out_of_bounds_triangle, map_info); - if(out_of_bounds == true) { - float temp_speed = -4; - y += temp_speed * cos(-angle * PI / 180); - x -= temp_speed * sin(-angle * PI / 180); + if(speed > 0) { //If the car is driving forward + temp_speed = -4; //The speed is temporarily reversed + } else if(speed < 0) { //If the car is driving backward + temp_speed = 4; //The speed is temporarily forward + } else { //Else if the car is still + if(Device.X_held() == true){ //If the car is acceleratng forward + temp_speed = -4; //The car is temporarily reversed + } else if(Device.A_held() == true){ //If the car is accelerating backward + temp_speed = 4; //The car is temporarily forward + } + } + y += temp_speed * cos(-angle * PI / 180); //The change in the y position is then claculated + x -= temp_speed * sin(-angle * PI / 180); //The change in the x position is then calculated } - y += speed * cos(-angle * PI / 180); - x -= speed * sin(-angle * PI / 180); + y += speed * cos(-angle * PI / 180); //The change in the x position is then claculated + x -= speed * sin(-angle * PI / 180); //The change in the y position is then claculated return {x,y}; } @@ -275,7 +289,7 @@ // CAR MODELS //************** -float Mechanics::Get_Max_Speed(int car_model) +float Mechanics::Get_Max_Speed(int car_model) //Returns the maximum speed of each vehicle { enum cars {Basic, Offroad, Drifter, Sportscar, Racecar, Stupid}; @@ -294,7 +308,7 @@ } } -float Mechanics::Get_Acceleration(int car_model) +float Mechanics::Get_Acceleration(int car_model) //Returns the acceleration of each vehicle { enum cars {Basic, Offroad, Drifter, Sportscar, Racecar, Stupid}; @@ -314,7 +328,7 @@ } -float Mechanics::Get_Deceleration(int car_model) +float Mechanics::Get_Deceleration(int car_model) //Returns the deceleration when the brakes are pressed for each vehicle { enum cars {Basic, Offroad, Drifter, Sportscar, Racecar, Stupid}; @@ -333,7 +347,7 @@ } } -float Mechanics::Get_Off_Road_Speed(int car_model) +float Mechanics::Get_Off_Road_Speed(int car_model) //Returns the maximum speed that the vehicle can travel off road { enum cars {Basic, Offroad, Drifter, Sportscar, Racecar, Stupid}; @@ -352,7 +366,7 @@ } } -int Mechanics::Get_Handling(int car_model) +int Mechanics::Get_Handling(int car_model) //Returns the value for the handling of each vehicle { enum cars {Basic, Offroad, Drifter, Sportscar, Racecar, Stupid};