Collision Detection in simple 2D games

I’m having some trouble finding out how this works. I’m making a tetris game in java and am able to make the blocks and all but the collision detection method that i have currently is not always (actually very rarely) accurate (important in Tetris :D)

Here is how i do it:

From main class Main class


...
/**
	 * This method controlls all of the logic in the game. Determines if a new block needs to be created,
	 * if the falling statues of the block should be changed, if the block needs to be moved left or right, etc.
	 *
	 */
	public void update(){
		
		boolean fallingBlockExists = false; // assume that there isn't one falling
		
		for(Block b: blocks){
			
			if(b.isFalling()){
				for(Block other: blocks){
					// first detect if the blocks have collided with eachother... if it has then stop the falling one
					if(b.hasColliedWith(other, getBufferStrategy().getDrawGraphics())) b.isFalling(false);
				}
			}
			
			if(b.isFalling()) fallingBlockExists = true; //if there is a falling block then theres no need to make more
		}
		
		if(blocks.size() == 0 || fallingBlockExists == false){
			blocks.add(new Block(0 + (int)(Math.random() * xLIMIT)));
		}
		
		for(Block b: blocks){
			
			
			if(leftPressed && !rightPressed && b.isFalling() && (b.getX() > 10)){ 
				// can't have both pressed at the same time...
				b.moveLeft();
			}
			
			if(rightPressed && !leftPressed && b.isFalling() && (b.getX() < xLIMIT)){
				//can't have both pressed at the same time...
				b.moveRight();
			}
			
			if(b.getY() >= yLIMIT) b.isFalling(false);
			
			if(b.isFalling()) b.moveDown();
		}
		
		drawStuff();
		
		try{Thread.sleep(50);}catch(Exception e){};
	}

...

and in the block class

	
...
public boolean hasColliedWith(Block other, Graphics g){
		Graphics2D g2D = (Graphics2D) g;
		otherRect.setRect(other.getX(), other.getY(), other.getWidth(), other.getHeight());
		
		return(g2D.hit((Rectangle)myRect, (Shape)otherRect, true));	
	}
...

I’ve also tried using the intersect() method from the rectangle class and had same results. Any help would be greatly appreciated…

Thanks :]

Well you can use simple bounding box collision detection, if you´re making a tetris game, every piece of tetris is made of rects
for example a


http://img233.imageshack.us/img233/9808/tetrishelpbe5.th.png

              [b]Determining Intersections with Bounding Boxes[/b]

The easiest way to determine if two sprites intersect is by comparing their bounding boxes. A bounding box is the smallest rectangle, with edges parallel to the x and y coordinates, that contains the entire sprite.

Here’s a simple formula for determining if two sprites intersect. Let’s say that the smallest pair of coordinates of box 1 is (x1,y1), and the largest pair is (x2,y2). Similarly, box 2’s smallest coordinates are (x3,y3), and the largest are (x4,y4). Figure 5-11 shows these two boxes in the applet coordinate system. Box 1 intersects with box 2 if and only if the following condition is true:

(x2 >= x3) && (x4 >= x1) && // x-extents overlap

(y2 >= y3) && (y4 >= y1) // y-extents overlap

Here’s another way of describing this equation. The x extent of a box is the range of x coordinates that the box occupies; y extents are defined analogously for y coordinates. The two boxes intersect if both their x extents and their y extents overlap. You can extend this intersection formula to three dimensions by testing whether the z extents also overlap

So this is the boolean you need :wink:

// compare bounding boxes

public boolean intersect(int x1,int y1,int x2,int y2) {

  return (x2 >= locx) && (locx+width >= x1)
    && (y2 >= locy) && (locy+height >= y1);

}

This routine checks if the sprite at (locx,locy), with the given width and height, intersects the bounding box between the coordinates (x1,y1) and (x2,y2).

so as example you can add a inspection rutine in the update method


public void update (Graphics g){

if(intersect(objective_x,objective_y,
                               objective_x+objective_h,objective_y+objective_w)){

                               	Puntos++;
                               	objective_x = Rndm_num(applet_width - objective_w);
                               	objective_y = Rndm_num(applet_height - objective_h);
                               }
. . . . 
}

Now the only thing you have to do is implementing this boolean in your code and defining sets of Rects to each piece of tetris :slight_smile:

this looks good, thanks. I wasn’t using sprites at first but i can figure out how it would work with the Graphics class… might even change over to real sprites.

Ok, will try this out when i get home tonight. Thanks for the help!