不规则图形的碰撞检测

简介: public static class CheckHit { public static bool CheckCollision(FrameworkElement control1, FrameworkElement controlElem1, FrameworkElement c...

public static class CheckHit
{
    public static bool CheckCollision(FrameworkElement control1, FrameworkElement controlElem1, FrameworkElement control2, FrameworkElement controlElem2)
    {
        // first see if sprite rectangles collide
        Rect rect1 = UserControlBounds(control1);
        Rect rect2 = UserControlBounds(control2);
        rect1.Intersect(rect2);
        if (rect1 == Rect.Empty)
        {
            // no collision - GET OUT!
            return false;
        }
        else
        {
            bool bCollision = false;
            Point ptCheck = new Point();
            // NOTE that creating the writeablebitmap is a bit intense
            // so we will do this once and store results in Tag property
            // in a real game, you might abstract this into a Sprite class.

            if (controlElem1 is Image)
                controlElem1.Tag = GetWriteableBitmap(control1);
            if (controlElem2 is Image)
                controlElem2.Tag = GetWriteableBitmap(control2);

            // now we do a more accurate pixel hit test

            for (int x = Convert.ToInt32(rect1.X); x < Convert.ToInt32(rect1.X + rect1.Width); x++)
            {
                for (int y = Convert.ToInt32(rect1.Y); y < Convert.ToInt32(rect1.Y + rect1.Height); y++)
                {
                    ptCheck.X = x;
                    ptCheck.Y = y;

                    if (CheckCollisionPoint(ptCheck, control1, controlElem1))
                        if (CheckCollisionPoint(ptCheck, control2, controlElem2))
                        {
                            bCollision = true;
                            break;
                        }
                }
                if (bCollision) break;
            }
            return bCollision;
        }
    }

    private static bool CheckCollisionPoint(Point pt, FrameworkElement control, FrameworkElement controlElem)
    {
        if (controlElem is Image)
        {
            // NOTE that we saved the WB in the Tag object for performance.
            // in a real app, you would abstract this in your sprite class.
            WriteableBitmap wb = controlElem.Tag as WriteableBitmap;
            int width = wb.PixelWidth;
            double offSetX = Convert.ToDouble(control.GetValue(Canvas.LeftProperty));
            double offSetY = Convert.ToDouble(control.GetValue(Canvas.TopProperty));
            pt.X = pt.X - offSetX;
            pt.Y = pt.Y - offSetY;
            int offset = Convert.ToInt32((width * pt.Y) + pt.X);
            return (wb.Pixels[offset] != 0);
        }
        else
        {

            List<UIElement> hits = System.Windows.Media.VisualTreeHelper.FindElementsInHostCoordinates(pt, controlElem) as List<UIElement>;
            return (hits.Contains(controlElem));
        }

    }

    private static WriteableBitmap GetWriteableBitmap(FrameworkElement control)
    {
        WriteableBitmap wb = new WriteableBitmap((int)control.Width, (int)control.Height);
        wb.Render(control, new TranslateTransform());
        wb.Invalidate();

        return wb;
    }

    private static Rect UserControlBounds(FrameworkElement control)
    {
        Point ptTopLeft = new Point(Convert.ToDouble(control.GetValue(Canvas.LeftProperty)), Convert.ToDouble(control.GetValue(Canvas.TopProperty)));
        Point ptBottomRight = new Point(Convert.ToDouble(control.GetValue(Canvas.LeftProperty)) + control.Width, Convert.ToDouble(control.GetValue(Canvas.TopProperty)) + control.Height);

        return new Rect(ptTopLeft, ptBottomRight);
    }
}
相关文章
|
5月前
|
存储 编解码 算法
第5章-着色基础-5.4-锯齿和抗锯齿
第5章-着色基础-5.4-锯齿和抗锯齿
49 1
|
6月前
|
计算机视觉 Python
图像绘制以及写字
【7月更文挑战第28天】图像绘制以及写字。
38 7
|
5月前
|
机器学习/深度学习
第5章-着色基础-5.2-光源
第5章-着色基础-5.2-光源
34 0
|
5月前
|
编解码 定位技术
航摄比例尺、成图比例尺、地面分辨率与航摄设计用图比例尺
航摄比例尺、成图比例尺、地面分辨率与航摄设计用图比例尺
392 0
|
8月前
|
算法 图形学
【计算机图形学】实验四 二维图形的缩放、旋转,平移,组合变换
【计算机图形学】实验四 二维图形的缩放、旋转,平移,组合变换
369 2
|
8月前
|
计算机视觉
OpenCV(十三):图像中绘制直线、圆形、椭圆形、矩形、多边形和文字
OpenCV(十三):图像中绘制直线、圆形、椭圆形、矩形、多边形和文字
100 0
|
C++ 计算机视觉 Python
C++ VS OpenGL绘制教室三维立体旋转图像
C++ VS OpenGL绘制教室三维立体旋转图像
188 0
C++ VS OpenGL绘制教室三维立体旋转图像
|
算法 图形学
矩形范围碰撞检测
矩形范围碰撞检测
webgl图形变换、投影与摄像机
入坑webgl,从图形变换、投影与摄像机开始,基操学得好,以后才能怎么炫酷怎么来~快快快,点进来~~
Threejs使用LOD根据摄像机距离物体的距离显示不同的物体
Threejs使用LOD根据摄像机距离物体的距离显示不同的物体
757 0