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@10:9f54a6366e94, 2019-04-23 (annotated)
- Committer:
- ellisbhastroud
- Date:
- Tue Apr 23 12:03:38 2019 +0000
- Revision:
- 10:9f54a6366e94
- Parent:
- 9:bc34f2243e43
- Child:
- 11:6d2027253aa9
Added corner bounces to bounce check algorithm. More levels now added with complex courses.
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 | 9:bc34f2243e43 | 21 | _x_pos = start_pos.x; |
| ellisbhastroud | 9:bc34f2243e43 | 22 | _y_pos = start_pos.y; |
| ellisbhastroud | 4:035448357749 | 23 | _x_vel = 0.0f; |
| ellisbhastroud | 4:035448357749 | 24 | _y_vel = 0.0f; |
| ellisbhastroud | 9:bc34f2243e43 | 25 | _total_shot_count = _total_shot_count + _shot_count; |
| ellisbhastroud | 4:035448357749 | 26 | _shot_count = 0; |
| ellisbhastroud | 3:a8960004d261 | 27 | } |
| ellisbhastroud | 3:a8960004d261 | 28 | |
| ellisbhastroud | 5:0b31909caf7f | 29 | void Ball::drawBall(N5110 &lcd) |
| 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 | 5:0b31909caf7f | 34 | void Ball::printShotCount(N5110 &lcd) |
| ellisbhastroud | 3:a8960004d261 | 35 | { |
| ellisbhastroud | 5:0b31909caf7f | 36 | char buffer[14]; |
| ellisbhastroud | 5:0b31909caf7f | 37 | sprintf(buffer,"Shot %i",_shot_count); |
| ellisbhastroud | 5:0b31909caf7f | 38 | lcd.printString(buffer,40,0); |
| ellisbhastroud | 4:035448357749 | 39 | } |
| ellisbhastroud | 4:035448357749 | 40 | |
| ellisbhastroud | 8:d410856c6d04 | 41 | void Ball::drawPower(N5110 &lcd, float mag) |
| 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 | 8:d410856c6d04 | 48 | void Ball::drawAim(N5110 &lcd, Vector2D joy_coord, float angle) |
| ellisbhastroud | 8:d410856c6d04 | 49 | { |
| ellisbhastroud | 8:d410856c6d04 | 50 | if(angle != -1.0f) { |
| ellisbhastroud | 8:d410856c6d04 | 51 | lcd.drawLine(10,16,10+12*joy_coord.x,16+-12*joy_coord.y,1); |
| ellisbhastroud | 8:d410856c6d04 | 52 | } |
| ellisbhastroud | 8:d410856c6d04 | 53 | |
| ellisbhastroud | 8:d410856c6d04 | 54 | } |
| ellisbhastroud | 8:d410856c6d04 | 55 | |
| ellisbhastroud | 10:9f54a6366e94 | 56 | void Ball::move_ball() |
| ellisbhastroud | 3:a8960004d261 | 57 | { |
| ellisbhastroud | 10:9f54a6366e94 | 58 | _x_pos = _x_pos + _x_vel*10.0f/_frame_rate; //move ball position at rate proportional to velocity in each direction |
| ellisbhastroud | 10:9f54a6366e94 | 59 | _y_pos = _y_pos + _y_vel*10.0f/_frame_rate; |
| ellisbhastroud | 10:9f54a6366e94 | 60 | _x_vel = _x_vel*(1.0f-(0.6f/_frame_rate)); //ball slows down each loop caled by time between frames to ensure same movement at different frame rates |
| ellisbhastroud | 10:9f54a6366e94 | 61 | _y_vel = _y_vel*(1.0f-(0.6f/_frame_rate)); |
| ellisbhastroud | 8:d410856c6d04 | 62 | if(_x_vel != 0 && _y_vel != 0 && abs(_x_vel) < 0.2f && abs(_y_vel) < 0.2f) { //to make ball come to complete stop once velocity is nearly 0 |
| ellisbhastroud | 8:d410856c6d04 | 63 | _x_vel = 0; |
| ellisbhastroud | 8:d410856c6d04 | 64 | _y_vel = 0; |
| ellisbhastroud | 8:d410856c6d04 | 65 | } |
| ellisbhastroud | 3:a8960004d261 | 66 | } |
| ellisbhastroud | 3:a8960004d261 | 67 | |
| ellisbhastroud | 4:035448357749 | 68 | Vector2D Ball::get_ball_pos() |
| ellisbhastroud | 4:035448357749 | 69 | { |
| ellisbhastroud | 4:035448357749 | 70 | Vector2D pos = {_x_pos, _y_pos}; |
| ellisbhastroud | 4:035448357749 | 71 | return pos; |
| ellisbhastroud | 4:035448357749 | 72 | } |
| ellisbhastroud | 4:035448357749 | 73 | |
| ellisbhastroud | 8:d410856c6d04 | 74 | void Ball::shoot_ball(Gamepad &pad, Vector2D _joy_coord) |
| ellisbhastroud | 8:d410856c6d04 | 75 | { |
| ellisbhastroud | 8:d410856c6d04 | 76 | if(pad.check_event(Gamepad::A_PRESSED) == true && abs(_x_vel) < TOL && abs(_y_vel) < TOL){ //if ball stationary and a pressed then shoot |
| ellisbhastroud | 8:d410856c6d04 | 77 | _x_vel = 6.0f * _joy_coord.x; //scale x velocity by joystick direction and magnitude |
| ellisbhastroud | 8:d410856c6d04 | 78 | _y_vel = 6.0f * -_joy_coord.y; //scale y velocity by joystick direction and magnitude |
| ellisbhastroud | 5:0b31909caf7f | 79 | _shot_count ++; //increment shot count |
| ellisbhastroud | 3:a8960004d261 | 80 | } |
| ellisbhastroud | 3:a8960004d261 | 81 | |
| ellisbhastroud | 3:a8960004d261 | 82 | } |
| ellisbhastroud | 3:a8960004d261 | 83 | |
| ellisbhastroud | 4:035448357749 | 84 | int Ball::get_shot_count() |
| ellisbhastroud | 4:035448357749 | 85 | { |
| ellisbhastroud | 4:035448357749 | 86 | int shot_count = _shot_count; |
| ellisbhastroud | 4:035448357749 | 87 | return shot_count; |
| ellisbhastroud | 4:035448357749 | 88 | } |
| ellisbhastroud | 4:035448357749 | 89 | |
| ellisbhastroud | 9:bc34f2243e43 | 90 | int Ball::get_total_shot_count() |
| ellisbhastroud | 3:a8960004d261 | 91 | { |
| ellisbhastroud | 9:bc34f2243e43 | 92 | int total_shot_count = _total_shot_count; |
| ellisbhastroud | 9:bc34f2243e43 | 93 | return total_shot_count; |
| ellisbhastroud | 9:bc34f2243e43 | 94 | } |
| ellisbhastroud | 9:bc34f2243e43 | 95 | |
| ellisbhastroud | 9:bc34f2243e43 | 96 | void Ball::set_total_shot_count(int total_shot_count) |
| ellisbhastroud | 9:bc34f2243e43 | 97 | { |
| ellisbhastroud | 9:bc34f2243e43 | 98 | int _total_shot_count = total_shot_count; |
| ellisbhastroud | 3:a8960004d261 | 99 | } |
| ellisbhastroud | 3:a8960004d261 | 100 | |
| ellisbhastroud | 9:bc34f2243e43 | 101 | bool Ball::check_hole(Coord hole) //returns true when ball is hit in hole and next level begins |
| ellisbhastroud | 9:bc34f2243e43 | 102 | { |
| ellisbhastroud | 9:bc34f2243e43 | 103 | if(_x_pos > hole.x - 1 && _x_pos < hole.x + 2 && _y_pos > hole.y - 1 && _y_pos < hole.y + 2) { |
| ellisbhastroud | 9:bc34f2243e43 | 104 | _x_vel = 0; //stop ball moving |
| ellisbhastroud | 9:bc34f2243e43 | 105 | _y_vel = 0; |
| ellisbhastroud | 9:bc34f2243e43 | 106 | _x_pos = hole.x; |
| ellisbhastroud | 9:bc34f2243e43 | 107 | _y_pos = hole.y; |
| ellisbhastroud | 9:bc34f2243e43 | 108 | return true; //causes next level process to begin |
| ellisbhastroud | 9:bc34f2243e43 | 109 | } |
| ellisbhastroud | 9:bc34f2243e43 | 110 | else { |
| ellisbhastroud | 9:bc34f2243e43 | 111 | return false; |
| ellisbhastroud | 9:bc34f2243e43 | 112 | } |
| ellisbhastroud | 9:bc34f2243e43 | 113 | } |
| ellisbhastroud | 9:bc34f2243e43 | 114 | |
| ellisbhastroud | 9:bc34f2243e43 | 115 | void Ball::check_wall_bounce(WallMap map[], int size) //uses information from WallMap array for each level to check for bounces |
| ellisbhastroud | 3:a8960004d261 | 116 | { |
| ellisbhastroud | 8:d410856c6d04 | 117 | for(int i = 0; i < size; i ++) { |
| ellisbhastroud | 3:a8960004d261 | 118 | |
| ellisbhastroud | 10:9f54a6366e94 | 119 | if(map[i].wall == LEFT) { //each bounce check algorithm uses wall type and start/end coordinates then bounces if the next frame ball position is past the wall |
| ellisbhastroud | 8:d410856c6d04 | 120 | left_bounce(map[i].start,map[i].end); |
| ellisbhastroud | 8:d410856c6d04 | 121 | } |
| ellisbhastroud | 8:d410856c6d04 | 122 | else if(map[i].wall == RIGHT) { |
| ellisbhastroud | 8:d410856c6d04 | 123 | right_bounce(map[i].start,map[i].end); |
| ellisbhastroud | 8:d410856c6d04 | 124 | } |
| ellisbhastroud | 8:d410856c6d04 | 125 | else if(map[i].wall == TOP) { |
| ellisbhastroud | 8:d410856c6d04 | 126 | top_bounce(map[i].start,map[i].end); |
| ellisbhastroud | 8:d410856c6d04 | 127 | } |
| ellisbhastroud | 8:d410856c6d04 | 128 | else if(map[i].wall == BOTTOM) { |
| ellisbhastroud | 8:d410856c6d04 | 129 | bottom_bounce(map[i].start,map[i].end); |
| ellisbhastroud | 8:d410856c6d04 | 130 | } |
| ellisbhastroud | 10:9f54a6366e94 | 131 | else if(map[i].wall == BOTTOMLEFT) { |
| ellisbhastroud | 10:9f54a6366e94 | 132 | bottom_left_bounce(map[i].start,map[i].end); |
| ellisbhastroud | 10:9f54a6366e94 | 133 | } |
| ellisbhastroud | 10:9f54a6366e94 | 134 | else if(map[i].wall == BOTTOMRIGHT) { |
| ellisbhastroud | 10:9f54a6366e94 | 135 | bottom_right_bounce(map[i].start,map[i].end); |
| ellisbhastroud | 10:9f54a6366e94 | 136 | } |
| ellisbhastroud | 10:9f54a6366e94 | 137 | else if(map[i].wall == TOPLEFT) { |
| ellisbhastroud | 10:9f54a6366e94 | 138 | top_left_bounce(map[i].start,map[i].end); |
| ellisbhastroud | 10:9f54a6366e94 | 139 | } |
| ellisbhastroud | 10:9f54a6366e94 | 140 | else if(map[i].wall == TOPRIGHT) { |
| ellisbhastroud | 10:9f54a6366e94 | 141 | top_right_bounce(map[i].start,map[i].end); |
| ellisbhastroud | 10:9f54a6366e94 | 142 | } |
| ellisbhastroud | 8:d410856c6d04 | 143 | |
| ellisbhastroud | 8:d410856c6d04 | 144 | } |
| ellisbhastroud | 3:a8960004d261 | 145 | } |
| ellisbhastroud | 5:0b31909caf7f | 146 | |
| ellisbhastroud | 8:d410856c6d04 | 147 | void Ball::left_bounce(Coord start, Coord end) //top check for left wall collision |
| ellisbhastroud | 5:0b31909caf7f | 148 | { |
| ellisbhastroud | 10:9f54a6366e94 | 149 | if(_x_pos + _x_vel*10.0f/_frame_rate - 1 < start.x && _x_pos + _x_vel*10.0f/_frame_rate - 1 > start.x - 5 && _y_pos >= start.y && _y_pos + 1 <= end.y && _x_vel < 0){ // left wall |
| ellisbhastroud | 10:9f54a6366e94 | 150 | _x_pos = start.x; |
| ellisbhastroud | 10:9f54a6366e94 | 151 | _x_vel = -_x_vel; |
| ellisbhastroud | 5:0b31909caf7f | 152 | } |
| ellisbhastroud | 8:d410856c6d04 | 153 | } |
| ellisbhastroud | 8:d410856c6d04 | 154 | |
| ellisbhastroud | 8:d410856c6d04 | 155 | void Ball::right_bounce(Coord start, Coord end) |
| ellisbhastroud | 8:d410856c6d04 | 156 | { |
| ellisbhastroud | 10:9f54a6366e94 | 157 | if(_x_pos + _x_vel*10.0f/_frame_rate + 1 > start.x && _x_pos + _x_vel*10.0f/_frame_rate + 1 < start.x + 5 && _y_pos >= start.y && _y_pos + 1 <= end.y && _x_vel > 0){ //right wall x + 1 |
| ellisbhastroud | 8:d410856c6d04 | 158 | _x_pos = start.x - 1; |
| ellisbhastroud | 8:d410856c6d04 | 159 | _x_vel = -_x_vel; |
| ellisbhastroud | 5:0b31909caf7f | 160 | } |
| ellisbhastroud | 8:d410856c6d04 | 161 | } |
| ellisbhastroud | 8:d410856c6d04 | 162 | |
| ellisbhastroud | 8:d410856c6d04 | 163 | void Ball::top_bounce(Coord start, Coord end) |
| ellisbhastroud | 8:d410856c6d04 | 164 | { |
| ellisbhastroud | 10:9f54a6366e94 | 165 | if(_y_pos + _y_vel*10.0f/_frame_rate - 1 < start.y && _y_pos + _y_vel*10.0f/_frame_rate - 1 > start.y - 5 && _x_pos >= start.x && _x_pos + 1 <= end.x && _y_vel < 0){ //top wall y -1 |
| ellisbhastroud | 10:9f54a6366e94 | 166 | _y_pos = start.y; |
| ellisbhastroud | 8:d410856c6d04 | 167 | _y_vel = -_y_vel; |
| ellisbhastroud | 5:0b31909caf7f | 168 | } |
| ellisbhastroud | 8:d410856c6d04 | 169 | } |
| ellisbhastroud | 8:d410856c6d04 | 170 | |
| ellisbhastroud | 8:d410856c6d04 | 171 | void Ball::bottom_bounce(Coord start, Coord end) |
| ellisbhastroud | 8:d410856c6d04 | 172 | { |
| ellisbhastroud | 10:9f54a6366e94 | 173 | if(_y_pos + _y_vel*10.0f/_frame_rate + 2 > start.y && _y_pos + _y_vel*10.0f/_frame_rate + 2 < start.y + 5 && _x_pos >= start.x && _x_pos + 1 <= end.x && _y_vel > 0){ //bottom wall |
| ellisbhastroud | 8:d410856c6d04 | 174 | _y_pos = start.y - 1; |
| ellisbhastroud | 8:d410856c6d04 | 175 | _y_vel = -_y_vel; |
| ellisbhastroud | 8:d410856c6d04 | 176 | } |
| ellisbhastroud | 5:0b31909caf7f | 177 | } |
| ellisbhastroud | 10:9f54a6366e94 | 178 | |
| ellisbhastroud | 10:9f54a6366e94 | 179 | void Ball::bottom_left_bounce(Coord start, Coord end) |
| ellisbhastroud | 10:9f54a6366e94 | 180 | { |
| ellisbhastroud | 10:9f54a6366e94 | 181 | if((_y_pos + _y_vel*10.0f/_frame_rate + 1) > (_x_pos + _x_vel*10.0f/_frame_rate - 1) + (start.y-start.x) && _x_pos >= start.x && _x_pos <= end.x && _y_pos >= start.y && _y_pos <= end.y ) { |
| ellisbhastroud | 10:9f54a6366e94 | 182 | swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped |
| ellisbhastroud | 10:9f54a6366e94 | 183 | } |
| ellisbhastroud | 10:9f54a6366e94 | 184 | } |
| ellisbhastroud | 10:9f54a6366e94 | 185 | |
| ellisbhastroud | 10:9f54a6366e94 | 186 | void Ball::bottom_right_bounce(Coord start, Coord end) //start to end = left to right sides of line |
| ellisbhastroud | 10:9f54a6366e94 | 187 | { |
| ellisbhastroud | 10:9f54a6366e94 | 188 | if((_x_pos + _x_vel*10.0f/_frame_rate + 1) > -(_y_pos + _y_vel*10.0f/_frame_rate + 1) + (start.x+start.y) && _x_pos >= start.x && _x_pos <= end.x &&_y_pos >= end.y && _y_pos <= start.y){ |
| ellisbhastroud | 10:9f54a6366e94 | 189 | |
| ellisbhastroud | 10:9f54a6366e94 | 190 | _x_vel = -_x_vel; |
| ellisbhastroud | 10:9f54a6366e94 | 191 | _y_vel = -_y_vel; |
| ellisbhastroud | 10:9f54a6366e94 | 192 | swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped |
| ellisbhastroud | 10:9f54a6366e94 | 193 | } |
| ellisbhastroud | 10:9f54a6366e94 | 194 | } |
| ellisbhastroud | 10:9f54a6366e94 | 195 | |
| ellisbhastroud | 10:9f54a6366e94 | 196 | void Ball::top_left_bounce(Coord start, Coord end) |
| ellisbhastroud | 10:9f54a6366e94 | 197 | { |
| ellisbhastroud | 10:9f54a6366e94 | 198 | if((_x_pos + _x_vel*10.0f/_frame_rate - 1) < -(_y_pos + _y_vel*10.0f/_frame_rate + 1) + (start.x+start.y) && _x_pos >= start.x && _x_pos <= end.x &&_y_pos >= end.y && _y_pos <= start.y){ |
| ellisbhastroud | 10:9f54a6366e94 | 199 | _x_vel = -_x_vel; //as is negative wall |
| ellisbhastroud | 10:9f54a6366e94 | 200 | _y_vel = -_y_vel; |
| ellisbhastroud | 10:9f54a6366e94 | 201 | |
| ellisbhastroud | 10:9f54a6366e94 | 202 | swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped |
| ellisbhastroud | 10:9f54a6366e94 | 203 | } |
| ellisbhastroud | 10:9f54a6366e94 | 204 | } |
| ellisbhastroud | 10:9f54a6366e94 | 205 | |
| ellisbhastroud | 10:9f54a6366e94 | 206 | void Ball::top_right_bounce(Coord start, Coord end) |
| ellisbhastroud | 10:9f54a6366e94 | 207 | { |
| ellisbhastroud | 10:9f54a6366e94 | 208 | if((_y_pos + _y_vel*10.0f/_frame_rate - 1) < (_x_pos + _x_vel*10.0f/_frame_rate + 1) + (start.y-start.x) && _x_pos >= start.x && _x_pos <= end.x && _y_pos >= start.y && _y_pos <= end.y){ |
| ellisbhastroud | 10:9f54a6366e94 | 209 | |
| ellisbhastroud | 10:9f54a6366e94 | 210 | swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped |
| ellisbhastroud | 10:9f54a6366e94 | 211 | } |
| ellisbhastroud | 10:9f54a6366e94 | 212 | } |
| ellisbhastroud | 10:9f54a6366e94 | 213 | |
| ellisbhastroud | 10:9f54a6366e94 | 214 | void Ball::set_frame_rate(int frame_rate) |
| ellisbhastroud | 10:9f54a6366e94 | 215 | { |
| ellisbhastroud | 10:9f54a6366e94 | 216 | _frame_rate = frame_rate; |
| ellisbhastroud | 10:9f54a6366e94 | 217 | } |
| ellisbhastroud | 3:a8960004d261 | 218 | //private methods |
| ellisbhastroud | 3:a8960004d261 | 219 |