# multiple rect collisions

• 01-08-2013, 02:35 AM
totj
multiple rect collisions
hello all, and thanks in advance for the replies
i have an arraylist of enemies, and i want to check collision between each enemy, and move any enemies out of the way when they collide..
this is the collision code i have so far..
Code:

```                for(int i=0;i<eList.size();i++){                    //compare this enemy with                         for(int j=i+1;j<eList.size();j++){  //this one                                 Rectangle Rect1 = new Rectangle(eList.get(i).x, eList.get(i).y, eList.get(i).w, eList.get(i).h);                                 Rectangle Rect2 = new Rectangle(eList.get(j).x, eList.get(j).y, eList.get(j).w, eList.get(j).h);                                 if(Rect1.intersects(Rect2){  //check collision between two enemies                                         if(eList.get(i).x < eList.get(j).x){                  //if its to the left                                                 eList.get(i).x -= 1;                        //move more left                                         }else{                                                 eList.get(i).x += 1;                      //else its to the right, move right                                         }                                 }                         }                 }```
enemies are moved by

Code:

```                int speed = 2;                                 float deltaX = px-this.x;                 float deltaY = py-this.y;                 float distance = (float) Math.sqrt(((deltaX*deltaX) + (deltaY*deltaY)));                 this.vx = speed / distance * deltaX;                 this.vy = speed / distance * deltaY;```
and in the update method each enemy's.x += vx; and .y+=vy;
px is the players x, so the enemies are always following the player

currently i have two problems:
(1): when the player "herds" them all into a small space(circling around them), collision doesnt seem to trigger between certain enemies, and
(2): once all enemies reach the same point (or players position), they "teleport" to 0,0 on screen instead of moving out of the way

if i didnt post enough information, or posted in the wrong place, please let me know.. i posted what i thought was necessary for the question, i thought other classes and unused variables for soon-to-be methods would just confuse
• 01-08-2013, 05:54 AM
kaydell2
Re: multiple rect collisions
Quote:

if i didnt post enough information
Well, yeah, I think that you should have a short self contained correct example that compiles at least.

Short, Self Contained, Correct Example

I did notice that there might be a problem when an enemy is colliding with itself. That might be a problem.

I'd like to try out the game. If the code is too much to post in this forum, you can email it to me and I'd be interested in looking it over. My email address is: kaydell@yahoo.com
• 01-08-2013, 07:11 AM
totj
Re: multiple rect collisions
my main class-
Code:

```import java.applet.Applet; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.Timer; public class Main extends Applet implements ActionListener{         private static final long serialVersionUID = 1L;         ArrayList<enemy> eList = new ArrayList<enemy>();         int spawnCount = 0;         Timer t;                 public void init(){                 setSize(900,800);                 for(int i=0;i<=11;i++){                         eList.add(new enemy(spawnCount, i));                         spawnCount++;                         if(spawnCount >= 4){                                 spawnCount = 0;                         }                 }                 t = new Timer(50, this);                 t.setInitialDelay(0);                 t.start();                 setBackground(Color.black);         }         public void paint(Graphics g){                 g.setColor(Color.blue);                 for(int i=0;i<eList.size();i++){                         g.fillRect(eList.get(i).x, eList.get(i).y, eList.get(i).h, eList.get(i).w);                         g.drawString(Integer.toString(i), eList.get(i).x, eList.get(i).y);                 }         }                 public void actionPerformed(ActionEvent e) {                 repaint();                                 for(int i=0;i<eList.size();i++){                         eList.get(i).setV(450, 400);                         eList.get(i).x += eList.get(i).vx;  //move enemies                         eList.get(i).y += eList.get(i).vy;                                                 for(int j=i+1;j<eList.size();j++){  //enemy collision                                 Rectangle Rect1 = new Rectangle(eList.get(i).x, eList.get(i).y, eList.get(i).w, eList.get(i).h);                                 Rectangle Rect2 = new Rectangle(eList.get(j).x, eList.get(j).y, eList.get(j).w, eList.get(j).h);                                 if(Rect1.intersects(Rect2) || Rect2.intersects(Rect1)){                                         if(eList.get(i).x < eList.get(j).x){                                                 eList.get(i).x -= 1;                                         }else{                                                 eList.get(i).x += 1;                                         }                                 }                         }                 }         }                 public void update(Graphics g) {  //for double buffering             Graphics offgc;             Image offscreen = null;             Dimension d = size();             offscreen = createImage(d.width, d.height);             offgc = offscreen.getGraphics();             offgc.setColor(getBackground());             offgc.fillRect(0, 0, d.width, d.height);             offgc.setColor(getForeground());             paint(offgc);             g.drawImage(offscreen, 0, 0, this);     }         }```
and my enemy class..
Code:

```public class enemy {         int x;         int y;         int h = 10;         int w = 10;         float vx;         float vy;         int spawnPoint1x = 450;         int spawnPoint1y = 0;         int spawnPoint2x = 0;         int spawnPoint2y = 400;         int spawnPoint3x = 900;         int spawnPoint3y = 400;         int spawnPoint4x = 450;         int spawnPoint4y = 800;                 public enemy(int n, int i) {                                switch(n){                 case 0:        this.x = spawnPoint1x;                                 this.y = spawnPoint1y;                                 break;                 case 1:        this.x = spawnPoint2x;                                 this.y = spawnPoint2y;                                 break;                 case 2:        this.x = spawnPoint3x;                                 this.y = spawnPoint3y;                                 break;                 case 3:        this.x = spawnPoint4x;                                 this.y = spawnPoint4y;                                 break;                 }         }                 public void setV(int px, int py) {                 int speed = 2;                                 float deltaX = px-this.x;                 float deltaY = py-this.y;                 float distance = (float) Math.sqrt(((deltaX*deltaX) + (deltaY*deltaY)));                 this.vx = speed / distance * deltaX;                 this.vy = speed / distance * deltaY;         } }```
enemies will spawn consecutively in the middle of the four lines of the screen and begin towards player, or in this instance 450,400
i sent
Code:

`eList.get(i).setV(450, 400);`
the coordinates 450,400 instead of locating player position & implementing player class
also, at the beginning of spawn enemies 4 & 11 seem not to be colliding..
• 01-08-2013, 08:12 AM
Potato
Re: multiple rect collisions
If the distance between the player and enemy is 0, then how can you divide by 0 in that instance:

Code:

```this.vx = speed / distance * deltaX; // AKA this.vx = speed / 0 * deltaX;```
you can't divide by 0.
• 01-09-2013, 01:49 AM
kaydell2
Re: multiple rect collisions
Quote:

you can't divide by 0.
Oh yeah. With floats, dividing by zero produces Infinity.
• 01-09-2013, 07:12 AM
totj
Re: multiple rect collisions
Quote:

Originally Posted by Potato
If the distance between the player and enemy is 0, then how can you divide by 0 in that instance:

Code:

```this.vx = speed / distance * deltaX; // AKA this.vx = speed / 0 * deltaX;```
you can't divide by 0.

of course!! :frusty: its always something simple...thats why i didnt want to post the whole program..
thank you so much for the help

edit: i searched for how to mark a post [solved], i cannot edit thread since it has been over 24 hrs..
any other suggestions on how to to so would also be appreciated,
or if a moderator can mark this as solved it would be cool :)
• 01-09-2013, 09:19 AM
DarrylBurke
Re: multiple rect collisions
Quote:

Originally Posted by totj
or if a moderator can mark this as solved it would be cool :)

Done.

db