Checking elements in an Array against other elements

How to check if a location on the JFrame is taken up by an object

Page 1 of 1

4 Replies - 795 Views - Last Post: 30 October 2009 - 02:56 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

Checking elements in an Array against other elements

Post icon  Posted 30 October 2009 - 10:25 AM

Hey im once again asking about collision detection, i learned that using bounding boxes for my puyo puyo game was not the easiest thing to use to check for collision. The method i was suggested to use was taking the current puyo and checking it against every other puyo in my puyo array. Basically check if there is a puyo in a particular location on the game board and if another puyo tries to fit in that location there is a collision. But my problem is i really don't know how i could implement this. If anyone can help and provide me with some advice id really appreciate it thanks. Here is what i tried so far


public boolean drop(){   
	   boolean dropped=false; //indicate one ball has hit one of the accumulated balls
	   for(int i=0;i<2;i++){
		 if(puyolist[current[i].x][current[i].y-1] !=  null){
			 puyolist[current[i].x][current[i].y]=current[i].color;
			 dropp=true;
		 }
	   }	   
   	 }


Here is the full code of my logic class with the array list and this code implemented
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 numDroppingPuyos = 2;
  public static final int puyoEmpty=0;
  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[];
  Rectangle r1, r2;
  Timer pptimer;
  Random myRand = new Random(); 
  boolean keyRight,keyLeft,keyUp; 
  boolean collide;
  
	 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();
		}
	  }
	  
	 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 boolean drop(){   
	   boolean dropped=false; //indicate one ball has hit one of the accumulated balls
	   for(int i=0;i<2;i++){
		 if(puyolist[current[i].x][current[i].y-1] !=  null){
			 puyolist[current[i].x][current[i].y]=current[i].color;
			 dropp=true;
		 }
	   }	   
   	 }
   	   
   	
		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);
				 r1 = new Rectangle(current[0].x,current[0].y,image_width,image_height);
				 r2 = new Rectangle(current[1].x,current[1].y,image_width,image_height);
			}
		}
			
		
		
			
			  /*for (int i = 0; i < puyolist.length; i++){
				  for (int j = i + 1; j < puyolist.length; j++){
				  if((r1.intersects(puyolist[i][j]))&&(r2.intersects(puyolist[i][j]))){
					  
					  System.out.println("" + puyolist.length);
						  System.out.println("" + puyolist[0].length);
					 pptimer.stop();
				  }
			  }
			}*/
   	
		
		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();
		  }
		}
	  }

This post has been edited by Riz 01: 30 October 2009 - 11:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Checking elements in an Array against other elements

#2 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,103
  • Joined: 26-September 08

Re: Checking elements in an Array against other elements

Posted 30 October 2009 - 12:45 PM

So is it that you have multiple objects in your array of different types? Because if that IS the case you should try using the keyword

object instanceof Puyo


using the appropriate object name on the left, and the appropriate class name on the right.

Other than that you'll have to get a little more specific.
Was This Post Helpful? 0
  • +
  • -

#3 Riz 01  Icon User is offline

  • New D.I.C Head

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

Re: Checking elements in an Array against other elements

Posted 30 October 2009 - 01:01 PM

View PostTriggaMike, on 30 Oct, 2009 - 11:45 AM, said:

So is it that you have multiple objects in your array of different types? Because if that IS the case you should try using the keyword

object instanceof Puyo


using the appropriate object name on the left, and the appropriate class name on the right.

Other than that you'll have to get a little more specific.



No i have an array full of the same types. My array is full of puyo images if you have ever played the game they are different colored balls that are connected side by side. Puyo puyo is basically a variation on tetris. Anyway i have a grid that is 12 X 6 and within that grid i want to check a set of connected puyos that has fallen and is a the bottom of the frame against a set of puyos that is currently falling. If the space is occupied by a set of puyos i want there to be a collision and have the images stop. Like in tetris when block fall and stop on top of each other. Hope this makes some sense.
Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Checking elements in an Array against other elements

Posted 30 October 2009 - 01:23 PM

I take it that this is going to be on a grid?
If so then look into Gridworld, it is a nice way to do these thigns easily, or just make your own grid and at each location check to see if it is null at the location and if it isnt then you know that there is already a Puyo there?
Was This Post Helpful? 0
  • +
  • -

#5 Riz 01  Icon User is offline

  • New D.I.C Head

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

Re: Checking elements in an Array against other elements

Posted 30 October 2009 - 02:56 PM

View PostFuzzyness, on 30 Oct, 2009 - 12:23 PM, said:

I take it that this is going to be on a grid?
If so then look into Gridworld, it is a nice way to do these thigns easily, or just make your own grid and at each location check to see if it is null at the location and if it isnt then you know that there is already a Puyo there?


Hi,
Yes it is going to be on a grid and i already have a grid i understand the theory if there is something say in cooridinates 0,11 then there is a collision my problem is, is that i really dont have any idea how to implement this in code form. Ive tried and ive posted my best effort but im really stuck so if youve got any advice please let me know.

Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1