Ejemplo n.º 1
0
        /// <summary>
        /// Perfect pixel test collision
        /// </summary>
        /// <param name="entityA">Sprite 1</param>
        /// <param name="entityB">Sprite 2</param>
        /// <returns></returns>
        public static bool PerfectPixelCollide(IColladable2PerfectPixel entityA, IColladable2PerfectPixel entityB)
        {
            int top    = Math.Max(entityA.Rectangle.Top, entityB.Rectangle.Top);
            int bottom = Math.Min(entityA.Rectangle.Bottom, entityB.Rectangle.Bottom);
            int left   = Math.Max(entityA.Rectangle.Left, entityB.Rectangle.Left);
            int right  = Math.Min(entityA.Rectangle.Right, entityB.Rectangle.Right);

            for (int y = top; y < bottom; y++)     // De haut en bas
            {
                for (int x = left; x < right; x++) // de gauche à droite
                {
                    int index_A = (x - entityA.Rectangle.Left) + (y - entityA.Rectangle.Top) * entityA.Rectangle.Width;
                    int index_B = (x - entityB.Rectangle.Left) + (y - entityB.Rectangle.Top) * entityB.Rectangle.Width;

                    Color[] colorsSpriteA = YnGraphics.GetTextureColors(entityA.Texture);
                    Color[] colorsSpriteB = YnGraphics.GetTextureColors(entityB.Texture);

                    Color colorSpriteA = colorsSpriteA[index_A];
                    Color colorSpriteB = colorsSpriteB[index_B];

                    if (colorSpriteA.A != 0 && colorSpriteB.A != 0)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Perfect pixel test collision
        /// </summary>
        /// <param name="entityA">Sprite 1</param>
        /// <param name="entityB">Sprite 2</param>
        /// <returns></returns>
        public static bool PerfectPixelCollide(IColladable2PerfectPixel entityA, IColladable2PerfectPixel entityB)
        {
            int top = Math.Max(entityA.Rectangle.Top, entityB.Rectangle.Top);
            int bottom = Math.Min(entityA.Rectangle.Bottom, entityB.Rectangle.Bottom);
            int left = Math.Max(entityA.Rectangle.Left, entityB.Rectangle.Left);
            int right = Math.Min(entityA.Rectangle.Right, entityB.Rectangle.Right);

            for (int y = top; y < bottom; y++)  // De haut en bas
            {
                for (int x = left; x < right; x++)  // de gauche à droite
                {
                    int index_A = (x - entityA.Rectangle.Left) + (y - entityA.Rectangle.Top) * entityA.Rectangle.Width;
                    int index_B = (x - entityB.Rectangle.Left) + (y - entityB.Rectangle.Top) * entityB.Rectangle.Width;

                    Color[] colorsSpriteA = YnGraphics.GetTextureColors(entityA.Texture);
                    Color[] colorsSpriteB = YnGraphics.GetTextureColors(entityB.Texture);

                    Color colorSpriteA = colorsSpriteA[index_A];
                    Color colorSpriteB = colorsSpriteB[index_B];

                    if (colorSpriteA.A != 0 && colorSpriteB.A != 0)
                        return true;
                }
            }
            return false;
        }