bouncing_ball backend

Fork of bouncing_ball by Stephen Licht

Committer:
Eric_Rupert
Date:
Thu Nov 09 14:37:44 2017 +0000
Revision:
6:68a764001a73
Parent:
4:73a00b0667fc
Added header comment.  Includes bug fixes (moves ball off wall after bounce and reset_upside_down is public)

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.
Eric_Rupert 4:73a00b0667fc 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;
Eric_Rupert 4:73a00b0667fc 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;
Eric_Rupert 3:30b5a3adc15f 67 if ( posx <= radius){
Eric_Rupert 3:30b5a3adc15f 68 _posx_f+=radius;
Eric_Rupert 3:30b5a3adc15f 69 } else {
Eric_Rupert 3:30b5a3adc15f 70 _posx_f-=radius;
Eric_Rupert 3:30b5a3adc15f 71 }
slicht 2:ba1844b0eb9f 72 }
slicht 2:ba1844b0eb9f 73 if (( posy <= radius) || ((posy + radius) >= _height)) {
slicht 2:ba1844b0eb9f 74 speedy = -speedy;
Eric_Rupert 3:30b5a3adc15f 75 if ( posy <= radius){
Eric_Rupert 3:30b5a3adc15f 76 _posy_f+=radius;
Eric_Rupert 3:30b5a3adc15f 77 } else {
Eric_Rupert 3:30b5a3adc15f 78 _posy_f-=radius;
Eric_Rupert 3:30b5a3adc15f 79 }
slicht 2:ba1844b0eb9f 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;
Eric_Rupert 4:73a00b0667fc 94 //}
slicht 2:ba1844b0eb9f 95 }
slicht 2:ba1844b0eb9f 96
Eric_Rupert 4:73a00b0667fc 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;
slicht 2:ba1844b0eb9f 102 if (color > 3) {
slicht 2:ba1844b0eb9f 103 color = 0;
slicht 1:0aa572c0f2b1 104 }
slicht 1:0aa572c0f2b1 105 }