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.
Diff: PongEngine/PongEngine.cpp
- Revision:
- 4:6bd488b5b31a
- Parent:
- 3:5719d0fe94ed
- Child:
- 6:7f79f320b827
diff -r 5719d0fe94ed -r 6bd488b5b31a PongEngine/PongEngine.cpp
--- a/PongEngine/PongEngine.cpp Thu Apr 18 22:04:36 2019 +0000
+++ b/PongEngine/PongEngine.cpp Fri Apr 19 12:30:58 2019 +0000
@@ -86,6 +86,7 @@
check_wall_collision(pad);
check_paddle_collisions(pad);
+ check_brick_collisions(pad);
}
void PongEngine::check_wall_collision(Gamepad &pad)
@@ -120,6 +121,7 @@
// update ball parameters
_ball.set_velocity(ball_velocity);
_ball.set_pos(ball_pos);
+
}
void PongEngine::check_paddle_collisions(Gamepad &pad)
@@ -133,12 +135,12 @@
// see if ball has hit the paddle by checking for overlaps
if (
- (ball_pos.y >= p1_pos.x) && //left
- (ball_pos.y <= p1_pos.x + _paddle_width) && //right
- (ball_pos.x >= _p1y) && //bottom
- (ball_pos.x <= _p1y + _paddle_height) //top
- ) {
- // if it has, fix position and reflect x velocity
+ (ball_pos.x >= p1_pos.x) && //left
+ (ball_pos.x <= p1_pos.x + _paddle_width) && //right
+ (ball_pos.y >= _p1y) && //bottom
+ (ball_pos.y <= _p1y + _paddle_height) //top
+ ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
+ // if it has, fix position and reflect x velocity
ball_pos.y = _p1y + _paddle_height;
ball_velocity.y = -ball_velocity.y;
// audio feedback
@@ -150,6 +152,35 @@
_ball.set_pos(ball_pos);
}
+void PongEngine::check_brick_collisions(Gamepad &pad)
+{
+ // read current ball attributes
+ Vector2D ball_pos = _ball.get_pos();
+ Vector2D ball_velocity = _ball.get_velocity();
+
+ // check p1 first
+ Vector2D _grid1_pos = _grid1.get_pos();
+
+ // see if ball has hit the paddle by checking for overlaps
+ if (
+ (ball_pos.x >= _grid1_pos.x) && //left
+ (ball_pos.x <= _grid1_pos.x + WIDTH_BRICK) && //right
+ (ball_pos.y >= _grid1_pos.y) && //bottom
+ (ball_pos.y <= _grid1_pos.y + HEIGHT_BRICK) //top
+ ) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
+ // if it has, fix position and reflect x velocity
+ ball_pos.y = _grid1_pos.y;
+ ball_velocity.y = -ball_velocity.y;
+ // audio feedback
+ pad.tone(1000.0,0.1);
+ }
+
+ // write new attributes
+ _ball.set_velocity(ball_velocity);
+ _ball.set_pos(ball_pos);
+ _grid1.hit();
+}
+
void PongEngine::check_goal(Gamepad &pad)
{
Vector2D ball_pos = _ball.get_pos();