0
\$\begingroup\$

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?

\$\endgroup\$
  • \$\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\$ – Andrew Wilson Feb 26 '15 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 Feb 26 '15 at 9:26
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.