Zero milliseconds since last frame and time based movements
Hi,
I was trying to do a time based movement in opengl following this article: http://www.gamedev.net/reference/articles/article1604.asp
My code for that is this:
timeNow = glutGet(GLUT_ELAPSED_TIME);
timeDiffMilli = timeNow - timeLast;
timeDiffSec = timeDiffMilli / 1000.0f;
timeLast= timeNow;
Then, I have:
position = position + speed * (timeDiffSec);
The problem is that timeDiffSec sometime is zero, so the position of my object does not change. The object seems to "shake" while moving.
Does anyone know a good way to solve that?
You could try moving the object only when time has elapsed:
if(timeDiffSec > 0) { position += speed * timeDiffSec; }
Or, if you're forced to move, clamp the time difference to some minimum, like 0.01f or something small compared to whatever time scale you're working with:
if(timeDiffSec == 0) { timeDiffSec = 0.01f; }
[Edited by - Omega147 on May 10, 2007 3:05:03 PM]
if(timeDiffSec > 0) { position += speed * timeDiffSec; }
Or, if you're forced to move, clamp the time difference to some minimum, like 0.01f or something small compared to whatever time scale you're working with:
if(timeDiffSec == 0) { timeDiffSec = 0.01f; }
[Edited by - Omega147 on May 10, 2007 3:05:03 PM]
Quote:Original post by Omega147
if(timeDiffSec == 0) { timeDiffSec = 0.01f; }
Unluckly, this means that for fast systems, movement is NOT time based.
If you're on Win32, do yourself a favor: use the performance counter!
I agree that you should use a high-resolution (better than milliseconds) timer for games. But depending on your needs, it might be better to base it off an absolute timer rather than a delta-based timer.
I.e.
Instead of:
try:
This approach is generally not good for most game engines, but I've found it to be quite useful for some very fast moving deterministic objects I've been coding lately.
I.e.
Instead of:
init: position = 0update: delta = now - lastlast = nowposition += delta * speed
try:
init: startPos = 0startTime = nowupdate: delta = now - startTime position = startPos + delta * speed //N.B. "=" instead of "+="
This approach is generally not good for most game engines, but I've found it to be quite useful for some very fast moving deterministic objects I've been coding lately.
i find its best to update over an average of the last few frames this way glitches get smoothed out
the best method is to run your updates at a fixed speed (ie dont use a delta value)
thus pos += vel; + not pos += vel * delta;
read
http://www.gaffer.org/game-physics/fix-your-timestep/
the best method is to run your updates at a fixed speed (ie dont use a delta value)
thus pos += vel; + not pos += vel * delta;
read
http://www.gaffer.org/game-physics/fix-your-timestep/
I second the suggestion to update using fixed time stamps.
I do something like this:
I do something like this:
while( timeNow > nextUpdate ) { position = position + speed*(TIME_PER_TICK); nextUpdate += TIME_PER_TICK;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement