James Cummins / Mbed 2 deprecated el17jnc

Dependencies:   mbed

Ball/Ball.cpp

Committer:
JamesCummins
Date:
2019-04-27
Revision:
29:42651f87522b
Parent:
23:61fa82f76808
Child:
38:a85bc227b907

File content as of revision 29:42651f87522b:

#include "Ball.h"

Ball::Ball()
{}

Ball::~Ball()
{}

void Ball::init(int radius){
    _radius = radius;
    _x = 42;
    _y = 24;
    _velocity.x = 0;
    _velocity.y = 0;
    _ball_speed = 5;
}

void Ball::draw(N5110 &lcd){
    int radius = get_radius();
    lcd.drawCircle(_x, _y, radius, FILL_BLACK);
}

void Ball::read_input(FXOS8700CQ &accelerometer){
    Data values = accelerometer.get_values();
    _velocity.x = -(5+_ball_speed)*values.ay;        //axes of the accelerometer are different to orientation of the screen
    _velocity.y = -(5+_ball_speed)*values.ax;        //negative to account for reversed direction
    /*printf("ax = %f | ay = %f     |        ", values.ax, values.ay);
    printf("vel.x = %f | vel.y = %f\n", _velocity.x, _velocity.y);*/
}

void Ball::update(){
    int RADIUS = get_radius();
    _x += _velocity.x;
    _y += _velocity.y;
    if (_x < RADIUS){ _x = RADIUS;}     //check wall collisions
    if (_x > 84 - RADIUS){ _x = 83 - RADIUS;}
    if (_y < RADIUS){ _y = RADIUS;}
    if (_y > 48 - RADIUS){ _y = 47 - RADIUS;}
    printf("ball speed = %d\n", _ball_speed);
}

Vector2D Ball::get_position(){
    Vector2D pos = {_x, _y};
    return pos;
}

Vector2D Ball::get_velocity(){
    Vector2D vel = {_velocity.x, _velocity.y};
    return vel;
}

int Ball::get_radius(){
    int radius = _radius;
    return radius;
}

int Ball::get_ball_speed(){
    int ball_speed = _ball_speed;
    return ball_speed;
}

void Ball::set_ball_speed(int ball_speed){
    _ball_speed = ball_speed;
}

void Ball::set_velocity(Vector2D vel){
    _velocity.x = vel.x;
    _velocity.y = vel.y;
}

void Ball::set_position(Vector2D pos){
    _x = pos.x;
    _y = pos.y;
}

void Ball::set_radius(int radius){
    _radius = radius;
}