Fork of bouncing_ball by
bouncing_ball.cpp@5:dd8df17df666, 2017-11-14 (annotated)
- Committer:
- rsean10
- Date:
- Tue Nov 14 14:57:59 2017 +0000
- Revision:
- 5:dd8df17df666
- Parent:
- 3:b3d6cd568c6c
- Child:
- 6:1e5cd98fc2aa
2017/11/14 - 09:57 - Fixed edge grab and disappearing balls
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
slicht | 1:0aa572c0f2b1 | 1 | #include "mbed.h" |
slicht | 1:0aa572c0f2b1 | 2 | #include "bouncing_ball.h" |
slicht | 1:0aa572c0f2b1 | 3 | |
slicht | 2:ba1844b0eb9f | 4 | //Constructor. Default values are set in the library header for all member variables. |
slicht | 2:ba1844b0eb9f | 5 | physics_ball::physics_ball() |
slicht | 1:0aa572c0f2b1 | 6 | { |
slicht | 2:ba1844b0eb9f | 7 | // Set all values to defaults defined above (avoids magic numbers)! |
slicht | 1:0aa572c0f2b1 | 8 | speedx = DEFAULT_SPEEDX; |
slicht | 1:0aa572c0f2b1 | 9 | speedy = DEFAULT_SPEEDY; |
slicht | 1:0aa572c0f2b1 | 10 | posx = DEFAULT_POSX; |
slicht | 1:0aa572c0f2b1 | 11 | posy = DEFAULT_POSY; |
slicht | 2:ba1844b0eb9f | 12 | color = DEFAULT_COLOR; |
slicht | 2:ba1844b0eb9f | 13 | radius = DEFAULT_RADIUS; |
slicht | 2:ba1844b0eb9f | 14 | _width = DEFAULT_WIDTH; |
slicht | 2:ba1844b0eb9f | 15 | _height = DEFAULT_HEIGHT; |
slicht | 2:ba1844b0eb9f | 16 | _posy_f = (float) posx; |
slicht | 2:ba1844b0eb9f | 17 | _posx_f = (float) posy; |
slicht | 1:0aa572c0f2b1 | 18 | } |
slicht | 1:0aa572c0f2b1 | 19 | |
slicht | 2:ba1844b0eb9f | 20 | //Deconstructor exists, but does nothing. |
slicht | 1:0aa572c0f2b1 | 21 | physics_ball::~physics_ball() |
slicht | 1:0aa572c0f2b1 | 22 | { |
slicht | 1:0aa572c0f2b1 | 23 | } |
slicht | 1:0aa572c0f2b1 | 24 | |
slicht | 2:ba1844b0eb9f | 25 | //Provides the ability to reset the size of the space that the ball is bouncing in. |
slicht | 2:ba1844b0eb9f | 26 | //Width and height do noy have to be the same size as the LCD screen. |
slicht | 2:ba1844b0eb9f | 27 | void physics_ball::define_space(int desired_width, int desired_height) |
slicht | 1:0aa572c0f2b1 | 28 | { |
slicht | 2:ba1844b0eb9f | 29 | _width = desired_width; |
slicht | 2:ba1844b0eb9f | 30 | _height = desired_height; |
slicht | 1:0aa572c0f2b1 | 31 | } |
slicht | 1:0aa572c0f2b1 | 32 | |
slicht | 2:ba1844b0eb9f | 33 | //An alternative means of setting desired radius and colored. |
slicht | 2:ba1844b0eb9f | 34 | void physics_ball::set_param(int desired_radius, int desired_color) |
slicht | 1:0aa572c0f2b1 | 35 | { |
slicht | 2:ba1844b0eb9f | 36 | radius = desired_radius; |
slicht | 2:ba1844b0eb9f | 37 | color = desired_color; |
slicht | 1:0aa572c0f2b1 | 38 | } |
slicht | 1:0aa572c0f2b1 | 39 | |
slicht | 2:ba1844b0eb9f | 40 | //An alternative means of setting desired positions and velocities. |
slicht | 2:ba1844b0eb9f | 41 | void physics_ball::set_state(int desired_x, int desired_y, float desired_vx, float desired_vy) |
slicht | 1:0aa572c0f2b1 | 42 | { |
slicht | 2:ba1844b0eb9f | 43 | posx = desired_x; |
slicht | 2:ba1844b0eb9f | 44 | posy = desired_y; |
slicht | 2:ba1844b0eb9f | 45 | _posx_f = (float) posx; |
slicht | 2:ba1844b0eb9f | 46 | _posy_f = (float) posy; |
slicht | 2:ba1844b0eb9f | 47 | speedx = desired_vx; |
slicht | 2:ba1844b0eb9f | 48 | speedy = desired_vy; |
slicht | 1:0aa572c0f2b1 | 49 | } |
slicht | 1:0aa572c0f2b1 | 50 | |
slicht | 2:ba1844b0eb9f | 51 | //The heart of the matter: the calculation used to update the position of a ball |
slicht | 2:ba1844b0eb9f | 52 | //using inputs from an accelerometer. |
slicht | 2:ba1844b0eb9f | 53 | void physics_ball::update(float time_step, MMA8452Q& accelerometer) |
slicht | 1:0aa572c0f2b1 | 54 | { |
slicht | 2:ba1844b0eb9f | 55 | //If upside down, hold ball still. |
slicht | 2:ba1844b0eb9f | 56 | if (accelerometer.readZ() < 0) { |
slicht | 2:ba1844b0eb9f | 57 | if (_upside_down_status == 0) { |
slicht | 2:ba1844b0eb9f | 58 | _reset_upside_down(); |
slicht | 2:ba1844b0eb9f | 59 | _upside_down_status = 1; |
slicht | 2:ba1844b0eb9f | 60 | } |
slicht | 2:ba1844b0eb9f | 61 | } else { |
slicht | 2:ba1844b0eb9f | 62 | _upside_down_status = 0; |
slicht | 1:0aa572c0f2b1 | 63 | |
slicht | 2:ba1844b0eb9f | 64 | // Make circle bounce off of edges if at the edges: |
rsean10 | 5:dd8df17df666 | 65 | if (posx <= radius) { |
rsean10 | 5:dd8df17df666 | 66 | speedx = -speedx; |
rsean10 | 5:dd8df17df666 | 67 | posx = radius + BUFFER; |
rsean10 | 5:dd8df17df666 | 68 | } |
rsean10 | 5:dd8df17df666 | 69 | if ((posx + radius) >= _width) { |
slicht | 2:ba1844b0eb9f | 70 | speedx = -speedx; |
rsean10 | 5:dd8df17df666 | 71 | posx = posx + radius - BUFFER; |
rsean10 | 5:dd8df17df666 | 72 | } |
rsean10 | 5:dd8df17df666 | 73 | if (posy <= radius) { |
slicht | 2:ba1844b0eb9f | 74 | speedy = -speedy; |
rsean10 | 5:dd8df17df666 | 75 | posy = radius + BUFFER; |
rsean10 | 5:dd8df17df666 | 76 | } |
rsean10 | 5:dd8df17df666 | 77 | if ((posy + radius) >= _height){ |
rsean10 | 5:dd8df17df666 | 78 | speedy = -speedy; |
rsean10 | 5:dd8df17df666 | 79 | posy = posy + radius - BUFFER; |
rsean10 | 5:dd8df17df666 | 80 | } |
slicht | 1:0aa572c0f2b1 | 81 | |
slicht | 2:ba1844b0eb9f | 82 | //Accelerate per actual real world physics: |
slicht | 2:ba1844b0eb9f | 83 | speedx -= GRAVITY * accelerometer.readY(); //* time_step; |
slicht | 2:ba1844b0eb9f | 84 | speedy -= GRAVITY * accelerometer.readX(); //* time_step; |
slicht | 2:ba1844b0eb9f | 85 | |
slicht | 2:ba1844b0eb9f | 86 | //Position update depends on speed: |
slicht | 2:ba1844b0eb9f | 87 | _posx_f = _posx_f + speedx * time_step; |
slicht | 2:ba1844b0eb9f | 88 | _posy_f = _posy_f + speedy * time_step; |
slicht | 2:ba1844b0eb9f | 89 | |
slicht | 2:ba1844b0eb9f | 90 | //Actual position has to be able to update in less than integer increments. |
slicht | 2:ba1844b0eb9f | 91 | //Report out position is expected in integers, however. |
slicht | 2:ba1844b0eb9f | 92 | posx = (int) _posx_f; |
slicht | 2:ba1844b0eb9f | 93 | posy = (int) _posy_f; |
slicht | 1:0aa572c0f2b1 | 94 | } |
slicht | 2:ba1844b0eb9f | 95 | } |
slicht | 2:ba1844b0eb9f | 96 | |
slicht | 2:ba1844b0eb9f | 97 | void physics_ball::_reset_upside_down() |
slicht | 2:ba1844b0eb9f | 98 | { |
slicht | 2:ba1844b0eb9f | 99 | //Increment color when the accelerometer turned upside down. |
slicht | 2:ba1844b0eb9f | 100 | //It's up to the main code to interpret the color. |
slicht | 2:ba1844b0eb9f | 101 | color += 1; |
rsean10 | 3:b3d6cd568c6c | 102 | if (color > 2) { |
slicht | 2:ba1844b0eb9f | 103 | color = 0; |
slicht | 1:0aa572c0f2b1 | 104 | } |
slicht | 1:0aa572c0f2b1 | 105 | } |