Ball physics library for OCE360 HW#4
bouncing_ball.cpp@1:0aa572c0f2b1, 2017-10-24 (annotated)
- Committer:
- slicht
- Date:
- Tue Oct 24 12:33:39 2017 +0000
- Revision:
- 1:0aa572c0f2b1
- Parent:
- 0:aff81f4c72f3
- Child:
- 2:ba1844b0eb9f
Compiles and runs with physics moved entirely to bouncing_ball object. Creates large, unmoving ball in center.
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 | 1:0aa572c0f2b1 | 4 | physics_ball::physics_ball(int color_in, int radius_in) |
slicht | 1:0aa572c0f2b1 | 5 | { |
slicht | 1:0aa572c0f2b1 | 6 | speedx = DEFAULT_SPEEDX; |
slicht | 1:0aa572c0f2b1 | 7 | speedy = DEFAULT_SPEEDY; |
slicht | 1:0aa572c0f2b1 | 8 | posx = DEFAULT_POSX; |
slicht | 1:0aa572c0f2b1 | 9 | posy = DEFAULT_POSY; |
slicht | 1:0aa572c0f2b1 | 10 | color = color_in; |
slicht | 1:0aa572c0f2b1 | 11 | radius = radius_in; |
slicht | 1:0aa572c0f2b1 | 12 | } |
slicht | 1:0aa572c0f2b1 | 13 | |
slicht | 1:0aa572c0f2b1 | 14 | physics_ball::~physics_ball() |
slicht | 1:0aa572c0f2b1 | 15 | { |
slicht | 1:0aa572c0f2b1 | 16 | } |
slicht | 1:0aa572c0f2b1 | 17 | |
slicht | 1:0aa572c0f2b1 | 18 | void physics_ball::define_space(int width, int height) |
slicht | 1:0aa572c0f2b1 | 19 | { |
slicht | 1:0aa572c0f2b1 | 20 | space_width = width; |
slicht | 1:0aa572c0f2b1 | 21 | space_height = height; |
slicht | 1:0aa572c0f2b1 | 22 | } |
slicht | 1:0aa572c0f2b1 | 23 | |
slicht | 1:0aa572c0f2b1 | 24 | void physics_ball::set_param(int radius_in, int color_in) |
slicht | 1:0aa572c0f2b1 | 25 | { |
slicht | 1:0aa572c0f2b1 | 26 | radius = radius_in; |
slicht | 1:0aa572c0f2b1 | 27 | color = color_in; |
slicht | 1:0aa572c0f2b1 | 28 | } |
slicht | 1:0aa572c0f2b1 | 29 | |
slicht | 1:0aa572c0f2b1 | 30 | void physics_ball::set_state(int x, int y, float vx, float vy) |
slicht | 1:0aa572c0f2b1 | 31 | { |
slicht | 1:0aa572c0f2b1 | 32 | posx = x; |
slicht | 1:0aa572c0f2b1 | 33 | posy = y; |
slicht | 1:0aa572c0f2b1 | 34 | speedx = vx; |
slicht | 1:0aa572c0f2b1 | 35 | speedy = vy; |
slicht | 1:0aa572c0f2b1 | 36 | } |
slicht | 1:0aa572c0f2b1 | 37 | |
slicht | 1:0aa572c0f2b1 | 38 | void physics_ball::update(float time_step, MMA8452Q accelerometer) |
slicht | 1:0aa572c0f2b1 | 39 | { |
slicht | 1:0aa572c0f2b1 | 40 | |
slicht | 1:0aa572c0f2b1 | 41 | // Move circle. IMPORTANT! Notice how we adjust for sensor orientation! |
slicht | 1:0aa572c0f2b1 | 42 | posx -= (speedx * accelerometer.readY()); |
slicht | 1:0aa572c0f2b1 | 43 | posy -= (speedy * accelerometer.readX()); |
slicht | 1:0aa572c0f2b1 | 44 | |
slicht | 1:0aa572c0f2b1 | 45 | // Make circle sit on edges |
slicht | 1:0aa572c0f2b1 | 46 | if ( posx <= radius + 1 ) { |
slicht | 1:0aa572c0f2b1 | 47 | posx = radius + 1; |
slicht | 1:0aa572c0f2b1 | 48 | } else if ( posx >= space_width - radius ) { |
slicht | 1:0aa572c0f2b1 | 49 | posx = space_width - radius; |
slicht | 1:0aa572c0f2b1 | 50 | } |
slicht | 1:0aa572c0f2b1 | 51 | if ( posy <= radius + 1 ) { |
slicht | 1:0aa572c0f2b1 | 52 | posy = radius + 1; |
slicht | 1:0aa572c0f2b1 | 53 | } else if ( posy >= space_height - radius ) { |
slicht | 1:0aa572c0f2b1 | 54 | posy = space_height - radius; |
slicht | 1:0aa572c0f2b1 | 55 | } |
slicht | 1:0aa572c0f2b1 | 56 | } |