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@8:d410856c6d04, 2019-04-18 (annotated)
- Committer:
- ellisbhastroud
- Date:
- Thu Apr 18 10:42:42 2019 +0000
- Revision:
- 8:d410856c6d04
- Parent:
- 5:0b31909caf7f
- Child:
- 9:bc34f2243e43
Course map draw and bounce algorithms complete
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 | 3:a8960004d261 | 19 | void Ball::init(float x, float y) //ball starts stationary in x and y positions given |
| ellisbhastroud | 3:a8960004d261 | 20 | { |
| ellisbhastroud | 3:a8960004d261 | 21 | _x_pos = x; |
| ellisbhastroud | 3:a8960004d261 | 22 | _y_pos = y; |
| ellisbhastroud | 4:035448357749 | 23 | _x_vel = 0.0f; |
| ellisbhastroud | 4:035448357749 | 24 | _y_vel = 0.0f; |
| ellisbhastroud | 4:035448357749 | 25 | _shot_count = 0; |
| ellisbhastroud | 3:a8960004d261 | 26 | |
| 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 | 5:0b31909caf7f | 43 | lcd.drawRect(0,0,36,6,FILL_TRANSPARENT); |
| ellisbhastroud | 8:d410856c6d04 | 44 | lcd.drawRect(0,0,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 | 5:0b31909caf7f | 56 | void Ball::move_ball(int frame_rate) |
| ellisbhastroud | 3:a8960004d261 | 57 | { |
| ellisbhastroud | 8:d410856c6d04 | 58 | _x_pos = _x_pos + _x_vel*10.0f/frame_rate; //move ball position at rate proportional to velocity in each direction |
| ellisbhastroud | 4:035448357749 | 59 | _y_pos = _y_pos + _y_vel*10.0f/frame_rate; |
| ellisbhastroud | 8:d410856c6d04 | 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 | 8:d410856c6d04 | 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 | 3:a8960004d261 | 90 | void Ball::set_vel(float x_vel, float y_vel) |
| ellisbhastroud | 3:a8960004d261 | 91 | { |
| ellisbhastroud | 3:a8960004d261 | 92 | _x_vel = x_vel; |
| ellisbhastroud | 3:a8960004d261 | 93 | _y_vel = y_vel; |
| ellisbhastroud | 3:a8960004d261 | 94 | } |
| ellisbhastroud | 3:a8960004d261 | 95 | |
| ellisbhastroud | 8:d410856c6d04 | 96 | void Ball::check_wall_bounce(Course map[], int size) //uses information from course struct to check if ball bounces against any of the walls |
| ellisbhastroud | 3:a8960004d261 | 97 | { |
| ellisbhastroud | 8:d410856c6d04 | 98 | for(int i = 0; i < size; i ++) { |
| ellisbhastroud | 3:a8960004d261 | 99 | |
| ellisbhastroud | 8:d410856c6d04 | 100 | if(map[i].wall == LEFT) { |
| ellisbhastroud | 8:d410856c6d04 | 101 | left_bounce(map[i].start,map[i].end); |
| ellisbhastroud | 8:d410856c6d04 | 102 | } |
| ellisbhastroud | 8:d410856c6d04 | 103 | else if(map[i].wall == RIGHT) { |
| ellisbhastroud | 8:d410856c6d04 | 104 | right_bounce(map[i].start,map[i].end); |
| ellisbhastroud | 8:d410856c6d04 | 105 | } |
| ellisbhastroud | 8:d410856c6d04 | 106 | else if(map[i].wall == TOP) { |
| ellisbhastroud | 8:d410856c6d04 | 107 | top_bounce(map[i].start,map[i].end); |
| ellisbhastroud | 8:d410856c6d04 | 108 | } |
| ellisbhastroud | 8:d410856c6d04 | 109 | else if(map[i].wall == BOTTOM) { |
| ellisbhastroud | 8:d410856c6d04 | 110 | bottom_bounce(map[i].start,map[i].end); |
| ellisbhastroud | 8:d410856c6d04 | 111 | } |
| ellisbhastroud | 8:d410856c6d04 | 112 | |
| ellisbhastroud | 8:d410856c6d04 | 113 | } |
| ellisbhastroud | 3:a8960004d261 | 114 | } |
| ellisbhastroud | 5:0b31909caf7f | 115 | |
| ellisbhastroud | 8:d410856c6d04 | 116 | void Ball::left_bounce(Coord start, Coord end) //top check for left wall collision |
| ellisbhastroud | 5:0b31909caf7f | 117 | { |
| ellisbhastroud | 8:d410856c6d04 | 118 | if(_x_pos+_x_vel - 1 < start.x && _y_pos >= start.y && _y_pos <= end.y && _x_vel < 0){ // left wall (x=9 ,26<=y<=40) |
| ellisbhastroud | 8:d410856c6d04 | 119 | _x_pos = start.x + 1; |
| ellisbhastroud | 8:d410856c6d04 | 120 | _x_vel = -_x_vel; //5% velocity lost on impact and bounce |
| ellisbhastroud | 5:0b31909caf7f | 121 | } |
| ellisbhastroud | 8:d410856c6d04 | 122 | } |
| ellisbhastroud | 8:d410856c6d04 | 123 | |
| ellisbhastroud | 8:d410856c6d04 | 124 | void Ball::right_bounce(Coord start, Coord end) |
| ellisbhastroud | 8:d410856c6d04 | 125 | { |
| ellisbhastroud | 8:d410856c6d04 | 126 | if(_x_pos+_x_vel + 1 > start.x && _y_pos >= start.y && _y_pos <= end.y && _x_vel > 0){ //right wall x + 1 |
| ellisbhastroud | 8:d410856c6d04 | 127 | _x_pos = start.x - 1; |
| ellisbhastroud | 8:d410856c6d04 | 128 | _x_vel = -_x_vel; |
| ellisbhastroud | 5:0b31909caf7f | 129 | } |
| ellisbhastroud | 8:d410856c6d04 | 130 | } |
| ellisbhastroud | 8:d410856c6d04 | 131 | |
| ellisbhastroud | 8:d410856c6d04 | 132 | void Ball::top_bounce(Coord start, Coord end) |
| ellisbhastroud | 8:d410856c6d04 | 133 | { |
| ellisbhastroud | 8:d410856c6d04 | 134 | if(_y_pos+_y_vel - 1 < start.y && _x_pos >= start.x && _x_pos <= end.x && _y_vel < 0){ //top wall y -1 |
| ellisbhastroud | 8:d410856c6d04 | 135 | _y_pos = start.y + 1; |
| ellisbhastroud | 8:d410856c6d04 | 136 | _y_vel = -_y_vel; |
| ellisbhastroud | 5:0b31909caf7f | 137 | } |
| ellisbhastroud | 8:d410856c6d04 | 138 | } |
| ellisbhastroud | 8:d410856c6d04 | 139 | |
| ellisbhastroud | 8:d410856c6d04 | 140 | void Ball::bottom_bounce(Coord start, Coord end) |
| ellisbhastroud | 8:d410856c6d04 | 141 | { |
| ellisbhastroud | 8:d410856c6d04 | 142 | if(_y_pos+_y_vel + 1 > start.y && _x_pos >= start.x && _x_pos <= end.x && _y_vel > 0){ //bottom wall y + 2 |
| ellisbhastroud | 8:d410856c6d04 | 143 | _y_pos = start.y - 1; |
| ellisbhastroud | 8:d410856c6d04 | 144 | _y_vel = -_y_vel; |
| ellisbhastroud | 8:d410856c6d04 | 145 | } |
| ellisbhastroud | 5:0b31909caf7f | 146 | } |
| ellisbhastroud | 3:a8960004d261 | 147 | //private methods |
| ellisbhastroud | 3:a8960004d261 | 148 |