What I have is a map represented as list of Rectangles and player's rectangle. Also I have function to determine depth of the collision: Point TestAABBAABB(Rectangle a, Rectangle b) { Point mt = new Point();
double rcx = Math.abs(a.getCenterX() - b.getCenterX());
double rx = (a.width / 2 + b.width / 2);
if (rcx > rx) {
return new Point();
}
double rcy = Math.abs(a.getCenterY() - b.getCenterY());
double ry = (a.height / 2 + b.height / 2);
if (rcy > ry) {
return new Point();
}
double overlap_x = rx - rcx;
double overlap_y = ry - rcy;
mt.x = 0;
mt.y = 0;
//this is the line
if (overlap_x < overlap_y) {
mt.x = (int) overlap_x;
if (a.getCenterX() < b.getCenterX()) {
mt.x = -mt.x;
}
} else {
mt.y = (int) overlap_y;
if (a.getCenterY() < b.getCenterY()) {
mt.y = -mt.y;
}
}
return mt;
}
It works fine if a player jumps "inside" the width of the wall, far from edges. //tail representation
But if player tries to jump near an edge, he moves through the wall and to the side as a result of non-zero X penetration depth of collision.
How do I avoid this situation so player slides under platform instead of getting pushed left or right and without having to set vx or vy to zero?