Collision detection not working

So I’m trying to place 20 circles around the screen randomly without touching each other and the collision detection doesn’t work. I’ve tried so many solutions that don’t work. Can anybody help me? If I need to put more code in here just lmk. Thank you in advance. :slight_smile:

code from Handler class


public void createLevel() {
		rand = new Random();
		Blob lastBlob = new Blob(0,0, ObjectId.Blob); //to hold last blob
		for (int i = 0; i < 20; i++){
			Blob blob = new Blob(rand.nextInt(Game.WIDTH - 32), rand.nextInt(Game.HEIGHT - 32), ObjectId.Blob); //makes blob at random postion on-screen
			addObject(blob);
			if(blob.collidesWith(lastBlob)){
				removeObject(blob);
				i--; //makes certain that we only get 20 blobs
				System.out.println("INTERSECTION"); //for debugging
			}
			lastBlob = blob; //makes the added blob the last blob
		}
	}

code from Blob class


public boolean collidesWith(GameObject object){
		if(getBounds().intersects(object.getBounds()))
			return true;
		else
			return false;
	}

public Rectangle getBounds() {
		return new Rectangle((int) x, (int) y, 32, 32);
	}

You’re only checking against the previous blob you put down… not, say, the other 10 you already laid. Each time you add a new one you have to check if it intersects with all the previous blobs.

Cas :slight_smile:

Thank you for the response!
So should I create a LinkedList to hold the other blobs or how would I go about doing that?

Sure, you could store your circles in some kind of data structure and loop over all of them to check for collisions.

But note that this is going to be very slow for large numbers of circles. You might want to look into circle packing algorithms instead.

If they’re circles, I’d just check the radii rather than convert to rectangles.

While @KevinWorkman’s advice is sound I suspect from the very nature of this post you are at the very very beginning of your computing career, and things like algorithms and data structures are very high-brow science to the beginner. With that in mind, take these two pieces of advice dearly to heart (should be written on a stone tablet or something):

Make it work, then make it fast

and

Premature optimisation is the root of all evil

Which in your case here means… just get it working without problems using brute force coding, no matter how slow it is, so you can understand the nature of the problem you’ve got fully. Then, if and only if it’s too slow for your purposes, start thinking about how to make it faster, which will be a whole other thread and get quite exciting as everyone comes up with genius optimisations and complex data structures, and then at the end of it you’ll tell us there were only ever going to be 20 blobs anyway and everyone will slink off feeling stupid.

Cas :slight_smile:

I’m locked out of my original account because I never received a activation email to the email I put down. Anyway, thank you all for your responses. So now I have a different problem, lol. Still regarding collision not working properly. I created a LinkedList to hold all the blobs as they are created, and I loop through it checking if the current blob collides with the ones in the LinkedList… and it collides every time so it’s an infinite loop. :clue: It just constantly deletes the blobs due to the collisions and I just don’t know what I’m doing wrong.

Here is the new code I have

from Hanlder class:


public void createLevel() {
		Random rand = new Random();
		LinkedList<GameObject> blobList = new LinkedList<GameObject>();
		for (int i = 0; i < 20; i++){
			Blob blob = new Blob(rand.nextInt(Game.WIDTH - 32), rand.nextInt(Game.HEIGHT - 32), ObjectId.Blob);
			addObject(blob);
			blobList.add(blob);
			if(blobList.size() > 1){
				for(int x = 0; x < blobList.size(); x++){
					if(blob.collidesWith(blobList.get(x).getBounds())){
						removeObject(blob);
						blobList.remove(blob);
						i--;
						System.out.println("INTERSECTION");
					}
					
				}
			}
			
			
		}
	}

from Blob class:


public boolean collidesWith(Rectangle rect){
		if(getBounds().intersects(rect.getBounds()))
			return true;
		else
			return false;
	}

You should experiment with putting a break statement somewhere in there.

Also, I don’t think you need that method from the blob class. you can just call intersects in the createLevel class.

I FIXED IT!!! In the second for loop when there were 2 blobs in the linked list it checked if it collided with the first blob and itself. I made sure it didn’t check if it collided with itself by making the for-loop loop until one before the end of the list.

that’s true, I just think it makes it more readable. Does it take up any resources by me doing that?

It would be slightly slower, and by slightly I mean you wont be able to tell the difference with 20 blobs, but with thousands of blobs you could tell. Glad you fixed it :slight_smile:

I plan on making a much bigger game than just 20 blobs on screen, haha. (it’ll be a game simulating evolution) those blobs will just be obstacles for the creatures.

Do you have any tips for optimization?

Yes, don’t optimize until you start seeing slowdown, like what @princec said. If it works correctly now, then roll with that. When you start seeing a performance dip, that’s when you should start to optimize it. For now, just continue with the project! But one thing I have learned with my game that I mistakingly made too large, was to not comment. comment everything so later on in 6 months or a year when you’re trying to optimize it, you don’t have to go tracing code to see where you did something. That situation is much easier if you already have comments.

When you’ve got a few hundred blobs, and you’re trying to run it at 60 frames per second, you might start to see performance suffering.

Cas :slight_smile:

Thank you everyone for your contributions, I will take all your tips to heart. ;D ;D ;D