Elements used in the Balls and Things games for the RETRO.
Dependents: RETRO_BallsAndPaddle RETRO_BallAndHoles
Revision 2:74bc9b16fb88, committed 2015-02-25
- Comitter:
- maxint
- Date:
- Wed Feb 25 10:43:15 2015 +0000
- Parent:
- 1:71185a0aadfc
- Child:
- 3:441dc90d10ce
- Commit message:
- added methods for better bouncing experiments
Changed in this revision
--- a/Ball.cpp Fri Feb 06 10:18:02 2015 +0000 +++ b/Ball.cpp Wed Feb 25 10:43:15 2015 +0000 @@ -91,9 +91,37 @@ 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; + if(this->vSpeed.x>5.0) this->vSpeed.x=5; + if(this->vSpeed.y<-5.0) this->vSpeed.y=-5; + if(this->vSpeed.x<-5.0) this->vSpeed.x=-5; +} + +void Ball::BounceAgainst(Vector vBounce) +{ // change the direction in a certain direction + 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; + if(this->vSpeed.x>5.0) this->vSpeed.x=5; + 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; +*/ }
--- a/Ball.h Fri Feb 06 10:18:02 2015 +0000 +++ b/Ball.h Wed Feb 25 10:43:15 2015 +0000 @@ -34,6 +34,7 @@ Circle getBoundingCircle(); bool collides(Rectangle r); void Bounce(Vector vBounce); + void BounceAgainst(Vector vBounce); private: uint16_t uColor;
--- a/Vector.cpp Fri Feb 06 10:18:02 2015 +0000 +++ b/Vector.cpp Wed Feb 25 10:43:15 2015 +0000 @@ -57,4 +57,49 @@ { // multiply the vector by means of the other vector x*=vMult.x; y*=vMult.y; +} + +Vector Vector::getNormalized() +{ // get the Unit vector (= vector normalized to length 1) + // see http://en.wikipedia.org/wiki/Unit_vector + return(Vector(x/getSize(), y/getSize())); +} + +Vector Vector::getNormal() +{ // get the normal Vector + // see http://stackoverflow.com/questions/14885693/how-do-you-reflect-a-vector-over-another-vector + // vec.leftNormal --> vx = vec.vy; vy = -vec.vx; + // vec.rightNormal --> vx = -vec.vy; vy = vec.vx; + if(isLeft()) + return(Vector(y, -1 * x)); + else + return(Vector(-1 * y, x)); +} + +void Vector::bounce(Vector vBounce) +{ // bounce a vector against another vector + // see http://stackoverflow.com/questions/14885693/how-do-you-reflect-a-vector-over-another-vector + Vector vBounceNormalized=vBounce.getNormalized(); + Vector vBounceNormal=vBounce.getNormal(); + Vector vBounceNormalNormalized=vBounceNormal.getNormalized(); + + // 1. Find the dot product of vec1 and vec2 + // Note: dx and dy are vx and vy divided over the length of the vector (magnitude) + float dpA = x * vBounceNormalized.x + y * vBounceNormalized.y; + + // 2. Project vec1 over vec2 + float prA_vx = dpA * vBounceNormalized.x; + float prA_vy = dpA * vBounceNormalized.y; + + // 3. Find the dot product of vec1 and vec2's normal + float dpB = x * vBounceNormalNormalized.x + y * vBounceNormalNormalized.y; + + // 4. Project vec1 over vec2's left normal + float prB_vx = dpB * vBounceNormalNormalized.x; + float prB_vy = dpB * vBounceNormalNormalized.y; + + // 5. Add the first projection prA to the reverse of the second -prB + float new_vx = prA_vx - prB_vx; + float new_vy = prA_vy - prB_vy; + set(new_vx, new_vy); } \ No newline at end of file
--- a/Vector.h Fri Feb 06 10:18:02 2015 +0000 +++ b/Vector.h Wed Feb 25 10:43:15 2015 +0000 @@ -9,7 +9,7 @@ Vector(); Vector(float fX, float fY); - void set(float dX, float fY); + void set(float fX, float fY); void set(int nX, int nY); float getSize(); bool isLeft(); @@ -19,4 +19,7 @@ void add(float fAdd); void add(Vector vAdd); void multiply(Vector vMult); + Vector getNormal(); + Vector getNormalized(); + void bounce(Vector vBounce); };