Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Collision detection not working  (Read 2407 times)
0 Members and 1 Guest are viewing this topic.
Offline MattWorks

Junior Newbie


Exp: 1-3 months



« 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.  Smiley


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); //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
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);
   }
Offline princec

« JGO Spiffy Duke »


Medals: 976
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« 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 Smiley

Offline MattWorks

Junior Newbie


Exp: 1-3 months



« 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 Smiley

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!
Legends of Yore - The Casual Retro Roguelike
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 270
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« 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.

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline 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.

Offline princec

« JGO Spiffy Duke »


Medals: 976
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« 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 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 Smiley

Offline 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. Clueless  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;
   }
Offline FabulousFellini
« Reply #7 - Posted 2017-06-29 18:00:12 »

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

-FabulousFellini
www.fabulousfellini.com
Offline 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. 

-FabulousFellini
www.fabulousfellini.com
Offline 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. Clueless  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!
Legends of Yore - The Casual Retro Roguelike
Offline 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?
Offline 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 Smiley

-FabulousFellini
www.fabulousfellini.com
Offline 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 Smiley

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?
Offline 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 Smiley

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.

-FabulousFellini
www.fabulousfellini.com
Offline princec

« JGO Spiffy Duke »


Medals: 976
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« 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 Smiley

Offline MattWorks(again)
« Reply #15 - Posted 2017-06-29 19:30:29 »

Thank you everyone for your contributions, I will take all your tips to heart.  Grin Grin Grin
Pages: [1]
  ignore  |  Print  
 
 

 
xxMrPHDxx (17 views)
2017-11-21 16:21:00

xxMrPHDxx (11 views)
2017-11-21 16:14:31

xxMrPHDxx (14 views)
2017-11-21 16:10:57

Ecumene (114 views)
2017-09-30 02:57:34

theagentd (148 views)
2017-09-26 18:23:31

cybrmynd (248 views)
2017-08-02 12:28:51

cybrmynd (247 views)
2017-08-02 12:19:43

cybrmynd (244 views)
2017-08-02 12:18:09

Sralse (258 views)
2017-07-25 17:13:48

Archive (878 views)
2017-04-27 17:45:51
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!