java – In a top down game, once I have detected collision, how do I prevent it?

I’m making a top-down game in Java. I watched some videos, and this is the code I currently use when the player is colliding with an object:

x += -velX
y += -velY

When I apply force to a wall, the player dips into it, and can’t move along the wall.
Does anyone have a better method?