How to buffer a move toward a corridor in a pacman game?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By matos

Hi everyone.

I’m currently making a pacman clone, I’ve made some progress this week end, but I’m stuck on something I can’t wrap my head around yet.

I’ve got a raycast for telling if pacman should try to go where the player wants him. The goal of this raycast is to not let the player try to go up or down in an horizontal corridor, and left or right in a vertical one.

It works great, the problem is when the raycast doesn’t collide with anything, but pacman actually does collide with the end of the wall. This makes him bump on the wall. What I would want is for the input to be delayed until pacman can actually fit inside the wall, similar to how it works in the recent pacman championship edition games.

Anyone has an idea for it? I’ll post a few screenshots for you to understand the predicament. Thanks in advance!

Here pacman cannot go up or down, as wanted.

Here pacman is able to go up despite making him stuck on the wall

:bust_in_silhouette: Reply From: pouing

I am suspecting that you are using a single raycast from the center of Pacman.

A common technique to solve this problem is to shoot 2 raycasts from the corners of the player bounding box.

See illustration from this related article.

So to detect whether Pacman can go up, shoot 2 raycasts up from the top left and top right corners. If neither collide with anything, then Pacman can go up.