Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
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  
  LibGDX - Collisions using Rectangles inside ArrayLists with Iterator  (Read 1675 times)
0 Members and 1 Guest are viewing this topic.
Offline Apache

Senior Newbie


Projects: 1



« Posted 2013-09-19 03:16:10 »

Hello all, thanks for taking the time to help me out.

I'm trying to make a top down space shooter where you shoot asteroids as you fly through space.

I'm using a similar method as in the LibGDX tutorial for making the 'Drop' game for generating asteroids and having them fall down the screen, as well as adding the asteroids to an ArrayList to keep track of all of them as well as remove them if necessary.

I'm using that same method for keeping track of the shots that the player fires.

The problem isn't in when the asteroids collide with the player, but having the asteroids collide with the bullets.

This is the first part of the approach that I'm taking to this, the iterator: my problem is that I'm unable to say
1  
if(asteroid.overlaps(shot)) { iter.remove(); }
because the shots and asteroids are declared locally in their spawn methods...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
      Iterator<Rectangle> iter = asteroids.iterator();
      while(iter.hasNext()) {
         Rectangle asteroid = iter.next();
         asteroid.y -= 200 * Gdx.graphics.getDeltaTime();
         if(asteroid.y < -64) {
            iter.remove();
         }
         if(asteroid.overlaps(playerShip)) {
            //
            iter.remove();
         }

        }
     
      Iterator<Rectangle> shotIter = shots.iterator();
      while(shotIter.hasNext()) {
         Rectangle shot = shotIter.next();
         shot.y += 500 * Gdx.graphics.getDeltaTime();
         if(shot.y > 600) {
            shotIter.remove();
         }

      }


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
   public void fire() {
      Rectangle shot = new Rectangle();
      shot.x = playerShip.x + 26;
      shot.y = playerShip.y + 32;
      shot.width = 32;
      shot.height = 32;
      shots.add(shot);
      lastFired = TimeUtils.nanoTime();
     
   }
   
   public void spawnAsteroid() {
      Rectangle asteroid = new Rectangle();
      asteroid.x = MathUtils.random(600 - 64);
      asteroid.y = 320;
      asteroid.width = 64;
      asteroid.height = 64;
      asteroids.add(asteroid);
      lastAsteroid = TimeUtils.nanoTime();
   }


Are there any suggestions or should is there another beginner-friendly way to manage collisions like this?

I know it's messy but I'm very new to Java game development (but not to Java itself)
Offline Nate

JGO Kernel


Medals: 150
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #1 - Posted 2013-09-19 12:26:18 »

my problem is that I'm unable to say
1  
if(asteroid.overlaps(shot)) { iter.remove(); }
because the shots and asteroids are declared locally in their spawn methods...
Not sure what the problem is. You have the shots and asteroids in separate lists, you can check if any of them overlap.

Offline Apache

Senior Newbie


Projects: 1



« Reply #2 - Posted 2013-09-19 13:05:08 »

I get a runtime error and the game crashes when I try to do that.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #3 - Posted 2013-09-19 13:32:12 »

Can you post your error? I can't find any flaw in the code you posted.

Offline drabla

Junior Duke


Medals: 5



« Reply #4 - Posted 2013-09-19 13:44:19 »

It seems to me that your problem is the position in the code of your
1  
if(asteroid.overlaps(shot)) { iter.remove(); }


A solution would be:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
Iterator<Rectangle> iter = asteroids.iterator();
      while(iter.hasNext()) {
          Rectangle asteroid = iter.next();
          if(asteroid.y < -64 || asteroid.overlaps(playerShip)) {
              iter.remove(); continue(); // We continue because the asteroid has been remove & can't collide with a Shot
          }
          Iterator<Rectangle> shotIter = shots.iterator();  // Loop of shots to check if they collide with the asteroid
          while(shotIter.hasNext()) {
              Rectangle shot = shotIter.next();
              if(shot.y > 600) {
                  shotIter.remove(); continue; // Remove the bullets as it is out of screen
              if(asteroid.overlaps(shot)) {
                  shotIter.remove(); iter.remove(); break; // Again no need to check for further collisions with this asteroid because it has been removed
              }
         }
      }

      
Offline SHC
« Reply #5 - Posted 2013-09-19 13:51:37 »

@Drabla Check the last iteration. You should not break, you should continue.

1  
2  
-    shotIter.remove(); iter.remove(); break;
+    shotIter.remove(); iter.remove(); continue;

Offline drabla

Junior Duke


Medals: 5



« Reply #6 - Posted 2013-09-19 13:56:08 »

Hmm.

It tought of it this way:

Loop Asteroids {
   Loop Shoots {
        checkForCollisions(); remove Asteroid && Shoot;

If the collision happens: We remove the asteroid and the shot.
If we would continue; we would just check collisions with the next bullet, but the same asteroid (that we just removed).
If we break; we leave the shots loop and skip to the next asteroid.

Or do i just have a big brain f*rt? Smiley
Offline SHC
« Reply #7 - Posted 2013-09-19 14:06:09 »

Yeah, didn't notice it. But one thing again.

Your code

1  
2  
3  
4  
5  
6  
if (shot.y > 600 || asteroid.overlaps(shot))
{
    shotIter.remove();
    iter.remove();
    break;
}

Had a problem. It kills both the shot and the asteroid even if the shot moves out of screen without happening collision.

Offline drabla

Junior Duke


Medals: 5



« Reply #8 - Posted 2013-09-19 15:27:31 »

Ah there you got me Wink
Fixed.
Thanks!
Offline Nate

JGO Kernel


Medals: 150
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #9 - Posted 2013-09-19 16:20:23 »

I get a runtime error and the game crashes when I try to do that.
I'm sure you didn't, else you would have most certainly posted the error.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Apache

Senior Newbie


Projects: 1



« Reply #10 - Posted 2013-09-19 22:30:29 »

I get a runtime error and the game crashes when I try to do that.
I'm sure you didn't, else you would have most certainly posted the error.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.Error: Unresolved compilation problem: 
   The method overlaps(Rectangle) is undefined for the type ArrayList<Rectangle>

   at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Caused by: java.lang.Error: Unresolved compilation problem:
   The method overlaps(Rectangle) is undefined for the type ArrayList<Rectangle>

   at com.connorkuehl.blasteroids.PlayScreen.render(PlayScreen.java:162)
   at com.badlogic.gdx.Game.render(Game.java:46)
   at com.connorkuehl.blasteroids.BlasteroidsGame.render(BlasteroidsGame.java:31)
   at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207)
   at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
Offline Nate

JGO Kernel


Medals: 150
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #11 - Posted 2013-09-19 23:04:29 »

That is a complication error, so you are using the overlaps method wrong. You need to write loops and use rectangles in overlaps, see drabla's posts above.

Offline Apache

Senior Newbie


Projects: 1



« Reply #12 - Posted 2013-09-19 23:35:06 »

Special thanks to drabla, and thanks for everyone else who's been contributing thus far you guys are great.

I used the loop approach and I think it would work but there's just one snag, the shots nor the asteroids are moving anymore.

The rectangle overlap loop looks like so, and it was also supposed to control the downward or upward movement of the asteroid or the shot respectively. Should I be controlling their movement another way?


EDIT: Thank you all, I've fixed the issue. The problem with the objects not moving was simply me overlooking a logic error.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
      Iterator<Rectangle> asteroidIter = asteroids.iterator();
      while(asteroidIter.hasNext()) {
         Rectangle asteroid = asteroidIter.next();
         asteroid.y -= 200 * Gdx.graphics.getDeltaTime();
         if(asteroid.y < -64 || asteroid.overlaps(playerShip)) {
            asteroidIter.remove(); continue;
         }
         Iterator<Rectangle> shotIter = shots.iterator();
         while(shotIter.hasNext()) {
            Rectangle shot = shotIter.next();
            shot.y += 500 * Gdx.graphics.getDeltaTime();
            if(shot.y > 600) {
               shotIter.remove(); continue;
            }
            if(asteroid.overlaps(shot)) {
               shotIter.remove();
               asteroidIter.remove();
               break;
            }
         }
      }
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.

theagentd (20 views)
2014-10-25 15:46:29

Longarmx (53 views)
2014-10-17 03:59:02

Norakomi (47 views)
2014-10-16 15:22:06

Norakomi (35 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (69 views)
2014-10-14 00:39:48

TehJavaDev (69 views)
2014-10-14 00:35:47

TehJavaDev (61 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (47 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!