7 Replies Latest reply: Dec 5, 2009 3:15 PM by 795507 RSS

    2D Grid Collision Detection

    843853
      Hi guys, im having a problem understanding 2D grid collision detection for my puyo puyo game a game similar to tetris... i currently have a 12 by 6 grid/game board on which my puyos fall...what i am unable to understand/code is how do i check to see if an area on the grid/board is occupied by a puyo and if it is then when another puyo falls and tries to fit into that occupied space how would i have it stop on top of the other puyo instead of just overlapping it? Ive tried a lot of different things none of which have worked i posted a similar question on another forum and received an answer but it wasn't exactly what i was looking for...so far i figured why not try listing some of the possible coordinates and see if they are occupied or not but i haven't gotten anywhere so if anyone can help me id really appreciate it thanks. Here is my code so far for grid collision:
          //The code here represents coordinates along the bottom of my game board
         //but i am unsure what else to code in order to have it check to see if there is a puyo
        //in the specified location and if there is have the puyo stop on top of the puyo occupying
       //the space...unsure if there is an easier way to do this...
              p1 = puyolist[0][11];
               p2 = puyolist[1][11];
               p3 = puyolist[2][11];
               p4 = puyolist[3][11];
                  p5 = puyolist[4][11];
               p6 = puyolist[5][11];  
            
          if(p1 != null && p2 != null){
            if(p3 != null && p4 != null){
              if(p5 != null && p6 != null){
      
         }
        }
      }
       
           // The code above will be posted here in the pain component...here i have an array which draws the puyos
          // to the board i just wanted to post this so people would now that i am using an array to draw my images
         // im not sure how to use the code above to check if the images drawn collide or not
      
      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);
            }
            
      Edited by: Riz01 on Nov 23, 2009 10:54 AM
        • 1. Re: 2D Grid Collision Detection
          796262
          Riz01 wrote:
          Hi guys, im having a problem understanding 2D grid collision detection for my puyo puyo game a game similar to tetris... i currently have a 12 by 6 grid/game board on which my puyos fall...what i am unable to understand/code is how do i check to see if an area on the grid/board is occupied by a puyo and if it is then when another puyo falls and tries to fit into that occupied space how would i have it stop on top of the other puyo instead of just overlapping it? Ive tried a lot of different things none of which have worked i posted a similar question on another forum and received an answer but it wasn't exactly what i was looking for...so far i figured why not try listing some of the possible coordinates and see if they are occupied or not but i haven't gotten anywhere so if anyone can help me id really appreciate it thanks. Here is my code so far for grid collision:
          Not going with my solution? :(

          What is puyolist a 2D array of? I'm assuming they're puyo Objects of some kind, which is a weird way to store them (IMHO, anyway).

          If you want to use a grid approach, there are two ways that pop out to me:

          Have a 2D array of booleans that every puyo Object references. If a position of the boolean array is true, that means it is occupied. Before attempting to move to a new position, a puyo checks against this array. If the puyo can/does move, it updates the boolean array.

          The other option is to have each puyo have an array (or a List) of the position(s) it is taking up. When a puyo moves, it has to check against the Lists of all of the other puyos to make sure it can move to the appropriate position.
          • 2. Re: 2D Grid Collision Detection
            843853
            Heh...its not that im not going with your solution i gave it it try but i didn't get anywhere with it...anyway puyolist is a 2D array of images each 32 by 32 question what your saying makes sense create a list which the images check against but what exactly would cause the images to stop in the end?
            • 3. Re: 2D Grid Collision Detection
              gimbal2
              You don't need an array of booleans, you already have your array of images. If a cell is null, it is empty, if it is not null it is occupied. It is as simple as that. Guiding you any further is going to be difficult however because you seem to ignore basic advice given in previous posts. For example, why is your puyo list still a 2D array? I advised you to turn it into a class that can do simple operations on the grid. This makes your code much more readable and it allows you to define your problems better.

              In any case: how to stop a puyo from entering the space of another puyo.
              if(cell[y+1][x] != null){
               // CELL BELOW ME IS BLOCKED, DON'T MOVE!
              }
              Of course you will need to add a check if the puyo is not in the bottom row.
              • 4. Re: 2D Grid Collision Detection
                796262
                gimbal2 wrote:
                You don't need an array of booleans, you already have your array of images.
                ...
                For example, why is your puyo list still a 2D array?
                Yeah, the only reason I offered the 2D array of booleans suggestion was because it seemed very strange to store the puyos in the 2D array. I might be done trying to help on this one. :-/
                • 5. Re: 2D Grid Collision Detection
                  843853
                  Hi gimbal2, its not that i didnt take your advice its just that honestly i have absolutely no idea how to implement what you are saying...if it is at all possible to explain how to implement what you are saying concerning creating a class in order to do simple operations id really appreciate it...thanks.

                  Edited by: Riz01 on Nov 24, 2009 7:57 PM
                  • 6. Re: 2D Grid Collision Detection
                    843853
                    After doing some research i was able to come up with this i created a new class a Boolean class which returns true if the cell is occupied and false if it isnt... i also created a new array in which i put board width and the board height and then within my collision code i check the length of the board width and height and then do my calculations to check if certain cells are occupied. I defined the specific coordinates at the bottom of the board as well i.e p1 = puyolist[0][11]...if this helps clarify things i really hope someone could help me to figure out what else i am missing in order to get this to finally work...thanks.

                    Also the puyos are not stored in the 2D array they are just being drawn by the 2D array puyolist...puyolist cycles through and checks the board to see if any spots on the board are not null it isnt if there is a puyo on the board then it goes ahead and draws the next set of puyo images. The way my program works is that i define two current puyos i then draw those two puyos to the game board. Once they reach the bottom of the board the location of the puyo is saved so it does not disappear and then it is redrawn making look as if another puyo was loaded and dropped but in actuality im simply saving the location of the current puyo and then redrawing it and dropping it again...hope this helps to solve my problem
                           
                           public boolean collisionTest()
                             {
                            p1 = puyolist[0][11];
                             p2 = puyolist[1][11];
                             p3 = puyolist[2][11];
                             p4 = puyolist[3][11];
                                p5 = puyolist[4][11];
                             p6 = puyolist[5][11];
                    
                                  for(int i = 0; i < accumBalls.length; i++){
                                  for(int j = i + 1; j < accumBalls[0].length; j++){
                                     
                                      
                                        if(accumBalls[i][j] != null){
                                            if(p1 != null && p2 != null)
                                                 
                                             return true;
                                      }
                                   }
                              }
                           return false;
                         }
                    Edited by: Riz01 on Nov 24, 2009 8:34 PM
                    • 7. Re: 2D Grid Collision Detection
                      795507
                      One thing you could do is input a velocity variable in your code. Set it to 1 initially (i.e. move one cell per tick), and when your object collides with something, set the velocity to 0.

                      Just what I would do, anyway.