CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2012
    Posts
    3

    Question Need help with object collision

    Hi all,

    I am new to the forums and I am not quite sure how to approach the problem I have at the moment. I currently have a control array of 60 Image boxes. These image boxes cannot overlap each other. They stop when one collides into the other.

    The image boxes are named Image1(0), Image1(1), Image1(2).....Image1(60). I know how to write the programming when something else hits it, but not when they hit each other. For example, this is what I have when a DIFFERENT image box hits it from the top, the other image box being FallImage(0):

    For Index = 0 to 60
    If FallImage(0).Top + FallImage(0).Height > Image1(Index).Top And FallImage(0).Left + FallImage(0).Width > Image1(Index).Left And FallImage(0).Top < Image1(Index).Top + Image1(Index).Height And FallImage(0).Left < Image1(Index).Left + Image1(Index).Height Then
    Timer4.Enabled = False
    End If
    Next


    So if anyone has any ideas as to how I can get that same effect with just Image1 I would very much appreciate it.

    Thanks

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Need help with object collision

    The trick here is to check each item against the others...

    To do so one uses a nested loop...

    Code:
    For IndexA = 0 to 59
        For IndexB = IndexA to 60
            'Check for collisions
        Next
    Next
    However you also need to optomise your collision detection...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    May 2012
    Posts
    3

    Re: Need help with object collision

    Quote Originally Posted by GremlinSA View Post
    The trick here is to check each item against the others...

    To do so one uses a nested loop...

    Code:
    For IndexA = 0 to 59
        For IndexB = IndexA to 60
            'Check for collisions
        Next
    Next
    However you also need to optomise your collision detection...
    Thanks. I am new to Visual Basic so I may be wrong but I think I see a problem with your nested loop. For example, lets say IndexA equals 30. Then IndexB can only be a number in between 30 to 60. How is it meant to check for collision between images 0 to 30?

    Like I said, I'm new so I may be wrong so feel free to prove me otherwise. And yes my collision detection is only for an image box colliding from the top. I have 3 other collision detectors coming from the other 3 sides as well as another few lines of programming that readjusts the position of the image boxes so that they are exactly side by side once they collide.

    Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Need help with object collision

    Use VB.Net. Each ImageBox can be a class that has the same properties inherited. The MovingBox can have Collision Detection Rules defined ONCE (where you can change them ALL).

    Games usually run on a Game Loop. 60 FPS, so you automatically get a loop
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Need help with object collision

    Thanks. I am new to Visual Basic so I may be wrong but I think I see a problem with your nested loop. For example, lets say IndexA equals 30. Then IndexB can only be a number in between 30 to 60. How is it meant to check for collision between images 0 to 30?
    Well the basic's of this type of code is what difference is there in checking #1 to #4 and then #4 back to #1 .. .surely both collision checks should return true...

    So while IndexA is between 0 and 29, IndexB will at sometime be 30 and the checking will be done...

    The trick here is to do all the tests within this one loop.. Also it's preferred if you try to optomise your calculations to speed things up... Collision detection can really slow things down...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

Tags for this Thread

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

Featured