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.
Dependencies: mbed FATFileSystem
Ball/Ball.cpp
- Committer:
- ellisbhastroud
- Date:
- 2019-04-17
- Revision:
- 5:0b31909caf7f
- Parent:
- 4:035448357749
- Child:
- 8:d410856c6d04
File content as of revision 5:0b31909caf7f:
#include "Ball.h"
//constructor
Ball::Ball()
{
}
//deconstructor
Ball::~Ball()
{
}
//public methods
void Ball::init(float x, float y) //ball starts stationary in x and y positions given
{
_x_pos = x;
_y_pos = y;
_x_vel = 0.0f;
_y_vel = 0.0f;
_shot_count = 0;
}
void Ball::drawBall(N5110 &lcd)
{
lcd.drawRect(_x_pos,_y_pos,2,2,FILL_BLACK); //draws ball
}
void Ball::printShotCount(N5110 &lcd)
{
char buffer[14];
sprintf(buffer,"Shot %i",_shot_count);
lcd.printString(buffer,40,0);
}
void Ball::drawPower(N5110 &lcd, Gamepad &pad)
{
_mag = pad.get_mag();
lcd.drawRect(0,0,36,6,FILL_TRANSPARENT);
lcd.drawRect(0,0,36*_mag,6,FILL_BLACK);
}
void Ball::move_ball(int frame_rate)
{
_x_pos = _x_pos + _x_vel*10.0f/frame_rate; //frame_rate used to scale ball movement so that it is the same for any fps
_y_pos = _y_pos + _y_vel*10.0f/frame_rate;
_x_vel = _x_vel*(1.0f-(0.8f/frame_rate)); //ball slows down 10% each loop (scaled by time between frames)
_y_vel = _y_vel*(1.0f-(0.8f/frame_rate));
}
Vector2D Ball::get_ball_pos()
{
Vector2D pos = {_x_pos, _y_pos};
return pos;
}
void Ball::shoot_ball(Gamepad &pad)
{
_joystick = pad.get_mapped_coord();
if(pad.check_event(Gamepad::A_PRESSED) == true && abs(_x_vel) < 0.05f && abs(_y_vel) < 0.05f){ //if ball stationary and a pressed then shoot
_x_vel = 8.0f * _joystick.x; //scale x velocity by joystick direction and magnitude
_y_vel = 8.0f * -_joystick.y; //scale y velocity by joystick direction and magnitude
_shot_count ++; //increment shot count
}
}
int Ball::get_shot_count()
{
int shot_count = _shot_count;
return shot_count;
}
void Ball::set_vel(float x_vel, float y_vel)
{
_x_vel = x_vel;
_y_vel = y_vel;
}
void Ball::read_joy(Gamepad &pad)
{
_joystick = pad.get_mapped_coord(); //x and y coordinates from joystick assigned
}
void Ball::check_wall_bounce() //check before and after move_ball called
{
if(_x_pos - 1 < 9 && _y_pos >= 26 && _y_pos <= 40 && _x_vel < 0){ // left wall (x=9 ,26<=y<=40)
_x_pos = 10;
_x_vel = -0.95f*_x_vel; //5% velocity lost on impact and bounce
}
if(_x_pos + 1 > 74 && _y_pos <= 40 && _y_pos >= 9 && _x_vel > 0){ //right wall x + 1
_x_pos = 72;
_x_vel = -0.95f*_x_vel;
}
if(_y_pos - 1 < 9 && _x_pos <= 74 && _x_pos >= 50 && _y_vel < 0){ //top wall y -1
_y_pos = 10;
_y_vel = -0.95f*_y_vel;
}
if(_y_pos + 1 > 40 && _x_pos >= 9 && _x_pos <= 74 && _y_vel > 0){ //bottom wall y + 2
_y_pos = 39;
_y_vel = -0.95f*_y_vel;
}
if(_x_pos - 1 < 50 && _y_pos >= 9 && _y_pos <= 26 && _x_vel < 0){ // left wall x -1
_x_pos = 51;
_x_vel = -0.95f*_x_vel; //5% velocity lost on impact and bounce
}
if(_y_pos - 1 < 26 && _x_pos <= 50 && _x_pos >= 9 && _y_vel < 0 ){ //top wall y -1
_y_pos = 27;
_y_vel = -0.95f*_y_vel;
}
}
//private methods