Hi, I am developing a top-down shooter and I have implemented walls on my map. I want my player to be able to move, but not go through the walls. I tried this method:

Move the player
If there is collision: undo the movement.

Problem is that sometimes my player got stuck, and other times, on touching the wall the player stopped completely (If the player is moving diagonally (up and right for example) and hots a wall on its right, I would like it to still move up like on all other games)

Any ideas on this?

Thank you,

Edit: As my walls are circles, "L" shapes and polygons, I have created a sprite containing the outlines and using pixel perfect collision, but the problem persists. Please, I need a consistent method and some code if possible please.

When I say move the point, you can actually compose your player of multiple points, and collide each point with whatever is nearby (you'll have to figure out a way to make sure you don't do collision with all objects)

This is very simple point-circle and point-rectangle collision. If your player is small enough relative to the objects he's colliding with (i.e. there's no wall with a size smaller than the player's size), this will work relatively well.

Alternatively, there's some more complicated, but also more versatile collision methods described here: http://www.metanetsoftware.com/technique/tutorialA.html - as I mentioned on another similar post, they provide source, which should be fairly easy to adapt to your purpose - i did so for the 2d platformer I'm making right now.

Or you could use a library like Box2D - though since I've never used it, i don't know if it has everything you need.

I would suggest moving your player in the x-direction 1st, then checking for collision. If it collided, move it back to where it was (by just adding the -velocity). Then move the player in the y-direction and check for collisions. If it collided, move it back to where it was. This would keep the player always just off the wall.

This is a simple method of doing it, but its effective. it will handle those cases where the player is moving up and right, and he hits a wall to his right. You will keep moving him right, then placing him back on the x-axis, then move him up, and he won't hit anything bove him, and the y-position will change.

I would suggest moving your player in the x-direction 1st, then checking for collision. If it collided, move it back to where it was (by just adding the -velocity). Then move the player in the y-direction and check for collisions. If it collided, move it back to where it was. This would keep the player always just off the wall.

This is a simple method of doing it, but its effective. it will handle those cases where the player is moving up and right, and he hits a wall to his right. You will keep moving him right, then placing him back on the x-axis, then move him up, and he won't hit anything bove him, and the y-position will change.

interesting, separation of the axis. This would work for rectangle collision, but it won't give accurate results for collision vs a circle. Here's one case, where the y-axis will fail, but the x-axis won't, and you'll end up in the wrong spot (the red arrows are the axis separation, the green is the minimum distance displacement)

Worst case I can think of actually ends up where both your x and y axis tests will fail (both will collide with circle) and you'll end up being stuck on the circle.

Oops, he was asking about circles and polygons. Yes, my solution could cause problems. I thought it was only rectangles.

Be careful using my solution mypel.

I'm just nitpicking here but I also have to point out that with your solution, under certain not so difficult to achieve conditions, the player sprite will 'collide' with the wall before he is physically touching it - in the case where the player is, say, 0.2 meter from the wall, and your 'move' takes you 0.3 m into the wall, your collision method would stop the player from moving any closer to the wall and he will remain 0.2m from the wall.

The faster the movement speeds, as well as the slower the position update, the less accurate the results will be. Of course at 60fps and normal human speeds, this isn't an issue, which is why it's just nitpicking

Collision detetion is fairly easy and straight forward. It's what to do after you've detected a collision thats the hard part.

The following link is not meant to solve your issues however it should give you a good deal of insight as to how to go about things. It was written with 3D in mind and quite honestly never quite worked right due to floating point errors (maybe later fixed?) however I think it's still worth reading.

As the article is 3D oriented, it can certainly be "dumbed down" for 2D as the principles still apply.