In 2d, I have a ship that I want to have move to specific coordinates. I've used Sidar's answer in How do I calculate how an object will move from one point to another? to get something that works allright, but it overshoots and at the end the ship just jumps around the destination. Basically the speed is 10, but the destination isn't a multiple of 10, so it can never be reached. What would be a good way to prevent that ?
Currently the function looks like that (sorry, Haskell, but it should be easy enough to understand) :
let n = normalize $ dest ^-^ cur in let (V2 dx dy) = n ^* speed in (curx + dx, cury + dy)
That's basically what's in Sidar's post, substract the destination coordinates with the current coordinates, normalize that, then multiply it by the speed and add that vector to the current coordinates to get the new ones. The question is : is there a way to prevent that from overshooting the destination, ideally without adding an if for each direction for x and y (that would be a bit ugly, but I don't see how else) ?