3

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

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
Ktt
  • 469
  • 3
  • 8
  • 18

2 Answers2

2

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.

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • the simplest case looks very useful. I have the intersection rectangle , can you help me about the idea? to find the color of intersection area – Ktt May 07 '14 at 14:28
  • @Ktt - Are your controls images, or can you easily get the underlying image in the controls? – Mike Dinescu May 07 '14 at 14:30
  • I can get the underlying image Image im1 = (Image)ctrl1.FindName("paintImage"); Image im2 = (Image)ctrl2.FindName("crossImage"); – Ktt May 07 '14 at 14:31
  • @Ktt - if you have images the have a look at this question that shows how to examine the individual pixels of a BitmapImage: http://stackoverflow.com/questions/1176910/finding-specific-pixel-colors-of-a-bitmapimage – Mike Dinescu May 07 '14 at 14:33
0

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;
}
Abbas
  • 14,186
  • 6
  • 41
  • 72
Bura Chuhadar
  • 3,653
  • 1
  • 14
  • 17
  • @Abbas can you tell me how do you format the code automatically in stackoverflow? Are you just copying the code into Visual Studio and then here? – Bura Chuhadar May 07 '14 at 14:21
  • 1
    Code formatting happens automatically but you have to indent the code sections with 4 leading spaces on each line in the code section. – Mike Dinescu May 07 '14 at 14:26
  • @MikyDinescu what about the inner part of the code? They have 8 leading spaces. I don't want to go each line and give 8 indent space for those. Is there an automatic way to do that? – Bura Chuhadar May 07 '14 at 14:37
  • No, unfortunately you do have to put a little manual effort into the indentation. After all, it's just a few lines of code.. – Mike Dinescu May 07 '14 at 14:40
  • @MikyDinescu that was my main question. But thanks for the help. Sometimes when I was planning to send a huge code, it is always making it like that. I was curious that there is a way to fix it. I accustomed to press the tab key all the time. So it becomes frustrating on the editor to press the tab button. – Bura Chuhadar May 07 '14 at 14:46