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: Ball/Ball.cpp
- Revision:
- 22:745b4d352183
- Parent:
- 21:bcc84d5cb068
- Child:
- 25:28c57be06933
--- a/Ball/Ball.cpp Tue May 08 12:20:04 2018 +0000 +++ b/Ball/Ball.cpp Tue May 08 12:55:17 2018 +0000 @@ -57,4 +57,148 @@ Vector2D Ball::get_pos() { Vector2D p = {_circx,_circy}; return p; +} + +void Ball::check_wall_collision(Gamepad &pad) +{ + // FOR LOOP TO STOP BALL FROM GOING THROUGH WALLS + for (int i = 0; i <= 12; i++) { + //printf(" Stage %d\n", i); + + if(i == 0) { // SET VARIABLES FOR EACH WALL + _a = 10; + _b = 0; + _c = 1; + _d = 39; + + } else if (i == 1) { + _a = 18; + _b = 32; + _c = 1; + _d = 15; + } + + else if (i == 2) { + _a = 36; + _b = 25; + _c = 1; + _d = 25; + } + + else if(i == 3) { + _a = 45; + _b = 0; + _c = 1; + _d = 11; + } + + else if (i == 4) { + _a = 45; + _b = 18; + _c = 1; + _d = 30; + } + + else if (i == 5) { + _a = 55; + _b = 6; + _c = 1; + _d = 45; + } + + else if (i == 6) { + _a = 64; + _b = 0; + _c = 1; + _d = 20; + } + + else if (i == 7) { + _a = 64; + _b = 27; + _c = 1; + _d = 13; + } + + else if (i == 8) { + _a = 72; + _b = 10; + _c = 1; + _d = 30; + } + + else if (i == 9) { + _a = 18; + _b = 25; + _c = 18; + _d = 1; + } else if (i == 10) { + _a = 18; + _b = 18; + _c = 27; + _d = 1; + } + + else if (i == 11) { + _a = 18; + _b = 10; + _c = 27; + _d = 1; + } + + else if (i == 12) { + _a = 64; + _b = 40; + _c = 20; + _d = 1; + } + + + if ( + (_circy >= _b - 2) && //top + (_circy <= 1 + _b + _d) && //bottom + (_circx >= _a - 2) && //left + (_circx <= _a + _c + 1) //right + ) { + printf("COLLISION"); + //left + if (_circx <= _a - 2) { + if(_circx >= _a - 3) { + _circx = _a - 3; + } + } + + //right + if(_circx >= _a + 2) { + if(_circx <= _a + 3) { + _circx = _a + 3; + } + } + + //top + if(_circy <= _b - 2) { + if(_circy >= _b - 3) { + _circy = _b - 3; + } + } + + //bottom + if(_circy >= _b + _d) { + if(_circy <= 2 + _b + _d) { + (_circy = 2 + _b + _d); + } + } + } + } + + + // WHEN THE BALL REACHES THE Y-AXIS NEEDED WHICH IS 27, THEN THE JOYSTICK FREELY MOVE THE BALL RIGHT THROUGH THE OPENING OF THE SMAZE WALL, + // HOWEVER, IF THE BALL IS NOT EQUAL TO THE Y-AXIS NEEDED, THEN THE BALL MUST BE RESTRICTED TO MOVING SO THAT IT DOES NOT PASS THE WALLS. + if (_circy == 27) { + if (_circx > WIDTH) { + _circx = WIDTH; + } + } else if (_circx > 80) { + _circx = 80; + } } \ No newline at end of file