There are many ways to handle collisions in your game.
One possible solution is to revert to the position before the collision happened, like the first method you mentioned. I would advise against this solution. It gives a weird feeling when you are playing and prevents you from "sliding across a wall". I used to handle collisions like that. I wish I hadn't.
Instead, I would recommend doing it in a more "realistic" way. Depending on your game environment, you could use a physics engine if your game uses a lot of physics. I think this may be the most complete way to go. There are many engines out there depending on your programming language and framework. Find one that fits your needs and is well documented.
However, you can program it yourself if your game is simple enough and you want to do it yourself out of curiosity. If you are making a game where your environment is made up of squares (a grid, such as a platformer game or a top-down tower defense, etc.), then you can do it yourself. When a collision happens, push the entity across the shallow axis (in order to make the collision response less apparent). This way, you'll be able to move while walking into a wall, and you'll simply slide against it. It should feel natural. You can take a look at this article for a tutorial about it. You can find a lot of other resources online to do this as well.
The general algorithm for this method is the following:
- Find how far the entity is pushed inside an object of the environment on each axis (by comparing their rectangles).
- Find which is the shallow axis (
abs(x) < abs(y)
or the other way around).
- Push against this axis to undo the collision
- Adjust velocities if needed (this depends on your game, you could apply a bounce effect in a physics-based game, stop a jump in a platformer when you jump under a wall)
I wish you the best of luck to find a solution that fits your needs.
Happy coding!