# I am bad at tile collision :(

This topic is 3961 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This should be the easiest thing in the world, but for some reason I just can't get it right. I'm doing a 2D, tile-based side-scroller. Each of a tile's four edges has a material assigned; the material determines whether that edge obstructs movement, and if it does, how much friction does it have. It is possible to create "one-way" tiles, f.ex. the top edge is the only obstructed edge (that would be a platform you can stand on, or jump up through from below), as well as tiles with multiple materials, f.ex. ice on top but stone on the sides and bottom. There are two design constraints. First, tiles have a regular size and square shape. There are no partial tiles, slopes, etc. Given this constraint, I use an axis-aligned bounding box to test collision against a tile. The second constraint is that dynamic objects (player, bullets, etc.) are never larger than a single tile. This lets me assume that a dynamic object will, at any time, be colliding with 1-4 tiles. Collision detection is simple: grab the four corner's of the dynamic object's bounding box and figure out which tile each corner falls in. Then test the dynamic AABB against each collided tile's AABB. (There is filtering to prevent re-testing the same tile, should more than one corner lie in the same tile.) When there's a collision, there is some interpenetration happening which needs to be resolved. I do this by calculating, for each tile, the penetration depth in each axis, X and Y. Whichever axis is shorter, I add to a final displacement vector. When all four tiles have been tested, the final displacement vector is added to the player's position to move him out of collision. This almost works perfectly. There is one specific type of problem. Imagine the player jumping at a wall to his left. There are two tiles stacked on top of one another. The right edge of both tiles is obstructed. Here's a crude diagram:
----|
|   PPP
----PPP
|   PPP
-----

The P's represent the player's collision box. Sometimes this situation resolves correctly, i.e. he gets pushed out to the right and falls down alongside the wall. Mostly, however, he registers a smaller Y displacement on the lower tile, collides with that horizontal edge between the two, and hangs up in mid-air. This is the dumbest problem ever, but for some reason I can't seem to see the correct solution. I suppose I could hack out something messy, but the only hacks I've thought of so far make me kinda nervous, and generally seem like the wrong solution. Please tell me I'm missing something obvious. :P

##### Share on other sites
I think you should check for the X-displacement and then the Y-displacement in two separate iterations, for each surrounding tile. That way, in the first iteration (x) the player would be pushed out, and in the second iteration (y), he won't be colliding with any of the tiles, and would not be pushed up.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 13
• 9
• 9
• 15
• ### Forum Statistics

• Total Topics
634076
• Total Posts
3015353
×