what's the best logic to force the player to change its direction when he's just next to an obstacle (building, rock, tree..) ?
I know this is related to collision detection but maybe my logic isn't as good as needed or maybe it needs some improvements.
Here is the pseudocode for it :
for obstacle in obstacles: if player.mask.overlap(obstacle.mask, offset): blocked_dir = player.direction else: blocked_dir = '' # movement if key_pressed[K_d] and blocked_dir != 'right': player.x += player.speed elif key_pressed[K_z] and blocked_dir != 'up': player.y -= player.speed elif key_pressed[K_q] and blocked_dir != 'left': player.x -= player.speed elif key_pressed[K_s] and blocked_dir != 'down': player.y += player.speed # There are also other 4 directions (topright, topleft, bottomleft, bottomright) and they all have the same logic.
So the problem with this code is that it's not accurate : sometimes the player glitches and overlaps the obstacle a bit and sometimes he can't move in a direction other than the blocked one since he's still in collision with the obstacle. (that other direction in this case becomes the blocked one).
So is there a better logic ? If not, how can I fix my own ?