Keeping constant speed with variable framerate??
Heya...
I'm having trouble holding a constant speed at the same time as having reliable physics - wonder if someone could help?
I'm producing lots of particles. Each particle has a location, velocity & acceleration.
Each frame, these are updated:
velocity += acceleration;
location += velocity;
All well & good - particles follow nice, defined paths & seem to follow the laws of physics. Except when there's only a few particles, they move a lot faster than when there's 1000.
So, I implement a 'time' variable, that measures the time taken on each frame.
So, now I'm updating
velocity += acceleration*time
location += velocity*time
Obviously the forces involved had to be scaled to deal with the fact that I'm now multiplying everything by 0.01 or so, but once I've done that I get back to something reasonably like my previous, time independant program. This time, its framerate independant for particle speed. However, the physics is very whacky. Say I start up Sonique or something else fairly CPU expensive, the particles go way out of line....
I can't quite figure out whether this is a bug, or a feature of my time implementation.
Has anyone got any alternate ways of doing this?
Cheers
Catfish
Edited by - Catfish on January 24, 2001 8:11:18 PM
If you had big time gaps between the calls to:
velocity += acceleration*time
location += velocity*time
things could get out of wack, since you calculate the maximum velocity that would have been attained after ''time'' amount of time, and then you multiply this maximum velocity by time to get the displacement. The large ''time'' becomes, the more off track your particles will move.
remember that:
displacement = vt + (1/2)(v)(t^2)
where v is the intial velocity, and t is time.
In the model you use, if time equaled 2 seconds, you would increase velocity by acceleration*2, then you would increase location by this velocity * 2. Keep in mind that ''velocity'' is now equal to the velocity at the end of 2 seconds, so the distance you are calculating is too large.
This may not be a big deal, but it is something to consider as time becomes large. You could use the equation above, or you could average the two velocities and multiply by that average velocity (not as accurate tho).
Another thing that might be causing problems is the timing code. Make sure that you are receiving time in the unit you expect (whether that be seconds, milliseconds etc..).
Another thing to check for is very small values for time. If your code runs very quickly on your computer, you may be getting a time elapsed of zero each time. (If you set currentTime as lastTime regardless of whether minimum time elasped).
Check your timing code, that is probably where your problem lies. Check to make sure time is not always so small that it either equals 0. If it is 0, don''t update the time.
velocity += acceleration*time
location += velocity*time
things could get out of wack, since you calculate the maximum velocity that would have been attained after ''time'' amount of time, and then you multiply this maximum velocity by time to get the displacement. The large ''time'' becomes, the more off track your particles will move.
remember that:
displacement = vt + (1/2)(v)(t^2)
where v is the intial velocity, and t is time.
In the model you use, if time equaled 2 seconds, you would increase velocity by acceleration*2, then you would increase location by this velocity * 2. Keep in mind that ''velocity'' is now equal to the velocity at the end of 2 seconds, so the distance you are calculating is too large.
This may not be a big deal, but it is something to consider as time becomes large. You could use the equation above, or you could average the two velocities and multiply by that average velocity (not as accurate tho).
Another thing that might be causing problems is the timing code. Make sure that you are receiving time in the unit you expect (whether that be seconds, milliseconds etc..).
Another thing to check for is very small values for time. If your code runs very quickly on your computer, you may be getting a time elapsed of zero each time. (If you set currentTime as lastTime regardless of whether minimum time elasped).
Check your timing code, that is probably where your problem lies. Check to make sure time is not always so small that it either equals 0. If it is 0, don''t update the time.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement