Results 1 to 9 of 9

Thread: Collision Detection on Transparent PictureBox Images...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Collision Detection on Transparent PictureBox Images...

    I am able to change a color of a PictureBox to transparent. I am wondering how I can detect when it collides with other PictureBox controls that also have certain parts of them transparent.

    Below, you'll see he passes behind the first tree but crosses the second.
    Name:  walker1.gif
Views: 926
Size:  143.4 KB
    I would like for him to stop at (collide with) the second tree, but only at the "trunk" of it.

    Thank You.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Collision Detection on Transparent PictureBox Images...

    This isn't just simple collision testing, it appears you want 3D collision testing?

    Is that correct? If so, I'll monitor your thread for curiosity, but 3D math makes my head hurt & there are some modules/projects out & about for such things; maybe even in our games/graphics portion of the forum?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Collision Detection on Transparent PictureBox Images...

    You can make a User Control that will allow you to detect the visible image part of a transparent image instead of the entire boundary box the image is in. Using Image Controls or Pictureboxes will not give you this feature that I know of but a UC can do this without a lot of overhead coding. In addition, you can have the UC image show Z Order to get your walking man show behind and in front of other images.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: Collision Detection on Transparent PictureBox Images...

    I have the Z-Order down. It's recognizing when the transparent images inside two PictureBox controls collide with each other... not just the boxes themselves. And I didn't think I was doing any 3D. I've seen some posts about region intersecting, but couldn't fully understand it.

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Collision Detection on Transparent PictureBox Images...

    I don't know but I think pixel collisions in this situation is kind of not meaningful. You don't really have the perspective needed, and of course there are parts of the images that should be allowed to "collide" because even thought the images collide visually in the 2d projection, the objects are really in front or behind the object if you had 3d information.

    In my case, what I've done is essentially associated a 2d rectangle with each object that represents a collision region based on the ground contact point of the objects.
    I don't know how you are positioning your pictureboxes and what point you are using for reference, but in the case of the man which looks like a straight down from overhead I might have a 5 pixel by 10 pixel rectangle offset from your picturebox location to align with an area around where his feet "contact" the ground, and aligned long ways across the body based on direction of movement.
    Likewise, the tree would have a rectangle assigned that is as wide as the trunk and aligned with it, and vertically would extend up or down as far as you want the collision zone to be, to represent the area around where the tree contacts the ground.
    Now you do a simple rectangle intersection between the two. If they intersect, you stop the man moving and move him back to where they don't intersect.
    You also test the "ground contact" point of the man against the "ground contact" point of the tree to determine whether the man is drawn first (passing behind the tree) or drawn second (passing in front of the tree).
    If you're moving pictureboxes, that would mean changing the zorder and that seems like a challenge. It would seem that at the begining of a frame you would have to go through all the pictureboxes to reorder them in Z-order since you can't just move a control up or down the z-order, but can only move to the top or bottom.
    You would have to loop through them all in needed "ground contact" order and set them to one end of the z-order so they end up in the order (back to front) you need to allow the man to move in front of some objects and behind others.
    Last edited by passel; Mar 14th, 2015 at 10:32 AM.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: Collision Detection on Transparent PictureBox Images...

    Passel: Thank you. I have thought about that and actually tried it last night a 4am when I couldn't sleep. I do understand that the "man" is top-down, whilst the trees are not so much, but whether I used top-down or the latter, the issue would still arise if I were to use, say, a house, which isn't as slender as a tree might be. In which case I would just use, as you stated, a simple rectangle intersection.

    But if the house were to look like this:
    Name:  topdown.jpg
Views: 542
Size:  3.5 KB

    ...and I needed the "man" to move inside the house in the middle where the indention in the house is (using a single PictureBox), that is what I am attempting to perform. Of course, I could just split the house into sections and just catch the intersecting rectangles that way, but there has to be a way to "reshape" a PictureBox (possibly using SetRectRgn, which I'm not familiar with) and then use an algorithm to find out if a control crosses those newly-shaped regions.

    I realize this is a lot to take on, but if this can be done I'm sure others would benefit from a short coded routine to make it happen. I think I'm on the right path with the SetRectRgn, but I can't be sure.

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Collision Detection on Transparent PictureBox Images...

    2D intersecting logic is fairly straight-forward (after one gets their head around it)

    1st step: test intersection of overall image bounds, i.e., rectangles. If the rectangles don't intersect, then the shaped images can't intersect. Rectangular intersection is very fast to determine

    Additional steps depend on desired accuracy and complexity of the image.

    If the image is a basic shape that can be described with points, i.e., polygon, then there are polygon collision routines that are pretty darn quick

    If the image is complex, one option is to use regions. A region is created from the image (possibly current frame if image shape changes quite a bit from frame to frame) and also from the collision object determined from step #1. Now use favorite region collision routine. One way is to combine the 2 regions to see if there is an intersection region. If there is, then a collision occurred.

    The above are generalities. To piggyback on Passel's comments. Haven't really seen a good, responsive, game where windows are used for animation & objects. Usually bitmaps are used, updated as needed, then painted to the game surface

    Edited: As I suggested earlier & if not done already, you may want to spend time in the Games and Graphics Programming section looking at results from a search on the keyword: Collision. Why re-invent the wheel if not needed? You may get ideas you are more comfortable with and haven't yet considered.
    Last edited by LaVolpe; Mar 14th, 2015 at 11:02 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Collision Detection on Transparent PictureBox Images...

    Quote Originally Posted by Conroy Vanderbluff View Post
    I have the Z-Order down. It's recognizing when the transparent images inside two PictureBox controls collide with each other... not just the boxes themselves. And I didn't think I was doing any 3D. I've seen some posts about region intersecting, but couldn't fully understand it.
    That's why I suggested a UC because it allows you to detect the image inside the box


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: Collision Detection on Transparent PictureBox Images...

    jms: Care to elaborate?

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