Path following

Hello,
I have a problem with the code: Pathfinding - Happy Coding
Namely, when changing the agent speed to a higher one, an error with the checkCollision() method appears. Vector2.dst does not detect the agent because it passes too fast. I have tried increasing the detection distance but this causes inaccurate localization and looks bad. What to do with this? Maybe there is another way to detect a collision? Please help.

Rather than moving the agent by a certain number of pixels each frame, you might look into linear interpolation where you set the agent’s location somewhere between two points based on how much time has elapsed.

Alternatively, if your agent moves fast enough that it can skip through the target obstacle, then you might need to write more complex collision detection code. Rather than checking for collision between the agent and the obstacle, you’d check for collision between the agent’s path and the obstacle.

In other words, you wouldn’t check just the agent’s current position. You’d create a line segment from the path between the agent’s previous position and its current position, and then check whether that line segment collides with the obstacle.

That sounds promising. How could I do this? Maybe I could get an example? :grin:

I wrote a general guide on collision detection here:

But if I were you I’d google something like “line circle collision detection” for a bunch of results. Good luck!

But honestly if it were me, I’d probably try to slow down my agent so that the easier collision detection works. That might be me being lazy, but if your agent is moving so fast that it’s jumping over obstacles, my guess is that the animation is hard to follow anyway.

A very helpful tutorial. However, in my case, the agent can navigate the designated paths. The problem occurs when entering a new node. When I increased the collision detection range, the agent stops too early and eventually shows up in the wrong place over time.
I don’t know how to do it right. Detecting a collision earlier (before the agent crosses the node) causes it to stop earlier. The goal would be to stop 1 pixel from the center of the node. Unfortunately, 1 is not enough for higher speed (already at 2 - 3 there are problems).

Problem solved. It resulted from my mistake / inattention. Thanks for help!

1 Like

Awesome, I’m glad you got it figured out!

Out of curiosity, what was the problem?