# How to resolse collision between moving and static AABB?

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?

• Setting vx or vy to 0 would look funny. Like he stops when he hits it and then starts accelerating downward. Instead set vx = -vx and vy = -vy at the moment of collision and continue with your acceleration downward. The Law of Conservation of Momentum says V1xi * m1 + V2xi * m2 = V1xf * m1 + V2xf * m2 Where v is velocity, 1 and 2 are the respective objects in the collision, i is initial, f is final, and m is mass. Implementing momentum equations would be the most elegant solution but more difficult. – Andrew Wilson Feb 26 '15 at 3:39
• @AndrewWilson It's setting velocity if needed (i believe). I found another way to describe problem. Movement proceeding like it's rotatable box without no angle of rotation calculated. But it's weird to have a player that feels like a box. Here's demonstration of current behavior. How to avoid SO REALISTIC MUCH REAL WORLD collision and have player's rectangle colliding wall throughout its width and height? – abvgde Feb 26 '15 at 9:26  