WPF图像科里森检测 [英] WPF Image Collison Detection

查看:109
本文介绍了WPF图像科里森检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,检测碰撞;

I have some code which detects collision ;

public bool DetectCollision(ContentControl ctrl1, ContentControl ctrl2)
{

    Rect ctrl1Rect = new Rect(
        new Point(Convert.ToDouble(ctrl1.GetValue(Canvas.LeftProperty)),
                  Convert.ToDouble(ctrl1.GetValue(Canvas.TopProperty))),
        new Point((Convert.ToDouble(ctrl1.GetValue(Canvas.LeftProperty)) + ctrl1.ActualWidth),
                  (Convert.ToDouble(ctrl1.GetValue(Canvas.TopProperty)) + ctrl1.ActualHeight)));

    Rect ctrl2Rect = new Rect(
        new Point(Convert.ToDouble(ctrl2.GetValue(Canvas.LeftProperty)),
                  Convert.ToDouble(ctrl2.GetValue(Canvas.TopProperty))),
        new Point((Convert.ToDouble(ctrl2.GetValue(Canvas.LeftProperty)) + ctrl2.ActualWidth),
                  (Convert.ToDouble(ctrl2.GetValue(Canvas.TopProperty)) + ctrl2.ActualHeight)));

    ctrl1Rect.Intersect(ctrl2Rect);
    return !(ctrl1Rect == Rect.Empty);
}



据检测,当2矩形已经结束了。有在给定的参数ContentControls图像。我希望能够检测是否这些图像相交不是rectangels。下面的图像显示whatn我想;

It detects when 2 rectangles are over. There are images in the given parameter ContentControls. I want to be able to detect if those images intersects not the rectangels. Following images shows whatn I want ;

推荐答案

那你是不是在找矩形碰撞检测,但实际上像素级的碰撞检测,并且将是更加密集的处理。

Then you are not looking for rectangular collision detection but actually pixel-level collision detection and that is going to be much more processing intensive.

在你已经实现了您必须检查在重叠矩形区域两个图像的每个像素的矩形碰撞检测的顶部。

On top of the rectangular collision detection that you already have implemented you will have to examine each pixel of both images in the overlapping rectangular region.

在简单的情况下,如果同时有两个重叠像素具有非透明的颜色,那么你有一个碰撞。

In the simplest case, if both of two overlapping pixels have non transparent color then you have a collision.

如果你想使事情变得复杂您可能要添加的阈值,如:要求以触发碰撞重叠像素的百分比;或设定的像素的组合的α电平的阈值,而不是使用任何非零值

If you want to complicate things you may want to add thresholds such as: requiring a percentage of overlapping pixels in order to trigger a collision; or setting a threshold for the combined alpha level of the pixels instead of using any non zero value.

这篇关于WPF图像科里森检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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