//Checks to see if the pixels overlap in two graphicsObjects private static bool PixelCollision( GraphicsObject a, GraphicsObject b, Matrix transformA, int pixelWidthA, int pixelHeightA, Matrix transformB, int pixelWidthB, int pixelHeightB) { //set A transformation relative to B. B remains at (0,0) Matrix atoB = transformA * Matrix.Invert(transformB); //Generate normal vectors to each rectangle's side Vector2 columnStep = Vector2.TransformNormal(Vector2.UnitX, atoB); Vector2 rowStep = Vector2.TransformNormal(Vector2.UnitY, atoB); //Calculate the top left corner of A Vector2 rowStartPosition = Vector2.Transform(Vector2.Zero, atoB); // Search each row of pixels in A. Start at the top and move down for (int rowA = 0; rowA < pixelHeightA; rowA++) { //begin at the left Vector2 pixelPositionA = rowStartPosition; // for each column in the row (move left to right) for (int colA = 0; colA < pixelWidthA; colA++) { // get the pixel position var x = (int)Math.Round(pixelPositionA.X); var y = (int)Math.Round(pixelPositionA.Y); //if the pixel is within the bounds of B if (x >= 0 && x < pixelWidthB && y >= 0 && y < pixelHeightB) { //Get colors of overlapping pixels Color colorA = a.GetPixelColor(colA + rowA * pixelWidthA); Color colorB = b.GetPixelColor(x + y * pixelWidthB); //If both pixels are not completely transparent if (colorA.A != 0 && colorB.A != 0) return true; // Colision! } // move to the next pixel in the row of A pixelPositionA += columnStep; } // move to the next row of A rowStartPosition += rowStep; } return false; }