Collison Detection

Cant get collision detection to work

Page 1 of 1

3 Replies - 2608 Views - Last Post: 14 December 2009 - 08:54 PM Rate Topic: -----

#1 Riz 01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 27-August 09

Collison Detection

Post icon  Posted 19 October 2009 - 07:47 PM

Hi guys im currently making a puyo puyo game but i cant seem to get my collision detection working im using bounding rectangles in order to check for collision but i cant seem to get it to work. Id appreciate any help/advice you guys might have. Here is my code

 //Draw Player 1(BOX)
	Rectangle box1 = new Rectangle(current[0].x,current[0].y, image_height, image_width);
		
		//Draw Player 2(BOX)
		
		Rectangle box2 = new Rectangle(current[1].x,current[1].y, image_height, image_width);

		public void checkCollisions(boolean box1, box2){
			 for(int i=0; i<box1.length; i++)
			 {
				  for(int j=0; j<box2.length; j++)
				  {
					  if(box1.intersects(box2));
						  return true;
				  }
			 }
			  return false;
		};


here is the complete code for my logic class

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;  
import javax.swing.event.*;
import java.util.Random;




public class PuyoLogic extends JPanel { 

  final int board_height = 12;
  final int board_width = 6;
  final int image_height= 32;
  final int image_width = 32;
  final int MAX_X = board_width*image_width;
  final int MAX_Y = board_height*image_height;
  private static final int DELTA_Y = 2;
  private static final int TIMER_DELAY = 20;

  Image images[]; 
  Puyo puyolist[][];
  Puyo current[];
  Timer pptimer;
  Random myRand = new Random(); 
  boolean keyRight,keyLeft,keyUp; 
  
	 public PuyoLogic() {
	 super();
   	 
   	 puyolist = new Puyo[board_width][board_height];
   	 
   	 current = new Puyo[2];
   	 current[0] = new Puyo(myRand.nextInt(4), (board_width * image_width /3), 0);
   	 current[1] = new Puyo(myRand.nextInt(4), (board_width * image_width /3)+image_width, 0);
   	 
	 images = new Image[4];
	 images[0] = Toolkit.getDefaultToolkit().getImage("puyo_yellow.png");
	 images[1] = Toolkit.getDefaultToolkit().getImage("puyo_blue.png");
	 images[2] = Toolkit.getDefaultToolkit().getImage("puyo_green.png");
	 images[3] = Toolkit.getDefaultToolkit().getImage("puyo_red.png");
	  
	 setFocusable(true);
	 PuyoMove puyo_move = new PuyoMove(); // Make a new video game KeyListener
	 addKeyListener(puyo_move);
	 setBackground(Color.BLACK);
	 

	  pptimer = new Timer(TIMER_DELAY, new TimerAction());
	  pptimer.start();
	 }  
	 	
	   public void setAnimation(boolean OnandOff){
		if (OnandOff) {
			pptimer.start();  
		} else {
			pptimer.stop();
		}
	  }
	  
	  //Draw Player 1(BOX)
	Rectangle box1 = new Rectangle(current[0].x,current[0].y, image_height, image_width);
		
		//Draw Player 2(BOX)
		
		Rectangle box2 = new Rectangle(current[1].x,current[1].y, image_height, image_width);

		public void checkCollisions(boolean box1, box2){
			 for(int i=0; i<box1.length; i++)
			 {
				  for(int j=0; j<box2.length; j++)
				  {
					  if(box1.intersects(box2));
						  return true;
				  }
			 }
			  return false;
		};
		

	  
	
	 

	 public void NewPuyo(){
   	 current[0] = new Puyo(myRand.nextInt(4), (board_width * image_width /3), 0);
   	 current[1] = new Puyo(myRand.nextInt(4), (board_width * image_width /3)+image_width, 0);
  
  
	}
	public void puyoBoundsRight(Puyo[] current){
	if ((current[0].x + image_width <= MAX_X - image_width) && (current[1].x + image_width <= MAX_X - image_width)){
	current[0].x += image_width;
	current[1].x += image_width;
  }
 }

	public void puyoBoundsLeft(Puyo[] current){
	if ((current[0].x - image_width >= 0) && (current[1].x - image_width >= 0)){
	current[0].x -= image_width;
	current[1].x -= image_width;
  } 
}   

	 private class PuyoMove implements KeyListener {
	 	
	 public void keyTyped(KeyEvent k){}
	 public void keyReleased(KeyEvent k){}
	 public void keyPressed(KeyEvent k){
	 	
		switch (k.getKeyCode()){
	 	
	 	case KeyEvent.VK_LEFT:
	 		keyLeft = true;
	 		break;
	 		
	 	case KeyEvent.VK_RIGHT:
	 		keyRight = true;
	 		break;
	   }
	 }
   }
   
	   

   
		public void paintComponent(Graphics g){
		super.paintComponent(g);  
		for(int i = 0; i < board_width; i++){
			for(int j = 0; j < board_height; j++){
			
				if(puyolist[i][j] != null)
					g.drawImage(images[puyolist[i][j].color],puyolist[i][j].x,puyolist[i][j].y,this);
			}
		}
		
		
		

		g.drawImage(images[current[0].color],current[0].x,current[0].y,this);
		g.drawImage(images[current[1].color],current[1].x,current[1].y,this);
	  }
		
	   
		class TimerAction implements ActionListener {
	   
		public void actionPerformed(ActionEvent e) {
	 
		  if (keyLeft){
		   
			
			puyoBoundsLeft(current); 
			keyLeft = false;
			
		}
		  else if (keyRight) {  
			  
			puyoBoundsRight(current);
			keyRight = false;
		   
		  }
		  if (current[0].y + image_height <= MAX_Y && current[1].y + image_height  <= MAX_Y)
		  {
	 		
	 		
	 		current[0].y += DELTA_Y;
	 		current[1].y += DELTA_Y;
	 	
		} 
		else {
				  
				   
				  puyolist[current[0].x/image_width][current[0].y/image_height] = current[0];
				  puyolist[current[1].x/image_width][current[1].y/image_height] = current[1];
				  NewPuyo();
			 }
			 repaint();
		  }
		}
	  }


Is This A Good Question/Topic? 0
  • +

Replies To: Collison Detection

#2 SixOfEleven  Icon User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Collison Detection

Posted 19 October 2009 - 08:19 PM

Part of your problem may be where you create your Rectangles. It might not matter if your Rectangles are actually squares but your height and width parameters are reversed. Rectangles are constructed with (X, Y, Width, Height) from what I recall.

I believe that the intersects method alone will tell you if the Rectangles will intersect. Just doing:

if (box1.intersects(box2))
  // Collision detected



Should tell you if the two Rectangles intersect.
Was This Post Helpful? 0
  • +
  • -

#3 HunterE30633  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 21-October 09

Re: Collison Detection

Posted 14 December 2009 - 05:10 PM

View PostSixOfEleven, on 19 Oct, 2009 - 07:19 PM, said:

Part of your problem may be where you create your Rectangles. It might not matter if your Rectangles are actually squares but your height and width parameters are reversed. Rectangles are constructed with (X, Y, Width, Height) from what I recall.

I believe that the intersects method alone will tell you if the Rectangles will intersect. Just doing:

if (box1.intersects(box2))
  // Collision detected



Should tell you if the two Rectangles intersect.


For
box1.intersects(box2)

So I'm looking at the API for class Rectangle, does the intersect method only check to see if lines cross? Would it return true if say one rectangle was completely inside of another one?
Was This Post Helpful? 0
  • +
  • -

#4 PixelStation  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 44
  • Joined: 12-November 09

Re: Collison Detection

Posted 14 December 2009 - 08:54 PM

Quote

intersects

public boolean intersects(Rectangle r)

Determines whether or not this Rectangle and the specified Rectangle intersect. Two rectangles intersect if their intersection is nonempty.

Parameters:
r - the specified Rectangle
Returns:
true if the specified Rectangle and this Rectangle insersect; false otherwise.


It returns true if the rectangles are overlapped in any situation, including if one surrounds the other.

This post has been edited by PixelStation: 14 December 2009 - 08:55 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1