Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Pixel perfect collision detection.

    Hello there everybody.

    I'm pretty new to graphics in java, but for a school project I've got to make a game. Now I like a challenge, so I choose a pretty complex game. But to do this, I'll need pixel-perfect collision. I've currently got this code:
    Java Code:
    public boolean checkCollision(GameObject b) {
    		int ax1 = x.get();
    		int ay1 = y.get();
    		int ax2 = ax1 + getImg().getWidth();
    		int ay2 = ay1 + getImg().getHeight();
    		int bx1 = b.x.get();
    		int by1 = b.y.get();
    		int bx2 = bx1 + b.getImg().getWidth();
    		int by2 = by1 + b.getImg().getHeight();
    		if (by2 < ay1 || ay2 < by1 || bx2 < ax1 || ax2 < bx1) {
    			return false;
    		}
    		HashSet<String> maskPlayer1 = getMask();
    		HashSet<String> maskPlayer2 = b.getMask();
    		maskPlayer1.retainAll(maskPlayer2);
    		if (maskPlayer1.size() > 0) {
    			return true;
    		}
    		return false;
    	}
    
    public HashSet<String> getMask() {
    		if (mask != null && mask.size() >= 1) {
    			return mask;
    		}
    		mask = new HashSet<String>();
    		BufferedImage image = getImg();
    		int pixel, a;
    		for (int i = 0; i < image.getWidth(); i += 1) {
    			for (int j = 0; j < image.getHeight(); j += 1) {
    				pixel = image.getRGB(i, j);
    				a = (pixel >> 24) & 0xff;
    				if (a != 0) {
    					mask.add((x.get() + i) + "," + (y.get() - j));
    				}
    			}
    		}
    		return mask;
    	}
    
    	public HashSet<String> getQuickMask(int size) {
    		HashSet<String> mask = new HashSet<String>();
    		BufferedImage image = getImg();
    		int pixel, a;
    		for (int i = 0; i < image.getWidth(); i += size) {
    			for (int j = 0; j < image.getHeight(); j += size) {
    				pixel = image.getRGB(i, j);
    				a = (pixel >> 24) & 0xff;
    				if (a != 0) {
    					mask.add((x.get() + i) + "," + (y.get() - j));
    				}
    			}
    		}
    		return mask;
    	}
    This code is part of my GameObject class. I think most of the code is pretty clear, except for the x.get(), since the game is online I made my own IntegerObject class, this basically holds an Integer, .get() returns a simple Integer. getMask, well, it gives a mask, and getQuickMask is used to do a really rough check.

    The code works, but it happens quite often that you get stuck, especially around corners and curved parts of the map.
    The map gets split up into a whole bunch of TerrainParts:
    Java Code:
    package gameObjects;
    
    import launcher.MainThread;
    
    public class TerrainPart extends GameObject {
    
    	public TerrainPart(MainThread main) {
    		super(main);
    	}
    
    	@Override
    	public void init() {
    		if (getQuickMask(4).size() == 0) {
    			main.removeGameObject(this);
    		}
    		super.init();
    	}
    }
    by the TerrainObject:
    Java Code:
    package gameObjects;
    
    import launcher.MainThread;
    
    public class TerrainObject extends GameObject {
    	MainThread main;
    
    	public TerrainObject(MainThread main) {
    		super(main);
    		setImg(main.imgLoader.loadBufferedImage("terrain.png"));
    		x.set(-1100);
    		y.set(-1100);
    		for (int i = 0; i < getImg().getWidth(); i += 32) {
    			for (int j = 0; j < getImg().getWidth(); j += 32) {
    				TerrainPart tPart = new TerrainPart(main);
    				tPart.setImg(getImg().getSubimage(i, j, 32, 32));
    				tPart.x.set(i + x.get());
    				tPart.y.set(j + y.get());
    				tPart.solid = true;
    				tPart.visible = false;
    				main.addGameObject(tPart);
    				tPart.init();
    			}
    		}
    		this.main = main;
    	}
    
    }
    The white parts of the map are transparent, it's just the uploading that makes them white.
    I was thinking that maybe there were some 95% transparent pixels, but then it shouldn't get stuck since it should detect them as soon as it tries to move.

    Has anybody got any idea on how to fix this?
    Thanks in advance
    Attached Thumbnails Attached Thumbnails Pixel perfect collision detection.-terrain.png  
    Last edited by TheSourceCode95; 10-10-2012 at 05:20 PM. Reason: Added some info

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default Re: Pixel perfect collision detection.

    The code might be clear to you, but people are going to have a hard time digesting it if you don't post an SSCCE that we can play with. I'd try to boil it down to as small a problem as possible- is there any way you can make an example problem with just a couple pixels that demonstrates the core problem? (preferably in the form of print statements instead of program behavior)

    Also, are you sure you really need pixel-perfect collision detection? This is almost never necessary.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Pixel perfect collision detection.

    Hmm, I'll try to make an sscce, but the core is:
    Java Code:
    maskPlayer1.retainAll(maskPlayer2);
            if (maskPlayer1.size() > 0) {
                return true;
            }
    Do I really need it... Well, the problem is, how else am I going to make a map with every strange figure you can find, to test collisions on any figure? I mean, it would probably be possible to reduce everything to circles and squares, but that would take really long I think...

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default Re: Pixel perfect collision detection.

    I'm honestly not quite sure what that code is trying to do. Seems like a weird way to do collision detection.

    Testing collisions on any type of figure is usually done with polygons, at least on some level.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Pixel perfect collision detection.

    This code just places the 2 masks over each other, removes every single place where one or none of the masks has black, and then checks for the size. If the size is 0, it's sure that it doesn't collide anywhere.
    I've got a ripoff of my project now, I did change it a bit, now it returns the amount of pixels it collides with and then the move void checks if it's zero, but that's the only change.
    Somehow the attachment system fails, Copy of WvW.zip download - 2shared

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default Re: Pixel perfect collision detection.

    Sorry, I'm behind a firewall that doesn't allow me to go to hosting sites like that, which is one of the reasons we ask for an SSCCE- so we can copy it directly without an fuss.

    I understand your goal now, but isn't that very inefficient? Have you tried mapping out the pixels that remain (maybe by drawing them in red) and seeing where the collision is being detected?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Pixel perfect collision detection.

    Hmmm, good idea, I'll try that.

  8. #8
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Pixel perfect collision detection.

    I can't seem to get that to work, the red pixels showing up I mean, would it be an idea to rewrite this system so it makes a simple HashMap<String, boolean> like <"x,y",true> that contains the collision points, or would this be really slow?

    Edit:
    nvm, wasn't thinking, this is exactly what I've got already...

    2nd edit:
    Okay, I've made the terrain parts so they draw white squares now, and appearently somehow when the player enters a white square from the top it stops the player on the white part, from the left you'll just walk straight in and get stuck as soon as you hit the wall, from the right it's the same, except for the fact that you now only get stuck when you're more than 50% in the white part and from the bottom it stops you as soon as you hit the white, and you will get stuck. Does this give any indications of where I might be going wrong?
    Last edited by TheSourceCode95; 10-10-2012 at 11:48 PM.

  9. #9
    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    Default Re: Pixel perfect collision detection.

    I think I just might have had a major breakthrough.
    Java Code:
    	public HashSet<String> getMask() {
    		if (mask != null && mask.size() >= 1) {//This part
    			return mask;
    		}
    		mask = new HashSet<String>();
    		BufferedImage image = getImg();
    		int pixel, a;
    		for (int i = 0; i < image.getWidth(); i += 1) {
    			for (int j = 0; j < image.getHeight(); j += 1) {
    				pixel = image.getRGB(i, j);
    				a = (pixel >> 24) & 0xff;
    				if (a != 0) {
    					mask.add((x.get() + i) + "," + (y.get() - j));//and this part
    				}
    			}
    		}
    		return mask;
    	}
    Those 2 parts... They just don't go together. I'm saving the current x and y to the permanent mask. So when the player moves, the mask doesn't get updated, it's still at the old position.

    Edit:
    Yup, removing the returning of the old mask value did solve the problem of getting stuck. Just got one more flaw in here somewhere but I think I'll be able to deal with that one on my own.
    Thank you all for trying to help me out, and sorry for not being able to create a good SSCCE.
    Last edited by TheSourceCode95; 10-11-2012 at 01:37 AM.

Similar Threads

  1. Collision detection
    By mwr1976 in forum Java 2D
    Replies: 2
    Last Post: 04-14-2012, 01:45 PM
  2. Collision Detection
    By Äppelpaj in forum Java 2D
    Replies: 1
    Last Post: 10-13-2011, 04:29 PM
  3. Collision Detection
    By sunde887 in forum Java 2D
    Replies: 2
    Last Post: 10-08-2011, 12:40 AM
  4. Collision Detection?
    By Alerhau in forum New To Java
    Replies: 39
    Last Post: 09-07-2011, 05:55 PM
  5. Really Need help with some collision detection
    By Harwad in forum New To Java
    Replies: 1
    Last Post: 01-23-2011, 01:38 AM

Posting Permissions

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