Ball physics library for OCE360 HW#4
Diff: bouncing_ball.cpp
- Revision:
- 1:0aa572c0f2b1
- Parent:
- 0:aff81f4c72f3
- Child:
- 2:ba1844b0eb9f
diff -r aff81f4c72f3 -r 0aa572c0f2b1 bouncing_ball.cpp --- a/bouncing_ball.cpp Thu Oct 19 14:07:25 2017 +0000 +++ b/bouncing_ball.cpp Tue Oct 24 12:33:39 2017 +0000 @@ -0,0 +1,56 @@ +#include "mbed.h" +#include "bouncing_ball.h" + +physics_ball::physics_ball(int color_in, int radius_in) +{ + speedx = DEFAULT_SPEEDX; + speedy = DEFAULT_SPEEDY; + posx = DEFAULT_POSX; + posy = DEFAULT_POSY; + color = color_in; + radius = radius_in; +} + +physics_ball::~physics_ball() +{ +} + +void physics_ball::define_space(int width, int height) +{ + space_width = width; + space_height = height; +} + +void physics_ball::set_param(int radius_in, int color_in) +{ + radius = radius_in; + color = color_in; +} + +void physics_ball::set_state(int x, int y, float vx, float vy) +{ + posx = x; + posy = y; + speedx = vx; + speedy = vy; +} + +void physics_ball::update(float time_step, MMA8452Q accelerometer) +{ + + // Move circle. IMPORTANT! Notice how we adjust for sensor orientation! + posx -= (speedx * accelerometer.readY()); + posy -= (speedy * accelerometer.readX()); + + // Make circle sit on edges + if ( posx <= radius + 1 ) { + posx = radius + 1; + } else if ( posx >= space_width - radius ) { + posx = space_width - radius; + } + if ( posy <= radius + 1 ) { + posy = radius + 1; + } else if ( posy >= space_height - radius ) { + posy = space_height - radius; + } +} \ No newline at end of file