# Updating object position

An object - a player or npc or whatever - has a position, direction and speed and such.

Before drawing each frame, we want to compute the current position of each object.

How do you do this?

If you take the elapsed time since last frame, you run the very real risk of the game running at the wrong speed if the frame-rate is very fast and the clock precision poor (as it often is on some platforms).

And how can you incorporate player controls such as the player wanting to go left or right (e.g. in a simple asteroids clone without ship inertia)?

• I don't know any platform that doesn't offer sub-millisecond clock precision. What do you mean by "as it often is on some platforms"? May 18 '12 at 10:17
• @SamHocevar Google Native Client has been giving me problems; people have even managed to jump to ledges they shouldn't etc.
– Will
May 18 '12 at 10:52
• So you mean that position += direction * speed * elapsed is giving you problems? May 18 '12 at 11:15
• @DavidGouveia yes; hence wondering what other approaches work better
– Will
May 18 '12 at 11:45
• If your problem is the frame rate becoming too high, how about modifying your game loop to enforce a fixed frame rate? gafferongames.com/game-physics/fix-your-timestep May 18 '12 at 11:51

position[t + 1] = position[t] + velocity[t] * dt

It works as long as dt is sufficiently small. If Newton-Euler doesn't work for you, you generally move straight on to Runge-Kutta 4. RK4 is a little bit more complex. You will have to store not just the previous and next states of the system, but 4 more intermediate points between the previous and next. Essentially, RK4 tries to predit the next state of the system given the previous state and its derivatives. However, it requires you to be able to say what the position, velocity and acceleration of the object will be in the future as well as the present. This is easy in some systems (like orbiting bodies or falling characters), but is nigh-but-impossible when you've got stuff like collisions involved -- but otherwise it will give you nearly perfect results even when your timestep is quite large.