4

Are there any predefined methods in c# which allow for collision detection?

I am new to c# and am trying to get collision detection of two ellipses are there any predefined ways collision detection can be implemented?

I already have code which draws the ellipses, what would be a good way to start the collision detection?

private void timer1_Tick(object sender, EventArgs e)
    {
        //Remove the previous ellipse from the paint canvas.
        canvas1.Children.Remove(ellipse);

        if (--loopCounter == 0)
            timer.Stop();

        //Add the ellipse to the canvas
        ellipse = CreateAnEllipse(20, 20);
        canvas1.Children.Add(ellipse);

        Canvas.SetLeft(ellipse, rand.Next(0, 500));
        Canvas.SetTop(ellipse, rand.Next(0, 310));
    }

    // Customize your ellipse in this method
    public Ellipse CreateAnEllipse(int height, int width)
    {
        SolidColorBrush fillBrush = new SolidColorBrush() { Color = Colors.Yellow};
        SolidColorBrush borderBrush = new SolidColorBrush() { Color = Colors.Black };

        return new Ellipse()
        {
            Height = height,
            Width = width,
            StrokeThickness = 1,
            Stroke = borderBrush,
            Fill = fillBrush
        }; 
    }

this is the code to draw an ellipse which then gets removed and appears in another position.

user1866990
  • 115
  • 2
  • 3
  • 6
  • Could you post an example of that code? – m.edmondson Apr 21 '13 at 15:57
  • wpf is not a game framework, it doesn't have sprite-like functionality like collision detection. The best you could do is get the bounding rectangle of the two ellipses and use `RectangleF.IntersectsWith`. Otherwise, you'll have to calculate the angle of approach of the two ellipses, the radius at that angle, then see if adding the length of the two radii is less than or equal to the distance between two foci of the ellipses. – Peter Ritchie Apr 21 '13 at 16:05

4 Answers4

5

I have tested this, it worked, at least for me

enter image description here

var x1 = Canvas.GetLeft(e1);
var y1 = Canvas.GetTop(e1);
Rect r1 = new Rect(x1, y1, e1.ActualWidth, e1.ActualHeight);


var x2 = Canvas.GetLeft(e2);
var y2 = Canvas.GetTop(e2);
Rect r2 = new Rect(x2, y2, e2.ActualWidth, e2.ActualHeight);

if (r1.IntersectsWith(r2))
    MessageBox.Show("Intersected!");
else
    MessageBox.Show("Non-Intersected!");
David
  • 15,894
  • 22
  • 55
  • 66
4

Would something like the following work?

var ellipse1Geom = ellipse1.RenderedGeometry;
var ellipse2Geom = ellipse2.RenderedGeometry;
var detail = ellipse1Geom.FillContainsWithDetail(ellipse2Geom);
if(detail != IntersectionDetail.Empty)
{
    // We have an intersection or one contained inside the other
}

The Geometry.FillContainsWithDetail(Geometry) method is defined as

Returns a value that describes the intersection between the current geometry and the specified geometry.

Lukazoid
  • 19,016
  • 3
  • 62
  • 85
1

I think you should definitely give a look at the XNA framework, it has loads of method to do collision detection.

Check out this other link on how to implement it manually in c# it might be helpful.

Community
  • 1
  • 1
codingadventures
  • 2,924
  • 2
  • 19
  • 36
1

Provided that your Ellipses are always circles (i.e. their Width and Height properties are set to the same value) and they always have the Canvas.Left and Canvas.Top properties set, the following helper method checks for a collision:

public static bool CheckCollision(Ellipse e1, Ellipse e2)
{
    var r1 = e1.ActualWidth / 2;
    var x1 = Canvas.GetLeft(e1) + r1;
    var y1 = Canvas.GetTop(e1) + r1;
    var r2 = e2.ActualWidth / 2;
    var x2 = Canvas.GetLeft(e2) + r2;
    var y2 = Canvas.GetTop(e2) + r2;
    var d = new Vector(x2 - x1, y2 - y1);
    return d.Length <= r1 + r2;
}
Clemens
  • 123,504
  • 12
  • 155
  • 268