Java - collision detection through Array-stored collision

I am currently working on a collision for my 2D game. I did some research and read that I should use the method of storing pixels of alpha value in the "mask" of the image of the object, and the same for the other. Then I take the x and y coordinates of both entities, as well as the height and width, and also create a Rectangle object and use the method Rectangle.intersects(Rectangle r)to check if they actually create an inorder collision to make it more efficient, rather than going through 2 for.

IF they intersect, then I create a new array with dimensions:

 int maxLengthY = Math.max(thisEntity.getMask().length, e.getMask().length);
 int maxLengthX = Math.max(thisEntity.getMask()[0].length, thisEntity.getMask()[0].length);

 int minX = Math.min(thisEntity.getX(), e.getX());
 int minY = Math.min(thisEntity.getY(), e.getY());

 int[][] map = new int[maxLengthX + minX][maxLengthY + minY];

and then add two other masks to this with their respective y and x "borders", for example:

for(int curX = 0; curX < maxLengthX + minX; curX++) { //only loop through the co-ords that area affected                                                              
for(int curY = 0; curY < maxLengthY + minY; curY++) {                                                                                                             

    int this_x = thisEntity.getX();                                                                                                                               
    int this_width = thisEntity.getImage().getWidth();                                                                                                            
    int this_y = thisEntity.getY();                                                                                                                               
    int this_height = thisEntity.getImage().getHeight();                                                                                                          
    int[][] this_mask = thisEntity.getMask();                                                                                                                     
    if(curX < (this_x + this_width) && curX < this_x) {//check that the co-ords used are relevant for thisEntity mask                                           

        if(curY < (this_y + this_height) && curY < this_y) {                                                                                                      
            map[curX][curY] = this_mask[Math.abs(curX - this_x)][Math.abs(curY - this_y)]; // store data from mask to map                                         
        }                                                                                                                                                         
    }                                                                                                                                                             

    int other_x = e.getX();                                                                                        
    int other_width = e.getImage().getWidth();                                                                     
    int other_y = e.getY();                                                                                        
    int other_height = e.getImage().getHeight();                                                                   
    int[][] other_mask = e.getMask();                                                                              

    if(curX < (other_x + other_width) && curX > other_x) { //check that the co-ords used are relevant for e mask 
        if(curY < (other_y + other_height) && curY > other_y) {                                                    
            if(map[curX][curY] == 1) { //check if this segment is already written by thisEntity                    
                map[curX][curY] = 2;   //if yes, set to 2 instead of e value to show collision                   
            } else {                                                                                               
                map[curX][curY] = other_mask[curX][curY]; // the minus to nullify minX and minY "requirements"     
            }                                                                                                      
        }                                                                                                          
    }                                                                                                              
}                                                                                                                  
}            

Array "", SO:

img ( 1337)

:

public Entity[] collisions(Entity thisEntity) {                                                                                                              
ArrayList<Entity> list = new ArrayList<Entity>();                                                                                                        
try {                                                                                                                                                    
    for (Entity e : getLevel().getEntities()) {                                                                                                          

        System.out.println("rect contains = "+thisEntity.getRect().contains(e.getRect()));                                                               

        if (!thisEntity.equals(e)) {                                                                                                                     
            Rectangle r = e.getRect();                                                                                                                   
            r = thisEntity.getRect();                                                                                                                    

            if (thisEntity.getRect().intersects(e.getRect())) {                                                                                          

                //get variables to create a space designated for the intersection areas involved                                                         
                int maxLengthY = Math.max(thisEntity.getMask().length, e.getMask().length);                                                              
                int maxLengthX = Math.max(thisEntity.getMask()[0].length, thisEntity.getMask()[0].length);                                               

                int minX = Math.min(thisEntity.getX(), e.getX());                                                                                        
                int minY = Math.min(thisEntity.getY(), e.getY());                                                                                        

                int[][] map = new int[maxLengthX + minX][maxLengthY + minY]; //create a matrix which merges both Entity mask to compare                                                                                                                                                                                                                                                                                                                                       

                for(int curX = 0; curX < maxLengthX + minX; curX++) { //only loop through the co-ords that area affected                                 
                    for(int curY = 0; curY < maxLengthY + minY; curY++) {                                                                                

                        int this_x = thisEntity.getX();                                                                                                  
                        int this_width = thisEntity.getImage().getWidth();                                                                               
                        int this_y = thisEntity.getY();                                                                                                  
                        int this_height = thisEntity.getImage().getHeight();                                                                             
                        int[][] this_mask = thisEntity.getMask();                                                                                        
                        if(curX < (this_x + this_width) && curX > this_x) {//check that the co-ords used are relevant for thisEntity mask              

                            if(curY < (this_y + this_height) && curY > this_y) {                                                                         
                                map[curX][curY] = this_mask[Math.abs(curX - this_x)][Math.abs(curY - this_y)]; // store data from mask to map            
                            }                                                                                                                            
                        }                                                                                                                                

                        int other_x = e.getX();                                                                                                          
                        int other_width = e.getImage().getWidth();                                                                                       
                        int other_y = e.getY();                                                                                                          
                        int other_height = e.getImage().getHeight();                                                                                     
                        int[][] other_mask = e.getMask();                                                                                                

                        if(curX < (other_x + other_width) && curX > other_x) { //check that the co-ords used are relevant for e mask                   
                            if(curY < (other_y + other_height) && curY > other_y) {                                                                      
                                if(map[curX][curY] == 1) { //check if this segment is already written by thisEntity                                      
                                    map[curX][curY] = 2;   //if yes, set to 2 instead of e value to show collision                                     
                                } else {                                                                                                                 
                                    map[curX][curY] = other_mask[curX][curY]; // the minus to nullify minX and minY "requirements"                       
                                }                                                                                                                        
                            }                                                                                                                            
                        }                                                                                                                                
                    }                                                                                                                                    
                }                                                                                                                                                                                                                                                                             
            }                                                                                                                                            
        }                                                                                                                                                
    }                                                                                                                                                    
} catch (Exception excp) {                                                                                                                               
    excp.printStackTrace();                                                                                                                              
}                                                                                                                                                        
return list.toArray(new Entity[1]);                                                                                                                      
}                                                                                                                                                            

, getMask():

    public int[][] getMask() {
        return mask;
    }

    ...

    private void createMask(BufferedImage image) {

    final int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
    final int width = image.getWidth();
    final int height = image.getHeight();
    final boolean hasAlphaChannel = image.getAlphaRaster() != null;

    int[][] result = new int[height][width];
    if (hasAlphaChannel) {
        for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += 4) {
            int alpha = pixels[pixel];
            if(alpha != 0) {
                result[row][col] = 1;
            } else {
                result[row][col] = 0;
            }

            if (col == width) {
                col = 0;
                row++;
            }
        }
    }
    mask = result;

}

... , , , IndexOutOfBounds, , , , -...

, , :

  • ?
  • ?
  • ?
  • ? , ?
+4
1

, ? (entity.mask [0] [0] , [0] [0] "" ) :

map[curX][curY] = other_mask[curX][curY];

( , Math.abs(a-b))

, .

+1

All Articles