# Issues with AABB on a platformer game

i'm facing an issue at the moment in my javascript/html platformer game that despite searching for this issue i can't solve. The problem is this:

When i place two platforms next to each other and the player moves between them, the algorithm believes the player is colliding with the side (left or right) face of the next platform. This is because all my bounding boxes are placed in an array and iterated through, the algorithm doesn't care if the player has to pass through one collider to get to another, it just thinks it has collided with both.

One solution is of course to make it consider what it has collided with first, another is simply not to place two platforms together. My solution would be to implement a kind of occlusion culling with collides, which if the player collides with a top face it can't collide with a side face if that side face's top face is higher or equal to top first that was being collided in.

That was hard enough to explain let alone implement and i just feel like there is a better work around that wouldn't be too difficult to implement and would be efficient.

My solution:

Another solution is to disable gravity if the player is in contact with the ground, this must be done carefully not to trigger an oscillation effect and also means that two iterations need to be made, one for collision in the y (done first) and the other for collisions in the x (done second).

• Possible duplicate of In a 2D platform game, how to ensure the player moves smoothly over sloping ground? Commented Apr 7, 2019 at 18:10
• I accidentally voted to close with the wrong link and can't revote now, but this appears to be a duplicate of gamedev.stackexchange.com/questions/29036/… Commented Apr 7, 2019 at 18:12
• Bennett Foddy talks about this common issue and some ways to mitigate it in this GDC session Commented Apr 7, 2019 at 18:31
• @SeanMiddleditch His problem seems to be slightly different as his algorithm works slightly differently but the solutions seem useful non the less, the flag option is an interesting one, but his second solution seems like it would be the most effective. I think i may actually go with the last solution i listed, it's a bit hacky but considering my tight deadline for this it would be the quickest to implement.
– Dom
Commented Apr 7, 2019 at 18:35
• Ah, indeed, it perhaps is a bit different. Real answer incoming, then. :) Commented Apr 8, 2019 at 3:06

Try splitting your collision/response into two separate phases; first for the Y axis and then a second pass for the X axis. This will ensure that the character is already pushed "up" according to whatever is under the character's feet before wall collisions are applied and stop momentum.

You'll want to apply a threshold/limit to how much vertical separation is applied, otherwise the character will be able to "walk over" any wall. The threshold should be a fraction of the character's height, typically; usually between a quarter and an eighth, maybe even less.

This approach not only avoids ghost collisions between adjacent tiles, it also helps address minor discrepancies in position (e.g. if the new platform is 0.0073f units higher than the current platform, the character will still be able to walk onto it smoothly). As an added bonus, the character will now be able to "walk up" shallow steps! In pseudo code:

for each tile/platform:
if character intersects:
find Y overlap
if overlap < threshold:
character Y position -= Y overlap

for each tile/platform:
if character intersects:
find X overlap
character X position -= X overlap


You might want to invert the order of axis checking when the character is falling or jumping so that the character will slide on the sides of any tiles/platforms.

This approach is not perfect; there are still "edge" cases (sorry) where you'll get ghost collisions, but it works for a good number of games.

I also highly recommend that you read and then re-read http://higherorderfun.com/blog/2012/05/20/the-guide-to-implementing-2d-platformers/. It doesn't answer your specific scenario, but it might answer some other questions you don't know you have yet. :)