Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  Bullet Collision Loop  (Read 1176 times)
0 Members and 1 Guest are viewing this topic.
Offline Hazzy

Senior Newbie





« Posted 2012-05-22 11:42:38 »

I'm looping through an ArrayList to check if an enemy is colliding with one of the player's bullets. It runs smoothly with only a few enemies on the screen but when there's a bunch of them then the game starts to lag because every enemy is looping through every bullet.

Code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   public void checkCollision(ArrayList<ShipBullet> bullets){
      for (int i = 0; i < bullets.size(); i++){
         ShipBullet bul = bullets.get(i);
         Rect rect1, rect2;
         rect1 = new Rect(bul.x,bul.y,bul.x+1,bul.y+4);
         rect2 = new Rect(x, y, x+graphic.getWidth(), y+graphic.getHeight());
         if (rect1.intersect(rect2)){
            destroyed = true;
            break;
         }
      }
   }

Is there any better way of doing this? I was thinking of pooling the bullets but I'm not familiar with pooling just yet (pretty much just found it on Google). Just wondering if there are other methods that I can try.

The method is checked every frame and there can be up to 10~ bullets on the screen from the player alone. And it starts lagging when 6 or so enemies are present. Not to mention I'm working on a cheap Android device but I'd still like to optimize it.

Thanks!
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #1 - Posted 2012-05-22 11:50:36 »

Have you tried profiling it? How many bullets are we talking about?

You could probably save a *lot* of time if you didn't create two new Rect objects to test for each intersection. Either do the math manually or just create some Rects earlier and reuse them.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #2 - Posted 2012-05-22 11:51:46 »

Is this collision method run for each entity?  Because that seems wrong to me. Also just use an arraylist just like enemies.

But a short fix is to perform a distance check before collision (avoid sqrt)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #3 - Posted 2012-05-22 12:00:53 »

But a short fix is to perform a distance check before collision (avoid sqrt)
A distance check isn't going to be much more expensive than a rect-rect check, you're just adding extra work. The 'new' overheard (especially on an Android VM) is going to dwarf that.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Hazzy

Senior Newbie





« Reply #4 - Posted 2012-05-22 13:55:41 »

Have you tried profiling it? How many bullets are we talking about?

You could probably save a *lot* of time if you didn't create two new Rect objects to test for each intersection. Either do the math manually or just create some Rects earlier and reuse them.
That was just some sloppy code on my behalf, I didn't think it would have affected the performance that much though? (I'll smooth it out a bit now)
And well bullets... uhm, can be around 20 maximum at any one time I think, by which time they leave the screen and get removed from the ArrayList. That's just for the player bullets minus any additional ones from enemies (which I have yet to add).
Edit: Nope, no profiling - yet.

Is this collision method run for each entity?  Because that seems wrong to me. Also just use an arraylist just like enemies.

But a short fix is to perform a distance check before collision (avoid sqrt)
I run that method for every enemy ship in the enemy ArrayList.
Like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
         //Enemies
        for (int i = 0; i < enemyList.size(); i++){
            Enemy enemy = enemyList.get(i);
            enemy.checkCollision(player.getBullets());
           
            if (enemy.getDestroyed()){
               propList.add(new GameExplosion(enemy.x,enemy.y));
               enemyList.remove(i);
               continue;
            }
            enemy.step(g);
         }

The bullets original ArrayList is held within the player object, and I just pass it to each enemy every step. If that's a bad idea how otherwise would I get each enemy to check for collisions against bullets?

Edit 2:
Thanks for the suggestion about creating the Rects early on and reusing them. Got a noticeable performance boost. I don't detect any lag unless I fill the screen up with about 40 enemies - the actual game won't get that half as many enemies on the screen at once.
Offline 65K
« Reply #5 - Posted 2012-05-22 18:46:50 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
         //Enemies
        for (int i = 0; i < enemyList.size(); i++){
            Enemy enemy = enemyList.get(i);
            enemy.checkCollision(player.getBullets());
           
            if (enemy.getDestroyed()){
               propList.add(new GameExplosion(enemy.x,enemy.y));
               enemyList.remove(i);
               continue;
            }
            enemy.step(g);
         }

For optimized performance I wouldn't use ArrayLists if items are removed frequently - just take a look into the source code to see what happens if its not the last item you remove.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (18 views)
2014-07-24 01:59:36

Riven (17 views)
2014-07-23 21:16:32

Riven (14 views)
2014-07-23 21:07:15

Riven (17 views)
2014-07-23 20:56:16

ctomni231 (45 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!