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.
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.
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!
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