el15mh 200929957

Dependencies:   mbed

Committer:
el15mh
Date:
Thu May 04 17:39:23 2017 +0000
Revision:
10:989e5dbd12ee
Parent:
9:960dfc71c224
Documented and final revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el15mh 9:960dfc71c224 1 #include "Ball.h"
el15mh 9:960dfc71c224 2
el15mh 9:960dfc71c224 3 /**
el15mh 9:960dfc71c224 4 * @details - constructor
el15mh 9:960dfc71c224 5 */
el15mh 9:960dfc71c224 6 Ball::Ball()
el15mh 9:960dfc71c224 7 {
el15mh 9:960dfc71c224 8
el15mh 9:960dfc71c224 9 }
el15mh 9:960dfc71c224 10
el15mh 9:960dfc71c224 11 /**
el15mh 9:960dfc71c224 12 * @details - destructor
el15mh 9:960dfc71c224 13 */
el15mh 9:960dfc71c224 14 Ball::~Ball()
el15mh 9:960dfc71c224 15 {
el15mh 9:960dfc71c224 16
el15mh 9:960dfc71c224 17 }
el15mh 9:960dfc71c224 18
el15mh 9:960dfc71c224 19 /**
el15mh 9:960dfc71c224 20 * @details - Initiases the ball.
el15mh 9:960dfc71c224 21 */
el15mh 9:960dfc71c224 22 void Ball::init(float x, // x coordinate for centre
el15mh 9:960dfc71c224 23 float y, // y coordinate for centre
el15mh 9:960dfc71c224 24 int radius, // radius size
el15mh 9:960dfc71c224 25 bool colour) // type of ball
el15mh 9:960dfc71c224 26 {
el15mh 9:960dfc71c224 27 // properties of ball are set with values passed down from engine
el15mh 9:960dfc71c224 28 _x = x;
el15mh 9:960dfc71c224 29 _y = y;
el15mh 9:960dfc71c224 30 _radius = radius;
el15mh 9:960dfc71c224 31 _colour = colour;
el15mh 9:960dfc71c224 32
el15mh 9:960dfc71c224 33 // printf("INPUT: x = %i, y = %i, radius = %i \n", x, y, radius);
el15mh 9:960dfc71c224 34 // printf("DRAW FUNCTION: x = %f, y = %f, radius = %f \n", _x, _y, _radius);
el15mh 9:960dfc71c224 35 }
el15mh 9:960dfc71c224 36
el15mh 9:960dfc71c224 37 /**
el15mh 9:960dfc71c224 38 * @details - Updates position of the ball
el15mh 9:960dfc71c224 39 */
el15mh 9:960dfc71c224 40 void Ball::update(Vector2D position)
el15mh 9:960dfc71c224 41 {
el15mh 9:960dfc71c224 42 velocity = position;
el15mh 9:960dfc71c224 43
el15mh 9:960dfc71c224 44 // smooths out small signal fluctuations from accelerometer
el15mh 9:960dfc71c224 45 // used by joystick output also but with neglible effect
el15mh 9:960dfc71c224 46 checkForInterference(velocity);
el15mh 9:960dfc71c224 47
el15mh 9:960dfc71c224 48 // new coordinates for the centre of the ball
el15mh 9:960dfc71c224 49 _x += velocity.x;
el15mh 9:960dfc71c224 50 _y -= velocity.y; // +ve y joystick motion => -ve ball motion
el15mh 9:960dfc71c224 51 }
el15mh 9:960dfc71c224 52
el15mh 9:960dfc71c224 53 /**
el15mh 9:960dfc71c224 54 * @details - Function draws the ball
el15mh 9:960dfc71c224 55 */
el15mh 9:960dfc71c224 56 void Ball::draw(N5110 &lcd)
el15mh 9:960dfc71c224 57 {
el15mh 9:960dfc71c224 58 // goes into main 'draw' function in engine
el15mh 9:960dfc71c224 59 // main draw function in engine then called by menu during 'playGame' function
el15mh 9:960dfc71c224 60 if (_colour){// true colour => outline
el15mh 9:960dfc71c224 61 lcd.drawCircle(_x, _y, _radius, FILL_TRANSPARENT);
el15mh 9:960dfc71c224 62 }
el15mh 9:960dfc71c224 63 else { // false colour => solid
el15mh 9:960dfc71c224 64 lcd.drawCircle(_x, _y, _radius, FILL_BLACK);
el15mh 9:960dfc71c224 65 }
el15mh 9:960dfc71c224 66
el15mh 9:960dfc71c224 67 }
el15mh 9:960dfc71c224 68
el15mh 9:960dfc71c224 69 /**
el15mh 9:960dfc71c224 70 * @details - Sets the velocity of the ball
el15mh 9:960dfc71c224 71 */
el15mh 9:960dfc71c224 72 void Ball::setVelocity(Vector2D v)
el15mh 9:960dfc71c224 73 {
el15mh 9:960dfc71c224 74 velocity.x = v.x;
el15mh 9:960dfc71c224 75 velocity.y = v.y;
el15mh 9:960dfc71c224 76 }
el15mh 9:960dfc71c224 77
el15mh 9:960dfc71c224 78 /**
el15mh 9:960dfc71c224 79 * @details - Sets the position of the ball
el15mh 9:960dfc71c224 80 */
el15mh 9:960dfc71c224 81 void Ball::setPosition(Vector2D p)
el15mh 9:960dfc71c224 82 {
el15mh 9:960dfc71c224 83 _x = p.x;
el15mh 9:960dfc71c224 84 _y = p.y;
el15mh 9:960dfc71c224 85 }
el15mh 9:960dfc71c224 86
el15mh 9:960dfc71c224 87 /**
el15mh 9:960dfc71c224 88 * @details - Returns a struct containing current ball velocity information
el15mh 9:960dfc71c224 89 */
el15mh 9:960dfc71c224 90 Vector2D Ball::getVelocity()
el15mh 9:960dfc71c224 91 {
el15mh 9:960dfc71c224 92 Vector2D _velocity = {velocity.x, velocity.y};
el15mh 9:960dfc71c224 93
el15mh 9:960dfc71c224 94 return _velocity;
el15mh 9:960dfc71c224 95 }
el15mh 9:960dfc71c224 96
el15mh 9:960dfc71c224 97 /**
el15mh 9:960dfc71c224 98 * @details - Returns a struct containing current ball position information
el15mh 9:960dfc71c224 99 */
el15mh 9:960dfc71c224 100 Vector2D Ball::getPosition()
el15mh 9:960dfc71c224 101 {
el15mh 9:960dfc71c224 102 Vector2D p = {_x,_y};
el15mh 9:960dfc71c224 103
el15mh 9:960dfc71c224 104 return p;
el15mh 9:960dfc71c224 105 }
el15mh 9:960dfc71c224 106
el15mh 9:960dfc71c224 107 /**
el15mh 9:960dfc71c224 108 * @details - Smooths out data from accelerometer and returns values in struct
el15mh 9:960dfc71c224 109 */
el15mh 9:960dfc71c224 110 Vector2D Ball::checkForInterference(Vector2D velocity)
el15mh 9:960dfc71c224 111 {
el15mh 9:960dfc71c224 112 // +ve x
el15mh 9:960dfc71c224 113 if ((velocity.x < 0.1f) && (velocity.x > 0.0f)){
el15mh 9:960dfc71c224 114 velocity.x = 0.0f;
el15mh 9:960dfc71c224 115 }
el15mh 9:960dfc71c224 116 // -ve x
el15mh 9:960dfc71c224 117 if ((velocity.x > -0.1f) && (velocity.x < 0.0f)){
el15mh 9:960dfc71c224 118 velocity.x = 0.0f;
el15mh 9:960dfc71c224 119 }
el15mh 9:960dfc71c224 120
el15mh 9:960dfc71c224 121 // +ve y
el15mh 9:960dfc71c224 122 if ((velocity.y < 0.1f) && (velocity.y > 0.0f)){
el15mh 9:960dfc71c224 123 velocity.y = 0.0f;
el15mh 9:960dfc71c224 124 }
el15mh 9:960dfc71c224 125 // -ve y
el15mh 9:960dfc71c224 126 if ((velocity.y > -0.1f) && (velocity.y < 0.0f)){
el15mh 9:960dfc71c224 127 velocity.y = 0.0f;
el15mh 9:960dfc71c224 128 }
el15mh 9:960dfc71c224 129
el15mh 9:960dfc71c224 130 return velocity;
el15mh 9:960dfc71c224 131 }