Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Potato

Thread: multiple rect collisions

  1. #1
    totj is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default 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..
    Java 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

    Java 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
    Last edited by DarrylBurke; 01-09-2013 at 08:18 AM.

  2. #2
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: multiple rect collisions

    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

  3. #3
    totj is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default Re: multiple rect collisions

    sorry about that..
    my main class-
    Java 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..
    Java 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
    Java 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..

  4. #4
    Potato is offline Member
    Join Date
    Dec 2011
    Posts
    25
    Rep Power
    0

    Default Re: multiple rect collisions

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

    Java Code:
    this.vx = speed / distance * deltaX;
    
    // AKA
    
    this.vx = speed / 0 * deltaX;
    you can't divide by 0.
    totj likes this.

  5. #5
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: multiple rect collisions

    you can't divide by 0.
    Oh yeah. With floats, dividing by zero produces Infinity.

  6. #6
    totj is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default Re: multiple rect collisions

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

    Java Code:
    this.vx = speed / distance * deltaX;
    
    // AKA
    
    this.vx = speed / 0 * deltaX;
    you can't divide by 0.
    of course!! 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 :)
    Last edited by totj; 01-09-2013 at 06:17 AM.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default Re: multiple rect collisions

    Quote Originally Posted by totj View Post
    or if a moderator can mark this as solved it would be cool :)
    Done.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Need help setting up collisions for a Java game
    By sur4j in forum New To Java
    Replies: 5
    Last Post: 12-08-2012, 02:12 PM
  2. MouseEntered to rect
    By g6pd in forum New To Java
    Replies: 3
    Last Post: 03-10-2011, 01:52 PM
  3. Solving Collisions in a car game
    By Rectal Exambot in forum Advanced Java
    Replies: 1
    Last Post: 09-29-2010, 02:13 PM
  4. Inelastic circle collisions
    By Atriamax in forum New To Java
    Replies: 9
    Last Post: 08-28-2010, 05:55 PM
  5. Check The Collisions
    By Luff in forum AWT / Swing
    Replies: 7
    Last Post: 06-23-2010, 12:30 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •