Fork of bouncing_ball by Stephen Licht

Committer:
rsean10
Date:
Tue Nov 07 15:24:26 2017 +0000
Revision:
3:b3d6cd568c6c
Parent:
2:ba1844b0eb9f
Child:
5:dd8df17df666

        

Who changed what in which revision?

UserRevisionLine numberNew 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:
slicht 2:ba1844b0eb9f 65 if (( posx <= radius) || ((posx + radius) >= _width)) {
slicht 2:ba1844b0eb9f 66 speedx = -speedx;
slicht 2:ba1844b0eb9f 67 }
slicht 2:ba1844b0eb9f 68 if (( posy <= radius) || ((posy + radius) >= _height)) {
slicht 2:ba1844b0eb9f 69 speedy = -speedy;
slicht 2:ba1844b0eb9f 70 }
slicht 1:0aa572c0f2b1 71
slicht 2:ba1844b0eb9f 72 //Accelerate per actual real world physics:
slicht 2:ba1844b0eb9f 73 speedx -= GRAVITY * accelerometer.readY(); //* time_step;
slicht 2:ba1844b0eb9f 74 speedy -= GRAVITY * accelerometer.readX(); //* time_step;
slicht 2:ba1844b0eb9f 75
slicht 2:ba1844b0eb9f 76 //Position update depends on speed:
slicht 2:ba1844b0eb9f 77 _posx_f = _posx_f + speedx * time_step;
slicht 2:ba1844b0eb9f 78 _posy_f = _posy_f + speedy * time_step;
slicht 2:ba1844b0eb9f 79
slicht 2:ba1844b0eb9f 80 //Actual position has to be able to update in less than integer increments.
slicht 2:ba1844b0eb9f 81 //Report out position is expected in integers, however.
slicht 2:ba1844b0eb9f 82 posx = (int) _posx_f;
slicht 2:ba1844b0eb9f 83 posy = (int) _posy_f;
slicht 1:0aa572c0f2b1 84 }
slicht 2:ba1844b0eb9f 85 }
slicht 2:ba1844b0eb9f 86
slicht 2:ba1844b0eb9f 87 void physics_ball::_reset_upside_down()
slicht 2:ba1844b0eb9f 88 {
slicht 2:ba1844b0eb9f 89 //Increment color when the accelerometer turned upside down.
slicht 2:ba1844b0eb9f 90 //It's up to the main code to interpret the color.
slicht 2:ba1844b0eb9f 91 color += 1;
rsean10 3:b3d6cd568c6c 92 if (color > 2) {
slicht 2:ba1844b0eb9f 93 color = 0;
slicht 1:0aa572c0f2b1 94 }
slicht 1:0aa572c0f2b1 95 }