Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to write some code to change an acceleration factor so a ball will arrive at a destination in a desired time.

The problem is I don't know time (t) but I do know I want it to arrive at (t - x) where I can feed the value of x.

The ball has an initial velocity so the equations of motion I imagine to use is:

s = ut + 0.5at^2

I have the distance (s), the starting velocity (u) and the current acceleration (a).

Ultimately, I want to be able to get a new acceleration value (a) that will make the ball arrive, for example, 100ms later than it's intention with the current (a) value.

Any pointers, much appreciated! (I'm using javascript).

share|improve this question
    
You should really ask that at Mathematics –  user1306322 Mar 2 '13 at 21:58
    
I've checked maths but how it looks as code will help more to be honest. It's like a hybrid :) –  dolyth Mar 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 –  bummzack Mar 3 '13 at 11:27
add comment

3 Answers 3

up vote 11 down vote accepted

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:

How you might go about calculating the initial time

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:

  1. 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.
  2. Rearrange s = u*t + a*t*t*/2 to solve for the new acceleration value a.
  3. Substitute t0 + desiredDelay for t.
  4. Solve for a.
share|improve this answer
    
Thanks for this. The problem is finding t first. I need to work out what t is and then factor a new t back into the equation like you've suggested here. –  dolyth Mar 3 '13 at 10:43
    
I've edited the answer to include how to find the "first t" (t0). It's a bit harder because t is quadratic, but I hope it makes sense! –  Anko Mar 3 '13 at 12:21
1  
Ahha! Yes, I did this at school 10 years ago and have forgotten the use of the quadratic formula! Thanks a lot for putting a lot of effort in here. –  dolyth Mar 3 '13 at 12:25
add comment

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

share|improve this answer
    
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. –  dolyth Mar 3 '13 at 14:54
add comment

If you just want to interpolate some variable between 2 values during some time, I recommend to use some tweening library, e.g. Tweener. It contains linear interpolation, quadratic, cubic, sine etc.

BTW whenever you want to change position by editing velocity or acceleration, just compute the derivative of movement equation and solve it. You can copy movement equations from Tweener library.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.