Domanda

I am creating a game where I want to check if a picturebox is intersecting with a rectangle. I have got this working with .IntersectsWith(pbxPlayer.Bounds) but there is a large area of the image that is transparent, this means the hit detection in my game isn't very good. Is there a way I can check if something intersects with just the pixels in the image no the whole picturebox?

È stato utile?

Soluzione

Pixel-perfect collision detection can be slow, so many 2D games elect to use bounding boxes like you've already done. To improve accuracy you could use multiple bounding objects (e.g. more boxes, circles, or convex polygons) which resemble the shape of opaque regions in your bitmaps.

If you still want pixel-perfect collision detection you will need to iterate over all screen coordinates in the intersection area (i.e. the rectangular intersection of two bounding boxes). A collision is detected when an opaque pixel is found for both bitmaps in the same location.

Note that the Bitmap.GetPixel method is slow and probably not a good choice for real-time collision detection. You should create a mask for each of your bitmaps which represents the opaque pixels only, and then use the mask for collision detection. The mask could be a simple 2D array of booleans, one element for each pixel in the bitmap.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top