How viable is this idea in a game? You give each sprite (bouncing ball, bullet, monster, etc…) a java.awt.geom.Area object which represents it’s bounds. You could even have a list of them if you want to for animations which change shape. Then to test for a collision you can just see if the areas union equals their xor. If it does then no collision happened.
public boolean collision(Area a, Area b) {
Area union = new Area(a); // you make copies of a since Area's add() and exclusiveOr() methods will edit the object
Area xor = new Area(a);
union = aa.add(b);
xor.exclusiveOr(b);
return !union.equals(xor); // equals is built into Area
}
I don’t know how fast or slow these Area methods are (no doubt they are slower than Rectangle.contains()), but I don’t see why this wouldn’t work, especially for a small number of sprites on the screen. You could even have the Area object created automatically from your sprites Image. Anyone have any thoughts on this method? I came up with this while working on my Tower Defense game and trying to figure out a way to set certain parts of the map “unbuildable” (like where the enemies walk, and where you have already built something).