Skip to main content
gamedev.net
🔒 Locked

Time Based Movement

Started by knine 7 replies 450+ views
Original Post
knine
knine
Could someone explain to me how to do time based movement? I thought I was doing it correctly but I apparently am not. I''m doing: x = x + (TimeDelta * xvel); y = y + (TimeDelta * yvel); TimeDelta is the change in time from the last frame update to the current one. Thanks in advance! knine
Gladiator
Gladiator
The velocity should be represented in pixels per second, meaning your velocity has to represent the number of pixels the object travels in 1 second.

Also, I am assuming TimeDelta is in miliseconds. In order for your equation to work you have to convert your code to use second as the metric system in which case your equations become:

x = x + ( (float) TimeDelta/1000 ) * xvel
y = y + ( (float) TimeDelta/1000 ) * yvel

Since TimeDelta is in miliseconds, and there are 1000ms in a second, we have to divide by 1000 to convert to appropriate units.

Example:

If an object is traveling with a velocity of 10 pixels per second, and we want to find out what distance the object has traveled in half a second we do this:

d = 500ms/1000ms/sec * 10pixels/sec = 5pixels

As you can see units cancel out and we are left with pixels. That''s the number of pixels we travel in half a second.

Make sure you keep all the calculations in floating point format, otherwise results will be incorrect.

Let me know if you have any questions.
knine
knine
Thanks for the reply. That was very helpful! I''m using a dxutil lib that came with a book to get the time elapsed, but it is returning milliseconds. Right now x, y, velx, and vely are all int. Should those be changed to float or can I just cast them as float during the calculations?

thanks!
knine
Gladiator
Gladiator
Right. Cast them as floats during calculation, then convert them back to integers.
Neophyte
Neophyte
Casting from int to float is a fairly expensive operation, so unless you have a very good reason to use int''s I suggest you go with float''s instead.

-Neophyte
Dwiel
Dwiel
It might be a good idea to have your velocity in pixels per millisecond so that there is no need for the "1000/". Avoid division whenever possible.

Just my two sence

"The problem with the future is it keeps turning into the pressent"-Hobbes, Calvin''s best friend
Tazzel3d ~ Dwiel
Ronin Magus
Ronin Magus
Another way is to have a starting position, a destination position, and the amount of time you want to pass between them. Then write a function to return the current position based on the time that has passed since movement was started.

The only difference in the logic of this method is that you don't store a physical position but instead create a logical one. You also don't need to worry about multiplying by a pixel constant.


"aut viam inveniam aut faciam " - I will either find a way or make one.

[edited by - Ronin Magus on December 18, 2002 8:39:17 PM]
knine
knine
Should I calculate the TimeDelta like this every frame?

fTimeDelta = timeGetTime() - fLastTime;

Thanks!
knine
Gladiator
Gladiator
I don''t know, what do you think?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.