hello,
i have this code to draw images and rectangles :
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 :
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
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
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 :
if(tmpR!=itSelf && tmpR.intersects(tmpR)){
System.out.println("Colliding");
}
thank you