Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Barros_Assignment4
Diff: physics_ball.cpp
- Revision:
- 2:6ba843cde0bb
- Parent:
- 1:a2dda7baafaa
- Child:
- 3:d4138ad4f452
--- a/physics_ball.cpp Tue Oct 31 14:51:13 2017 +0000
+++ b/physics_ball.cpp Fri Nov 17 04:09:18 2017 +0000
@@ -2,17 +2,16 @@
//Constructor
physics_ball::physics_ball(){
- posx = 0;
- posy = 0;
- speedx = 25;
- speedy = 25;
-
- radius = 10;
- color = 1;
-
- width = 128;
- height = 128;
+ speedx = DEFAULT_SPEEDX;
+ speedy = DEFAULT_SPEEDY;
+ posx = DEFAULT_POSX;
+ posy = DEFAULT_POSY;
+ radius = DEFAULT_RADIUS;
+ width = DEFAULT_WIDTH;
+ height = DEFAULT_HEIGHT;
+ posy_f = (float) posx;
+ posx_f = (float) posy;
}
void physics_ball::set_param(int radius_in, int color_in)
@@ -27,21 +26,23 @@
height = height_in;
}
-void physics_ball::set_state(int x_in, int y_in, float speedx_in, float speedy_in)
+void physics_ball::set_state(float x_in, float y_in, float speedx_in, float speedy_in)
{
- posx = x_in;
- posy = y_in;
+ posx_f = x_in;
+ posy_f = y_in;
speedx = speedx_in;
speedy = speedy_in;
}
-void physics_ball::update( float time_step_in , MMA8452Q & accel)
+void physics_ball::update(MMA8452Q & accel)
{
- time_step = time_step_in;
-
- posx = posx - (speedx * accel.readY());
- posy = posy - (speedy * accel.readX());
+ posx_f = posx_f - (speedx * accel.readY());
+ posy_f = posy_f - (speedy * accel.readX());
+
+ posx = (int) posx_f;
+ posy = (int) posy_f;
+
//Ball Physics
if ((posx <= radius) || (posx >= 128-radius)){
@@ -53,6 +54,19 @@
if ((posy <= radius) || (posy >= 128-radius)) {
speedy = -1 * speedy;
}
- }
+
+ old_posx = posx;
+ old_posy = posy;
+
+ // Reset when sensor is flipped upside down (For Ball 1 & 2)
+ if (accel.readZ() < 0){
+
+
+ set_state(DEFAULT_POSX, DEFAULT_POSY, 5, 5);
+ }
+ }
+
+
+
\ No newline at end of file
