Implementation details:
Bricks are represented as a vector of Objects. When the ball collides with a brick, it is removed from the vector. This part works fine at the moment, the question comes into play when figuring out changes in the ball's velocity due to a collision.
Right now if the ball hits a brick or the paddle I simply reverse the y velocity. This works fine if it hits the top of the paddle or the bottom of a brick.. but what if it hits the side? Obviously I would negate the x velocity as opposed to the y, but how would I determine with which side of a brick the ball has collided?
This is what I've written to start:
Code: Select all
if( (ball.x + ball.w + ball.xVel > brick.x) && (ball.x + ball.xVel < brick.x + brick.w) && (ball.y + ball.h + ball.yVel > brick.y) && (ball.y + ball.yVel < brick.y + brick.h))
{
delete (*bricksIter);
bricks.erase( bricksIter );
ball->yVel = -ball->yVel;
}
Any ideas/suggestions (yes I realize this is elementary rectangular collision...)?