该代码如何执行“每像素碰撞检测”?

发布于 2024-12-02 16:19:10 字数 1315 浏览 1 评论 0 原文

我目前正在尝试了解每像素碰撞检测。

这是我不明白的代码:

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;
}

我真的不明白这些嵌套循环。我很高兴能得到一些关于它如何工作的解释。

I'm currently trying to understand per pixel collision detection.

This is the code I don't understand:

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;
}

I really don't understand these nested loops. I'll be glad for some explanation about how it works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蹲在坟头点根烟 2024-12-09 16:19:10

首先,它找到两个图像矩形相交的区域,然后迭代该区域中的每个像素,并比较每个像素的每个图像的 alpha 值。如果两者的 alpha 值都不为 0,则它们都被视为“实体”,因此会发生碰撞。

在此处输入图像描述

First up, it finds the region the two image rectangles intersect, then it iterates through each pixel in that region, and compares the alpha values of each image of each pixel. If neither has an alpha value of 0, they are both considered 'solid' and therefore colliding.

enter image description here

╰沐子 2024-12-09 16:19:10

这并不难(在本例中)。您为算法提供对象的两个边界框(因此整个对象都在其框内),以及一个包含它们颜色信息的数组。
如果一个点不透明,该算法就假定该点属于该对象。这很重要。

第一步是计算相交矩形。如果像本例那样将两个边平行于轴的矩形相交,您将再次得到一个矩形,否则会得到一个空集。

下一步是在这个相交矩形中迭代内部的所有 (x,y) 坐标,首先是 y,然后是 x,这样你就得到了正常的第一个 x,然后是 y,但这是一个小问题,并不重要。

最后,算法获得对象 A 和 B 在当前像素 (x,y) 处的颜色。如果两种颜色都不透明,则像素位于两个对象中,并且对象必须在这一点相交。然后算法以“是的,它们相交”结束。

如果检查了边界框相交处的所有像素并且没有发现公共(例如,非透明)像素,则对象不相交,因此算法以“不,它们不相交”终止。

我希望这有帮助。

It's not that hard (in this case). You give the algorithm the two bounding-boxes of your objects (so the whole object is inside its box), and an array with color-information for them.
The algorithm assumes that a point belongs to the object if it is not transparent. This is important.

The first step is to calculate the intersecting rectangle. If you intersect two rectangles that have sides parallel to the axes like in this case, you will get a rectangle again or else an empty set.

The next step is to iterate in this intersecting rectangle for all (x,y) coordinates inside, first y, then x, so you get your normal first x, then y inside, but this is a minor point and not important.

Then finally the algorithm gets the color for objects A and B at the current pixel (x,y). If both colors are NOT transparent then the pixel is in both objects and the objects have to intersect at this point. Then the algorithm terminates with "YES they intersect".

If all pixels in the intersection of the bounding boxes were checked and no common (e.g. non-transparent) pixel was found the objects don't intersect and so the algorithm terminates with "NO they don't intersect".

I hope this helps.

被你宠の有点坏 2024-12-09 16:19:10

for (int y = top; y 从上到下循环生成的矩形的线条,而 for (int x = left; x 从左到右循环遍历每行内的像素。

for (int y = top; y < bottom; y++) loops over the lines of the resulting rectangle from top to bottom, and for (int x = left; x < right; x++) loops over pixels inside each line, left to right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文