Anko's answer is excellent, but I'd like to summarize it a bit and fill in some missing details.
As you correctly note, the formula for motion under constant acceleration is:
s = vt + ½at2
where s
is the distance the object moves during a time interval of length t
, v
is the initial velocity of the object at the beginning of the interval, and a
is the acceleration. As written, this formula gives s
as a function of v
, t
and a
, but we can solve it for a
as a function of s
, v
and t
:
½at2 = s − vt
a = 2(s − vt) / t2
Solving for t
as a function of s
, v
and a
is also possible, but requires the quadratic formula:
½at2 + vt - s = 0
t = −v/a ± sqrt(v2 + 2as) / a
Note that the formula gives us two solutions, depending on the sign in front of the sqrt
; we don't want negative solutions, so we should choose the plus sign if a
is positive and the minus sign if a
is negative, or, in other words:
t = −v/a + sqrt(v2 + 2as) / abs(a)
(This formula will always give the larger of the two solutions, if there are any. It's possible for even this solution to be negative, if we're both moving and accelerating away from the target and will thus never meet it again with the current acceleration. It's also possible to have two positive solutions, if we're going to pass the target once in each direction, or to have no real-valued solutions at all, if our trajectory has never passed the target and never will; e.g. the trajectory is that of a thrown rock, and the target is the moon. In that last case, the input to the sqrt
function will be negative.)
Now we can just use this solution to calculate the expected arrival time, adjust it any way we like, and then use the earlier solution for a
above to calculate the acceleration needed to arrive at that time.
Now, the remaining problem is that the formula above is for exact Newtonian mechanics. If your game is using a poor physics simulation, it may deviate from the exact motion, in which case the simulated path of your object may not match the predictions of this formula.
The best solution is to use a better physics simulation.
In particular, to get accurate trajectories under constant acceleration, you should apply the first formula above on each time step to compute new object positions, like this:
x' = x + vΔt + ½aΔt2
v' = v + aΔt
Here, x
and v
are the initial position and velocity of the object before the timestep, x'
and v'
are the new position and velocity of the object after the time step, Δt
is the length of the time step and a
is the acceleration.
As long as a
is constant, this formula gives exact results (up to the limits of numerical accuracy, anyway). If a
can vary, you can get even better results using the velocity Verlet method described in the linked post:
a = a(x, v, t)
x' = x + vΔt + ½aΔt2
a' = a(x', v + aΔt, t + Δt)
v' = v + ½(a + a')Δt
X
in a given timet
\$\endgroup\$