views:

1328

answers:

5

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?

+19  A: 

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
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
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
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
+1  A: 

You can google it. I've found this: http://www.ugrad.math.ubc.ca/coursedoc/math101/notes/applications/velocity.html

But if you don't want to read, it's:

p(t) = x(0) + v(0)t + (1/2)a*t^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
asterite
+1  A: 

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
+2  A: 

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
+1  A: 

Assuming constant acceleration and initial velocity v0,

x(t) = (1/2 * a * t^2) + (v0 * t)
jholl