Problem with GeneralPath intersects()

Hi,

I’m having a problem when I check for collision detection with 2 GeneralPath objects. The following test class prints true while from my understanding it prints false. The code constructs 2 shapes that are composed of blocks and look like this:

oo
o

and

o
oo

So when translated the 2 shapes are set like this:

oo
oo
oo

public class GeneralPathTest {

	public static void main(String[] args) {
		int blockSize = 32;
		
		Polygon bounds = new Polygon();
		bounds.addPoint(0, 0);
		bounds.addPoint(blockSize * 2 - 1, 0);
		bounds.addPoint(blockSize * 2 - 1, blockSize - 1);
		bounds.addPoint(blockSize - 1, blockSize - 1);
		bounds.addPoint(blockSize - 1, blockSize * 2  - 1);
		bounds.addPoint(0, blockSize * 2 - 1);

		Polygon bounds2 = new Polygon();
		bounds2.addPoint(blockSize - 1, 0);
		bounds2.addPoint(blockSize * 2 - 1, 0);
		bounds2.addPoint(blockSize * 2 - 1, blockSize * 2 - 1);
		bounds2.addPoint(0, blockSize * 2 - 1);
		bounds2.addPoint(0, blockSize - 1);
		bounds2.addPoint(blockSize - 1, blockSize - 1);

		GeneralPath poly1 = new GeneralPath(bounds);
		poly1.transform(AffineTransform.getTranslateInstance(100, 100));

		GeneralPath poly2 = new GeneralPath(bounds2);
		poly2.transform(AffineTransform.getTranslateInstance(100, 132));
		
		System.out.println(poly1.intersects(poly2.getBounds()));
		System.out.println(poly2.intersects(poly1.getBounds()));
	}
}

What’s wrong here???

Forget about that. I just figured out that you can’t simply test for accurate intersection between two GeneralPath objects since you have to pass a Rectangle2D to the intersects method. I should have realized that before. But it’s just too bad that 2 GeneralPath cannot test for intersection accurately.

As GeneralPath implements Shape, does this work?

new Area(generalShape).intersect(new Area(otherGeneralShape))

The Shape interface defines 2 intersects methods. One that accepts a Rectangle2D and the other accepts and x,y coordinates with a width and height. So no you can’t pass a geometry object to these methods, which is really constraining in my opinion.

It also provides a PathIterator, through which the Area class can determine the closed area(s) of the shape and perform CAG operations.

It isn’t fast, but is the most general approach possible when dealing with 2d geometry - and should be sufficient for most cases.