Class containing methods to draw a ball within the maze game with the specified position, size and fill style parameters.
Ball.cpp
- Committer:
- el15mh
- Date:
- 2017-05-03
- Revision:
- 2:bcb96ab2848b
- Parent:
- 1:ba8bb10ebd5a
- Child:
- 3:569a3f2786ec
File content as of revision 2:bcb96ab2848b:
/* @file Ball.cpp (c) Max Houghton 02.14.17 Roller Maze Project, ELEC2645, Univeristy of Leeds */ #include "Ball.h" Ball::Ball() { } Ball::~Ball() { } 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; _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(Vector2D position) { velocity = position; checkForInterference(velocity); // new coordinates for the centre of the ball _x += velocity.x; _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::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() { 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; }