Results 1 to 6 of 6

Thread: Collision Detection - Brick Breaker Game

  1. #1

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Collision Detection - Brick Breaker Game

    I'm not sure how to go about this I already have the game running with basic collision detection which just checks if two rectangles intersect but I need it to be more precise. Here's an explanation of what I need to do:

    Ball/Brick Collision
    The ball is drawn using a rectangle however instead of using the rectangle I would like to be more precise and only check a collision of the actual ball.

    I need to check the collision of the ball with the bricks. However I need to check if the ball collides with specific sides of the brick so I know which way to bounce it off and to change the velocities.

    Can anyone point me in the right direction?

    I have added the images in case it helps.
    Attached Images Attached Images   

  2. #2

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Re: Collision Detection - Brick Breaker Game

    This is one not so bright idea I came up with. Split the block into four sections as shown in the attached image and check for a collision with each rectangle and bounce it off as required.
    Attached Images Attached Images  

  3. #3

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Re: Collision Detection - Brick Breaker Game

    Can anyone help with this? I've implemented the above concept which is better but is quite glitchy and does strange things every so often.

  4. #4
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: Collision Detection - Brick Breaker Game

    You don't need to do that much. You just need to check if the ball hit the block and if so invert the axis.

  5. #5
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Collision Detection - Brick Breaker Game

    The checking of the collision is probably done by some IF-conditions, the colision is detected if any of those conditions is met, you would need the evaluate which of those has been met in order to know which side was hit. By knowing that you can invert the movement in the correct way.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  6. #6

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Re: Collision Detection - Brick Breaker Game

    Ok well this is what I am doing at the moment but it is quite glitchy. Any suggestions?

    Code:
    Public Sub CheckBlockCollision(ByVal BallPosition As Vector2)
    
            Dim BlockSize As Size = Game.BlockSize 'Used For Testing'
            Dim BallTestRect As New Rectangle(BallPosition.X, BallPosition.Y, 15, 15) 'Used For Testing'
            Dim BlockTestRectLeft As Rectangle 'Used For Testing'
            Dim BlockTestRectRight As Rectangle 'Used For Testing'
            Dim BlockTestRectTop As Rectangle 'Used For Testing'
            Dim BlockTestRectBottom As Rectangle 'Used For Testing'
    
            For X As Integer = 0 To 12
                For Y As Integer = 0 To 12
                    If Game.Blocks(X, Y) IsNot Nothing Then
                        BlockTestRectLeft = New Rectangle((X * BlockSize.Width) + OffsetX, (Y * BlockSize.Height) + OffsetY, 10, 20) 'Create Rectangle For Block Location'
                        BlockTestRectRight = New Rectangle((X * BlockSize.Width) + OffsetX + BlockSize.Width - 10, (Y * BlockSize.Height) + OffsetY, 10, 20) 'Create Rectangle For Block Location'
                        BlockTestRectTop = New Rectangle((X * BlockSize.Width) + OffsetX + 10, (Y * BlockSize.Height) + OffsetY, 20, 10) 'Create Rectangle For Block Location'
                        BlockTestRectBottom = New Rectangle((X * BlockSize.Width) + OffsetX + 10, (Y * BlockSize.Height) + OffsetY, 20, 10) 'Create Rectangle For Block Location'
    
                        If BlockTestRectLeft.Intersects(BallTestRect) Or BlockTestRectRight.Intersects(BallTestRect) Then 'Check For Collision'
                            PlaySound("BlockHit")
                            If Game.Blocks(X, Y).BlockLife = 1 Then 'Test If Block Is To Be Removed'
                                Game.Blocks(X, Y) = Nothing
                                AddScore(1)
                            Else
                                AddScore(Game.Blocks(X, Y).BlockLife)
                                Game.Blocks(X, Y).BlockLife -= 1 'If Not Decrement The Block Life'
                            End If
    
                            Game.xVel = -Game.xVel
    
                        ElseIf BlockTestRectTop.Intersects(BallTestRect) Or BlockTestRectBottom.Intersects(BallTestRect) Then 'Check For Collision'
                            PlaySound("BlockHit")
                            If Game.Blocks(X, Y).BlockLife = 1 Then 'Test If Block Is To Be Removed'
                                Game.Blocks(X, Y) = Nothing
                                AddScore(1)
                            Else
                                AddScore(Game.Blocks(X, Y).BlockLife)
                                Game.Blocks(X, Y).BlockLife -= 1 'If Not Decrement The Block Life'
                            End If
    
                            Game.yVel = -Game.yVel
                        End If
    
                    End If
                Next Y
            Next X
    
        End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width