Pixel perfect collision with paths (Android)
Posted
by
keysersoze
on Game Development
See other posts from Game Development
or by keysersoze
Published on 2014-08-18T15:14:25Z
Indexed on
2014/08/18
16:46 UTC
Read the original article
Hit count: 300
android
|collision-detection
Hi I'm writing a game and I'm trying to do some pixel perfect collisions with paths. The player's character has a bitmask which looks for example like this:
Currenly my code that handles player's collision with path looks like this:
private boolean isTerrainCollisionDetected() {
if(collisionRegion.op(player.getBounds(), terrain.getBottomPathBounds(), Region.Op.INTERSECT)
|| collisionRegion.op(player.getBounds(), terrain.getTopPathBounds(), Region.Op.INTERSECT)) {
collisionRegion.getBounds(collisionRect);
for(int i = collisionRect.left; i < collisionRect.right; i++) {
for(int j = collisionRect.top; j < collisionRect.bottom; j++) {
if(player.getBitmask().getPixel(i - player.getX(), j - player.getY()) != Color.TRANSPARENT) {
return true;
}
}
}
}
return false;
}
The problem is that collisions aren't pixel perfect. It detects collisions in situations like this:
The question is: what can I do to improve my collision detection?
© Game Development or respective owner