473,468 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Very simple collision detection question

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

Nov 23 '05 #1
3 2271
This problem made me think of the examples found in KPL

http://msdn.microsoft.com/coding4fun...L/default.aspx

Maybe looking at the code from some of those programs will give you a
solution.

Greg
<wa*********@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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

Nov 23 '05 #2
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).

<wa*********@hotmail.com> schreef in bericht
news:11**********************@g47g2000cwa.googlegr oups.com...
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

Nov 23 '05 #3
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.

<wa*********@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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

Nov 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: barnesc | last post by:
Hi! Here's a simple hashcash implementation in Python. 28 lines of actual code. Can be reduced to 17 lines for instructional purposes, if you don't want clustering, and use xrange() instead...
3
by: Thorsten Reichelt | last post by:
Hi, I'm involved in a research project on spatial prepositions. In that project we use very simple, static 3D maps that are represented in a tiny subset of x3d enriched with some few linguistic...
7
by: Szar | last post by:
JS noob. I've seen plenty of browser detection scripts but they all seem to be slightly different and don't really fit my needs. I have various places where if the browser is IE I'd like to display...
7
by: carl.manaster | last post by:
I'm new to this game. I can find my way around C# without any trouble, and I've used Access, a little bit, in the past. Now a friend wants an application of mine to read from his Access database....
8
by: Maya | last post by:
Hello all, I'm using MD5 hashing in my application to give unique values to huge list of items my application receives, originally every item's name was difficult to use as an id for this item...
0
by: licombo | last post by:
I am making a flash game which is similar to the classic game Asteroids. I am stuck in the final step of the game where I have to make the collision detection working before the whole game is...
126
by: jacob navia | last post by:
Buffer overflows are a fact of life, and, more specifically, a fact of C. All is not lost however. In the book "Value Range Analysis of C programs" Axel Simon tries to establish a...
1
by: May Amor | last post by:
Helu gurus!!! I have a code below about hashing method with collision resolution...My problem is how to use the collsion resolution again if the hash index though has already a value. Please kinda...
10
by: Conrad Lender | last post by:
In a recent thread in this group, I said that in some cases object detection and feature tests weren't sufficient in the development of cross-browser applications, and that there were situations...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.