1

I've found this code here: Per-Pixel collision code

        /// <summary>
        /// Determines if there is overlap of the non-transparent pixels
        /// between two sprites.
        /// </summary>
        /// <param name="rectangleA">Bounding rectangle of the first sprite</param>
        /// <param name="dataA">Pixel data of the first sprite</param>
        /// <param name="rectangleB">Bouding rectangle of the second sprite</param>
        /// <param name="dataB">Pixel data of the second sprite</param>
        /// <returns>True if non-transparent pixels overlap; false otherwise</returns>
        static bool IntersectPixels(Rectangle rectangleA, Color[] dataA,
                                    Rectangle rectangleB, Color[] dataB) {
            // Find the bounds of the rectangle intersection
            int top = Math.Max(rectangleA.Top, rectangleB.Top);
            int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
            int left = Math.Max(rectangleA.Left, rectangleB.Left);
            int right = Math.Min(rectangleA.Right, rectangleB.Right);

            // Check every point within the intersection bounds
            for (int y = top; y < bottom; y++) {
                for (int x = left; x < right; x++) {
                    // Get the color of both pixels at this point
                    Color colorA = dataA[(x - rectangleA.Left) +
                                         (y - rectangleA.Top) * rectangleA.Width];
                    Color colorB = dataB[(x - rectangleB.Left) +
                                         (y - rectangleB.Top) * rectangleB.Width];

                    // If both pixels are not completely transparent,
                    if (colorA.A != 0 && colorB.A != 0) {
                        // then an intersection has been found
                        return true;
                    }
                }
            }

            // No intersection found
            return false;
        }

However, it accepts an array of pixel data, extracted from a Texture2D object. I'm not using XNA for my project, just plain forms, so I would like to know how to convert it to accept two bitmaps as input instead of two arrays of pixel data. I've tried some approaches myself but they've all failed, and I kept getting out of bounds exceptions.

Any advice is appreciated!

Thanks in advance!

Community
  • 1
  • 1
Qub1
  • 1,154
  • 2
  • 14
  • 31
  • I think you will have a much easier time extracting the pixel data out of two bitmap files and using them as input. – JoeManiaci Dec 22 '14 at 15:40
  • @JoeManiaci yeah but I'm not sure in what format to extract them, let alone what functions to use. Any advice? – Qub1 Dec 22 '14 at 15:42
  • Well you're going to have to dig knee deep into bitmap storage. For example, the bitmaps my company use store four different layers of color, CYMK, that are assembled together to produce a single image. Whereas most bitmaps are probably RGB. Some even use 8 different colors. http://www.fileformat.info/mirror/egff/ch03_04.htm – JoeManiaci Dec 22 '14 at 15:45
  • Seeing that you're in C#, it looks like you could create a new System.Drawing.Bitmap from a file, convert it into a GDI object, and then create a rectangle. Maybe. – JoeManiaci Dec 22 '14 at 16:08

0 Answers0