move_and_collide/slide is making my actor jump around when moving close to an obstacle

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

So, I’m making this isometric RTS-y game and I’m implementing basic navigation by means of potential fields so my actors automatically curve around obstacles towards their target. It works nicely by calculating the velocity of the actor by adding up all the “repulsion” vectors from the obstacles and then using move_and_collide to figure out any collision that might happen on the way.

Neat. But it wasn’t always like that…

This weird thing was happening where if the actor was moving close to the tangent of the osbtacle (they both have flattened circles as collision shapes) the actor would start circling around, then suddenly jump back and then continue again, getting stuck forever…

I debugged the code and found that the KinematicCollision2D object move_and_collide was giving me had a weird value for travel which was completely against the direction of the vector the obejct was supposed to be moving towards. Now, as you’ve seen in the first GIF, I have fixed it, and the fix was simple:

  1. Check if collision happens before moving (setting the test_only argument to true)
  2. If no collision happens, then move
  3. If collision happens, check if travel is larger than expected (larger than speed * delta, basically)
  4. If so, then move by using the value in remainder

Which works, but I still have two questions:

  1. Why is the move_and_collide (or move_and_slide) method sending my actor way far from where it should be moving to in the next step?
  2. What exactly is the remaindervalue in KinematicCollision2D?