Exactly Eelco that is what i am trying to do, just know how to flip the speed vector. I tried your code out, and it doesnt work, but I think its because I am not sure about:
ball\x=ball\x-amount of pixels ball has moved 'into' the brick
not positive how to figure out amount of pixels ball has moved into brick.
my collision detection for the brick in first place is:
Ballx+=Ballxv;for(int i=0; i<g_currNumBricks; i++) //check all bricks on field{ if( ((theBall.x+theBall.width) > picBrick[i].x) && (theBall.x < (picBrick[i].x+picBrick[i].width)) && ( (theBall.y + theBall.height) > picBrick[i].y) && (theBall.y < (picBrick[i].y+picBrick[i].height) ) ) { Ballxv=-Ballxv; Ballx-=Ballxv;//guessing here }}Bally += Ballyv;for(i = 0; i<g_currNumBricks; i++){ if( ((theBall.x+theBall.width) > picBrick[i].x) && (theBall.x < (picBrick[i].x+picBrick[i].width)) && ( (theBall.y + theBall.height) > picBrick[i].y) && (theBall.y < (picBrick[i].y+picBrick[i].height) ) ) { Ballyv=-Ballyv; Bally-=Ballyv;//guessing here }}
but that works really funky...another friend of mine wanted to try this:
for(all bricks){ if collision with a brick { double attackangle = 0, angle = 0; attackangle = atan2(-Ballyv , -Ballxv); if(Ballxv > 0) // moving to the right { if(Ballyv > 0) // moving down as well { // the ball will impact with its lower right corner angle = atan2((picBrick[i].y+picBrick[i].height/2) - (theBall.y-theBall.height/2) ,(picBrick[i].x-picBrick[i].width/2) - (theBall.x+theBall.width/2)); if(angle > attackangle) //impacted on top Ballyv=-Ballyv; else //impacted on eft Ballxv=-Ballxv; } else // moving up as well { // the ball will impact with its upper right corner angle = atan2((theBall.y+theBall.height/2) - (picBrick[i].y-picBrick[i].height/2) ,(theBall.x+theBall.width/2) - (picBrick[i].y-picBrick[i].width/2)); if(angle < attackangle) //impacted on right Ballxv=-Ballxv; else //impacted bottom Ballyv=-Ballyv; } } else // moving to the left { if(Ballyv> 0) // moving down as well { // the ball will impact with its lower left corner angle = atan2((picBrick[i].y+picBrick[i].height/2) - (theBall.y-theBall.height/2) ,(picBrick[i].x+picBrick[i].width/2) - (theBall.x-theBall.width/2)); if(angle > attackangle) //impacted right Ballxv=-Ballxv; else //impacted top Ballyv=-Ballyv; } else // moving up as well { // the ball will impact with its upper left corner angle = atan2((theBall.y+theBall.height/2) - (picBrick[i].y-picBrick[i].height/2) ,(theBall.x-theBall.width/2) - (picBrick[i].y+picBrick[i].width/2)); if(angle < attackangle) //impacted bottom Ballyv=-Ballyv; else //impacted right Ballxv=-Ballxv; } } } }
I know the second is computationaly expensive, but at this point I just want it to get working. The second one works somewhat, but sometimes the ball reflects back both axis when it shouldnt and sometimes it just plain goes through the brick (guessing its hitting corners here)
any other ideas ? suggestions? Iwouldnt of guessed something soo simple seemingly would be such a hang up on this game.
Thanks,
-Shane
[edited by - Codejoy on April 23, 2003 5:13:01 PM]