I've written some code which lets an arbitrary sized rectangle collide with a grid-based terrain setup (for a platformer game). The way I do it is something like this:
For each tile the rectangle intersects with, do:
Calculate the primary axis that this tile is on with respect to the rectangle
Calculate the interpenetration of this tile into the rectangle along the primary axis (factoring in previous position offsets from other tiles)
If this tile is solid, add that interpenetration to a total collision resolution vector
Adjust the rectangle's position by the total calculated collision resolution vector
Which works just fine, except i run into random "hang-ups" as my rectangle gets pulled into the ground just over the border of two tiles, my code decides that it needs to resolve the collision with this new tile by pushing it in the X axis, thus stopping the rectangle's motion unless it is manually pushed out of the terrain to get over it.
I've tried only resolving the collision on one axis at a time (so it ignores any x axis collision resolution if the Y axis resolution is the largest and vice versa), but that results in jittering when the rectangle is being pressed into a corner (as this is a situation that actually needs both axes resolved at once).
In short, what method can I use to fix both of these problems at once?