|
-
Feb 19th, 2005, 03:57 PM
#1
Thread Starter
Hyperactive Member
Simple Collision Detection [RESOLVED]
Heres my problem I have an image thats stationary and another image moving toward it. When the image touches any part of the other I want some events to happen. How can I do this? Ive tried using
if image.left = image2.left blah blah
But it doesn't work. What can I do?
Thanks in advance!
Last edited by Phade; Feb 20th, 2005 at 08:12 AM.
-
Feb 19th, 2005, 04:16 PM
#2
Re: Simple Collision Detection
Something like this should work:
VB Code:
If Picture1.Left + Picture1.Width >= Picture2.Left And Picture1.Left <= Picture2.Left + Picture2.Width And Picture1.Top + Picture1.Height >= Picture2.Top And Picture1.Top <= Picture2.Top + Picture2.Height Then
'Collition
End If
-
Feb 19th, 2005, 04:58 PM
#3
Thread Starter
Hyperactive Member
Re: Simple Collision Detection
VB Code:
Private Sub collision_Timer()
If Char.Left + Char.Width >= book.Left And Char.Left <= book.Left + book.Width And Char.Top + Char.Height >= book.Top And Char.Top <= book.Top + book.Height Then
MsgBox "test"
End If
End Sub
Doesn't work. Did i do something wrong?
-
Feb 19th, 2005, 05:53 PM
#4
Thread Starter
Hyperactive Member
Re: Simple Collision Detection
Heres the full code if it helps.
VB Code:
'The key array that shows true if the key is down, and false if not
Dim KeyArray(100) As Boolean
Dim iX As Integer 'The X coordinate for the Character
Dim iY As Integer 'The Y coordinate for the Character
Dim iXbook As Integer 'The X coordinate for the book
Dim iYbook As Integer 'The Y coordinate for the book
Private Sub collision_Timer()
If Char.Left + Char.Width >= book.Left And Char.Left <= book.Left + book.Width And Char.Top + Char.Height >= book.Top And Char.Top <= book.Top + book.Height Then
MsgBox "test"
End If
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'Marks the element in the array as true if it the key is pushed
KeyArray(KeyCode) = True
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
'Markes the element in the array as false if it the key is released
KeyArray(KeyCode) = False
End Sub
Private Sub Form_Load()
'Setting the starting values of the X,Y coordinates of the Character
iX = Char.Left
iY = Char.Top
iXbook = book.Left
iYbook = book.Top
'Setting some properties for the Form,
'and also painting the Character
With Form1
.AutoRedraw = True
.KeyPreview = True
.PaintPicture Char.Picture, iX, iY
End With
collision.Interval = "10"
collision.Enabled = True
GameLoop.Interval = "10"
GameLoop.Enabled = True
End Sub
Private Sub GameLoop_Timer()
If KeyArray(39) = True Then 'Character moves right
iX = iX + 25
Char.Picture = charright.Picture
End If
If KeyArray(37) = True Then 'Character moves left
iX = iX - 25
Char.Picture = charleft.Picture
End If
If KeyArray(38) = True Then 'Character moves up
iY = iY - 25
Char.Picture = charup.Picture
End If
If KeyArray(40) = True Then 'Character moves down
iY = iY + 25
Char.Picture = chardown.Picture
End If
'Clears the screen
Form1.Cls
'Paints the Character
Form1.PaintPicture Char.Picture, iX, iY
Form1.PaintPicture book.Picture, iXbook, iYbook
End Sub
-
Feb 20th, 2005, 07:23 AM
#5
Re: Simple Collision Detection
First of all, I wouldn't have called one of the pictures Char. Since I think it is a datatype in VB. Second of all, you don't need two timers. One for the Gameloop, and one for the collision test. You can do all everything in the same timer event. Fix those two possible "bugs", then attach your code if it doesn't help. Easier to spot the error for me then, since I havn't touched VB much the last 3 years.
ØØ
-
Feb 20th, 2005, 07:43 AM
#6
Thread Starter
Hyperactive Member
Re: Simple Collision Detection
VB Code:
Private Sub GameLoop_Timer()
If KeyArray(39) = True Then 'Character moves right
iX = iX + 5
charrightmove.Interval = "60"
charrightmove.Enabled = True
End If
If KeyArray(37) = True Then 'Character moves left
iX = iX - 5
charleftmove.Interval = "60"
charleftmove.Enabled = True
End If
If KeyArray(38) = True Then 'Character moves up
iY = iY - 5
charupmove.Interval = "60"
charupmove.Enabled = True
End If
If KeyArray(40) = True Then 'Character moves down
iY = iY + 5
chardownmove.Interval = "60"
chardownmove.Enabled = True
End If
'Clears the screen
Form1.Cls
'Paints the Character
Form1.PaintPicture Character.Picture, iX, iY
Form1.PaintPicture book.Picture, iXbook, iYbook
If Character.Left + Character.Width >= book.Left And Character.Left <= book.Left + book.Width And Character.Top + Character.Height >= book.Top And Character.Top <= book.Top + book.Height Then
MsgBox "test"
End If
End Sub
Still cant get it to work but thats the fixed version.
-
Feb 20th, 2005, 07:44 AM
#7
Re: Simple Collision Detection
Can you upload the source, so I can have a look at it?
ØØ
-
Feb 20th, 2005, 07:51 AM
#8
Thread Starter
Hyperactive Member
Re: Simple Collision Detection
Here it is. It's fairly ghetto heh but it's the best I could manage . Ain't bad if i say so though.
Last edited by Phade; Feb 20th, 2005 at 08:11 AM.
-
Feb 20th, 2005, 07:58 AM
#9
Re: Simple Collision Detection
Ohhh....I can't belive I didn't see this right away.... ....you are not moving the picture box. You are drawing it somewhere else...
You are actualy drawing it at:
iX, iY
and
iXbook, iYbook
so that is your character.left and so on. The widt can MAYBE still be character.width and height, but unsure. But at least change the left and top values with iX and iY, and so on.
ØØ
-
Feb 20th, 2005, 08:03 AM
#10
Thread Starter
Hyperactive Member
Re: Simple Collision Detection
There we go 
If iX + Character.Width >= iXbook And iX <= iXbook + book.Width And iY + Character.Height >= iYbook And iY <= iYbook + book.Height Then
MsgBox "test"
End If
Last edited by Phade; Feb 20th, 2005 at 08:08 AM.
-
Feb 20th, 2005, 08:07 AM
#11
Re: Simple Collision Detection
yeah sure.
In your collision detection code you are testing too see if the two pictureboxes are hittin each other. But you never move them.
You are moving two variables called iX and iY and iXBook and iYBook. And then you are drawing a picture of what is INSIDE the pictureboxes on the outside of it somewhere on the form. That way the pictureboxes will never move, and there for never collide.
So in your collision rutine you have to check on the variables iX and iY, and iXBook and iYBook in stead of Character.Left, Character.Height, book.Left and book.Top, since you are never actualy moving the picture box.
Tell me if that made some sense. If not, then tell me, so I can make you a sample app.
ØØ
-
Feb 20th, 2005, 08:10 AM
#12
Thread Starter
Hyperactive Member
Re: Simple Collision Detection
Makes sense. I also got it working thanks for the help.
-
Feb 20th, 2005, 08:15 AM
#13
Re: Simple Collision Detection [RESOLVED]
No problem. Just glad to be able to help.
Ohhh and BTW, not sure if you have noticed, but we are adding the green check mark to the first post now to mark our threads resolved... ...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|