每像素碰撞-代码说明 [英] Per Pixel Collision - Code explanation

查看:56
本文介绍了每像素碰撞-代码说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我不明白的代码:

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 haven't understood the all loop. I'll be glad for some explanation how it works.

推荐答案

并不难(在这种情况下)-您为算法提供了对象的两个边界框(因此,孔对象位于此框内),以及一个带有颜色信息的数组.Tha算法假定一个点属于对象IFF,它不是透明的-这很重要.

it's not that hard (in this case) - you give the algorithm the two bounding-boxes of your objects (so the hole object is inside this box), and a array with color-information for them. Tha algorithm assumes that a point belongs to the object IFF 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 an empty set.

下一步是对所有(x,y)-坐标insiede-先是y,然后是x-进行迭代,以便在此相交矩形中进行迭代,因此先得到普通的x,然后是y,但这是次要点,并不重要

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

然后,最后算法在当前像素(x,y)上获取对象A和对象B的颜色-如果两种颜色都不透明,则像素在两个对象中,并且对象必须在这一点相交-因此,算法以是,它们相交"结束

Then finally the algorithm gets the color for object 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 - so the algorithm terminates with "YES they intersect"

如果选中了边界框相交处的所有像素,但未发现任何普通(例如,不透明)像素,则该对象不相交,因此算法以否,它们不相交"结束

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

我希望这会有所帮助.

这篇关于每像素碰撞-代码说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆