I’m trying to make a platform game. I have the collision code (almost) but there seems to be a bug. I try this code:
for (int i = 0; i < world.ground.size(); i++) {
if (!world.ground.get(i).intersects((int) x, (int) y, player_width, player_height + (int) dy)) {
y += dy;
if (dy < 4) {
dy += 0.1;
}
} else {
dy = 0;
jumped = false;
}
}
But sometimes my character’s foot goes through the ground by 2 or 3 pixels. Is there a better way to do this? Please help, thanks.
It seems like you are using a
posteriori (discrete)collision detection. This causes your object penetrate through a little every time becuse it activates only when touched or penetrated. You may think to convert this to apriori (continuous)collision detection. This way, it never penetrates the ground because it checks before the collision then adjusts speed or position in order to avoid penetration.If you dont want to fiddle with this kind, you can just add a
correctionfunction that acts before painting.I see you implemented this: