0
\$\begingroup\$

I have a method here that returns true if a collision is detected between the player and tile.

            private bool CheckCollision()
    {
        foreach (CollisionTile tile in map.CollisionTiles)
        {
            if (tile.TileType == "stone" && player.rectangle.Intersects(tile.Rectangle))
            {
                return true;
            }

            if(tile.TileType == "water" && player.rectangle.Intersects(tile.Rectangle))
            {
                return true;
            }
        }
        return false;
    }

The collision detection works fine but when I try to move away from the tile I don't move at all. What could be my problem here?

              if (!CheckCollision())
        {
            player.Update();
        }

this is in my update method.

\$\endgroup\$
  • 2
    \$\begingroup\$ There is not enough information in you question to answer it. Additionally please don't just dump the rest of you code on us but explain conceptually what you wish to accomplish and why your method failed. Also I would recommend looking at the XNA platformer example and this question gamedev.stackexchange.com/questions/13954/…. \$\endgroup\$ – ClassicThunder Sep 17 '14 at 23:18
  • 1
    \$\begingroup\$ You stop updating your player as soon as there's a collision. You need to resolve that collision somehow or your player will be stuck. \$\endgroup\$ – bummzack Sep 18 '14 at 6:47
1
\$\begingroup\$

Here is a way to do it:

Update your player's position by storing a vector of the input result, then check collisions with only the movement in y, if there's no collision, you can simply add the y movement to the player's position and do the same with the x movement. If there's a collision, check how far you can go with this x or y component, then add it and so the same for the second component.

It's kind of tricky to help with collisions if we don't know how you are moving, if you are using a momentum based movement or how your whole system is built.

Essentially, the way you are working right now seems to check collisions and only update the player if he can move. With the method I explained above, you are sure that your player will move. And only stop in one direction if it hits something. Hope this helps, otherwise please post some code!

| improve this answer | |
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.