Very simple collision detection question

  • Thread starter Thread starter wandoledzep
  • Start date Start date
W

wandoledzep

I have been banging my head on a wall for two days now. All I have is a
picbox bouncing around the form, and I want it to react when it hits
the block (another picbox). Everything works correctly except when the
"ball" hits the "block" right in the ball's center, it reacts
incorrectly. I know this should not be this hard. Can anyone clear this
up any??? Thanks. BTW I'm working on a Breakout-type game. Here's my
code: (*P.S. Nevermind the overkill properties*)

Dim intMoveX As Integer = 2
Dim intMoveY As Integer = 2

Dim blnZero As Boolean

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
DetectWalls()
DetectTopBottomObject()
End Sub

Public Sub DetectWalls()
picBall.Top += intMoveY
picBall.Left += intMoveX

If picBall.Left + picBall.Width > Me.Width - 5 Then
intMoveX = -intMoveX
End If
If picBall.Left = 0 Then
intMoveX = 2
End If
If picBall.Top = 1 Then
intMoveY = -intMoveY
End If
If picBall.Top + picBall.Height + 30 > Me.Height Then
intMoveY = -2
End If
End Sub



Public Sub DetectTopBottomObject()
'Top and Bottom
If BallLeft > picBlock1.Left And _
BallRight < picBlock1.Left + picBlock1.Width And _
BallTop < picBlock1.Top + picBlock1.Height And _
BallBottom > picBlock1.Top Then
intMoveY = -intMoveY
End If
'Sides
If BallTop > picBlock1.Top And _
BallBottom < picBlock1.Top + picBlock1.Height And _
BallRight > picBlock1.Left And _
BallLeft < picBlock1.Left + picBlock1.Width Then
intMoveX = -intMoveX
End If
End Sub



Public Property BallLeft()
Get
Return picBall.Left()
End Get
Set(ByVal Value)
picBall.Left = Value
End Set
End Property

Public Property BallRight()
Get
Return picBall.Left + picBall.Width
End Get
Set(ByVal Value)

End Set
End Property

Public Property BallTop()
Get
Return picBall.Top
End Get
Set(ByVal Value)
picBall.Top = Value
End Set
End Property

Public Property BallBottom()
Get
Return picBall.Top + picBall.Height
End Get
Set(ByVal Value)

End Set
End Property
 
Dear wandoledzep,

Nice to see your working on a break-out type game! About your problem: why
don't you try to draw it out?
Draw your situation as pricise as possible and don't forget to add some
dimensions. Now replace your dimensions with code (eg
Width/Height/Top/Left).
and see how the algorythm for the collision works. Also, try not to include
that many And's in your If statement. This makes your code become unreadable
and not easy to understand (not for yourself nor for us).
 
Sorry to say this but youreally should not be using a picturebox for this
type of operation.

Each control is a window and not a graphic object. This job screams out for
a retained mode graphics system.

See the GDI+ FAQ for details.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Back
Top