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?

  • \$\begingroup\$ 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. \$\endgroup\$ Commented Feb 26, 2015 at 3:39
  • \$\begingroup\$ @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? \$\endgroup\$
    – abvgde
    Commented Feb 26, 2015 at 9:26

1 Answer 1


A simple approach might be to first resolve the movement in the X axis and after that in the Y axis. This way you'll always know which component of the movement caused the collision, and you can resolve the collision for that component without even having to touch the other one. For instance:

enter image description here

This will make sure we don't pull movement out of nowhere. Keep in mind that this works best for small steps because of the following:

enter image description here

I don't know if this is appropriate to the way you're doing things, but I hope it helps.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .