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.
Dependents: RETRO_BallsAndPaddle RETRO_BallAndHoles
Ball.cpp
00001 #include "Ball.h" 00002 00003 Ball::Ball() : vSpeed() 00004 { // constructor 00005 } 00006 00007 Ball::Ball(LCD_ST7735* pDisp) : vSpeed() 00008 { // constructor 00009 this->pDisp=pDisp; 00010 this->fActive=false; 00011 } 00012 00013 uint16_t Ball::dimmedColor(uint16_t uColor) 00014 { 00015 uint16_t r, g, b; 00016 00017 r=(uColor >> 11) <<3; 00018 g=((uColor >> 5) & 0x3F) <<2; 00019 b=(uColor & 0x1F) << 3; 00020 r=r*2/3; 00021 g=g*2/3; 00022 b=b*2/3; 00023 00024 return(Color565::fromRGB((uint16_t)r,(uint16_t)g,(uint16_t)b)); 00025 } 00026 00027 void Ball::initialize(int nX, int nY, int nR, uint16_t uColor) 00028 { 00029 this->pos.set(nX, nY); 00030 this->nRadius=nR; 00031 this->uColor=uColor; 00032 this->uColorHigh=uColor; 00033 this->uColorMid=dimmedColor(uColorHigh); 00034 this->uColorLow=dimmedColor(uColorMid); 00035 00036 } 00037 00038 void Ball::setSpeed(int X, int Y) 00039 { 00040 this->vSpeed.set(X, Y); 00041 } 00042 00043 void Ball::changeSpeed(bool fUp) 00044 { 00045 if(fUp) 00046 { 00047 if(this->vSpeed.getSize()<=4.0) this->vSpeed.add(1.0); 00048 } 00049 else 00050 { 00051 if(this->vSpeed.getSize()>=1.0) 00052 this->vSpeed.add(-1.0); 00053 else // TODO: added code below to allow zero speed pause of ball 00054 this->vSpeed.set(0,0); 00055 } 00056 } 00057 00058 void Ball::unmove() 00059 { // move back to previous position 00060 pos.set(pos.getPrev()); 00061 } 00062 00063 void Ball::update() 00064 { 00065 this->pos.move(this->vSpeed); 00066 } 00067 00068 Circle Ball::getBoundingCircle() 00069 { 00070 return(Circle(this->pos.getX(), this->pos.getY(), this->nRadius)); 00071 } 00072 00073 bool Ball::collides(Rectangle r) 00074 { 00075 Circle cBall=this->getBoundingCircle(); 00076 Rectangle rBall=cBall.getBoundingRectangle(); 00077 00078 /* 00079 char szBuffer[256]; 00080 sprintf(szBuffer, "c:%d,%d ", cBall.getX(), cBall.getY()); 00081 this->pDisp->drawString(font_oem, 0, 0, szBuffer); 00082 */ 00083 return(rBall.collides(r)); 00084 } 00085 00086 bool Ball::collides(Circle cObject) 00087 { 00088 Circle cBall=this->getBoundingCircle(); 00089 Rectangle rBall=cBall.getBoundingRectangle(); 00090 Rectangle rObject=cObject.getBoundingRectangle(); 00091 00092 return(rBall.collides(rObject)); 00093 } 00094 00095 00096 bool Ball::collides(Line ln) 00097 { 00098 Circle cBall=this->getBoundingCircle(); 00099 return(cBall.collides(ln)); 00100 } 00101 00102 void Ball::Bounce(Vector vBounce) 00103 { // change the direction in a certain direction 00104 this->unmove(); // undo move to pre-bouncing position to avoid drawing on colliding position 00105 00106 this->vSpeed.multiply(vBounce); 00107 00108 // check speed w/max 00109 if(this->vSpeed.y>5.0) this->vSpeed.y=5; 00110 if(this->vSpeed.x>5.0) this->vSpeed.x=5; 00111 if(this->vSpeed.y<-5.0) this->vSpeed.y=-5; 00112 if(this->vSpeed.x<-5.0) this->vSpeed.x=-5; 00113 } 00114 00115 void Ball::BounceAgainst(Vector vBounce) 00116 { // change the direction in a certain direction 00117 this->unmove(); // undo move to pre-bouncing position to avoid drawing on colliding position 00118 00119 //this->vSpeed.multiply(vBounce); 00120 this->vSpeed.bounce(vBounce); 00121 00122 // check speed w/max 00123 if(this->vSpeed.y>5.0) this->vSpeed.y=5; 00124 if(this->vSpeed.x>5.0) this->vSpeed.x=5; 00125 if(this->vSpeed.y<-5.0) this->vSpeed.y=-5; 00126 if(this->vSpeed.x<-5.0) this->vSpeed.x=-5; 00127 00128 // if(abs(vSpeed.x)>abs(vSpeed.y)) 00129 // vSpeed.x=vSpeed.x/abs(vSpeed.x) * abs(vSpeed.y); 00130 } 00131 00132 00133 00134 void Ball::clear() 00135 { 00136 Point p=pos.getCur(); 00137 this->pDisp->fillCircle(p.getX(),p.getY(), this->nRadius, Color565::Black, Color565::Black); 00138 } 00139 00140 void Ball::clearPrev() 00141 { 00142 Point p=pos.getPrev(); 00143 this->pDisp->fillCircle(p.getX(),p.getY(), this->nRadius, Color565::Black, Color565::Black); 00144 } 00145 00146 00147 void Ball::draw() 00148 { // render the object on the screen, based on its current position 00149 Point p=pos.getCur(); 00150 if(this->nRadius>3) 00151 { 00152 this->pDisp->fillCircle(p.getX(), p.getY(), this->nRadius, this->uColorLow, this->uColorLow); 00153 this->pDisp->fillCircle(p.getX()-this->nRadius/3, p.getY()-this->nRadius/3, this->nRadius/2, this->uColorMid, this->uColorMid); 00154 this->pDisp->setPixel(p.getX()-this->nRadius/2, p.getY()-this->nRadius/2, this->uColorHigh); 00155 } 00156 else 00157 this->pDisp->fillCircle(p.getX(), p.getY(), this->nRadius, this->uColorMid, this->uColorMid); 00158 } 00159 00160 void Ball::redraw() 00161 { // redraw the ball if its position has changed 00162 if(pos.hasChanged()) 00163 { 00164 clearPrev(); 00165 draw(); 00166 } 00167 }
Generated on Thu Jul 14 2022 10:55:45 by
1.7.2