Class containing methods to draw a ball within the maze game with the specified position, size and fill style parameters.
Diff: Ball.cpp
- Revision:
- 2:bcb96ab2848b
- Parent:
- 1:ba8bb10ebd5a
- Child:
- 3:569a3f2786ec
--- a/Ball.cpp Fri Apr 07 10:29:08 2017 +0000 +++ b/Ball.cpp Wed May 03 21:13:45 2017 +0000 @@ -1,10 +1,11 @@ -// -// ball.cpp -// -// -// Created by Max Houghton on 19/03/2017. -// -// +/* + +@file Ball.cpp + +(c) Max Houghton 02.14.17 +Roller Maze Project, ELEC2645, Univeristy of Leeds + +*/ #include "Ball.h" @@ -18,29 +19,90 @@ } -void Ball::init(int x, int y, int radius) +void Ball::init(float x, // x coordinate for centre + float y, // y coordinate for centre + int radius, // radius size + bool colour) // type of ball { // properties of ball are set with values passed down from engine _x = x; _y = y; _radius = radius; - - velocity.x = SPEED; // arbitrary speed of moving 1 pixel per update - velocity.y = SPEED; + _colour = colour; // printf("INPUT: x = %i, y = %i, radius = %i \n", x, y, radius); // printf("DRAW FUNCTION: x = %f, y = %f, radius = %f \n", _x, _y, _radius); } -void Ball::update() +void Ball::update(Vector2D position) { + velocity = position; + + checkForInterference(velocity); + // new coordinates for the centre of the ball _x += velocity.x; - _y += velocity.y; + _y -= velocity.y; // +ve y joystick motion => -ve ball motion +} + +// goes into main 'draw' function in engine +// main draw function in engine then called by menu during 'playGame' function +void Ball::draw(N5110 &lcd) +{ + if (_colour){// true colour => outline + lcd.drawCircle(_x, _y, _radius, FILL_TRANSPARENT); + } + else { // false colour => solid + lcd.drawCircle(_x, _y, _radius, FILL_BLACK); + } } -void Ball::draw(N5110 &lcd) +void Ball::setVelocity(Vector2D v) +{ + velocity.x = v.x; + velocity.y = v.y; +} + +void Ball::setPosition(Vector2D p) +{ + _x = p.x; + _y = p.y; +} + +Vector2D Ball::getVelocity() +{ + Vector2D _velocity = {velocity.x, velocity.y}; + + return _velocity; +} + +Vector2D Ball::getPosition() { - lcd.drawCircle(_x, _y, _radius, FILL_BLACK); -} \ No newline at end of file + Vector2D p = {_x,_y}; + + return p; +} + +Vector2D Ball::checkForInterference(Vector2D velocity) +{ + // +ve x + if ((velocity.x < 0.1f) && (velocity.x > 0.0f)){ + velocity.x = 0.0f; + } + // -ve x + if ((velocity.x > -0.1f) && (velocity.x < 0.0f)){ + velocity.x = 0.0f; + } + + // +ve y + if ((velocity.y < 0.1f) && (velocity.y > 0.0f)){ + velocity.y = 0.0f; + } + // -ve y + if ((velocity.y > -0.1f) && (velocity.y < 0.0f)){ + velocity.y = 0.0f; + } + + return velocity; +}