Results 1 to 12 of 12

Thread: Collision detection with a control array [SOLVED]

  1. #1

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692

    Collision detection with a control array [SOLVED]

    Is there any way to find out which box in the array the ball hits, and make that specific box invisible?

    I tried loops, but that was disastrous!!
    Last edited by hothead; Oct 14th, 2003 at 05:38 PM.

  2. #2

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    Whoops, I forgot my code. THis is what I tried:



    VB Code:
    1. For i = 0 To RedBox.Count - 1
    2.     If Sprite.X = Round(RedBox(i).Left, 0) And Sprite.Y = Round(RedBox(i).Top, 0) Then
    3.         RedBox(i).Visible = False
    4.     End If
    5. Next i
    Last edited by hothead; Oct 13th, 2003 at 05:58 AM.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    your code assumes that neither the sprite or the controls have any width/height - so the top left co-ordinates of the sprite must be the same as the control. Try this instead:
    VB Code:
    1. For i = 0 To RedBox.Count - 1
    2.     If Sprite.X >= Round(RedBox(i).Left, 0) _
    3.     And (Sprite.X + Sprite.Width) <= Round(RedBox(i).Left + RedBox(i).Width, 0) Then
    4.         If Sprite.Y >= Round(RedBox(i).Top, 0) _
    5.         And (Sprite.Y + Sprite.Height) <= Round(RedBox(i).Top + RedBox(i).Height, 0) Then
    6.             RedBox(i).Visible = False
    7.         End If
    8.     End If
    9. Next i

  4. #4

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    No bricks disappear at all when I try that code.

    I'll have to try again later, I'm pressed for time right now.

  5. #5
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Not sure about the code above, so i'll post how i do it.

    BTW, by using seperate if statements the program *should* be slightly faster.

    This code assumes the coordinates of the sprite is top left, not the middle.
    VB Code:
    1. For i = 0 to Ubound(RedBox)
    2.     If Sprite.X <= RedBox(i).Left + RedBox(i).Width Then
    3.         If Sprite.X + Sprite.Width >= RedBox(i).Left Then
    4.             If Sprite.Y <= RedBox(i).Top + RedBox(i).Height Then
    5.                 If Sprite.Y + Sprite.Height >= RedBox(i).Top Then
    6.                     'Collision
    7.                 End If
    8.             End If
    9.         End If
    10.     End If
    11. Next i

    Also this attached project will probably help (well maybe).
    Attached Files Attached Files
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  6. #6

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    For the record, I'm not using anything to handle the sprites, the balls are all created via a drawing, some BitBlt code, and double buffering. I know the exact height and width of the ball also. It was much easier to do it this way rather than using a picture box.

    Therefore, your Sprite.X + Sprite.Width and Sprite.Y + Sprite.Height codes are invalid. Use Sprite.X + 16 or Sprite.Y + 16.

    Just letting you know for future reference.

    Anyways, your code works fine (I had to edit it a bit to accomodate floating-point numbers, otherwise the program goes crazy and starts erasing every brick in the entire row), except for one thing: It works great on some bricks, but also leaves others behind. Then when the ball comes back around, it'll delete the bricks when it's nowhere near them.

    Here's what the code looks like now.

    VB Code:
    1. Private Function CheckForCollisions()
    2.     For i = 0 To RedBrick.Count - 1
    3.         If Sprite.X <= Round(RedBrick(i).Left, 0) + Round(RedBrick(i).Width, 0) Then
    4.             If Sprite.X + 2 >= Round(RedBrick(i).Left, 0) Then
    5.                 If Sprite.Y <= Round(RedBrick(i).Top, 0) + Round(RedBrick(i).Height, 0) Then
    6.                     If Sprite.Y + 2 >= Round(RedBrick(i).Top, 0) Then
    7.                         RedBrick(i).Visible = False
    8.                     End If
    9.                 End If
    10.             End If
    11.         End If
    12.     Next i
    13. End Function

    Should I try messing around with the settings a bit?
    Last edited by hothead; Oct 13th, 2003 at 01:42 PM.

  7. #7
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    im working on a ball game right now and the collition code works fine!

    here's the code:
    Attached Files Attached Files
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  8. #8
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Here's my take on the classic:
    Attached Files Attached Files

  9. #9
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Originally posted by cyborg
    im working on a ball game right now and the collition code works fine!

    here's the code:
    Nice ripple effects. It'd be cooler in DX though.


  10. #10
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    well...i've just started on the game, so who knows what i'll add....

    but it runs pretty smooth though...100 fps with 1000 balls moving with a 900mhz cpu
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  11. #11

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    DOH!!

    I nosed around your code digitalerror, and found one important thing: I didn't have the ScaleMode set to 3 - Pixel. I'm not used to doing that, as I only recently got into game programming.

    I switched it, and everything works great now. Thanks a lot!

    BTW, your game is flippin' fantastic, I couldn't stop playing it!

  12. #12

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    One more thing DigitalError.

    Could you explain what some of these variables are? I can't seem to get the bounce codes working correctly without an understanding of them.

    I need explanations on the following variables:

    BlR
    bRad
    BaB

    Thanks in advance.

    Oh, and since I'm using your code, would you like me to do anything special, like put your name in the credits or something?

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