Affian
Affian

Reputation: 3398

Get center of Shape intersection (2D)

I'm using the Slick java game lib and I've used the Slick implementation of Shape as a hit-box for collision and that's working fine. I use shape1.intersects(shape2) for checking if two shapes overlap. Now what I want to do is get the center point of the intersecting shape to use as a place to generate particles showing the collision between the 2 objects.

I can't find any explanations of algorithms that calculate shape intersections, maybe because I don't know if it has a specific name or not.

Upvotes: 0

Views: 1184

Answers (1)

casablanca
casablanca

Reputation: 70691

Looking at the documentation, there seem to be two methods -- union and subtract -- using which you can get the intersection as follows:

A ∩ B  =  A ∪ B - (A - B) - (B - A)

The only problem is that those methods seem to return an array of shapes instead of a single shape, whatever that means.

Of course, doing this in real-time could be quite costly, so you could alternatively approximate the collision point to be in between the centres of the bounding box/circle of the two shapes.

Upvotes: 1

Related Questions