Elements used in the Balls and Things games for the RETRO.
Dependents: RETRO_BallsAndPaddle RETRO_BallAndHoles
Revision 7:4fa3edaa1201, committed 2015-03-02
- Comitter:
- maxint
- Date:
- Mon Mar 02 09:04:13 2015 +0000
- Parent:
- 6:860b82c19ecf
- Child:
- 8:19dd2a538cbe
- Commit message:
- clean-up; better collision detection on rectangles
Changed in this revision
--- a/Ball.cpp Sat Feb 28 19:40:48 2015 +0000 +++ b/Ball.cpp Mon Mar 02 09:04:13 2015 +0000 @@ -20,9 +20,6 @@ r=r*2/3; g=g*2/3; b=b*2/3; -// r=r/2; -// g=g/2; -// b=b/2; return(Color565::fromRGB((uint16_t)r,(uint16_t)g,(uint16_t)b)); } @@ -107,7 +104,6 @@ this->unmove(); // undo move to pre-bouncing position to avoid drawing on colliding position this->vSpeed.multiply(vBounce); - //this->vSpeed.bounce(vBounce); // check speed w/max if(this->vSpeed.y>5.0) this->vSpeed.y=5; @@ -129,15 +125,8 @@ if(this->vSpeed.y<-5.0) this->vSpeed.y=-5; if(this->vSpeed.x<-5.0) this->vSpeed.x=-5; - if(abs(vSpeed.x)>abs(vSpeed.y)) - vSpeed.x=vSpeed.x/abs(vSpeed.x) * abs(vSpeed.y); - -/* - if(this->vSpeed.y>0 && this->vSpeed.y<1.0) this->vSpeed.y=1; - if(this->vSpeed.x>0 && this->vSpeed.x<1.0) this->vSpeed.x=1; - if(this->vSpeed.y<0 && this->vSpeed.y>-1.0) this->vSpeed.y=-1; - if(this->vSpeed.x<0 && this->vSpeed.x>-1.0) this->vSpeed.x=-1; -*/ +// if(abs(vSpeed.x)>abs(vSpeed.y)) +// vSpeed.x=vSpeed.x/abs(vSpeed.x) * abs(vSpeed.y); }
--- a/Paddle.cpp Sat Feb 28 19:40:48 2015 +0000 +++ b/Paddle.cpp Mon Mar 02 09:04:13 2015 +0000 @@ -36,8 +36,6 @@ void Paddle::move(Vector vDiff) { this->pos.move(vDiff); -// this->rPaddle.move(vDiff); - /* char szBuffer[256]; sprintf(szBuffer, "p:%d,%d ", pos.getX(), pos.getY()); @@ -69,18 +67,11 @@ void Paddle::redraw(bool fForceDraw) // fForceDraw=false { // redraw the paddle if its position has changed - //static int n=0; -//char szBuffer[256]; if(pos.hasChanged() || fForceDraw) { Point pPrev=pos.getPrev(); Point pCur=pos.getCur(); -/* -n++; -sprintf(szBuffer, "%d %d,%d - %d,%d", n, pPrev.getX(), pPrev.getY(), pCur.getX(), pCur.getY()); -this->pDisp->drawString(font_oem, 20, 0, szBuffer); -*/ clearPrev(); draw(); }
--- a/Physics.cpp Sat Feb 28 19:40:48 2015 +0000 +++ b/Physics.cpp Mon Mar 02 09:04:13 2015 +0000 @@ -26,14 +26,10 @@ void Position::set(float x, float y) { - //nPrevX=nCurX; - //nPrevY=nCurY; pPrev=pCur; vPos.x=x; vPos.y=y; pCur.set(rint(vPos.x), rint(vPos.y)); - //nCurX=rint(vPos.x); - //nCurY=rint(vPos.y); } void Position::set(int x, int y) @@ -58,14 +54,10 @@ void Position::move(float fDiffX, float fDiffY) { - //nPrevX=nCurX; - //nPrevY=nCurY; pPrev=pCur; vPos.x+=fDiffX; vPos.y+=fDiffY; pCur.set(rint(vPos.x), rint(vPos.y)); - //nCurX=rint(vPos.x); - //nCurY=rint(vPos.y); } void Position::move(int nDiffX, int nDiffY)
--- a/Shapes.cpp Sat Feb 28 19:40:48 2015 +0000 +++ b/Shapes.cpp Mon Mar 02 09:04:13 2015 +0000 @@ -83,7 +83,7 @@ int Line::getDistance(Point pt) { // get the distance of a line to a point - //TODO + // TODO: more precise calculation, currently it's a kind-of lame approximation Line ln1(get1(), pt); Line ln2(get2(), pt); int nDist1=ln1.getSize(); @@ -122,7 +122,6 @@ y2 = pt2.getY(); } - bool Rectangle::collides(Point pt) { if(pt.getX() >= x1 && pt.getX() <= x2) { @@ -133,28 +132,18 @@ return false; } -/* -Rectangle Rectangle::intersection(Rectangle r) -{ // return the intersection of two rectangles or null - Rectangle rResult; - rResult=Rectangle() - if(this->x2 >= -} -*/ - bool Rectangle::collides(Rectangle r) { // check if two rectangles collide - // method 1: check if corners of eithter rectangle collide - // method 2: compare sides - if(this->collides(r.get1()) || this->collides(r.get2())) return(true); - if(this->collides(r.get3()) || this->collides(r.get4())) return(true); - if(r.collides(this->get1()) || r.collides(this->get2())) return(true); - if(r.collides(this->get3()) || r.collides(this->get4())) return(true); - // TODO: check other corners - return(false); + // method: compare rectangle sides + // see http://stackoverflow.com/questions/7610129/simple-collision-detection-android + if ((getX2() < r.getX1()) || // A is to the left of B + (r.getX2() < getX1()) || // B is to the left of A + (getY2() < r.getY1()) || // A is above B + (r.getY2() < getY1())) // B is above A + return(false); + return(true); } - Point Rectangle::get1() { return(Point(x1, y1));
--- a/SoundFx.cpp Sat Feb 28 19:40:48 2015 +0000 +++ b/SoundFx.cpp Mon Mar 02 09:04:13 2015 +0000 @@ -69,7 +69,5 @@ if(this->fMute) return; // music.setCompletionCallback(this, &SoundFX::musicCompleted); - //music.play("T224L8O5CL16>C<P16GP16L8EL16P16>C<GP16L8E.L16P16L8C#L16>C#<P16G#P16L8FL16P16>C#<G#P16L8F.L16P16L8CL16>C<P16GP16L8EL16P16>C<GP16L8E.L16P16D#EFP16FF#GP16GG#AP16L8>C<P8L4>C"); - music.play("T120 O3 L16 F L4 C F L8 C"); - + music.play("T224L8O5CL16>C<P16GP16L8EL16P16>C<GP16L8E.L16P16L8C#L16>C#<P16G#P16L8FL16P16>C#<G#P16L8F.L16P16L8CL16>C<P16GP16L8EL16P16>C<GP16L8E.L16P16D#EFP16FF#GP16GG#AP16L8>C<P8L4>C"); } \ No newline at end of file