I have a simple Java game where you fire a bullet up at a moving target. Both objects are GRects. I have collision detection that checks when the ufo and bullet intersect each other, but here is the weirdness:
This works:
private void collideWithUFO() {
if (bullet != null) {
GObject collObj = getElementAt(ufo.getX(), ufo.getY());
if (collObj == bullet) {
remove(ufo);
remove(bullet);
ufo = null;
bullet = null;
}
}
}
..but if I change the getElementAt to bullet like below, and check with respect to ufo, it fails to detect collisions:
private void collideWithUFO() {
if (bullet != null) {
GObject collObj = getElementAt(bullet.getX(), bullet.getY());
if (collObj == ufo) {
remove(ufo);
remove(bullet);
ufo = null;
bullet = null;
}
}
}
It should be irrelevant whether I choose ufo or bullet first, but apparently it isn't. Now here is something that's even WEIRDER. If I change the bullet from GRect to GOval, all of a sudden the second form of collision detection works. I am a Java noob so please let me know if this behavior makes any sense.