21

How do I calculate the position of an accelerating body (e.g. a car) after a certain time (e.g. 1 second)?

For a moving body that it not accelerating, it is a linear relationship, so I presume for an accelerating body it involves a square somewhere.

Any ideas?

Robert Gamble
  • 106,424
  • 25
  • 145
  • 137
Iain
  • 9,432
  • 11
  • 47
  • 64

5 Answers5

36

The equation is: s = ut + (1/2)a t^2

where s is position, u is velocity at t=0, t is time and a is a constant acceleration.

For example, if a car starts off stationary, and accelerates for two seconds with an acceleration of 3m/s^2, it moves (1/2) * 3 * 2^2 = 6m

This equation comes from integrating analytically the equations stating that velocity is the rate-of-change of position, and acceleration is the rate-of-change of velocity.

Usually in a game-programming situation, one would use a slightly different formulation: at every frame, the variables for velocity and position are integrated not analytically, but numerically:

s = s + u * dt;
u = u + a * dt;

where dt is the length of a frame (measured using a timer: 1/60th second or so). This method has the advantage that the acceleration can vary in time.

Edit A couple of people have noted that the Euler method of numerical integration (as shown here), though the simplest to demonstrate with, has fairly poor accuracy. See Velocity Verlet (often used in games), and 4th order Runge Kutta (a 'standard' method for scientific applications) for improved algorithms.

Chris Johnson
  • 10,469
  • 4
  • 31
  • 35
  • 2
    For a 2D game you'd have to do this for both x and y (replacing s), knowing both the velocity and acceleration in the x and y directions. Add z for 3D physics. – Bill the Lizard Sep 30 '08 at 15:22
  • 1
    One should note this method is known as Euler's method, and is generally a very crude method with inaccurate results even for small step sizes. – freespace Sep 30 '08 at 15:33
  • 6
    If this method is used, you'll want to update u first, then s (i.e. use the newly calculated velocity to get the position, following the logical acceleration => velocity => position derivation path). – Alan Oct 27 '08 at 15:00
  • 1
    @Alan: that is known as the [Semi-implicit Euler](https://en.wikipedia.org/wiki/Semi_implicit_euler) method. – MestreLion Aug 02 '14 at 14:35
  • 1
    By the way, if acceleration is constant the Velocity Verlet equation for position is exactly the analytical solution `s += vt + (at^2)/2` you mentioned in your first line. The same for Velocity, which is also the same as Euler's. – MestreLion Aug 02 '14 at 17:03
  • @Chris Johnson To be sure, can you tell me what delta t (dt) in the Euler method is exactly? Is it the difference between the current time (ti) and the initial time (t0) or the difference between the current time (ti) and the last point in time of the calculation (ti-1)? – Dawid May 09 '22 at 13:35
7

Well, it depends on whether or not acceleration is constant. If it is it is simply

s = ut+1/2 at^2

If a is not constant, you need to numerically integrated. Now there is a variety of methods and none of them will beat doing this by hand for accuracy, as they are all ultimately approximate solutions.

The easiest and least accurate is Euler's method . Here you divide time into discrete chunks called time steps, and perform

v[n] = v[n-1] * t * a[t]

n is index, t is size of a time step. Position is similarly updated. This is only really good for those cases where accuracy is not all that important. A special version of Euler's method will yield an exact solution for projectile motion (see wiki), so while this method is crude, it can be perfect for some suituations.

The most common numerical integration method used in games and in some chemistry simulations is Velocity Verlet, which is a special form of the more generic Verlet method. I would recommend this one if Euler's is too crude.

freespace
  • 16,529
  • 4
  • 36
  • 58
3

In this article: http://www.ugrad.math.ubc.ca/coursedoc/math101/notes/applications/velocity.html (webarchive), you can find this formula:

p(t) = x(0) + v(0)*t + (1/2)at^2

where

  • p(t) = position at time t
  • x(0) = the position at time zero
  • v(0) = velocity at time zero (if you don't have a velocity, you can ignore this term)
  • a = the acceleration
  • t = your current itme
user2226755
  • 12,494
  • 5
  • 50
  • 73
asterite
  • 7,761
  • 2
  • 23
  • 18
1

Assuming you're dealing with constant acceleration, the formula is:

distance = (initial_velocity * time) + (acceleration * time * time) / 2

where

distance is the distance traveled

initial_velocity is the initial velocity (zero if the body is intially at rest, so you can drop this term in that case)

time is the time

acceleration is the (constant) acceleration

Make sure to use the proper units when calculating, i.e. meters, seconds and so on.

A very good book on the topic is Physics for Game Developers.

Mihai Limbășan
  • 64,368
  • 4
  • 48
  • 59
1

Assuming constant acceleration and initial velocity v0,

x(t) = (1/2 * a * t^2) + (v0 * t)
jholl
  • 2,044
  • 2
  • 18
  • 22