2D Platformer Collision Problems With Both Axes

Posted by AusGat on Stack Overflow See other posts from Stack Overflow or by AusGat
Published on 2010-04-17T02:11:12Z Indexed on 2010/04/17 2:13 UTC
Read the original article Hit count: 285

I'm working on a little 2D platformer/fighting game with C++ and SDL, and I'm having quite a bit of trouble with the collision detection.

The levels are made up of an array of tiles, and I use a for loop to go through each one (I know it may not be the best way to do it, and I may need help with that too). For each side of the character, I move it one pixel in that direction and check for a collision (I also check to see if the character is moving in that direction). If there is a collision, I set the velocity to 0 and move the player to the edge of the tile.

My problem is that if I check for horizontal collisions first, and the player moves vertically at more than one pixel per frame, it handles the horizontal collision and moves the character to the side of the tile even if the tile is below (or above) the character. If I handle vertical collision first, it does the same, except it does it for the horizontal axis.

How can I handle collisions on both axes without having those problems? Is there any better way to handle collision than how I'm doing it?

© Stack Overflow or respective owner

Related posts about c++

Related posts about sdl