Java-Gaming.org Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (549)
Games in Android Showcase (137)
games submitted by our members
Games in WIP (594)
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  
  how to check collision between object from the same arrayList  (Read 1246 times)
0 Members and 1 Guest are viewing this topic.
Offline alaslipknot
« Posted 2013-04-29 13:12:45 »

hello,
i have this code to draw images and rectangles :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
ArrayList enemies = enemy.getE();
      for (int i = 0; i < enemies.size(); i++) {
         Enemy tmpE = (Enemy) enemies.get(i);
         g2d.drawImage(tmpE.getI(), (int) tmpE.getX(), (int) tmpE.getY(),
               tmpE.getW(), tmpE.getH(), this);
         Rectangle tmpR = (Rectangle) tmpE.getBounds();
         g2d.draw(tmpR);
         enemyRects.add(tmpR);

      }


and this code to make them moves :
1  
2  
3  
4  
5  
ArrayList enemies = enemy.getE();
      for (int i = 0; i < enemies.size(); i++) {
         Enemy tmpE = (Enemy) enemies.get(i);
         tmpE.moveForward(hero.getX(), hero.getY());
      }


i want to check if the Rectangles (inside enemyRects) intersects with each other, i tried this
1  
2  
3  
4  
5  
6  
for (int i = 0; i < enemyRects.size(); i++) {
         Rectangle tmpR = (Rectangle) enemyRects.get(i);
         if (tmpR.intersects(tmpR)) {
            System.out.println("Colliding");
         }
      }

but it always print "colliding" cause it's checking the rectangle with it self,
i tried to do this
1  
2  
3  
if (tmpR.intersects((Rectangle) enemyRects.get(i+1))) {
            System.out.println("Colliding");
         }

but i get an out of bounds error
so i want to ask if there is a code that can do this :
1  
2  
3  
if(tmpR!=itSelf && tmpR.intersects(tmpR)){
            System.out.println("Colliding");
         }


thank you

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline Stranger

Senior Devvie


Medals: 9


I'm going slightly mad. It finally happened.


« Reply #1 - Posted 2013-04-29 14:00:31 »

I think this could be useful for you.

Anton
Offline alaslipknot
« Reply #2 - Posted 2013-04-29 14:08:27 »

I think this could be useful for you.
yes i tried that before, it lags soooo much !!
and it also keep printing "collide" even when they are not colliding

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2013-04-29 14:10:25 »

A really simple thing would be to do something like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public Enemey getFirstCollision(Enemy theEnemey, ArrayList<Enemey> enemies) {
    ArrayList<Enemey> enemeis = ...;

    for (int i=0; i<enemies.size(); i++) {
        Enemey e = enemies.get(i);
        if (e != theEnemey && theEnemey.intersects(e))
            return e;
    }
    return null;
}


The method returns the first other Enemy object that the given enemy collides with, or null if no collision exists.

Some things you should be doing:

1. Use generics; you should rarely ever need to cast objects like you are doing. e.g.

1  
2  
3  
4  
5  
//now the list only expects type Enemy... 
ArrayList<Enemy> list = new ArrayList<Enemey>();

//it will throw a compiler error if "e" is not of type Enemy
list.add(e);


2. Add important methods like intersects() into your Enemy definition:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public class Entity {
     private Rectangle bounds = new Rectangle();
     private BufferedImage image;

     ....

     public boolean intersects(Entity e) {
         return e.bounds.intersects(this.bounds);
     }

     public void draw(Graphics g) {
         g.drawImage(this.image, (int)bounds.x, (int)bounds.y, (int)bounds.width, (int)bounds.height, null);
         g.draw(bounds);
     }
}

...

public class Enemy extends Entity {
   ... enemy specific stuff ...
}


Not only does this result in cleaner code when rendering and checking collisions (since you don't need to touch the Rectangle object at all), but it also is more flexible for future changes. Let's say you want one of your enemies to have a Circle bounds instead of Rectangle, you only need to change their draw() and intersects() methods.

3. Use descriptive method names like getImage() instead of getI(). It's important to code with some "good practice" as it will make your program easier to read/debug, and also less prone to errors.

Offline alaslipknot
« Reply #4 - Posted 2013-04-29 14:27:48 »

@davedes
thanks a lot, that seems pretty good
the only problem now is that once again i had to re-do everything  Undecided

PS :
all i know to do with Java2D is drawing shapes,images and make them move with or without Key and/or Mouse listeners,
if i start learning lwjgl today, how much would it take me to reach that very low level i have now with Java2D ?
cause am starting to feel that am wasting my time here, i know that all my errors are not because of Java2D but sooner or later i had to stop developing with it and be forced to learn more advanced things,
what do you think ?
if i should start learning now, can you please post some "getting started" links ?

thank you

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline matheus23

JGO Kernel


Medals: 119
Projects: 3


You think about my Avatar right now!


« Reply #5 - Posted 2013-04-29 14:50:41 »

Basically, drawing Images:

1  
2  
3  
public void drawImage(SpriteBatch batch, Sprite sprite) {
    sprite.draw(batch, xposition, yposition);
}

you can even have fun:
1  
2  
sprite.setRotation(100f); // rotated.
sprite.setScale(10, 10); // 10x scaled on both x and y axis


Loading sprites is pretty trivial too:
1  
2  
Texture tex = new Texture("myfile.png");
Sprite sprite = new Sprite(tex);

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline SHC

2: log() expects parameter 1 to be double, string given
File: /home/jgo/public_html/addon_appreciation.php
Line: 191