Game Development Reference
Briefly, this is what's happening:
1. The code first checks to see whether there's an overlap on the x axis.
2. If there is, it checks for an overlap on the y axis.
3. If there's an overlap on both axes, a collision must be occurring.
That's the key to how this system works.
If you know a collision is occurring, the next step is to find out where the collision is happening. If, for
example, the player is colliding with the wall, you need to know whether the collision is happening on
the top, bottom, left, or right side of the wall. If the overlap is happening on the left side of the wall,
you need to subtract the amount of overlap from the player's t position to push it back toward the
left. If the overlap is happening on the right side of the wall, you need to add the amount of overlap to
the player's t position to push it to the right. If a collision is occurring on the top or bottom, you need
to do the same kind of repositioning. All this depends on knowing the direction of the collision.