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.
Dependencies: mbed FATFileSystem
Ball/Ball.cpp@16:c8d68cbd1ae2, 2019-05-09 (annotated)
- Committer:
- ellisbhastroud
- Date:
- Thu May 09 09:58:51 2019 +0000
- Revision:
- 16:c8d68cbd1ae2
- Parent:
- 15:d855e8c666e7
- Child:
- 17:98127ac75195
Penultimate Commit - Tidied a little and checking documentation.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ellisbhastroud | 3:a8960004d261 | 1 | #include "Ball.h" |
| ellisbhastroud | 3:a8960004d261 | 2 | |
| ellisbhastroud | 3:a8960004d261 | 3 | //constructor |
| ellisbhastroud | 3:a8960004d261 | 4 | |
| ellisbhastroud | 3:a8960004d261 | 5 | Ball::Ball() |
| ellisbhastroud | 3:a8960004d261 | 6 | { |
| ellisbhastroud | 3:a8960004d261 | 7 | |
| ellisbhastroud | 3:a8960004d261 | 8 | } |
| ellisbhastroud | 3:a8960004d261 | 9 | |
| ellisbhastroud | 3:a8960004d261 | 10 | //deconstructor |
| ellisbhastroud | 3:a8960004d261 | 11 | |
| ellisbhastroud | 3:a8960004d261 | 12 | Ball::~Ball() |
| ellisbhastroud | 3:a8960004d261 | 13 | { |
| ellisbhastroud | 3:a8960004d261 | 14 | |
| ellisbhastroud | 3:a8960004d261 | 15 | } |
| ellisbhastroud | 3:a8960004d261 | 16 | |
| ellisbhastroud | 3:a8960004d261 | 17 | //public methods |
| ellisbhastroud | 3:a8960004d261 | 18 | |
| ellisbhastroud | 9:bc34f2243e43 | 19 | void Ball::init(Coord start_pos) //ball starts new level stationary in x and y positions given |
| ellisbhastroud | 3:a8960004d261 | 20 | { |
| ellisbhastroud | 12:7f7fadb5c106 | 21 | _x_pos = start_pos.x; //moves ball position to coordinates given |
| ellisbhastroud | 9:bc34f2243e43 | 22 | _y_pos = start_pos.y; |
| ellisbhastroud | 12:7f7fadb5c106 | 23 | _x_vel = 0.0f; //makes ball stationary |
| ellisbhastroud | 4:035448357749 | 24 | _y_vel = 0.0f; |
| ellisbhastroud | 12:7f7fadb5c106 | 25 | _total_shot_count = _total_shot_count + _shot_count; //totals shot count from previous levels |
| ellisbhastroud | 12:7f7fadb5c106 | 26 | _shot_count = 0; //resets in level shot count for the next level |
| ellisbhastroud | 3:a8960004d261 | 27 | } |
| ellisbhastroud | 3:a8960004d261 | 28 | |
| ellisbhastroud | 12:7f7fadb5c106 | 29 | void Ball::drawBall(N5110 &lcd) //draws ball into lcd buffer |
| ellisbhastroud | 3:a8960004d261 | 30 | { |
| ellisbhastroud | 5:0b31909caf7f | 31 | lcd.drawRect(_x_pos,_y_pos,2,2,FILL_BLACK); //draws ball |
| ellisbhastroud | 3:a8960004d261 | 32 | } |
| ellisbhastroud | 3:a8960004d261 | 33 | |
| ellisbhastroud | 12:7f7fadb5c106 | 34 | void Ball::printShotCount(N5110 &lcd) //this is tallied for each level to give user an end score |
| ellisbhastroud | 3:a8960004d261 | 35 | { |
| ellisbhastroud | 5:0b31909caf7f | 36 | char buffer[14]; |
| ellisbhastroud | 16:c8d68cbd1ae2 | 37 | sprintf(buffer,"%i",_shot_count); |
| ellisbhastroud | 16:c8d68cbd1ae2 | 38 | lcd.printString(buffer,70,0); |
| ellisbhastroud | 4:035448357749 | 39 | } |
| ellisbhastroud | 4:035448357749 | 40 | |
| ellisbhastroud | 12:7f7fadb5c106 | 41 | void Ball::drawPower(N5110 &lcd, float mag) //draws power bar in top left of screen to indicate how hard a shot is hit |
| ellisbhastroud | 5:0b31909caf7f | 42 | { |
| ellisbhastroud | 10:9f54a6366e94 | 43 | lcd.drawRect(0,1,36,6,FILL_TRANSPARENT); |
| ellisbhastroud | 10:9f54a6366e94 | 44 | lcd.drawRect(0,1,36*mag,6,FILL_BLACK); |
| ellisbhastroud | 3:a8960004d261 | 45 | } |
| ellisbhastroud | 3:a8960004d261 | 46 | |
| ellisbhastroud | 3:a8960004d261 | 47 | |
| ellisbhastroud | 12:7f7fadb5c106 | 48 | void Ball::drawAim(N5110 &lcd, Vector2D joy_coord, float angle) //draws aim help for ball |
| ellisbhastroud | 12:7f7fadb5c106 | 49 | { //aim indicates path of ball if shot |
| ellisbhastroud | 12:7f7fadb5c106 | 50 | if(angle != -1.0f && abs(_x_vel) < TOL && abs(_y_vel) < TOL) { //if joystick off centre and ball stationary draw shot direction indicator |
| ellisbhastroud | 12:7f7fadb5c106 | 51 | lcd.drawLine(_x_pos,_y_pos,_x_pos-12*joy_coord.x,_y_pos+12*joy_coord.y,2); |
| ellisbhastroud | 8:d410856c6d04 | 52 | } |
| ellisbhastroud | 8:d410856c6d04 | 53 | } |
| ellisbhastroud | 8:d410856c6d04 | 54 | |
| ellisbhastroud | 15:d855e8c666e7 | 55 | void Ball::move_ball() //controls using |
| ellisbhastroud | 3:a8960004d261 | 56 | { |
| ellisbhastroud | 10:9f54a6366e94 | 57 | _x_pos = _x_pos + _x_vel*10.0f/_frame_rate; //move ball position at rate proportional to velocity in each direction |
| ellisbhastroud | 10:9f54a6366e94 | 58 | _y_pos = _y_pos + _y_vel*10.0f/_frame_rate; |
| ellisbhastroud | 12:7f7fadb5c106 | 59 | _x_vel = _x_vel*(1.0f-(0.6f/_frame_rate)); //velocity decreases as ball moves |
| ellisbhastroud | 12:7f7fadb5c106 | 60 | _y_vel = _y_vel*(1.0f-(0.6f/_frame_rate)); //inversely proportional to frame rate to ensure ball movement is the same at all frame rates |
| ellisbhastroud | 14:08ac9aaa34c3 | 61 | if(_x_vel != 0 && _y_vel != 0 && abs(_x_vel) < 0.15f && abs(_y_vel) < 0.15f) { //to make ball come to complete stop when velocity is small enough |
| ellisbhastroud | 8:d410856c6d04 | 62 | _x_vel = 0; |
| ellisbhastroud | 8:d410856c6d04 | 63 | _y_vel = 0; |
| ellisbhastroud | 8:d410856c6d04 | 64 | } |
| ellisbhastroud | 3:a8960004d261 | 65 | } |
| ellisbhastroud | 3:a8960004d261 | 66 | |
| ellisbhastroud | 14:08ac9aaa34c3 | 67 | void Ball::check_shoot_ball(Gamepad &pad, Vector2D _joy_coord) //checks if shoot coniditons met and A pressed then ball is shot using joystick input |
| ellisbhastroud | 12:7f7fadb5c106 | 68 | { //direction of shot is in opposite direction of joystick direction |
| ellisbhastroud | 14:08ac9aaa34c3 | 69 | if(pad.check_event(Gamepad::A_PRESSED) == true && abs(_x_vel) < TOL && abs(_y_vel) < TOL && pad.get_mag() > 0.05f){ //If ball stationary and A pressed then shoot |
| ellisbhastroud | 12:7f7fadb5c106 | 70 | _x_vel = 6.0f * -_joy_coord.x; //scale x velocity by joystick direction and magnitude |
| ellisbhastroud | 12:7f7fadb5c106 | 71 | _y_vel = 6.0f * _joy_coord.y; //scale y velocity by joystick direction and magnitude |
| ellisbhastroud | 12:7f7fadb5c106 | 72 | _shot_count ++; //increment shot count |
| ellisbhastroud | 14:08ac9aaa34c3 | 73 | pad.tone(392.00 , 0.25); //play note when ball shot (G4) |
| ellisbhastroud | 12:7f7fadb5c106 | 74 | } |
| ellisbhastroud | 4:035448357749 | 75 | } |
| ellisbhastroud | 4:035448357749 | 76 | |
| ellisbhastroud | 12:7f7fadb5c106 | 77 | void Ball::set_total_shot_count(int total_shot_count) //used to initialise value to 0 |
| ellisbhastroud | 12:7f7fadb5c106 | 78 | { |
| ellisbhastroud | 14:08ac9aaa34c3 | 79 | _total_shot_count = total_shot_count; |
| ellisbhastroud | 3:a8960004d261 | 80 | } |
| ellisbhastroud | 3:a8960004d261 | 81 | |
| ellisbhastroud | 12:7f7fadb5c106 | 82 | int Ball::get_total_shot_count() //used at end to add tally to highscore - the lowest shot count is the best score |
| ellisbhastroud | 3:a8960004d261 | 83 | { |
| ellisbhastroud | 16:c8d68cbd1ae2 | 84 | return _total_shot_count; |
| ellisbhastroud | 9:bc34f2243e43 | 85 | } |
| ellisbhastroud | 9:bc34f2243e43 | 86 | |
| ellisbhastroud | 16:c8d68cbd1ae2 | 87 | int Ball::get_shot_count() //returns shot count for current level - is used for tests |
| ellisbhastroud | 16:c8d68cbd1ae2 | 88 | { |
| ellisbhastroud | 16:c8d68cbd1ae2 | 89 | return _shot_count; |
| ellisbhastroud | 16:c8d68cbd1ae2 | 90 | } |
| ellisbhastroud | 9:bc34f2243e43 | 91 | bool Ball::check_hole(Coord hole) //returns true when ball is hit in hole and next level begins |
| ellisbhastroud | 9:bc34f2243e43 | 92 | { |
| ellisbhastroud | 9:bc34f2243e43 | 93 | if(_x_pos > hole.x - 1 && _x_pos < hole.x + 2 && _y_pos > hole.y - 1 && _y_pos < hole.y + 2) { |
| ellisbhastroud | 9:bc34f2243e43 | 94 | _x_vel = 0; //stop ball moving |
| ellisbhastroud | 9:bc34f2243e43 | 95 | _y_vel = 0; |
| ellisbhastroud | 9:bc34f2243e43 | 96 | _x_pos = hole.x; |
| ellisbhastroud | 9:bc34f2243e43 | 97 | _y_pos = hole.y; |
| ellisbhastroud | 9:bc34f2243e43 | 98 | return true; //causes next level process to begin |
| ellisbhastroud | 9:bc34f2243e43 | 99 | } |
| ellisbhastroud | 9:bc34f2243e43 | 100 | else { |
| ellisbhastroud | 9:bc34f2243e43 | 101 | return false; |
| ellisbhastroud | 9:bc34f2243e43 | 102 | } |
| ellisbhastroud | 9:bc34f2243e43 | 103 | } |
| ellisbhastroud | 9:bc34f2243e43 | 104 | |
| ellisbhastroud | 16:c8d68cbd1ae2 | 105 | void Ball::check_wall_bounce(const WallMap map[], int size) //uses information from WallMap array to check if ball should bounce |
| ellisbhastroud | 16:c8d68cbd1ae2 | 106 | { //every frame code runs through array of walls information to check if ball should bounce |
| ellisbhastroud | 12:7f7fadb5c106 | 107 | for(int i = 0; i < size; i ++) { //runs through each element in array of wall types to check if bounce condition is met by ball |
| ellisbhastroud | 3:a8960004d261 | 108 | |
| ellisbhastroud | 16:c8d68cbd1ae2 | 109 | if(map[i].wall == LEFT) { ////if current term of wall type struct array is a left wall |
| ellisbhastroud | 16:c8d68cbd1ae2 | 110 | left_bounce(map[i].start,map[i].end); //calls left_bounce() using start and end coordinates of the left wall |
| ellisbhastroud | 8:d410856c6d04 | 111 | } |
| ellisbhastroud | 16:c8d68cbd1ae2 | 112 | else if(map[i].wall == RIGHT) { //if current term of wall type struct array is a right wall |
| ellisbhastroud | 16:c8d68cbd1ae2 | 113 | right_bounce(map[i].start,map[i].end); //calls right_bounce() using start and end coordinates of the right wall |
| ellisbhastroud | 8:d410856c6d04 | 114 | } |
| ellisbhastroud | 16:c8d68cbd1ae2 | 115 | else if(map[i].wall == TOP) { //if current term of wall type struct array is a top wall |
| ellisbhastroud | 16:c8d68cbd1ae2 | 116 | top_bounce(map[i].start,map[i].end); //calls top_bounce() using start and end coordinates of top left wall |
| ellisbhastroud | 8:d410856c6d04 | 117 | } |
| ellisbhastroud | 16:c8d68cbd1ae2 | 118 | else if(map[i].wall == BOTTOM) { //if current term of wall type struct array is a bottom wall |
| ellisbhastroud | 16:c8d68cbd1ae2 | 119 | bottom_bounce(map[i].start,map[i].end); //calls bottom_bounce() using start and end coordinates of the bottom wall |
| ellisbhastroud | 8:d410856c6d04 | 120 | } |
| ellisbhastroud | 16:c8d68cbd1ae2 | 121 | else if(map[i].wall == BOTTOMLEFT) { //if current term of wall type struct array is a bottom left wall |
| ellisbhastroud | 16:c8d68cbd1ae2 | 122 | bottom_left_bounce(map[i].start,map[i].end); //calls bottom_left_bounce() using start and end coordinates of the bottome left wall |
| ellisbhastroud | 10:9f54a6366e94 | 123 | } |
| ellisbhastroud | 16:c8d68cbd1ae2 | 124 | else if(map[i].wall == BOTTOMRIGHT) { //if current term of wall type struct array is a bottom right wall |
| ellisbhastroud | 16:c8d68cbd1ae2 | 125 | bottom_right_bounce(map[i].start,map[i].end); //calls bottom_right_bounce() using start and end coordinates of the bottome right wall |
| ellisbhastroud | 10:9f54a6366e94 | 126 | } |
| ellisbhastroud | 16:c8d68cbd1ae2 | 127 | else if(map[i].wall == TOPLEFT) { //if current term of wall type struct array is a top left wall |
| ellisbhastroud | 16:c8d68cbd1ae2 | 128 | top_left_bounce(map[i].start,map[i].end); //calls top_left_bounce() using start and end coordinates of the top left wall |
| ellisbhastroud | 10:9f54a6366e94 | 129 | } |
| ellisbhastroud | 16:c8d68cbd1ae2 | 130 | else if(map[i].wall == TOPRIGHT) { //if current term of wall type struct array is a top right wall |
| ellisbhastroud | 16:c8d68cbd1ae2 | 131 | top_right_bounce(map[i].start,map[i].end); //calls top_right_bounce() using start and end coordinates of the top right wall |
| ellisbhastroud | 10:9f54a6366e94 | 132 | } |
| ellisbhastroud | 8:d410856c6d04 | 133 | } |
| ellisbhastroud | 3:a8960004d261 | 134 | } |
| ellisbhastroud | 5:0b31909caf7f | 135 | |
| ellisbhastroud | 10:9f54a6366e94 | 136 | void Ball::set_frame_rate(int frame_rate) |
| ellisbhastroud | 10:9f54a6366e94 | 137 | { |
| ellisbhastroud | 10:9f54a6366e94 | 138 | _frame_rate = frame_rate; |
| ellisbhastroud | 10:9f54a6366e94 | 139 | } |
| ellisbhastroud | 12:7f7fadb5c106 | 140 | |
| ellisbhastroud | 14:08ac9aaa34c3 | 141 | bool Ball::get_bounce_flag() |
| ellisbhastroud | 14:08ac9aaa34c3 | 142 | { |
| ellisbhastroud | 14:08ac9aaa34c3 | 143 | return _bounce_flag; |
| ellisbhastroud | 14:08ac9aaa34c3 | 144 | } |
| ellisbhastroud | 14:08ac9aaa34c3 | 145 | |
| ellisbhastroud | 14:08ac9aaa34c3 | 146 | void Ball::reset_bounce_flag() |
| ellisbhastroud | 14:08ac9aaa34c3 | 147 | { |
| ellisbhastroud | 14:08ac9aaa34c3 | 148 | _bounce_flag = false; |
| ellisbhastroud | 14:08ac9aaa34c3 | 149 | } |
| ellisbhastroud | 14:08ac9aaa34c3 | 150 | |
| ellisbhastroud | 3:a8960004d261 | 151 | //private methods |
| ellisbhastroud | 3:a8960004d261 | 152 | |
| ellisbhastroud | 16:c8d68cbd1ae2 | 153 | void Ball::left_bounce(Coord start, Coord end) //checks if there is a collision with left wall using coordinates and bounces accordingly |
| ellisbhastroud | 16:c8d68cbd1ae2 | 154 | { //if statement bounce conditions use coordinates from function and determine whether the ball will pass line in next frame - if so then bounces |
| ellisbhastroud | 12:7f7fadb5c106 | 155 | if(_x_pos + _x_vel*10.0f/_frame_rate - 1 < start.x && _x_pos + _x_vel*10.0f/_frame_rate > start.x - 5 && _y_pos >= start.y && _y_pos + 1 <= end.y && _x_vel < 0){ |
| ellisbhastroud | 12:7f7fadb5c106 | 156 | _x_pos = start.x + 1; |
| ellisbhastroud | 16:c8d68cbd1ae2 | 157 | _x_vel = -_x_vel; //left wall bounce causes x velocity to change sign |
| ellisbhastroud | 14:08ac9aaa34c3 | 158 | _bounce_flag = true; |
| ellisbhastroud | 12:7f7fadb5c106 | 159 | } |
| ellisbhastroud | 12:7f7fadb5c106 | 160 | } |
| ellisbhastroud | 12:7f7fadb5c106 | 161 | |
| ellisbhastroud | 16:c8d68cbd1ae2 | 162 | void Ball::right_bounce(Coord start, Coord end) //checks if there is a collision with a right wall using coordinates and bounces accordingly |
| ellisbhastroud | 16:c8d68cbd1ae2 | 163 | { //if statement bounce conditions use coordinates from function and determine whether the ball will pass line in next frame - if so then bounces |
| ellisbhastroud | 12:7f7fadb5c106 | 164 | if(_x_pos + _x_vel*10.0f/_frame_rate + 2 > start.x && _x_pos + _x_vel*10.0f/_frame_rate < start.x + 5 && _y_pos >= start.y && _y_pos + 1 <= end.y && _x_vel > 0){ //right wall x + 1 |
| ellisbhastroud | 12:7f7fadb5c106 | 165 | _x_pos = start.x - 1; |
| ellisbhastroud | 16:c8d68cbd1ae2 | 166 | _x_vel = -_x_vel; //right wall bounce causes x velocity to change sign |
| ellisbhastroud | 14:08ac9aaa34c3 | 167 | _bounce_flag = true; |
| ellisbhastroud | 12:7f7fadb5c106 | 168 | } |
| ellisbhastroud | 12:7f7fadb5c106 | 169 | } |
| ellisbhastroud | 12:7f7fadb5c106 | 170 | |
| ellisbhastroud | 16:c8d68cbd1ae2 | 171 | void Ball::top_bounce(Coord start, Coord end) //checks if there is a collision with a top wall using coordinates and bounces accordingly |
| ellisbhastroud | 16:c8d68cbd1ae2 | 172 | { //if statement bounce conditions use coordinates from function and determine whether the ball will pass line in next frame - if so then bounces |
| ellisbhastroud | 12:7f7fadb5c106 | 173 | if(_y_pos + _y_vel*10.0f/_frame_rate - 1 < start.y && _y_pos + _y_vel*10.0f/_frame_rate > start.y - 5 && _x_pos >= start.x && _x_pos + 1 <= end.x && _y_vel < 0){ //top wall y -1 |
| ellisbhastroud | 12:7f7fadb5c106 | 174 | _y_pos = start.y + 1; |
| ellisbhastroud | 16:c8d68cbd1ae2 | 175 | _y_vel = -_y_vel; //top wall bounce causes y velocity to change sign |
| ellisbhastroud | 14:08ac9aaa34c3 | 176 | _bounce_flag = true; |
| ellisbhastroud | 12:7f7fadb5c106 | 177 | } |
| ellisbhastroud | 12:7f7fadb5c106 | 178 | } |
| ellisbhastroud | 12:7f7fadb5c106 | 179 | |
| ellisbhastroud | 16:c8d68cbd1ae2 | 180 | void Ball::bottom_bounce(Coord start, Coord end) //checks if there is a collision with a bottom wall using coordinates and bounces accordingly |
| ellisbhastroud | 16:c8d68cbd1ae2 | 181 | { //if statement bounce conditions use coordinates from function and determine whether the ball will pass line in next frame - if so then bounces |
| ellisbhastroud | 12:7f7fadb5c106 | 182 | if(_y_pos + _y_vel*10.0f/_frame_rate + 2 > start.y && _y_pos + _y_vel*10.0f/_frame_rate < start.y + 5 && _x_pos >= start.x && _x_pos + 1 <= end.x && _y_vel > 0){ //bottom wall |
| ellisbhastroud | 12:7f7fadb5c106 | 183 | _y_pos = start.y - 1; |
| ellisbhastroud | 16:c8d68cbd1ae2 | 184 | _y_vel = -_y_vel; //bottom wall bounce causes y velocity to change sign |
| ellisbhastroud | 14:08ac9aaa34c3 | 185 | _bounce_flag = true; |
| ellisbhastroud | 12:7f7fadb5c106 | 186 | } |
| ellisbhastroud | 12:7f7fadb5c106 | 187 | } |
| ellisbhastroud | 12:7f7fadb5c106 | 188 | |
| ellisbhastroud | 16:c8d68cbd1ae2 | 189 | void Ball::bottom_left_bounce(Coord start, Coord end) //checks if there is a collision with a bottom left wall using coordinates and bounces accordingly |
| ellisbhastroud | 12:7f7fadb5c106 | 190 | { |
| ellisbhastroud | 14:08ac9aaa34c3 | 191 | if((_y_pos + _y_vel*10.0f/_frame_rate + 1) > (_x_pos + _x_vel*10.0f/_frame_rate - 1) + (start.y-start.x) && //bottom left bounce conditions |
| ellisbhastroud | 14:08ac9aaa34c3 | 192 | (_x_pos + _x_vel*10.0f/_frame_rate - 1) >= start.x && (_x_pos + _x_vel*10.0f/_frame_rate - 1) <= end.x && |
| ellisbhastroud | 14:08ac9aaa34c3 | 193 | (_y_pos + _y_vel*10.0f/_frame_rate + 1) >= start.y && (_y_pos + _y_vel*10.0f/_frame_rate + 1) <= end.y ) { |
| ellisbhastroud | 12:7f7fadb5c106 | 194 | swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped |
| ellisbhastroud | 14:08ac9aaa34c3 | 195 | _bounce_flag = true; |
| ellisbhastroud | 12:7f7fadb5c106 | 196 | } |
| ellisbhastroud | 12:7f7fadb5c106 | 197 | } |
| ellisbhastroud | 12:7f7fadb5c106 | 198 | |
| ellisbhastroud | 16:c8d68cbd1ae2 | 199 | void Ball::bottom_right_bounce(Coord start, Coord end) //checks if there is a collision with a bottom right wall using coordinates and bounces accordingly |
| ellisbhastroud | 16:c8d68cbd1ae2 | 200 | { //if statement bounce conditions use coordinates from function and determine whether the ball will pass line in next frame - if so then bounces |
| ellisbhastroud | 14:08ac9aaa34c3 | 201 | if((_x_pos + _x_vel*10.0f/_frame_rate + 1) > -(_y_pos + _y_vel*10.0f/_frame_rate + 1) + (start.x+start.y) //bottom right bounce conditions |
| ellisbhastroud | 16:c8d68cbd1ae2 | 202 | && (_x_pos + _x_vel*10.0f/_frame_rate + 1) >= start.x - 1 && (_x_pos + _x_vel*10.0f/_frame_rate + 1) <= end.x + 1 |
| ellisbhastroud | 16:c8d68cbd1ae2 | 203 | && (_y_pos + _y_vel*10.0f/_frame_rate + 1) >= end.y - 1 && (_y_pos + _y_vel*10.0f/_frame_rate + 1) <= start.y + 1) { |
| ellisbhastroud | 12:7f7fadb5c106 | 204 | |
| ellisbhastroud | 16:c8d68cbd1ae2 | 205 | _x_vel = -_x_vel; //negative wall causes both velocities to change sign then swap |
| ellisbhastroud | 12:7f7fadb5c106 | 206 | _y_vel = -_y_vel; |
| ellisbhastroud | 12:7f7fadb5c106 | 207 | swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped |
| ellisbhastroud | 14:08ac9aaa34c3 | 208 | _bounce_flag = true; |
| ellisbhastroud | 12:7f7fadb5c106 | 209 | } |
| ellisbhastroud | 12:7f7fadb5c106 | 210 | } |
| ellisbhastroud | 12:7f7fadb5c106 | 211 | |
| ellisbhastroud | 16:c8d68cbd1ae2 | 212 | void Ball::top_left_bounce(Coord start, Coord end) //checks if there is a collision with a top left wall using coordinates and bounces accordingly |
| ellisbhastroud | 16:c8d68cbd1ae2 | 213 | { //if statement bounce conditions use coordinates from function and determine whether the ball will pass line in next frame - if so then bounces |
| ellisbhastroud | 14:08ac9aaa34c3 | 214 | if((_x_pos + _x_vel*10.0f/_frame_rate - 1) < -(_y_pos + _y_vel*10.0f/_frame_rate + 1) + (start.x+start.y) //top left bounce conditions |
| ellisbhastroud | 16:c8d68cbd1ae2 | 215 | && (_x_pos + _x_vel*10.0f/_frame_rate - 1) >= start.x - 1&& (_x_pos + _x_vel*10.0f/_frame_rate - 1) <= end.x + 1 |
| ellisbhastroud | 16:c8d68cbd1ae2 | 216 | && (_y_pos + _y_vel*10.0f/_frame_rate + 1) >= end.y - 1 && (_y_pos + _y_vel*10.0f/_frame_rate + 1) <= start.y + 1) { |
| ellisbhastroud | 12:7f7fadb5c106 | 217 | |
| ellisbhastroud | 16:c8d68cbd1ae2 | 218 | _x_vel = -_x_vel; //negative wall causes both velocities to change sign then swap |
| ellisbhastroud | 12:7f7fadb5c106 | 219 | _y_vel = -_y_vel; |
| ellisbhastroud | 12:7f7fadb5c106 | 220 | swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped |
| ellisbhastroud | 14:08ac9aaa34c3 | 221 | _bounce_flag = true; |
| ellisbhastroud | 12:7f7fadb5c106 | 222 | } |
| ellisbhastroud | 12:7f7fadb5c106 | 223 | } |
| ellisbhastroud | 12:7f7fadb5c106 | 224 | |
| ellisbhastroud | 16:c8d68cbd1ae2 | 225 | void Ball::top_right_bounce(Coord start, Coord end) //checks if there is a collision with a top right wall using coordinates and bounces accordingly |
| ellisbhastroud | 16:c8d68cbd1ae2 | 226 | { //if statement bounce conditions use coordinates from function and determine whether the ball will pass line in next frame - if so then bounces |
| ellisbhastroud | 14:08ac9aaa34c3 | 227 | if((_y_pos + _y_vel*10.0f/_frame_rate - 1) < (_x_pos + _x_vel*10.0f/_frame_rate + 1) + (start.y-start.x) //top right bounce conditions |
| ellisbhastroud | 16:c8d68cbd1ae2 | 228 | && (_x_pos + _x_vel*10.0f/_frame_rate + 1) >= start.x - 1 && (_x_pos + _x_vel*10.0f/_frame_rate + 1) <= end.x + 1 |
| ellisbhastroud | 16:c8d68cbd1ae2 | 229 | && (_y_pos + _y_vel*10.0f/_frame_rate - 1) >= start.y - 1 && (_y_pos + _y_vel*10.0f/_frame_rate - 1) <= end.y + 1) { |
| ellisbhastroud | 12:7f7fadb5c106 | 230 | |
| ellisbhastroud | 12:7f7fadb5c106 | 231 | swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped |
| ellisbhastroud | 16:c8d68cbd1ae2 | 232 | _bounce_flag = true; //used to trigger beep |
| ellisbhastroud | 12:7f7fadb5c106 | 233 | } |
| ellisbhastroud | 12:7f7fadb5c106 | 234 | } |