Circle and Polygon Collision
Rick

How would one go about checking collision between a circle and a polygon. More specifically a diamond shape? But I imagine there would be a generic algorithm for any polygon and a circle. 2 Rectangles I could do. 2 Circles I could do also, but mixing them I don't know how to start.

FMC

Wouldn't it be easier to turn the circle into a polygon and then check for collision between two polygons?

Rick

I'll give it a try. Thanks.

Fladimir da Gorf

Heh, sorry I just had to...

In OpenLayer:

Poly polygon( ... );
Circle circle( pos, radius );

if( polygon.Collides( Poly( circle.ToPolygon() ))) {
  // Collision!
}

james_lohr
Quote:

Wouldn't it be easier to turn the circle into a polygon and then check for collision between two polygons

That's not a good idea because firstly it would only be an approximation, and secondly it's a waste of resources.

You can do it properly by breaking the polygon up into line segments, and then calculating the perpendicular distance from the circle's centre to each line. If the perpendicular distance is smaller than the radius of the circle and the perpendicular intersects the line segment, then you have a collision. Of course, you also have a collision if the distance from any of the points describing the polygon is closer to the centre of the circle than its radius. Finally, for the case when the circle is completely inside the polygon, you'll need to check if the cenre of the circle is inside the polygon, which is your standard polygon-point intersection problem.

Thread #580534. Printed from Allegro.cc