Search code examples
c#.netcollision-detectionshapesplane

Collision detecting custom sketched shape, represented as list of points


I have a set of points, drawn by the user. They will be drawing around some objects.

I need to somehow turn this set of points into a shape, so I can find the area to detect collisions.

An image will clarify:

Set of points represented as shape http://www.imagechicken.com/uploads/1277188630025178800.jpg .

The best idea I have had so far involves iterating over every pixel determining if it is 'inside' or 'outside' the shape, but that would be horribly slow, and I'm not even sure how to do the determining 'inside'/'outside' bit...

Any hints? I am using .NET (C# and XNA) if that helps you help me!


Solution

  • Well I got it working thanks to some help on another forum.

    I used the GraphicsPath class to do all the hard work for me.

    This is what my method ended up looking like:

    public bool IsColliding(Vector2 point)
    {
        GraphicsPath gp = new GraphicsPath();
    
        Vector2 prevPoint = points[0];
        for (int i = 1; i < points.Count; i++)
        {
            Vector2 currentPoint = points[i];
    
            gp.AddLine(prevPoint.X, prevPoint.Y, currentPoint.X, currentPoint.Y);
    
            prevPoint = currentPoint;
        }
        gp.CloseFigure();   //closing line segment
    
        return gp.IsVisible(point.X, point.Y);
    }
    

    Thanks for your suggestions both of you