2D Platformer Collision Problems With Both Axes

前端 未结 2 2014
北荒
北荒 2021-01-13 08:30

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

2条回答
  •  离开以前
    2021-01-13 09:03

    XNA's 2D platformer example uses tile-based collision as well. The way they handle it there is pretty simple and may useful for you. Here's a stripped down explanation of what's in there (removing the specific-to-their-demo stuff):

    1. After applying movement, it checks for collisions.
    2. It determines the tiles the player overlaps based on the player's bounding box.
    3. It iterates through all of those tiles...
      1. If the tile being checked isn't passable:
      2. It determines how far on the X and Y axes the player is overlapping the non-passable tile
      3. Collision is resolved only on the shallow axis:
        1. If Y is the shallow axis (abs(overlap.y) < abs(overlap.x)), position.y += overlap.y; likewise if X is the shallow axis.
        2. The bounding box is updated based on the position change
      4. Move on to the next tile...

    It's in player.cs in the HandleCollisions() function if you grab the code and want to see what they specifically do there.

提交回复
热议问题