# Time based object motion

I have a very simple question about moving objects based on time. In my
program, I have the following code to calculate the time elapsed:

curTime = SDL_GetTicks();
lag = float(curTime - lastUpdate);
lastUpdate = curTime;

Then when I move an object, I use:

xpos += .1 * lag;

Is this a good way to do this?

On my system (a Pentium 4, 1.6MHz) lag normally ends up being 9 and
sometimes it is 10. I would also like this to run on older systems (ex,
Pentium 1, 133MHz). I am trying to get the object to move 1 pixel every
update on my system.

It occurred to me that since this time is in milliseconds that maybe I

lag = float(curTime - lastUpdate) * .001;
xpos += 100 * lag;

Will this make any difference written this way rather than the way it is
above? Or is there a better way that is a completely different method?–
Jason Stechschulte
stech at enormousplow.com
http://www.enormousplow.com

Good-bye. I am leaving because I am bored.
– George Saunders’ dying words

I am trying to get the object to move 1 pixel every
update on my system.

Your objects should not move x pixels per update, but x world units
per time unit. I am assuming you are talking about a 3D game here,
but the principle is the same for a 2D game.

Suppose you have a car, and it drives 10 meters (world units) per
second (time unit). If the last frame took only half a second, then
the car has not moved 10 meters, but half of that, i.e. 5 meters. If
the frame took 2 seconds, then the car moved twice the distance,
i.e. 20 meters. It is easiest if you express all speeds and
accelerations etc using the same time unit. Seconds in this example,
but you could also use milliseconds if you fancy that sort of thing.

At the start of the next frame you calculate how much time has
passed since you last updated the objects (i.e. how long the last
frame took). Again in seconds, of course. Then you multiply your
speeds etc with this factor.