Esempio n. 1
0
        //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;
        }
Esempio n. 2
0
        //Main method to check for a collision between two graphics objects
        public bool CheckCollisions(GraphicsObject a, GraphicsObject b)
        {
            //transforms the rectangles which surrounds each sprite
            Matrix aTransform = Transform(a.Center, 0.0f, a.Position);
            Rectangle aRectangle = TransformRectangle(aTransform, a.Width, a.Height);

            Matrix bTransform = Transform(b.Center, 0.0f, b.Position);
            Rectangle bRectangle = TransformRectangle(bTransform, b.Width, b.Height);

            // collision checking
            if (aRectangle.Intersects(bRectangle))
            {// rough collision
                if (PixelCollision(a, b, aTransform, a.Width, a.Height,
                                   bTransform, b.Width, b.Height))
                    return true;  //What to do if there is a collision - in this case, stop the movement
            }
            return false;
        }