Advertisement

C++ Breakout: collision detection(and which side collided)

Started by November 17, 2013 02:20 PM
0 comments, last by Stragen 11 years, 3 months ago

Hello,

welcome everyone! It's my first post here! :)

I have a question about Collision detection in breakout. I have a little bit of a problem with making it work. I would like to know which side collided. I have this code:


namespace CollisionChecker
{
    enum
    {
        COLLISION_NONE,
        COLLISION_TOP,
        COLLISION_BOTTOM,
        COLLISION_LEFT,
        COLLISION_RIGHT,
        COLLISION_X,
    };

    template < class T >
    int CheckCollision(Ball& ball, T& block)
    {
        int collided = COLLISION_NONE;

        int ballY = ball.GetY() + ball.GetH()/2;
        int ballX = ball.GetX() + ball.GetW()/2;
        int r = ball.GetH()/2;

        if( (ballY + r < block.GetY())
            || ( ball.GetY() > block.GetY() + block.GetH())
            || (ball.GetX() > block.GetX() + block.GetW())
            || (ball.GetX() + r < block.GetX()) )
        {
            //No collision;
        }
        else
        {

            if(ballY <= block.GetY() - (block.GetH()/2))
            {
                collided = COLLISION_BOTTOM;
            }
              //Hit was from below the brick

            if(ballY >= block.GetY() + (block.GetH()/2))
            {
                collided = COLLISION_TOP;
            }
              //Hit was from above the brick

            if(ballX < block.GetX())
            {
                collided = COLLISION_LEFT;
            }
              //Hit was on left

            if(ballX > block.GetX())
            {
                collided = COLLISION_RIGHT;
            }
              //Hit was on right
        }
    return collided;
}

These conditionals were found in stackoverflow answer. These kinda work, but not quite as I would like them to.

Basically, this isn't this bad. The downside of this collision detection is, that ball can bounce from air, leaving 0 > distance > Ball's radius.

Also, these side detection also sometimes doesn't work as it should. For example, sometimes it collides from side, but bounces upwards, resulting in strange movement that can clear 2 rows of blocks.

And I would like to change paddle bounce later, so that ball would have bounce differently when bouncing from left side, middle, or right side.

Right now I just invert yVel.

Anyway, question is: how can I detect side collision correctly? Also, is there a way to make collision allow ball to bounce directly from block or paddle, instead of leaving this gap?

Thanks in advance guys!

What you've done in your code is what i like to call 'Block' collision detection, where you are testing for a case where Object A is within Object B.

This is fine if you have unit, integer, velocities and tracking that never exceeds 1, as soon as you introduce larger values it is possible for ghosting/clipping to occur. (Object A passes through Object B and appears on the other side) In this case you can end up within the object you're testing for which will cause your if statements to freak out (and the first in the order will be the reaction that applies).

To solve this unfortunately(fortunately for some) requires the use of some math, specifically the kind of stuff you could do in high school..

"I have a train that is traveling east at 25km/h, and a train traveling west at 10km/h, they are 40km apart, when will they collide?"

Determining the side at which its coming from requires calculating the direction of movement or a vector (if the ball is moving up, and the object is above the model, then it cant hit from the top can it?) and determining the reaction is a case of newtons third law (for every action, an equal and opposite reaction) which will change the vector's angle (flipping either X velocity or y velocity)

This topic is closed to new replies.

Advertisement