Need some minor help in fixing/implementing some code!

Hello! I’m relatively new to the Java scene, but I’ve studied most of the important stuff, how it works, etc. This is my first “Game”, as in with animations/sprites/what have you. I’m having a bit of trouble implementing my “kill mobs” style.

In brief, I have a “bullet” arraylist (of bullet objects, obviously), and a mob arraylist. In my “tick” method, I have iterators that are ticking and checking for collisions. The ticking works, but the collision doesn’t. Me and one of my friends have been puzzling over this the past day or 2, and we’re no closer to finding out what’s wrong. So, I thought of asking you guys for help!

Here’s the github link: https://github.com/RyanMB97/SideScroller.git and thanks in advance :smiley:
P.S. The problem is in the “tick” method of “Game”.

Could you state specifically whats wrong? Not many people want to read through all that code!

Right, sorry! My main problem is getting the collision to work. For some reason, the bullets will collide with 1 or 2 of the mobs, but not the others. The thing is, it should be checking against every single last one of the mobs/bullets for collisions. Here’s the spot in focus:

Iterator<Bullet> bulletIterator = bulletList.iterator();
		Iterator<Mob> mobIterator = mobList.iterator();

		while (bulletIterator.hasNext()) {
			Bullet bullet = bulletIterator.next();
			bullet.tick();
			if (!bullet.living) {
				bulletIterator.remove();
			}
		}

		while (mobIterator.hasNext()) {
			Mob mob = mobIterator.next();
			mob.tick();
			if (!mob.living) {
				mobIterator.remove();
			}
		}

		bulletIterator = bulletList.iterator(); // Re-creates the bullet iterator after removing dead bullets
		mobIterator = mobList.iterator();// Re-creates the mob iterator after removing dead mob

		while (bulletIterator.hasNext()) {
			Bullet bullet = bulletIterator.next();
			while (mobIterator.hasNext()) {
				Mob mob = mobIterator.next();

				if (bullet.intersects(mob)) {
					mob.living = bullet.living = false;
					System.out.println("Intersection!");
				}

				if (!mob.living) {
					mobIterator.remove();
				}
			}
			if (!bullet.living) {
				bulletIterator.remove();
			}
		}

Like I said, it’s only colliding with some of the mobs that appear, and going completely through other mobs when it should be “Destroying” them.

Slow your bullets down and see if the problem persists. Unless you are doing interpolation, the bullet could be passing completely through your target in a single tick which would mean it would never actually register a collision.

That’s not the issue. Both the mobs and “bullets” are moving 1 pixel per tick, tied into the main game loop. I can clearly see the bullets going through the mobs, but not colliding. The bullet is physically inside of the mob, and it’s not destroying itself. The strange thing is that some of the mobs DO destroy themselves, while the bullet just skips through others.

I would suggest using the distance formula (Math.abs(bullet.x - mob.x) + (bullet.y - mob.y) I believe that’s correct). It will allow you to create your own collision system, and it isn’t that hard to implement. While

object.intersects(object)

isn’t a bad way of handling small collisions in small programs, you will definitely need to create your own collision system later on to handle different types of collisions and what happens depending on how you collide. Search AABB on this site, there are at least two tutorials that I’ve seen on how to implement AABBs.
Good luck and here’s to no bugs! :smiley:

That’s Manhattan Distance. Cartesian distance requires you square each term and take the square root, though you can skip the square root step and just compare against the square of the distance you’re testing against.

Ahh thank you! I always get them mixed up!

the collision method you used is fine. the problem is these iterators (im not familiar with these but i hope what i say is right).

you create these two iterators, then you loop through the bullets. then you loop through all the mobs, but you never recreate the iterator for mobs.
when you come to check the second bullet in the list of bullets, (mobIterator.hasNext()) is now false because you’ve been through them all and none are left. this is why only the first bullet can collide with mobs.

so adding mobIterator = mobList.iterator(); right before you cycle through the mob, between lines 173-174, should solve… i hope…

gl

JESTERRRRRR, you are amazing! Thanks for pointing that out, I feel like an idiot now for not realizing that :P. Thanks to everyone else who responded, I’m sure that if Jester hadn’t posted, I’d have been able to do it with one of your methods. Thanks a bunch :D.