Pixel perfect collision with paths (Android)
- by keysersoze
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?