C Navigation System — Devlog #2
BFS Pathfinding & Automatic Navigation
So after the first version I wasn’t really satisfied. The robot moving manually was cool to build but it felt incomplete — a robot that needs a human to tell it where to go isn’t really a robot. I wanted it to find the path on its own.
The problem was I had no idea how BFS worked.
I spent a good chunk of the 7 hours on this just trying to understand the algorithm before touching any code. Watched some explanations, drew it out on paper, tried to wrap my head around queues and how you actually backtrack to reconstruct a path. It took a while. The moment it clicked was when I stopped thinking about it as “visiting nodes” and started thinking about it as water spreading through a maze — it fills every reachable cell layer by layer until it hits the destination.
Once that made sense, I rebuilt the project. The grid went from 5x5 to 10x10 with a more complex obstacle layout. I separated the map logic into its own function (eh_obstaculo) so the maze can be edited without touching anything else. The BFS itself uses a queue array, a visited matrix, and a parent matrix — the parent matrix is what lets you trace back the full path once you reach (9,9).
The animation was actually the fun part. After the path is reconstructed, the board redraws at each step with clear() and usleep(), showing the robot moving tile by tile. It even replays at the end at a faster speed. Seeing it run for the first time was genuinely satisfying.
The hardest part was getting the path reconstruction right — BFS gives you the path backwards, so you have to reverse it before animating. Took me a few tries to get that sorted.
Comments 0
No comments yet. Be the first!
Sign in to join the conversation.