Ktt
Ktt

Reputation: 469

WPF Image Collison Detection

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

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 ;

Not Collution

that's what I want to

Upvotes: 3

Views: 1484

Answers (2)

Bura Chuhadar
Bura Chuhadar

Reputation: 3751

You can try converting your images as a geometry object and then you can check if they are colliding correctly. But these images should be as a vector image. To convert images to a vector image, you can check this open source project.

public static Point[] GetIntersectionPoints(Geometry g1, Geometry g2)
{
    Geometry og1 = g1.GetWidenedPathGeometry(new Pen(Brushes.Black, 1.0));
    Geometry og2 = g2.GetWidenedPathGeometry(new Pen(Brushes.Black, 1.0));
    CombinedGeometry cg = new CombinedGeometry(GeometryCombineMode.Intersect, og1, og2);

    PathGeometry pg = cg.GetFlattenedPathGeometry();
    Point[] result = new Point[pg.Figures.Count];

    for (int i = 0; i < pg.Figures.Count; i++)
    {
        Rect fig = new PathGeometry(new PathFigure[] { pg.Figures[i] }).Bounds;
        result[i] = new Point(fig.Left + fig.Width / 2.0, fig.Top + fig.Height / 2.0);
    }
    return result;
}

Upvotes: 0

Mike Dinescu
Mike Dinescu

Reputation: 55730

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.

Upvotes: 2

Related Questions