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:
- 11:6d2027253aa9
- Parent:
- 10:9f54a6366e94
- Child:
- 12:7f7fadb5c106
--- a/Ball/Ball.cpp Tue Apr 23 12:03:38 2019 +0000
+++ b/Ball/Ball.cpp Thu Apr 25 15:16:38 2019 +0000
@@ -146,15 +146,15 @@
void Ball::left_bounce(Coord start, Coord end) //top check for left wall collision
{
- 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
- _x_pos = start.x;
+ if(_x_pos + _x_vel*10.0f/_frame_rate - 1 < start.x && _x_pos + _x_vel*10.0f/_frame_rate > start.x - 10 && _y_pos >= start.y && _y_pos + 1 <= end.y && _x_vel < 0){ // left wall
+ _x_pos = start.x + 1;
_x_vel = -_x_vel;
}
}
void Ball::right_bounce(Coord start, Coord end)
{
- 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
+ if(_x_pos + _x_vel*10.0f/_frame_rate + 2 > start.x && _x_pos + _x_vel*10.0f/_frame_rate < start.x + 10 && _y_pos >= start.y && _y_pos + 1 <= end.y && _x_vel > 0){ //right wall x + 1
_x_pos = start.x - 1;
_x_vel = -_x_vel;
}
@@ -162,21 +162,21 @@
void Ball::top_bounce(Coord start, Coord end)
{
- 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
- _y_pos = start.y;
+ if(_y_pos + _y_vel*10.0f/_frame_rate - 1 < start.y && _y_pos + _y_vel*10.0f/_frame_rate > start.y - 10 && _x_pos >= start.x && _x_pos + 1 <= end.x && _y_vel < 0){ //top wall y -1
+ _y_pos = start.y + 1;
_y_vel = -_y_vel;
}
}
void Ball::bottom_bounce(Coord start, Coord end)
{
- 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
+ if(_y_pos + _y_vel*10.0f/_frame_rate + 2 > start.y && _y_pos + _y_vel*10.0f/_frame_rate < start.y + 10 && _x_pos >= start.x && _x_pos + 1 <= end.x && _y_vel > 0){ //bottom wall
_y_pos = start.y - 1;
_y_vel = -_y_vel;
}
}
-void Ball::bottom_left_bounce(Coord start, Coord end)
+void Ball::bottom_left_bounce(Coord start, Coord end)
{
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 ) {
swap(_x_vel, _y_vel); //reflects from wall with velocity directions swapped