Results 1 to 13 of 13

Thread: Simple Collision Detection [RESOLVED]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Texas
    Posts
    299

    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.

  2. #2
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Simple Collision Detection

    Something like this should work:

    VB Code:
    1. 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
    2.     'Collition
    3. End If

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Texas
    Posts
    299

    Re: Simple Collision Detection

    VB Code:
    1. Private Sub collision_Timer()
    2. 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
    3.     MsgBox "test"
    4. End If
    5.  
    6. End Sub

    Doesn't work. Did i do something wrong?

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Texas
    Posts
    299

    Re: Simple Collision Detection

    Heres the full code if it helps.

    VB Code:
    1. 'The key array that shows true if the key is down, and false if not
    2. Dim KeyArray(100) As Boolean
    3.  
    4. Dim iX As Integer           'The X coordinate for the Character
    5. Dim iY As Integer           'The Y coordinate for the Character
    6. Dim iXbook As Integer           'The X coordinate for the book
    7. Dim iYbook As Integer           'The Y coordinate for the book
    8.  
    9. Private Sub collision_Timer()
    10. 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
    11.     MsgBox "test"
    12. End If
    13. End Sub
    14.  
    15. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    16.     'Marks the element in the array as true if it the key is pushed
    17.        
    18.     KeyArray(KeyCode) = True
    19. End Sub
    20.  
    21. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    22.  
    23.     'Markes the element in the array as false if it the key is released
    24.     KeyArray(KeyCode) = False
    25.    
    26. End Sub
    27.  
    28. Private Sub Form_Load()
    29.     'Setting the starting values of the X,Y coordinates of the Character
    30.     iX = Char.Left
    31.     iY = Char.Top
    32.     iXbook = book.Left
    33.     iYbook = book.Top
    34.    
    35.     'Setting some properties for the Form,
    36.     'and also painting the Character
    37.     With Form1
    38.         .AutoRedraw = True
    39.         .KeyPreview = True
    40.         .PaintPicture Char.Picture, iX, iY
    41.     End With
    42.  
    43. collision.Interval = "10"
    44. collision.Enabled = True
    45. GameLoop.Interval = "10"
    46. GameLoop.Enabled = True
    47. End Sub
    48.  
    49. Private Sub GameLoop_Timer()
    50. If KeyArray(39) = True Then         'Character moves right
    51.       iX = iX + 25
    52.       Char.Picture = charright.Picture
    53.     End If
    54.     If KeyArray(37) = True Then     'Character moves left
    55.       iX = iX - 25
    56.       Char.Picture = charleft.Picture
    57.     End If
    58.     If KeyArray(38) = True Then     'Character moves up
    59.       iY = iY - 25
    60.       Char.Picture = charup.Picture
    61.     End If
    62.     If KeyArray(40) = True Then     'Character moves down
    63.       iY = iY + 25
    64.       Char.Picture = chardown.Picture
    65.     End If
    66.    
    67.     'Clears the screen
    68.     Form1.Cls
    69.     'Paints the Character
    70.     Form1.PaintPicture Char.Picture, iX, iY
    71.     Form1.PaintPicture book.Picture, iXbook, iYbook
    72. End Sub

  5. #5
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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.



    ØØ

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Texas
    Posts
    299

    Re: Simple Collision Detection

    VB Code:
    1. Private Sub GameLoop_Timer()
    2. If KeyArray(39) = True Then         'Character moves right
    3.       iX = iX + 5
    4.       charrightmove.Interval = "60"
    5.       charrightmove.Enabled = True
    6.     End If
    7.     If KeyArray(37) = True Then     'Character moves left
    8.       iX = iX - 5
    9.       charleftmove.Interval = "60"
    10.       charleftmove.Enabled = True
    11.     End If
    12.     If KeyArray(38) = True Then     'Character moves up
    13.       iY = iY - 5
    14.       charupmove.Interval = "60"
    15.       charupmove.Enabled = True
    16.     End If
    17.     If KeyArray(40) = True Then     'Character moves down
    18.       iY = iY + 5
    19.       chardownmove.Interval = "60"
    20.       chardownmove.Enabled = True
    21.     End If
    22.     'Clears the screen
    23.     Form1.Cls
    24.     'Paints the Character
    25.     Form1.PaintPicture Character.Picture, iX, iY
    26.     Form1.PaintPicture book.Picture, iXbook, iYbook
    27.  
    28. 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
    29.     MsgBox "test"
    30. End If
    31.  
    32. End Sub

    Still cant get it to work but thats the fixed version.

  7. #7
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Simple Collision Detection

    Can you upload the source, so I can have a look at it?



    ØØ

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Texas
    Posts
    299

    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.

  9. #9
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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.



    ØØ

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Texas
    Posts
    299

    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.

  11. #11
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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.



    ØØ

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Texas
    Posts
    299

    Re: Simple Collision Detection

    Makes sense. I also got it working thanks for the help.

  13. #13
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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
  •  



Click Here to Expand Forum to Full Width