# Thread: Pixel perfect collision detection.

1. Member
Join Date
Oct 2012
Posts
6
Rep Power
0

## 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;
}
return true;
}
return false;
}

}
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) {
}
}
}
}

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) {
}
}
}
}```
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;

public class TerrainPart extends GameObject {

super(main);
}

@Override
public void init() {
main.removeGameObject(this);
}
super.init();
}
}```
by the TerrainObject:
Java Code:
```package gameObjects;

public class TerrainObject extends GameObject {

super(main);
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;
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?
Last edited by TheSourceCode95; 10-10-2012 at 04:20 PM. Reason: Added some info

2. ## 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.

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

## Re: Pixel perfect collision detection.

Hmm, I'll try to make an sscce, but the core is:
Java Code:
```maskPlayer1.retainAll(maskPlayer2);
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. ## 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.

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

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

6. ## 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?

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

## Re: Pixel perfect collision detection.

Hmmm, good idea, I'll try that.

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

## 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 10:48 PM.

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

## Re: Pixel perfect collision detection.

I think I just might have had a major breakthrough.
Java Code:
```	public HashSet<String> getMask() {
}
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) {
}
}
}
}```
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 12:37 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
•