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
- Committer:
- HenryWTriff
- Date:
- 2020-02-19
- Revision:
- 6:5f76dd718dc3
- Parent:
- 5:2d9f3c36bcb9
- Child:
- 7:2ce6e90f6d47
File content as of revision 6:5f76dd718dc3:
#include "Mechanics.h" bool Mechanics::Is_Offtrack(Point_2D position, Square_2D offtrack_square[], Triangle_2D offtrack_triangle[], Map_Data map_info) { return (Is_Offtrack_Square(offtrack_square, map_info.number_of_off_track_triangles, position) || Is_Offtrack_Triangle(offtrack_triangle, map_info.number_of_off_track_triangles, position)); } bool Mechanics::Is_Out_Of_Bounds(Point_2D position, Square_2D offtrack_square[], Triangle_2D offtrack_triangle[], Map_Data map_info) { return (Is_Out_Of_Bounds_Square(offtrack_square, map_info.number_of_out_of_bounds_squares, position) || Is_Out_Of_Bounds_Triangle(offtrack_triangle, map_info.number_of_out_of_bounds_triangles, position)); } //************* // OFF TRACK //************* bool Mechanics::Is_Offtrack_Square(Square_2D offtrack[], int size, Point_2D position) { for(int i = 0; i < size; i++) { 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 ) { return true; } } return false; } bool Mechanics::Is_Offtrack_Triangle(Triangle_2D offtrack[], int size, Point_2D position) { for(int i = 0; i < size; i++) { 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(offtrack[i].Type == 1) { Point_2D Translated_position = {position.x - offtrack[i].TL.x, position.y - offtrack[i].TL.y}; float position_limit = (offtrack[i].BR.y - offtrack[i].TL.y) / (offtrack[i].BR.x - offtrack[i].TL.x) * Translated_position.x; if(Translated_position.y < position_limit) { return true; } } else if(offtrack[i].Type == 2) { Point_2D Translated_position = {position.x - offtrack[i].TL.x, position.y - offtrack[i].BR.y}; float position_limit = (offtrack[i].TL.y - offtrack[i].BR.y) / (offtrack[i].BR.x - offtrack[i].TL.x) * Translated_position.x; if(Translated_position.y > position_limit) { return true; } } else if(offtrack[i].Type == 3) { Point_2D Translated_position = {position.x - offtrack[i].TL.x, position.y - offtrack[i].TL.y}; float position_limit = (offtrack[i].BR.y - offtrack[i].TL.y) / (offtrack[i].BR.x - offtrack[i].TL.x) * Translated_position.x; if(Translated_position.y > position_limit) { return true; } } else if(offtrack[i].Type == 4) { Point_2D Translated_position = {position.x - offtrack[i].TL.x, position.y - offtrack[i].BR.y}; float position_limit = (offtrack[i].TL.y - offtrack[i].BR.y) / (offtrack[i].BR.x - offtrack[i].TL.x) * Translated_position.x; if(Translated_position.y < position_limit) { return true; } } } } return false; } //***************** // OUT OF BOUNDS //***************** bool Mechanics::Is_Out_Of_Bounds_Square(Square_2D out_of_bounds[], int size, Point_2D position) { for(int i = 0; i < size; i++) { 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 ) { return true; } } return false; } bool Mechanics::Is_Out_Of_Bounds_Triangle(Triangle_2D out_of_bounds[], int size, Point_2D position) { for(int i = 0; i < size; i++) { 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(out_of_bounds[i].Type == 1) { Point_2D Translated_position = {position.x - out_of_bounds[i].TL.x, position.y - out_of_bounds[i].TL.y}; 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; if(Translated_position.y < position_limit) { return true; } } else if(out_of_bounds[i].Type == 2) { Point_2D Translated_position = {position.x - out_of_bounds[i].TL.x, position.y - out_of_bounds[i].BR.y}; 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; if(Translated_position.y > position_limit) { return true; } } else if(out_of_bounds[i].Type == 3) { Point_2D Translated_position = {position.x - out_of_bounds[i].TL.x, position.y - out_of_bounds[i].TL.y}; 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; if(Translated_position.y > position_limit) { return true; } } else if(out_of_bounds[i].Type == 4) { Point_2D Translated_position = {position.x - out_of_bounds[i].TL.x, position.y - out_of_bounds[i].BR.y}; 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; if(Translated_position.y < position_limit) { return true; } } } } return false; } //********* // GATES //********* int Mechanics::Get_Gate(Square_2D gates[], int number_of_gates, Point_2D position, int current_gate) { int next_gate; if(current_gate + 1 <= (number_of_gates - 1)) { next_gate = current_gate + 1; } else { next_gate = 0; } 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) { return next_gate; } return current_gate; } int Mechanics::Get_Laps(int laps, Square_2D gates[], int number_of_gates, Point_2D position, int current_gate) { int next_gate; if(current_gate + 1 <= (number_of_gates - 1)) { next_gate = current_gate + 1; } else { next_gate = 0; } 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) { if(next_gate == 0) { laps++; } } return laps; } Time Mechanics::Convert_To_Time(int game_fps, int number_of_frames) { int total_seconds = float(number_of_frames) / float(game_fps); int seconds = total_seconds % 60; float miliseconds_decimal = (float(number_of_frames) / float(game_fps)) - seconds; int miliseconds = float(miliseconds_decimal * 1000); int minuites = (total_seconds - seconds) / 60; return {minuites, seconds, miliseconds}; } //*************** // BOOST PLATE //*************** float Mechanics::Get_Boost_Speed(Triangle_2D plates[], int number_of_plates, Point_2D position, float speed) { for(int i = 0; i < number_of_plates; i++) { 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) { speed = 6; } } return speed; }