I am currently developing a small Spaceshooter Clone. The project can be found on github
I am having problems with my very simple collision detection (or better said some behaviour that I can't explain myself.)
I do have lasers and asteroids. (currently all represented by Rectangles)
To check for collision I'm using asteroid.overlaps(laser)
Basic handling of all my elements is as follows:
- Move all my asteroids to the left
- Check if an asteroid "left the screen" and if so destroy it.
- Move all my lasers to the right
- Check if a laser "left the screen" and if so destroy it.
- If the laser has "not left the screen" I check collision with all asteroids.
asteroids is a Libgdx-Array; lasers is a Libgdx-Array; ship is a Libgdx Rectangle
Iterator<Asteroid> asteroidsIterator = asteroids.iterator();
while (asteroidsIterator.hasNext()) {
Asteroid asteroid = asteroidsIterator.next();
asteroid.x -= 200 * Gdx.graphics.getDeltaTime();
if (asteroid.x < 0) {
asteroidsIterator.remove();
} else if (asteroid.overlaps(ship)) {
//quitGame();
}
}
Iterator<Rectangle> lasersIterator = lasers.iterator();
asteroidsIterator = asteroids.iterator(); //<-- it seems that this does not get a fresh iterator
while (lasersIterator.hasNext()) {
Rectangle laser = lasersIterator.next();
laser.x += 600 * Gdx.graphics.getDeltaTime();
if (laser.x > 800) {
lasersIterator.remove();
Gdx.app.log("SC", "Laser exited screen.");
} else {
while (asteroidsIterator.hasNext()) {
Asteroid asteroid = asteroidsIterator.next();
if (asteroid.overlaps(laser)) {
Gdx.app.log("SC", "COLLISION");
asteroidsIterator.remove();
lasersIterator.remove();
}
}
}
}
When I do single fire everything is ok.
The strange behaviour is:
When I do full-automatic fire then some asteroids are not hit. Only newly created asteroids after begining firing are destroyed and in addition to that they should be very much to the right of the screen.
EDIT:
After further investigation it seems that the second call to asteroids.iterator()
does not create a fresh instance. Is there a way to get a fresh/reset Iterator?