Page 1 of 3 123 LastLast
Results 1 to 20 of 56
Like Tree1Likes

Thread: pixelperfectcollision

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

    Default pixelperfectcollision

    Java 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.

  2. #2
    kalata is offline Member
    Join Date
    Aug 2011
    Location
    Bulgaria
    Posts
    29
    Rep Power
    0

    Default 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

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

    Default Re: pixelperfectcollision

    So you say that the shifting is false?? or is it too big?

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

    Default 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
    Last edited by jim829; 06-05-2013 at 07:14 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: pixelperfectcollision

    OK i think i found whats wrong here. All the values in the arrays pixels1 and pixels 2 are 0
    Last edited by vastrolorde; 06-05-2013 at 07:38 PM.

  6. #6
    kalata is offline Member
    Join Date
    Aug 2011
    Location
    Bulgaria
    Posts
    29
    Rep Power
    0

    Default 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

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

    Default 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

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

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default 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.
    Last edited by vastrolorde; 06-06-2013 at 09:29 AM.

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

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default 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.

    Java 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);
    			}

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

    Default Re: pixelperfectcollision

    Is there a problem in these lines relating to the pixelperfectcollision or not???

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

    Default 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
    Last edited by jim829; 06-07-2013 at 02:37 PM. Reason: added number 4.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default 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.

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

    Default Re: pixelperfectcollision

    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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: pixelperfectcollision

    OK im more than certain about that, for somereason the image that the pixelperfectcollision gets is a empty image.

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

    Default 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

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

    Default 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.

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

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

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

    Default 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.

Page 1 of 3 123 LastLast

Posting Permissions

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