2
votes

I've got a simple BreakOut game (vertical, paddle along bottom) up and running but the collision detection is a bit off. I'm using a Rectangle.Intersect to initially check if the ball intersects the given brick at all and then if it does have 4 more Intersect calls to check whether the ball is colliding vertically (side of brick, left+right) or horizontally (top+bottom of brick). On some occasions when the ball collides with a brick at an acute angle near the intersection of 2 horizontally adjacent bricks it removes both of them and continues in an upward direction. I assume this is a double up of collisions. I've forcibly exited the foreach loop which checks for rectangle intersects the moment it finds one but this hasn't resolved issue.

This is in the main Update() function of the game class:

        foreach (Brick brick in bricks)
        {
            if (brick.CheckCollision(ball))
            {
                break;
            }
        }

This is the Brick.CheckCollision() function:

    public bool CheckCollision(Ball ball)
    {
        if (alive && ball.Bounds.Intersects(location))
        {
            alive=false;
            ball.Deflection(this);
            return true;
        }
        return false;
    }

And this is the Ball.Deflection() function. In this I'm using rectangles with no width/heigth to mark the sides of the brick. Not sure if that is the correct way:

    public void Deflection(Brick brick)
    {
        //For vertical collision (top or bottom of brick) invert Y motion
        if (this.Bounds.Intersects(new Rectangle(brick.Location.Left, brick.Location.Top, brick.Location.Right - brick.Location.Left, 0)) ||
            this.Bounds.Intersects(new Rectangle(brick.Location.Left, brick.Location.Bottom, brick.Location.Right - brick.Location.Left, 0)))
        {
            motion.Y *= -1;
            return;
        }
        //For horizontal collision (left or right side of brick) invert X motion
        if (this.Bounds.Intersects(new Rectangle(brick.Location.Left, brick.Location.Top, 0, brick.Location.Bottom - brick.Location.Top)) ||
            this.Bounds.Intersects(new Rectangle(brick.Location.Right, brick.Location.Top, 0, brick.Location.Bottom - brick.Location.Top)))
        {
            motion.X *= -1;
            return;
        }
    }

I assume I'm making a blatantly obvious logic error but if the general approach can be improved I'm all ears!

1
I've just had a possible eureka/duh moment. If the position of the ball gets far enough into brick1 in one frame and after reversing direction and moving on another frame could it still be inside brick2? That would account for the double negative motion changes. Am I going about the detection the whole wrong way? I had planned to do something VERY basic (in case it wasn't obvious it's my first game) so I don't want to get too deep into the math side of things.KrustyGString
That's very possible. Does it change the behavior if you have the ball update after the bricks? Hopefully that would give the ball enough time to move out of another brick's bounding box.itsme86
That would be one way to go. Maybe capture the ball's location before the updates. If a collision occurs then in addition to changing the ball's velocity, you can also set the ball's location to its previous location.itsme86
It shouldn't cause a stuttering effect. It might make the ball look like it's rebounding off the brick a pixel or 2 early (depending on the velocity), but I don't expect that it would be noticeable. Remember the capturing of the current location, the update/collision check, and the possible resetting of the current location (with the modified velocity from the collision check) will all be happening during a single update cycle. There shouldn't be any drawing in between.itsme86
You would probably find it helpful to add some logging code that writes information to a debug portion of the screen.bmm6o

1 Answers

0
votes

Through discussion in the comments with itsme86 I've resolved my issues. I added logging (recommended by bmm6o) and confirmed that it was a doubling up of collisions caused by the ball travelling too far inside a block on one frame and still being partially inside adjacent block on next frame. I've decided to keep the logic simple and just move the ball back to the edge of the block it intersects when I reverse it's direction. This is done with no Draw() function in between so as not to produce stuttering effect. I am struggling to observe any jumps or stuttering at all so if there is any it's not visible.