🔒 Locked
Time Based Movement
Advertisement
Original Post
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
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.
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.
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
thanks!
knine
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]
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]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement