MattWorks
|
 |
«
Posted
2017-06-28 22:12:50 » |
|
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.  code from Handler class 1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public void createLevel() { rand = new Random(); Blob lastBlob = new Blob(0,0, ObjectId.Blob); 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); if(blob.collidesWith(lastBlob)){ removeObject(blob); i--; System.out.println("INTERSECTION"); } lastBlob = blob; } } |
code from Blob class 1 2 3 4 5 6 7 8 9 10
| 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); } |
|
|
|
|
princec
|
 |
«
Reply #1 - Posted
2017-06-28 22:42:05 » |
|
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 
|
|
|
|
MattWorks
|
 |
«
Reply #2 - Posted
2017-06-28 22:47:00 » |
|
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  Thank you for the response! So should I create a LinkedList to hold the other blobs or how would I go about doing that?
|
|
|
|
Games published by our own members! Check 'em out!
|
|
KevinWorkman
|
 |
«
Reply #3 - Posted
2017-06-29 02:28:50 » |
|
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.
|
|
|
|
SteveSmith
|
 |
«
Reply #4 - Posted
2017-06-29 08:18:41 » |
|
If they're circles, I'd just check the radii rather than convert to rectangles.
|
|
|
|
princec
|
 |
«
Reply #5 - Posted
2017-06-29 08:42:43 » |
|
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 fastand Premature optimisation is the root of all evilWhich 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 
|
|
|
|
MattWorks(again)
|
 |
«
Reply #6 - Posted
2017-06-29 17:56:22 » |
|
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.  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: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| 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: 1 2 3 4 5 6
| public boolean collidesWith(Rectangle rect){ if(getBounds().intersects(rect.getBounds())) return true; else return false; } |
|
|
|
|
FabulousFellini
|
 |
«
Reply #7 - Posted
2017-06-29 18:00:12 » |
|
You should experiment with putting a break statement somewhere in there.
|
|
|
|
FabulousFellini
|
 |
«
Reply #8 - Posted
2017-06-29 19:00:24 » |
|
Also, I don't think you need that method from the blob class. you can just call intersects in the createLevel class.
|
|
|
|
MattWorks(again)
|
 |
«
Reply #9 - Posted
2017-06-29 19:01:12 » |
|
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.  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: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| 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: 1 2 3 4 5 6
| public boolean collidesWith(Rectangle rect){ if(getBounds().intersects(rect.getBounds())) return true; else return false; } |
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.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
MattWorks(again)
|
 |
«
Reply #10 - Posted
2017-06-29 19:02:48 » |
|
Also, I don't think you need that method from the blob class. you can just call intersects in the createLevel class.
that's true, I just think it makes it more readable. Does it take up any resources by me doing that?
|
|
|
|
FabulousFellini
|
 |
«
Reply #11 - Posted
2017-06-29 19:06:51 » |
|
Also, I don't think you need that method from the blob class. you can just call intersects in the createLevel class.
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 
|
|
|
|
MattWorks(again)
|
 |
«
Reply #12 - Posted
2017-06-29 19:11:35 » |
|
Also, I don't think you need that method from the blob class. you can just call intersects in the createLevel class.
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  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?
|
|
|
|
FabulousFellini
|
 |
«
Reply #13 - Posted
2017-06-29 19:14:44 » |
|
Also, I don't think you need that method from the blob class. you can just call intersects in the createLevel class.
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  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.
|
|
|
|
princec
|
 |
«
Reply #14 - Posted
2017-06-29 19:24:36 » |
|
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 
|
|
|
|
|
|