[SOLVED] Simple ball collision depending on edge of impact
Posted: Fri Mar 26, 2010 5:25 pm
So I'm trying to make a ball change directions based on where it hits a brick (pointer iterator).
If it hits the top or bottom of a brick, I want the yVel to reverse, and if it hits the left or right of the brick,
I want the xVel to reverse; if it hits both, then I want it to reverse both velocities.
Right now, the Y bouncing is working fine, but the X bouncing is not.
EDIT:
Here is the rewritten understandable version of the code:
If it hits the top or bottom of a brick, I want the yVel to reverse, and if it hits the left or right of the brick,
I want the xVel to reverse; if it hits both, then I want it to reverse both velocities.
Right now, the Y bouncing is working fine, but the X bouncing is not.
EDIT:
Here is the rewritten understandable version of the code:
Code: Select all
if (IsCollision(&(*ballIter)->getDstRect(), &(*brickIter)->getDstRect()))
{
//If the left edge of the ball is inside the brick
if ((*ballIter)->getX() > (*brickIter)->getX() &&
(*ballIter)->getX() < (*brickIter)->getX() + Brick::WIDTH)
{
(*ballIter)->ReverseX(); //Reverse the ball's x velocity
}
//If the right edge of the ball is inside the brick
if ((*ballIter)->getX() + Ball::BALL_WIDTH > (*brickIter)->getX() &&
(*ballIter)->getX() + Ball::BALL_WIDTH < (*brickIter)->getX() + Brick::WIDTH)
{
(*ballIter)->ReverseX(); //Reverse the ball's x velocity
}
//If the top edge of the ball is inside the brick
if (((*ballIter)->getY() > (*brickIter)->getY() &&
(*ballIter)->getY() < (*brickIter)->getY() + Brick::HEIGHT))
{
(*ballIter)->ReverseY(); //Reverse the ball's y velocity
}
//If the bottom edge of the ball is inside the brick
if ((*ballIter)->getY() + Ball::BALL_HEIGHT > (*brickIter)->getY() &&
(*ballIter)->getY() + Ball::BALL_HEIGHT < (*brickIter)->getY() + Brick::HEIGHT)
{
(*ballIter)->ReverseY(); //Reverse the ball's y velocity
}
}