I've checked maths but how it looks as code will help more to be honest. It's like a hybrid :)
–
dolythMar 2 '13 at 22:29

1

@sarahm No it doesn't. Steering behaviors don't care about time. If I didn't misread the question, it is how to find the required acceleration to arrive at location X in a given time t
–
bummzackMar 3 '13 at 11:27

3 Answers
3

I'm assuming you want only 1-dimensional movement; movement in a straight line.

As you say, s = ut + a*t*t/2, where u is initial velocity, a is acceleration and t is time. Rearranging for a gives a = 2*(s - u*t) / (t*t).

To arrive at a different time, just substitute a different value for t. To get a feel for how it works, you could try it on Wolfram Alpha.

Note that if your physics are using approximating reality by running a calculation every step rather than actually following a differential equation curve (i.e. Euler integration), this won't quite match up. (Though with small and frequent steps, it's really close.)

Edit: So how do you calculate how long the ball takes to get there in the first place? It's a little harder, since t is quadratic, but it's still high-school maths.

Here's a sketch:

Now we could substitute the red equation for t0 into that last equation for a, but it would get messy.

I'd recommend just working it out in two steps like that.

To reiterate:

Use your known values to solve s = u*t0 + a*t0*t0*/2 for t0 to get the time the ball takes to get to its destination.

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 &pm; 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.

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:

Great, thanks for this. I've now updated my physics calculations to use velocity Verlet. I was wondering why the ball was getting slightly out of sync between different browsers using a fixed timestep. The game is as simple as pong so no need for forces or gravity.
–
dolythMar 3 '13 at 14:54