![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Two player imu pong
Dependencies: 4DGL-uLCD-SE IMUfilter LSM9DS0 PinDetect mbed
ball.cpp@0:941225f01ccc, 2015-10-22 (annotated)
- Committer:
- rrr93
- Date:
- Thu Oct 22 16:50:22 2015 +0000
- Revision:
- 0:941225f01ccc
qewrt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rrr93 | 0:941225f01ccc | 1 | |
rrr93 | 0:941225f01ccc | 2 | #include "ball.h" |
rrr93 | 0:941225f01ccc | 3 | //#include "tempModule.h" |
rrr93 | 0:941225f01ccc | 4 | |
rrr93 | 0:941225f01ccc | 5 | |
rrr93 | 0:941225f01ccc | 6 | |
rrr93 | 0:941225f01ccc | 7 | Ball::Ball(PinName pin) |
rrr93 | 0:941225f01ccc | 8 | { |
rrr93 | 0:941225f01ccc | 9 | //tempSensor = new TempModule(pin); |
rrr93 | 0:941225f01ccc | 10 | setBaseVx(2.0); |
rrr93 | 0:941225f01ccc | 11 | setBaseVy(2.0); |
rrr93 | 0:941225f01ccc | 12 | setVySign(1); |
rrr93 | 0:941225f01ccc | 13 | setVxSign(1); |
rrr93 | 0:941225f01ccc | 14 | radius = 5; |
rrr93 | 0:941225f01ccc | 15 | setX(50); |
rrr93 | 0:941225f01ccc | 16 | setY(21); |
rrr93 | 0:941225f01ccc | 17 | } |
rrr93 | 0:941225f01ccc | 18 | |
rrr93 | 0:941225f01ccc | 19 | Ball::Ball(PinName pin, float Vx, float Vy, int rad,int xp, int yp) |
rrr93 | 0:941225f01ccc | 20 | { |
rrr93 | 0:941225f01ccc | 21 | //tempSensor = new TempModule(pin , Vx, Vy); |
rrr93 | 0:941225f01ccc | 22 | setBaseVx(Vx); |
rrr93 | 0:941225f01ccc | 23 | setBaseVy(Vy); |
rrr93 | 0:941225f01ccc | 24 | setVxSign(1); |
rrr93 | 0:941225f01ccc | 25 | setVySign(1); |
rrr93 | 0:941225f01ccc | 26 | radius = rad; |
rrr93 | 0:941225f01ccc | 27 | setX(xp); |
rrr93 | 0:941225f01ccc | 28 | setY(yp); |
rrr93 | 0:941225f01ccc | 29 | |
rrr93 | 0:941225f01ccc | 30 | |
rrr93 | 0:941225f01ccc | 31 | } |
rrr93 | 0:941225f01ccc | 32 | |
rrr93 | 0:941225f01ccc | 33 | // set functions |
rrr93 | 0:941225f01ccc | 34 | void Ball::setX(int xp) |
rrr93 | 0:941225f01ccc | 35 | { |
rrr93 | 0:941225f01ccc | 36 | x = xp; |
rrr93 | 0:941225f01ccc | 37 | } |
rrr93 | 0:941225f01ccc | 38 | void Ball::setY(int yp) |
rrr93 | 0:941225f01ccc | 39 | { |
rrr93 | 0:941225f01ccc | 40 | y = yp; |
rrr93 | 0:941225f01ccc | 41 | |
rrr93 | 0:941225f01ccc | 42 | } |
rrr93 | 0:941225f01ccc | 43 | void Ball::setRand(int seed) |
rrr93 | 0:941225f01ccc | 44 | { |
rrr93 | 0:941225f01ccc | 45 | srand(seed); |
rrr93 | 0:941225f01ccc | 46 | int random = (rand() % (118 - 2*radius)) + radius; |
rrr93 | 0:941225f01ccc | 47 | setX(random); |
rrr93 | 0:941225f01ccc | 48 | int random2 = (rand() % (127 - 2*radius)) + radius; |
rrr93 | 0:941225f01ccc | 49 | setY(random2); |
rrr93 | 0:941225f01ccc | 50 | |
rrr93 | 0:941225f01ccc | 51 | } |
rrr93 | 0:941225f01ccc | 52 | void Ball::setBaseVx(float Vx) |
rrr93 | 0:941225f01ccc | 53 | { |
rrr93 | 0:941225f01ccc | 54 | basevx = Vx; |
rrr93 | 0:941225f01ccc | 55 | //tempSensor->setBaseVx(vxSign*Vx); |
rrr93 | 0:941225f01ccc | 56 | |
rrr93 | 0:941225f01ccc | 57 | } |
rrr93 | 0:941225f01ccc | 58 | |
rrr93 | 0:941225f01ccc | 59 | |
rrr93 | 0:941225f01ccc | 60 | void Ball::setBaseVy(float Vy) |
rrr93 | 0:941225f01ccc | 61 | { |
rrr93 | 0:941225f01ccc | 62 | basevy = Vy; |
rrr93 | 0:941225f01ccc | 63 | //tempSensor->setBaseVy(Vy); |
rrr93 | 0:941225f01ccc | 64 | |
rrr93 | 0:941225f01ccc | 65 | } |
rrr93 | 0:941225f01ccc | 66 | |
rrr93 | 0:941225f01ccc | 67 | void Ball::setVxSign(int s) |
rrr93 | 0:941225f01ccc | 68 | { |
rrr93 | 0:941225f01ccc | 69 | vxSign = s; |
rrr93 | 0:941225f01ccc | 70 | } |
rrr93 | 0:941225f01ccc | 71 | |
rrr93 | 0:941225f01ccc | 72 | void Ball::setVySign(int s) |
rrr93 | 0:941225f01ccc | 73 | { |
rrr93 | 0:941225f01ccc | 74 | vySign = s; |
rrr93 | 0:941225f01ccc | 75 | |
rrr93 | 0:941225f01ccc | 76 | } |
rrr93 | 0:941225f01ccc | 77 | |
rrr93 | 0:941225f01ccc | 78 | |
rrr93 | 0:941225f01ccc | 79 | //get functions |
rrr93 | 0:941225f01ccc | 80 | |
rrr93 | 0:941225f01ccc | 81 | int Ball::getVx() |
rrr93 | 0:941225f01ccc | 82 | { |
rrr93 | 0:941225f01ccc | 83 | return basevx; |
rrr93 | 0:941225f01ccc | 84 | } |
rrr93 | 0:941225f01ccc | 85 | |
rrr93 | 0:941225f01ccc | 86 | int Ball::getVy() |
rrr93 | 0:941225f01ccc | 87 | { |
rrr93 | 0:941225f01ccc | 88 | return basevy; |
rrr93 | 0:941225f01ccc | 89 | } |
rrr93 | 0:941225f01ccc | 90 | |
rrr93 | 0:941225f01ccc | 91 | int Ball::getFutureX() |
rrr93 | 0:941225f01ccc | 92 | { |
rrr93 | 0:941225f01ccc | 93 | return vxSign * getVx() + x ; |
rrr93 | 0:941225f01ccc | 94 | //return vxSign * tempSensor->getVx() + x ; |
rrr93 | 0:941225f01ccc | 95 | |
rrr93 | 0:941225f01ccc | 96 | } |
rrr93 | 0:941225f01ccc | 97 | |
rrr93 | 0:941225f01ccc | 98 | int Ball::getFutureY() |
rrr93 | 0:941225f01ccc | 99 | { |
rrr93 | 0:941225f01ccc | 100 | return vySign * getVy() + y; |
rrr93 | 0:941225f01ccc | 101 | //return vySign * tempSensor->getVy() + y; |
rrr93 | 0:941225f01ccc | 102 | |
rrr93 | 0:941225f01ccc | 103 | } |
rrr93 | 0:941225f01ccc | 104 | |
rrr93 | 0:941225f01ccc | 105 | int Ball::getRadius() |
rrr93 | 0:941225f01ccc | 106 | { |
rrr93 | 0:941225f01ccc | 107 | return radius; |
rrr93 | 0:941225f01ccc | 108 | } |
rrr93 | 0:941225f01ccc | 109 | //Member Functions |
rrr93 | 0:941225f01ccc | 110 | |
rrr93 | 0:941225f01ccc | 111 | void Ball::reverseXDirection() |
rrr93 | 0:941225f01ccc | 112 | { |
rrr93 | 0:941225f01ccc | 113 | |
rrr93 | 0:941225f01ccc | 114 | vxSign = - vxSign; |
rrr93 | 0:941225f01ccc | 115 | |
rrr93 | 0:941225f01ccc | 116 | } |
rrr93 | 0:941225f01ccc | 117 | void Ball::reverseYDirection() |
rrr93 | 0:941225f01ccc | 118 | { |
rrr93 | 0:941225f01ccc | 119 | vySign = -vySign; |
rrr93 | 0:941225f01ccc | 120 | |
rrr93 | 0:941225f01ccc | 121 | } |
rrr93 | 0:941225f01ccc | 122 | /* |
rrr93 | 0:941225f01ccc | 123 | void Ball::reset( int x, int y, int vx, int vy, uLCD_4DGL*) |
rrr93 | 0:941225f01ccc | 124 | { |
rrr93 | 0:941225f01ccc | 125 | |
rrr93 | 0:941225f01ccc | 126 | setBaseVx(vx); |
rrr93 | 0:941225f01ccc | 127 | setBaseVy(vy); |
rrr93 | 0:941225f01ccc | 128 | uLCD.circle(x,y,radius,WHITE); |
rrr93 | 0:941225f01ccc | 129 | |
rrr93 | 0:941225f01ccc | 130 | |
rrr93 | 0:941225f01ccc | 131 | } |
rrr93 | 0:941225f01ccc | 132 | */ |
rrr93 | 0:941225f01ccc | 133 | void Ball::update(uLCD_4DGL *uLCD) |
rrr93 | 0:941225f01ccc | 134 | { |
rrr93 | 0:941225f01ccc | 135 | uLCD->circle(x,y,radius,BLACK); |
rrr93 | 0:941225f01ccc | 136 | x = (int) getFutureX(); |
rrr93 | 0:941225f01ccc | 137 | y = (int) getFutureY(); |
rrr93 | 0:941225f01ccc | 138 | uLCD->circle(x,y,radius,WHITE); |
rrr93 | 0:941225f01ccc | 139 | |
rrr93 | 0:941225f01ccc | 140 | |
rrr93 | 0:941225f01ccc | 141 | } |
rrr93 | 0:941225f01ccc | 142 | /* |
rrr93 | 0:941225f01ccc | 143 | void Ball::dispTemp(uLCD_4DGL *uLCD) |
rrr93 | 0:941225f01ccc | 144 | { |
rrr93 | 0:941225f01ccc | 145 | |
rrr93 | 0:941225f01ccc | 146 | float garbage = tempSensor->getVx(uLCD); |
rrr93 | 0:941225f01ccc | 147 | |
rrr93 | 0:941225f01ccc | 148 | } |
rrr93 | 0:941225f01ccc | 149 | */ |
rrr93 | 0:941225f01ccc | 150 | bool Ball::CheckHit() |
rrr93 | 0:941225f01ccc | 151 | { bool hit = false; |
rrr93 | 0:941225f01ccc | 152 | /* |
rrr93 | 0:941225f01ccc | 153 | if ((getFutureX()<=radius+1)) |
rrr93 | 0:941225f01ccc | 154 | { |
rrr93 | 0:941225f01ccc | 155 | reverseXDirection(); |
rrr93 | 0:941225f01ccc | 156 | hit = true; |
rrr93 | 0:941225f01ccc | 157 | } |
rrr93 | 0:941225f01ccc | 158 | */ |
rrr93 | 0:941225f01ccc | 159 | if ((getFutureY()<=radius+1) || (getFutureY()>=126-radius)) |
rrr93 | 0:941225f01ccc | 160 | { |
rrr93 | 0:941225f01ccc | 161 | reverseYDirection(); |
rrr93 | 0:941225f01ccc | 162 | hit = true; |
rrr93 | 0:941225f01ccc | 163 | } |
rrr93 | 0:941225f01ccc | 164 | return hit; |
rrr93 | 0:941225f01ccc | 165 | |
rrr93 | 0:941225f01ccc | 166 | } |
rrr93 | 0:941225f01ccc | 167 | bool Ball::CheckLose() |
rrr93 | 0:941225f01ccc | 168 | { |
rrr93 | 0:941225f01ccc | 169 | if ((getFutureX()>=126-radius)) |
rrr93 | 0:941225f01ccc | 170 | { |
rrr93 | 0:941225f01ccc | 171 | //vx = 0; |
rrr93 | 0:941225f01ccc | 172 | //vy = 0; |
rrr93 | 0:941225f01ccc | 173 | return true; |
rrr93 | 0:941225f01ccc | 174 | } |
rrr93 | 0:941225f01ccc | 175 | |
rrr93 | 0:941225f01ccc | 176 | if ((getFutureX() <= radius)) |
rrr93 | 0:941225f01ccc | 177 | { |
rrr93 | 0:941225f01ccc | 178 | return true; |
rrr93 | 0:941225f01ccc | 179 | } |
rrr93 | 0:941225f01ccc | 180 | else |
rrr93 | 0:941225f01ccc | 181 | {return false;} |
rrr93 | 0:941225f01ccc | 182 | } |
rrr93 | 0:941225f01ccc | 183 |