每像素碰撞码 [英] Per-Pixel collision code

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

问题描述

我试图了解,检查游戏中的两个物体之间碰撞的代码 - 每个像素的碰撞。该代码是:

I tried to understand the code that checks the collision between two objects in the game - per pixel collision. The code is:

/// <summary>
/// Determines if there is overlap of the non-transparent pixels
/// between two sprites.
/// </summary>
/// <param name="rectangleA">Bounding rectangle of the first sprite</param>
/// <param name="dataA">Pixel data of the first sprite</param>
/// <param name="rectangleB">Bouding rectangle of the second sprite</param>
/// <param name="dataB">Pixel data of the second sprite</param>
/// <returns>True if non-transparent pixels overlap; false otherwise</returns>
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 found some explanations but no one explained the last lines:

// If both pixels are not completely transparent,
if (colorA.A != 0 && colorB.A != 0)
{
    // then an intersection has been found
    return true;
}



这是什么 colorA.A 属性?为什么这个if语句确定碰撞发生?

what is this colorA.A property? why this if-statement determine that a collision occured?

推荐答案

Color.A 属性是阿尔法值的颜色的。 0 表示完全透明,而 255 表示完全不透明的。

The Color.A property is the alpha-value of the color. 0 means completely transparent, and 255 means completely opaque.

如果对颜色之一的完全是在指定的像素透明,不存在冲突(因为对象之一,是不是在这个地方,但只有它的边框)。

If one of the colors is completely transparent at the specified pixel, there is no collision (because one of the objects is not at this place, but only its bounding box).

只有两个都是!= 0 ,居然有在同一地点的两个对象和碰撞已被处理。

Only if both of them are != 0, there actually are two objects at the same place and a collision has to be handled.

例如,见这一形象:

See for example this image:

的边框(黑色矩形)相交,但是你不会认为这是红色和黄色的圆圈之间的碰撞。

The bounding boxes (black rectangles) intersect, but you would not consider it to be a collision between the red and yellow circle.

因此,你必须在交叉像素检查的颜色。如果它们是透明的(白色在这个例子中),圆圈本身并不相交。

Therefore, you have to check the color at the intersecting pixels. If they are transparent (white in this example), the circles themselves are not intersecting.

这就是为什么你必须检查对象的颜色是透明的。如果其中之一是透明的,对象本身不相交的其他对象,只有其边界框

This is why you have to check if the colors of the objects are transparent. If one of them is transparent, the object itself is not intersecting the other object, only its bounding box.

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

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