Page 3 of 3 FirstFirst 123
Results 41 to 56 of 56
Like Tree1Likes

Thread: pixelperfectcollision

  1. #41
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: pixelperfectcollision

    So if i understood correctly. 0 means no image. 19 means that the image is incomplete.

  2. #42
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: pixelperfectcollision

    Yep, the PixelGrabber isn't ready yet but as far as I can see it doesn't grab the pixels asynchronously; why not get the pixels from the BufferedImage yourself by calling its getRGB( ... ) method?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #43
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: pixelperfectcollision

    Exception in thread "Thread-2" java.lang.ArrayIndexOutOfBoundsException: Coordin
    ate out of bounds!
    at sun.awt.image.ByteInterleavedRaster.getDataElement s(Unknown Source)
    at java.awt.image.BufferedImage.getRGB(Unknown Source)
    at boss.Board.pixelPerfectCollision(Board.java:162)
    at boss.Board.checkCollisions(Board.java:131)
    at boss.Board.cycle(Board.java:118)
    at boss.Board.run(Board.java:193)
    at java.lang.Thread.run(Unknown Source)
    I get this error.

    Java Code:
    image.getRGB(cornerTopX, cornerTopY, width, height, pixels1, 0, width);

  4. #44
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: pixelperfectcollision

    Did you read (and understand) the first line of the exception message?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #45
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: pixelperfectcollision

    Java Code:
    int p1 = image.getRGB((cornerTopX + i), cornerTopY);
    			pixels1[i] = (p1 >> 24) & 0x000000FF;
    			int p2 = image2.getRGB((cornerTopX + i), cornerTopY);
    			pixels2[i] = (p2 >> 24) & 0x000000FF;
    After bit fo investigateing i found tha i have a problem in "image.getRGB((cornerTopX + i), cornerTopY );" It seem the coordinates i give it are out of bounds.

    Coordinates are taken from here
    Java Code:
    int cornerTopX = (r1.x>r2.x)?r1.x:r2.x;
    		int cornerBottomX = ((r1.x + r1.width) < (r2.x+r2.width))?(r1.x+r1.width):(r2.x + r2.width);
    		
    		int cornerTopY = (r1.y>r2.y)?r1.y:r2.y;
            int cornerBottomY = ((r1.y+r1.height) < (r2.y+r2.height))?(r1.y+r1.height):(r2.y+r2.height);
    Last edited by vastrolorde; 06-13-2013 at 03:32 PM.

  6. #46
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: pixelperfectcollision

    Did i mis understand something or this command is weird... Coordinates cant be bigger than the size of the array... so i can only use 0 and 1 as X and y coords?

  7. #47
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: pixelperfectcollision

    Why does it keep giving me outofbounds. I have tryed cornerTopx/Y, cornerBottomX/Y and i even typed in the coordinates manually, i still get the same result.

  8. #48
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: pixelperfectcollision

    At this point it's hard to follow the code. But I thought you were using a subImage. So subImages are indexed starting at 0,0 in upper left corner.
    Earlier I saw where you specified cornerTopx + i for the x coordinate in the subImage. I think it should just have been i.

    I suggest you print out the width and height of the subImage you are working with and the values of the coordinates used to index into getRGB().

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #49
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: pixelperfectcollision

    Java Code:
    public boolean pixelPerfectCollision(BufferedImage image, Rectangle r1, BufferedImage image2, Rectangle r2){
    
    		int cornerTopX = (r1.x>r2.x)?r1.x:r2.x;
    		int cornerBottomX = ((r1.x + r1.width) < (r2.x+r2.width))?(r1.x+r1.width):(r2.x + r2.width);
    		
    		int cornerTopY = (r1.y>r2.y)?r1.y:r2.y;
            int cornerBottomY = ((r1.y+r1.height) < (r2.y+r2.height))?(r1.y+r1.height):(r2.y+r2.height);
    
    		int width=cornerBottomX-cornerTopX;
            int height=cornerBottomY-cornerTopY;
    
            int[] pixels1 = new int[width*height];
            int[] pixels2 = new int[width*height];
    		for(int i = 0; i < pixels1.length;i++){		            
    			pixels1[i] = image.getRGB(((r1.x - cornerTopX)* -1), ((r1.y - cornerTopY)* -1));
    			pixels2[i] = image2.getRGB((cornerTopX), cornerTopY );
    			}
    		
    		      
            for(int i=0;i<pixels1.length;i++) {
                int a = (pixels1[i] >> 24) & 0x000000ff;
                int a2 = (pixels2[i] >> 24) & 0x000000ff;
                if(a > 0 && a2 > 0) return true;     
    
            }
    
            return false;
        }
    This is the current collision class.

    Java Code:
    for(int i = 0; i < 6; i = i+1){
    			frame[i] = image.getSubimage((i*80), 0,80,184);
    			}
    This is fromm boss.java, it creates the array of subimages used in the painting methods and in collision.

    In missile.java there are no subimages, because there is only 1 sprite.

    with and height of the boss sub image is 80X184. Missile is 8X1.

    The coordinates used for X and Y in getRGB are X > 320 and Y > 0.

    So i can use the getRGB withthe cornertops with the missile image, but for boss image i have to use coordinates from the image itself.
    So i need to make a formula that calculates the collision coordinates on the image, when the images left top corner is 0;0
    Last edited by vastrolorde; 06-14-2013 at 05:11 PM.

  10. #50
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: pixelperfectcollision

    Here is a section of your code:
    Java Code:
    for(int i = 0; i < pixels1.length;i++){
       pixels1[i] = image.getRGB(((r1.x - cornerTopX)* -1), ((r1.y - cornerTopY)* -1));
       pixels2[i] = image2.getRGB((cornerTopX), cornerTopY );
    }
    Why are you multiplying by -1 in getRGB? And why are you copying pixels from getRGB to an array? Why not just do your comparison as soon as you grab the pixel from the getRGB method? Also, you are copying the same pixels into the arrays because the arguments to getRGB aren't changing.

    Here is an example of what I mean:

    Java Code:
    int image1Startx; // you need to decide what these are
    int image1Starty;
    int image2Startx;
    int imgate2Starty;
    int imageWidth;
    int imageHeight;
    for (int x = 0; x < imageWidth; x++) {
      for (int y = 0; y < imageHeight; y++) {
           int a1 = (image.get(image1Startx +x, image1Starty + y)) >> 24) & 0x000000ff;
           int a2 = (image2.get(image2Startx +x, image2Starty + y)) >> 24) & 0x000000ff;
           if (a1 > 0 && a2 > 0)
                 return true;
       }
    }
    return false;


    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #51
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: pixelperfectcollision

    Well i have the start X and start Y. they are the cornerTopX and cornerTopY. imagewithd and image height i alsohave, i have with and height that represent the area of collision. image1 and image2 sart x and y are the same, since in neet to compare pixels from 2 different images at the same location. Or am i missing something?

  12. #52
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: pixelperfectcollision

    OK. If you are comparing two different images at the same location you need to make certain that the coordinates you supply are within the width and height of the images. And what about the issues I pointed out? E.g. multiplying by -1.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #53
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: pixelperfectcollision

    I chaned the coordinates calculation. The -1 multiply, it seemed logical at the time. the coordinates are whidin the images. cornertopx y iare the coordinates of the collision rectangle top left corner. might it be, because 1 of the images is at the height of 1 pixel, that this is causing troubles?

  14. #54
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: pixelperfectcollision

    Could it be thay maybe i have to subtract form the X and Y coord so that the top left corner of the bigger image could be represented with 0;0 ?

  15. #55
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Posts
    124
    Rep Power
    0

    Default Re: pixelperfectcollision

    I got it working :D. I used getMinX() and getMinY() to get the minimum x and y coordinates of the image. Since they were both 0 i subtracted the x and y coordinates of the rectangle from the coordinates where the collision happened and it started working :D.

    Java Code:
    int cornerTopX = (r1.x>r2.x)?r1.x:r2.x;
    		int cornerBottomX = ((r1.x + r1.width) < (r2.x+r2.width))?(r1.x+r1.width):(r2.x + r2.width);
    		
    		int cornerTopY = (r1.y>r2.y)?r1.y:r2.y;
            int cornerBottomY = ((r1.y+r1.height) < (r2.y+r2.height))?(r1.y+r1.height):(r2.y+r2.height);
    
    		int width=cornerBottomX-cornerTopX;
            int height=cornerBottomY-cornerTopY;
    		
    		for (int x = 0; x < width; x++) {  
    			for (int y = 0; y < height; y++) {       
    				int a1 = ((image.getRGB(cornerTopX +x - r1.x, cornerTopY + y - r1.y)) >> 24) & 0x000000ff;       
    				int a2 = ((image2.getRGB(cornerTopX +x - r2.x, cornerTopY + y - r2.y)) >> 24) & 0x000000ff;       
    				if (a1 > 0 && a2 > 0)  return true;   
    				}
    			}
    			return false;
    This is the working code. :D
    DarrylBurke likes this.

  16. #56
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: pixelperfectcollision

    Congratulations!! It was just a matter of time.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Page 3 of 3 FirstFirst 123

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •