0

I need to check if some shapes are colliding in a 2D world.

To check collision between circle and rectangle I found this: Collision Detection with Rotated Rectangles

But now I need to add another shape to this code (triangle), and the triangle can rotate too.

How can I do that?

Community
  • 1
  • 1
  • Use a library. Geos, Shapely, [Jts](http://tsusiatsoftware.net/jts/main.html) – flup Jan 28 '13 at 07:13
  • 2
    But in general, a rectangle and a triangle are all polygons. All you really need to know is how to intersect lines and lines, and how to intersect lines and circles. – flup Jan 28 '13 at 07:15

1 Answers1

0

This JSTS example (in javascript) shows how to intersect two polygons.

https://github.com/bjornharrtell/jsts/blob/master/examples/overlay.html

In order to modify the example to make one of them into a circle, use this snippet:

// this returns a JSTS polygon circle approximation with provided center and radius
function pointJSTS(center,radius){
  var point = new jsts.geom.Point(center);
  return point.buffer(radius);
}
// ....
// insert this into the example above at line 17
b = pointJSTS({x:10,y:20}, 40);

Further modification to the example to make the first polygon into a triangle is trivial.

benathon
  • 7,455
  • 2
  • 41
  • 70
  • 1
    Link is dead now. :( – scribu Apr 12 '17 at 11:17
  • The repo seems to have been deleted and replaced with a different version. I found this which might help, sorry but I did not verify this: https://github.com/Emergya/google-gis-demo/tree/master/bower_components/jsts/examples – benathon Apr 12 '17 at 21:42
  • I messaged the creator, and indeed the repo was archived, the new link is here: https://github.com/bjornharrtell/jsts/tree/0.x – benathon Apr 13 '17 at 15:26