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.
Ball/Ball.cpp@29:42651f87522b, 2019-04-27 (annotated)
- Committer:
- JamesCummins
- Date:
- Sat Apr 27 12:50:30 2019 +0000
- Revision:
- 29:42651f87522b
- Parent:
- 23:61fa82f76808
- Child:
- 38:a85bc227b907
Classic wall collision coded but always returning true (preventing classic from running) - needs debugging
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JamesCummins | 11:2cf0d4ce8677 | 1 | #include "Ball.h" |
JamesCummins | 9:ce0a12fb205b | 2 | |
JamesCummins | 9:ce0a12fb205b | 3 | Ball::Ball() |
JamesCummins | 9:ce0a12fb205b | 4 | {} |
JamesCummins | 9:ce0a12fb205b | 5 | |
JamesCummins | 9:ce0a12fb205b | 6 | Ball::~Ball() |
JamesCummins | 9:ce0a12fb205b | 7 | {} |
JamesCummins | 9:ce0a12fb205b | 8 | |
JamesCummins | 9:ce0a12fb205b | 9 | void Ball::init(int radius){ |
JamesCummins | 9:ce0a12fb205b | 10 | _radius = radius; |
JamesCummins | 29:42651f87522b | 11 | _x = 42; |
JamesCummins | 9:ce0a12fb205b | 12 | _y = 24; |
JamesCummins | 17:5104ecef5bd0 | 13 | _velocity.x = 0; |
JamesCummins | 17:5104ecef5bd0 | 14 | _velocity.y = 0; |
JamesCummins | 23:61fa82f76808 | 15 | _ball_speed = 5; |
JamesCummins | 9:ce0a12fb205b | 16 | } |
JamesCummins | 9:ce0a12fb205b | 17 | |
JamesCummins | 13:e5a36fbd48ae | 18 | void Ball::draw(N5110 &lcd){ |
JamesCummins | 13:e5a36fbd48ae | 19 | int radius = get_radius(); |
JamesCummins | 13:e5a36fbd48ae | 20 | lcd.drawCircle(_x, _y, radius, FILL_BLACK); |
JamesCummins | 9:ce0a12fb205b | 21 | } |
JamesCummins | 9:ce0a12fb205b | 22 | |
JamesCummins | 17:5104ecef5bd0 | 23 | void Ball::read_input(FXOS8700CQ &accelerometer){ |
JamesCummins | 13:e5a36fbd48ae | 24 | Data values = accelerometer.get_values(); |
JamesCummins | 23:61fa82f76808 | 25 | _velocity.x = -(5+_ball_speed)*values.ay; //axes of the accelerometer are different to orientation of the screen |
JamesCummins | 23:61fa82f76808 | 26 | _velocity.y = -(5+_ball_speed)*values.ax; //negative to account for reversed direction |
JamesCummins | 23:61fa82f76808 | 27 | /*printf("ax = %f | ay = %f | ", values.ax, values.ay); |
JamesCummins | 23:61fa82f76808 | 28 | printf("vel.x = %f | vel.y = %f\n", _velocity.x, _velocity.y);*/ |
JamesCummins | 17:5104ecef5bd0 | 29 | } |
JamesCummins | 17:5104ecef5bd0 | 30 | |
JamesCummins | 17:5104ecef5bd0 | 31 | void Ball::update(){ |
JamesCummins | 17:5104ecef5bd0 | 32 | int RADIUS = get_radius(); |
JamesCummins | 9:ce0a12fb205b | 33 | _x += _velocity.x; |
JamesCummins | 9:ce0a12fb205b | 34 | _y += _velocity.y; |
JamesCummins | 14:108052b6222b | 35 | if (_x < RADIUS){ _x = RADIUS;} //check wall collisions |
JamesCummins | 13:e5a36fbd48ae | 36 | if (_x > 84 - RADIUS){ _x = 83 - RADIUS;} |
JamesCummins | 13:e5a36fbd48ae | 37 | if (_y < RADIUS){ _y = RADIUS;} |
JamesCummins | 13:e5a36fbd48ae | 38 | if (_y > 48 - RADIUS){ _y = 47 - RADIUS;} |
JamesCummins | 23:61fa82f76808 | 39 | printf("ball speed = %d\n", _ball_speed); |
JamesCummins | 9:ce0a12fb205b | 40 | } |
JamesCummins | 9:ce0a12fb205b | 41 | |
JamesCummins | 9:ce0a12fb205b | 42 | Vector2D Ball::get_position(){ |
JamesCummins | 9:ce0a12fb205b | 43 | Vector2D pos = {_x, _y}; |
JamesCummins | 9:ce0a12fb205b | 44 | return pos; |
JamesCummins | 9:ce0a12fb205b | 45 | } |
JamesCummins | 9:ce0a12fb205b | 46 | |
JamesCummins | 9:ce0a12fb205b | 47 | Vector2D Ball::get_velocity(){ |
JamesCummins | 9:ce0a12fb205b | 48 | Vector2D vel = {_velocity.x, _velocity.y}; |
JamesCummins | 9:ce0a12fb205b | 49 | return vel; |
JamesCummins | 9:ce0a12fb205b | 50 | } |
JamesCummins | 9:ce0a12fb205b | 51 | |
JamesCummins | 10:40c77d69e83c | 52 | int Ball::get_radius(){ |
JamesCummins | 10:40c77d69e83c | 53 | int radius = _radius; |
JamesCummins | 10:40c77d69e83c | 54 | return radius; |
JamesCummins | 10:40c77d69e83c | 55 | } |
JamesCummins | 10:40c77d69e83c | 56 | |
JamesCummins | 29:42651f87522b | 57 | int Ball::get_ball_speed(){ |
JamesCummins | 29:42651f87522b | 58 | int ball_speed = _ball_speed; |
JamesCummins | 29:42651f87522b | 59 | return ball_speed; |
JamesCummins | 29:42651f87522b | 60 | } |
JamesCummins | 29:42651f87522b | 61 | |
JamesCummins | 23:61fa82f76808 | 62 | void Ball::set_ball_speed(int ball_speed){ |
JamesCummins | 23:61fa82f76808 | 63 | _ball_speed = ball_speed; |
JamesCummins | 23:61fa82f76808 | 64 | } |
JamesCummins | 23:61fa82f76808 | 65 | |
JamesCummins | 9:ce0a12fb205b | 66 | void Ball::set_velocity(Vector2D vel){ |
JamesCummins | 9:ce0a12fb205b | 67 | _velocity.x = vel.x; |
JamesCummins | 9:ce0a12fb205b | 68 | _velocity.y = vel.y; |
JamesCummins | 9:ce0a12fb205b | 69 | } |
JamesCummins | 9:ce0a12fb205b | 70 | |
JamesCummins | 9:ce0a12fb205b | 71 | void Ball::set_position(Vector2D pos){ |
JamesCummins | 9:ce0a12fb205b | 72 | _x = pos.x; |
JamesCummins | 9:ce0a12fb205b | 73 | _y = pos.y; |
JamesCummins | 10:40c77d69e83c | 74 | } |
JamesCummins | 10:40c77d69e83c | 75 | |
JamesCummins | 10:40c77d69e83c | 76 | void Ball::set_radius(int radius){ |
JamesCummins | 10:40c77d69e83c | 77 | _radius = radius; |
JamesCummins | 9:ce0a12fb205b | 78 | } |