bouncing_ball backend

Fork of bouncing_ball by Stephen Licht

bouncing_ball.cpp

Committer:
slicht
Date:
2017-10-24
Revision:
1:0aa572c0f2b1
Parent:
0:aff81f4c72f3
Child:
2:ba1844b0eb9f

File content as of revision 1:0aa572c0f2b1:

#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;
    }
}