-
Mar 13th, 2015, 05:52 PM
#1
Thread Starter
Addicted Member
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.
I would like for him to stop at (collide with) the second tree, but only at the "trunk" of it.
Thank You.
-
Mar 13th, 2015, 08:58 PM
#2
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?
-
Mar 14th, 2015, 01:18 AM
#3
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.
-
Mar 14th, 2015, 04:48 AM
#4
Thread Starter
Addicted Member
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.
-
Mar 14th, 2015, 09:54 AM
#5
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.
-
Mar 14th, 2015, 10:10 AM
#6
Thread Starter
Addicted Member
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:
...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.
-
Mar 14th, 2015, 10:48 AM
#7
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.
-
Mar 14th, 2015, 12:05 PM
#8
Re: Collision Detection on Transparent PictureBox Images...
Originally Posted by Conroy Vanderbluff
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.
-
Mar 14th, 2015, 12:24 PM
#9
Thread Starter
Addicted Member
Re: Collision Detection on Transparent PictureBox Images...
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|