I have to admit it is not clear if in your scenario there are 1, 2 or 3 dimensions, I'll speak about a linear movement; remember that in a multidimensional environment velocity, acceleration and distance are vectors.
I would use the uniform acceleration formulas:
S = v0 * t + 1/2 * a * t ^ 2
vt = v0 + a * t
From your question it seems that the acceleration and the time should be the outputs of the problem.
Last thing not clear in your question is that before you stated "I know the distance I want to travel" later you need the movement end on a grid, these two sentences seems to be in contrast... I would handle this rounding the ending position at the beginning of the computation process.
Now our problem has following inputs:
- D (distance), known
- v0 (initial velocity), known
- vt (ending velocity), know: 0
- dt (delta time), known: the time between two consecutive frames, expressed in seconds (not in milliseconds)
Let's start expressing the time in function of the acceleration (2nd formula)
t = (vt - v0) / a
vt = 0, so
t = - v0 / a
let's substitute this in the 1st formula
S = - v0 ^ 2 / a + 1/2 * a (- v0 / a) ^ 2 = - (v0 ^ 2) / (2 * a)
from here we can find the acceleration
a = - (v0 ^ 2) / (2 * S)
and from the 2nd formula the time
t = - v0 / a
As we said, at the beginning of the process, we need to round the distance to a position snap to the grid:
rD = roundDistanceToGrid(D);
a = - velocity * velocity / 2 / rD;
t = - velocity / a;
t
will not be an integer multiplier of dt
from now on, until the time elapsed is lesser than t
, at each frame just
velocity += a * dt;
on the first frame after the time has elapsed, to fix errors due to rounding, set velocity at zero and place the object exactly on the grid.