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
Diff: Ball/Ball.cpp
- Revision:
- 8:d410856c6d04
- Parent:
- 5:0b31909caf7f
- Child:
- 9:bc34f2243e43
diff -r 5a19dd9fe8a4 -r d410856c6d04 Ball/Ball.cpp
--- a/Ball/Ball.cpp Wed Apr 17 11:13:54 2019 +0000
+++ b/Ball/Ball.cpp Thu Apr 18 10:42:42 2019 +0000
@@ -38,22 +38,31 @@
lcd.printString(buffer,40,0);
}
-void Ball::drawPower(N5110 &lcd, Gamepad &pad)
+void Ball::drawPower(N5110 &lcd, float mag)
{
-
- _mag = pad.get_mag();
lcd.drawRect(0,0,36,6,FILL_TRANSPARENT);
- lcd.drawRect(0,0,36*_mag,6,FILL_BLACK);
+ lcd.drawRect(0,0,36*mag,6,FILL_BLACK);
}
+void Ball::drawAim(N5110 &lcd, Vector2D joy_coord, float angle)
+{
+ if(angle != -1.0f) {
+ lcd.drawLine(10,16,10+12*joy_coord.x,16+-12*joy_coord.y,1);
+ }
+
+}
+
void Ball::move_ball(int frame_rate)
{
- _x_pos = _x_pos + _x_vel*10.0f/frame_rate; //frame_rate used to scale ball movement so that it is the same for any fps
+ _x_pos = _x_pos + _x_vel*10.0f/frame_rate; //move ball position at rate proportional to velocity in each direction
_y_pos = _y_pos + _y_vel*10.0f/frame_rate;
- _x_vel = _x_vel*(1.0f-(0.8f/frame_rate)); //ball slows down 10% each loop (scaled by time between frames)
- _y_vel = _y_vel*(1.0f-(0.8f/frame_rate));
-
+ _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
+ _y_vel = _y_vel*(1.0f-(0.6f/frame_rate));
+ 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
+ _x_vel = 0;
+ _y_vel = 0;
+ }
}
Vector2D Ball::get_ball_pos()
@@ -62,13 +71,11 @@
return pos;
}
-void Ball::shoot_ball(Gamepad &pad)
-{
- _joystick = pad.get_mapped_coord();
-
- if(pad.check_event(Gamepad::A_PRESSED) == true && abs(_x_vel) < 0.05f && abs(_y_vel) < 0.05f){ //if ball stationary and a pressed then shoot
- _x_vel = 8.0f * _joystick.x; //scale x velocity by joystick direction and magnitude
- _y_vel = 8.0f * -_joystick.y; //scale y velocity by joystick direction and magnitude
+void Ball::shoot_ball(Gamepad &pad, Vector2D _joy_coord)
+{
+ if(pad.check_event(Gamepad::A_PRESSED) == true && abs(_x_vel) < TOL && abs(_y_vel) < TOL){ //if ball stationary and a pressed then shoot
+ _x_vel = 6.0f * _joy_coord.x; //scale x velocity by joystick direction and magnitude
+ _y_vel = 6.0f * -_joy_coord.y; //scale y velocity by joystick direction and magnitude
_shot_count ++; //increment shot count
}
@@ -86,40 +93,56 @@
_y_vel = y_vel;
}
-void Ball::read_joy(Gamepad &pad)
+void Ball::check_wall_bounce(Course map[], int size) //uses information from course struct to check if ball bounces against any of the walls
{
-
- _joystick = pad.get_mapped_coord(); //x and y coordinates from joystick assigned
-
+ for(int i = 0; i < size; i ++) {
+ if(map[i].wall == LEFT) {
+ left_bounce(map[i].start,map[i].end);
+ }
+ else if(map[i].wall == RIGHT) {
+ right_bounce(map[i].start,map[i].end);
+ }
+ else if(map[i].wall == TOP) {
+ top_bounce(map[i].start,map[i].end);
+ }
+ else if(map[i].wall == BOTTOM) {
+ bottom_bounce(map[i].start,map[i].end);
+ }
+
+ }
}
-void Ball::check_wall_bounce() //check before and after move_ball called
+void Ball::left_bounce(Coord start, Coord end) //top check for left wall collision
{
- if(_x_pos - 1 < 9 && _y_pos >= 26 && _y_pos <= 40 && _x_vel < 0){ // left wall (x=9 ,26<=y<=40)
- _x_pos = 10;
- _x_vel = -0.95f*_x_vel; //5% velocity lost on impact and bounce
+ 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)
+ _x_pos = start.x + 1;
+ _x_vel = -_x_vel; //5% velocity lost on impact and bounce
}
- if(_x_pos + 1 > 74 && _y_pos <= 40 && _y_pos >= 9 && _x_vel > 0){ //right wall x + 1
- _x_pos = 72;
- _x_vel = -0.95f*_x_vel;
+}
+
+void Ball::right_bounce(Coord start, Coord end)
+{
+ if(_x_pos+_x_vel + 1 > start.x && _y_pos >= start.y && _y_pos <= end.y && _x_vel > 0){ //right wall x + 1
+ _x_pos = start.x - 1;
+ _x_vel = -_x_vel;
}
- if(_y_pos - 1 < 9 && _x_pos <= 74 && _x_pos >= 50 && _y_vel < 0){ //top wall y -1
- _y_pos = 10;
- _y_vel = -0.95f*_y_vel;
- }
- if(_y_pos + 1 > 40 && _x_pos >= 9 && _x_pos <= 74 && _y_vel > 0){ //bottom wall y + 2
- _y_pos = 39;
- _y_vel = -0.95f*_y_vel;
+}
+
+void Ball::top_bounce(Coord start, Coord end)
+{
+ if(_y_pos+_y_vel - 1 < start.y && _x_pos >= start.x && _x_pos <= end.x && _y_vel < 0){ //top wall y -1
+ _y_pos = start.y + 1;
+ _y_vel = -_y_vel;
}
- if(_x_pos - 1 < 50 && _y_pos >= 9 && _y_pos <= 26 && _x_vel < 0){ // left wall x -1
- _x_pos = 51;
- _x_vel = -0.95f*_x_vel; //5% velocity lost on impact and bounce
- }
- if(_y_pos - 1 < 26 && _x_pos <= 50 && _x_pos >= 9 && _y_vel < 0 ){ //top wall y -1
- _y_pos = 27;
- _y_vel = -0.95f*_y_vel;
- }
+}
+
+void Ball::bottom_bounce(Coord start, Coord end)
+{
+ if(_y_pos+_y_vel + 1 > start.y && _x_pos >= start.x && _x_pos <= end.x && _y_vel > 0){ //bottom wall y + 2
+ _y_pos = start.y - 1;
+ _y_vel = -_y_vel;
+ }
}
//private methods