# pixelperfectcollision

Show 40 post(s) from this thread on one page
Page 1 of 3 123 Last
• 06-05-2013, 02:52 PM
vastrolorde
pixelperfectcollision
Code:

```public class collision{         public boolean pixelPerfectCollision(BufferedImage image, Rectangle r1, BufferedImage image2, Rectangle r2){                 //rectangle r1 = image.getBounds();                 //rectangle r2 = image2.getBounds();                                 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];                                 PixelGrabber pg1 = new PixelGrabber(image, cornerTopX, cornerTopY, width, height, pixels1, 0, width);         PixelGrabber pg2 = new PixelGrabber(image2, cornerTopX, cornerTopY, width, height, pixels2, 0, width);         try {             pg1.grabPixels();             pg2.grabPixels();         } catch (InterruptedException ex){             System.out.println("interrupted");         }         for(int i=0;i<pixels1.length;i++) {             int a = (pixels1[i] >>> 24) & 0xff;             int a2 = (pixels2[i] >>> 24) & 0xff;             if(a > 0 && a2 > 0) return true;            }         return false;     } }```
Whats wrong here? For somereason it doesent work correct. all i get is that a and a2 equal 0 even when collision is happening.
• 06-05-2013, 04:28 PM
kalata
Re: pixelperfectcollision
Hi, just an example, though it may not be the reason and there for not helpful at all:

256 = 00000000 00000000 00000001 00000000

0xff = 255 = 00000000 00000000 00000000 11111111

A = 256 >>> 24 = 00000000 00000000 00000000 00000000 (shifted bits are filled with zeros)

A & 0xff = 00000000 00000000 00000000 00000000 = 0

BR,
Kalin
• 06-05-2013, 04:53 PM
vastrolorde
Re: pixelperfectcollision
So you say that the shifting is false?? or is it too big?
• 06-05-2013, 06:11 PM
jim829
Re: pixelperfectcollision
A couple of suggestions.

First, check out SwingUtilities (computeIntersection and computUnion) -- no need to reinvent the wheel. Your bit shifting appears to be fine. Are you certain that the high order 8 bits are non-zero in the test cases when collision is happening? Normally, that is where the alpha composite value is stored.

Regards,
Jim
• 06-05-2013, 06:25 PM
vastrolorde
Re: pixelperfectcollision
OK i think i found whats wrong here. All the values in the arrays pixels1 and pixels 2 are 0
• 06-05-2013, 08:20 PM
kalata
Re: pixelperfectcollision
My point was that when shifting with 24 bits to the right all values less than 2^24 will be zero. Jim actually gave some useful info, mine was a blind shot at array values at all :)

Cheers,
Kalin
• 06-05-2013, 08:27 PM
vastrolorde
Re: pixelperfectcollision
I see. but what would be your suggestion to get this code working? All the tests is did, whenever the collision happened both arrays only had 0 and nothing else
• 06-05-2013, 11:32 PM
jim829
Re: pixelperfectcollision
Are you absolutely certain that there is an image to grab? I recommend you incorporate grabbing a subimage in your collision code and drawing it out on a separate panel, just to be certain. You may also want to print some statements in your collision method. Example, the resulting rect from the intersection computation.

Regards,
Jim
• 06-06-2013, 08:21 AM
vastrolorde
Re: pixelperfectcollision
If i print out the values of pg1 ond the cornertopX etc. then i get the values of those. but if i print out a value of pixels1 or pixels2 its always 0. Oh and since i dont understadnd the paint function fully, i cant add the paint function tothe collision class, to draw the subimaes sent to pixelperfectcollision.
• 06-06-2013, 01:12 PM
jim829
Re: pixelperfectcollision
Your rectangles can be correct but that doesn't mean there is an image in image buffer. But have you actually drawn the image into the buffer? That's where the pixels are coming from.

Regards,
Jim
• 06-06-2013, 02:15 PM
vastrolorde
Re: pixelperfectcollision
So instead, the problem might be in the way how i call the pixeleprfectcollision function. That the images arent sent to the function.

Code:

```board.java has these lines private collision test; test.pixelPerfectCollision(boss.getFrame(),r4 ,m.getImage(), r1 ) Boss.java has these lines public BufferedImage getFrame(){                         if(sleep == 8){                                 sleep = 0;                                 return frame[sequence[nextframe()]];                                 }                         else{                                 sleep += 1;                                 return frame[sequence[currentframe]];                                 }                         } missile.java has these lines public BufferedImage getImage() {         return image;     } This is how i get the images. only difference between missile and boss is that missile doesent have that for loop, because it is only 1 picture, but boss4 is a spritesheet of 6 sprites. BufferedImage image = null;                 try{                         image = ImageIO.read(new File("C://Users//Marko//Desktop//java//boss//Boss4.png"));                 } catch (IOException e) {         }                 for(int i = 0; i < 6; i = i+1){                         frame[i] = image.getSubimage((i*80), 0,80,184);                         }```
• 06-07-2013, 08:59 AM
vastrolorde
Re: pixelperfectcollision
Is there a problem in these lines relating to the pixelperfectcollision or not???
• 06-07-2013, 01:15 PM
jim829
Re: pixelperfectcollision
It's difficult to say without seeing all your code (which you should *not* post). You need to draw the images from which you are getting your pixels so you can check the images. What are r4 and r1 set too in the call to the pixelPerfectCollisionMethod? I can make several suggestions:

1. Create a small Short, Self Contained, Correct Example that demonstrates the problem.
2. Use print statements to help track specific values and make certain they are what you expect them to be.
3. Learn to paint inside a JPanel so you can visually scrutinize the images you are testing.
4. Write smaller programs to test out parts of larger programs to ensure understanding of the API's etc.

And be patient with replies. Sometimes folks may take several days perhaps even weeks to get back to you. While they are doing that, perhaps thinking about the problem, you can continue to try new things and reply to your own thread. It will mark it as unread and can also act as a trigger to remind folks who may have forgotten.

Regards,
Jim
• 06-07-2013, 02:09 PM
vastrolorde
Re: pixelperfectcollision
r4 and r1 are the rectangles that surround the sprites, they are needed for the pixelperfectcollision, to mark the area where the pixels are where dectection should be done.

Printstatment, i have printed everysingle valu there is in hte collision detection. And only pixels1 and pixels2 give me 0 on all their elements.
I have already written a smaller program to test this collision code, it has only 3 moving objects(missile, creature, craft) and the backround.
I tryed to put a paint function into the .java file where the collision code is situated, but i ran into problems that i dont know how to solve.

pixelperfectcollision is situated in collision.java. the main code is in board.java. how can i call pixelperfectcollision from board.java? I am thinking that maybe this is the problem.
• 06-07-2013, 03:36 PM
jim829
Re: pixelperfectcollision
Quote:

how can i call pixelperfectcollision from board.java? I am thinking that maybe this is the problem.
There are several ways.

1. Put it as a method in Board.java
2. Put it inside a separate inner class within the Board class
3. Make it a public method inside another public class which can be either called as a static method or via an instance of its enclosing class.

This may require some slight redesign to ensure all variables and methods are available to make use of the method in this way.

Regards,
Jim
• 06-07-2013, 09:15 PM
vastrolorde
Re: pixelperfectcollision
OK im more than certain about that, for somereason the image that the pixelperfectcollision gets is a empty image.
• 06-08-2013, 11:25 AM
vastrolorde
Re: pixelperfectcollision
BufferedImage@13c4c09: type = 6 ColorModel: #pixelBits = 32 numComponents = 4 co
lor space = java.awt.color.ICC_ColorSpace@170ec24 transparency = 3 has alpha = t
rue isAlphaPre = false ByteInterleavedRaster: width = 80 height = 184 #numDataEl
ements 4 dataOff[0] = 1603

What does this mean? i used command System.out.println(image); Image is bufferedimage type
• 06-10-2013, 09:24 AM
vastrolorde
Re: pixelperfectcollision
BufferedImage@13c4c09: type = 6 ColorModel: #pixelBits = 32 numComponents = 4 co
lor space = java.awt.color.ICC_ColorSpace@170ec24 transparency = 3 has alpha = t
rue isAlphaPre = false ByteInterleavedRaster: width = 80 height = 184 #numDataEl
ements 4 dataOff[0] = 1603

If this means that the picture exsists then the problem is within the pixelgrabber. But i dont understadn what this means.
• 06-10-2013, 10:57 AM
JosAH
Re: pixelperfectcollision
That is a String representation of a BufferedImage; that's what it's toString() method produces and contains quite a bit of information about that particular BufferedImage.

kind regards,

Jos
• 06-10-2013, 11:31 AM
vastrolorde
Re: pixelperfectcollision
ok. to me it seems, that the image is sent to the function. So the problem is in pixelgrabber. im probably missing some parameters or some parameter are giving fasle results.
Show 40 post(s) from this thread on one page
Page 1 of 3 123 Last