Problem with SDL_GetTicks()

Hello,
I’m currently starting to get into SDL and OpenGL, and ran into a problem. I
wanted to use SDL_GetTicks() for frame rate independent movement, and I do it
similar to this:

for (;:wink:
{
unsigned ticks = SDL_GetTicks();

 /* All the movement and drawing here */

 unsigned gticks = SDL_GetTicks();
 // All movement should be multiplied with this:
 double factor = (gticks-ticks) / 1000.0;

}

While debug-outputting this factor I noticed that the factor is very often
0.001 or 0.000, and then suddenly jumps to ~0.050 at irregular intervals. When
I increase the resolution from 640x480 to 1399x1009, the frame rate drops from
about 1050 FPS to about 200 FPS, but the effect stays the same, only with a
factor of 0.225 instead of 0.050.

The system I’m running this on is a Pentium-M 1.73 GHz with Gentoo Linux Kernel
2.6.11-r11.

Has anybody an idea why this happens or how I could get around it?

Has anybody an idea why this happens or how I could get around it?

Maybe you could get around it by using system suggested in this article: http://www.gaffer.org/articles/Timestep.html

Koshmaar

As some may have noticed, this is the method I’m using in Kobo Deluxe
and the “Fixed Rate Pig” example.

So, now I no longer need to write that paper! ;-D

//David Olofson - Programmer, Composer, Open Source Advocate

.- Audiality -----------------------------------------------.
| Free/Open Source audio engine for games and multimedia. |
| MIDI, modular synthesis, real time effects, scripting,… |
`-----------------------------------> http://audiality.org -’
http://olofson.nethttp://www.reologica.se —On Sunday 28 August 2005 23.16, Koshmaar wrote:

Has anybody an idea why this happens or how I could get around it?

Maybe you could get around it by using system suggested in this
article: http://www.gaffer.org/articles/Timestep.html

Has anybody an idea why this happens or how I could get around it?

Maybe you could get around it by using system suggested in this
article: http://www.gaffer.org/articles/Timestep.html

As some may have noticed, this is the method I’m using in Kobo Deluxe
and the “Fixed Rate Pig” example.

So, now I no longer need to write that paper! ;-D

//David Olofson - Programmer, Composer, Open Source Advocate

Huh, so it’s working! :slight_smile: I’m going to implement such system within next few days, so it’s nice to know that’s tested.

Koshmaar

Koshmaar wrote:

Has anybody an idea why this happens or how I could get around it?

Maybe you could get around it by using system suggested in this article: http://www.gaffer.org/articles/Timestep.html

First, thanks for answering. But it seems as if that method doesn’t work. I
don’t use the integration methods described in that article as I’m currently
only using constant velocity, but if I got it right it shouldn’t make any
difference this way.
Still the unsteady framerate creates visible stuttering of the moving images,
as it jumps to about 50 times itself every couple of hundred of frames.

I’ll go on fiddling around with the code, but any additional help would still
be appreciated.

Wait, SDL_GetTicks occasionally returns a value smaller than the one it
returned previously? That’s not good. I don’t think it’s a problem in SDL
since I use SDL_GetTicks in similar ways all the time but have never
encountered this; it sounds like a problem with your system timebase. Check
your dmesg. See anything interesting?On August 28, 2005 01:01 pm, Kolja Kube wrote:

Hello,
I’m currently starting to get into SDL and OpenGL, and ran into a problem.
I wanted to use SDL_GetTicks() for frame rate independent movement, and I
do it similar to this:

for (;:wink:
{
unsigned ticks = SDL_GetTicks();

 /* All the movement and drawing here */

 unsigned gticks = SDL_GetTicks();
 // All movement should be multiplied with this:
 double factor = (gticks-ticks) / 1000.0;

}

While debug-outputting this factor I noticed that the factor is very often
0.001 or 0.000, and then suddenly jumps to ~0.050 at irregular intervals.

The reason why you don’t get constant “factor” values is that you are
running on a multiprocessing OS: at certain points in time, the OS is
going to run a different process, but the clock does not stop.

Also, it’s worth noting that because SDL_GetTicks returns only a 32 bit
value, it may roll over on you.

Cheers

OE

Kolja Kube wrote:> Hello,

I’m currently starting to get into SDL and OpenGL, and ran into a
problem. I wanted to use SDL_GetTicks() for frame rate independent
movement, and I do it similar to this:

for (;:wink:
{
unsigned ticks = SDL_GetTicks();

/* All the movement and drawing here */

unsigned gticks = SDL_GetTicks();
// All movement should be multiplied with this:
double factor = (gticks-ticks) / 1000.0;

}

While debug-outputting this factor I noticed that the factor is very
often 0.001 or 0.000, and then suddenly jumps to ~0.050 at irregular
intervals. When I increase the resolution from 640x480 to 1399x1009, the
frame rate drops from about 1050 FPS to about 200 FPS, but the effect
stays the same, only with a factor of 0.225 instead of 0.050.

The system I’m running this on is a Pentium-M 1.73 GHz with Gentoo Linux
Kernel 2.6.11-r11.

Has anybody an idea why this happens or how I could get around it?


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

  Ovid East
  http://www.ovideast.com
  home of Win32 LibSDL Lean and Mean

Hello,
I’m currently starting to get into SDL and OpenGL, and ran into a problem. I
wanted to use SDL_GetTicks() for frame rate independent movement, and I do it
similar to this:

for (;:wink:
{
unsigned ticks = SDL_GetTicks();

 /* All the movement and drawing here */

 unsigned gticks = SDL_GetTicks();
 // All movement should be multiplied with this:
 double factor = (gticks-ticks) / 1000.0;

}

While debug-outputting this factor I noticed that the factor is very often
0.001 or 0.000, and then suddenly jumps to ~0.050 at irregular intervals. When
I increase the resolution from 640x480 to 1399x1009, the frame rate drops from
about 1050 FPS to about 200 FPS, but the effect stays the same, only with a
factor of 0.225 instead of 0.050.

The system I’m running this on is a Pentium-M 1.73 GHz with Gentoo Linux Kernel
2.6.11-r11.

Has anybody an idea why this happens or how I could get around it?

Could it be C’s float rules?
Try
double factor = ((double)(gticks - ticks)) / 1000.0;

– JoshOn 8/28/05, Kolja Kube <kolja.kube at gmail.com> wrote:


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

I think you misread the “~” as a minus.

  • SROn 8/30/05, Tyler Montbriand wrote:

While debug-outputting this factor I noticed that the factor is very often
0.001 or 0.000, and then suddenly jumps to ~0.050 at irregular intervals.
Wait, SDL_GetTicks occasionally returns a value smaller than the one it
returned previously? […]

Joshua Oreman wrote:

Hello,
I’m currently starting to get into SDL and OpenGL, and ran into a problem. I
wanted to use SDL_GetTicks() for frame rate independent movement, and I do it
similar to this:

for (;:wink:
{
unsigned ticks = SDL_GetTicks();

/* All the movement and drawing here */

unsigned gticks = SDL_GetTicks();
// All movement should be multiplied with this:
double factor = (gticks-ticks) / 1000.0;

}

While debug-outputting this factor I noticed that the factor is very often
0.001 or 0.000, and then suddenly jumps to ~0.050 at irregular intervals. When
I increase the resolution from 640x480 to 1399x1009, the frame rate drops from
about 1050 FPS to about 200 FPS, but the effect stays the same, only with a
factor of 0.225 instead of 0.050.

The system I’m running this on is a Pentium-M 1.73 GHz with Gentoo Linux Kernel
2.6.11-r11.

Has anybody an idea why this happens or how I could get around it?

Could it be C’s float rules?
Try
double factor = ((double)(gticks - ticks)) / 1000.0;

Don’t think so. (gticks - ticks) is of type unsigned, but divided by 1000.0 (a
double, I checked that) everything should be allright. The program is written
in C++ and compiled as such, too, so that shouldn’t be the matter.

Sure enough, you’re right. I’ve learned to be suspicious of C(++)'s
float rules, after having several hard-to-find bugs from them :slight_smile:

No idea what else it could be.

– JoshOn 8/30/05, Kolja Kube <kolja.kube at gmail.com> wrote:

On 8/28/05, Kolja Kube <kolja.kube at gmail.com> wrote:

Kolja Kube wrote:

Has anybody an idea why this happens or how I could get around it?

Try adding a SDL_Delay(10); to the loop. The loop will be a bit slower
in the fastest case, but should be more even. The delay makes your
program tell the OS it’s okay to give up CPU time to other processes at
that time, and the OS will be less inclined to get frustrated and take
away large chunks at other, inconvenient times.

  • Gerry

Kolja Kube wrote:

Hello,
I’m currently starting to get into SDL and OpenGL, and ran into a
problem. I wanted to use SDL_GetTicks() for frame rate independent
movement, and I do it similar to this:

for (;:wink:
{
unsigned ticks = SDL_GetTicks();

/* All the movement and drawing here */

unsigned gticks = SDL_GetTicks();
// All movement should be multiplied with this:
double factor = (gticks-ticks) / 1000.0;

}

While debug-outputting this factor I noticed that the factor is very
often 0.001 or 0.000, and then suddenly jumps to ~0.050 at irregular
intervals. When I increase the resolution from 640x480 to 1399x1009, the
frame rate drops from about 1050 FPS to about 200 FPS, but the effect
stays the same, only with a factor of 0.225 instead of 0.050.

The system I’m running this on is a Pentium-M 1.73 GHz with Gentoo Linux
Kernel 2.6.11-r11.

Has anybody an idea why this happens or how I could get around it?

I am currently learning how to do frame rate independent movement, and I
am currently seeing the same issue.

deltaTime: 0
currentTime: 1034
lastTime: 1034
deltaTime: 0
currentTime: 1034
lastTime: 1034
deltaTime: 0
currentTime: 1034
lastTime: 1034
deltaTime: 0.1
currentTime: 1035
lastTime: 1035
deltaTime: 3.1
currentTime: 1066
lastTime: 1066

This is a marked improvement over having deltaTime constantly be 0. I
tried using doubles instead of floats.

I think the main thing is that I don’t do a lot with this new code, so
there isn’t a lot to do between frames in the first place. Perhaps you
also don’t have a lot of things going on?

http://sol.gfxile.net/gp/ch09.html This is a tutorial that I am
following currently, and I can’t readily see why it is able to render
smoothly and quickly while my project sits without doing anything for
some time before jumping. Again, it could just be that I am not doing
enough to make it matter.–
GBGames’ Blog: http://www.gbgames.com/blog

Im in the middle of a 2d side scroller which uses delta time and let me tell
you - it works fairly ok but there are some very big faults.

imagine this scenario, a player hits a “force field” which makes their
momentum be opposite the direction of the force field. every frame, physics
is done on the player multiplied by delta time (momentum becomes movement,
drag decreases momentum etc)

For some reason or another you hit a lag spike and dt is 100 instead of the
usual 12 or so. Your guy will move too far! This means physics can be very
erratic in this kind of setup.

Also this means that with my game atleast, lower fps = faster walking and
running.

Kinda crappy.

The way I wish i did it now is where it puts all the dt’s into a "bucket"
variable and when the bucket overfills (lets say at 100ms for 10 updates a
second - slow but just for demonstration purposes), it will preform a logic
update to the game and subtract the “threshhold” value from the bucket.

Ie if the bucket is 115, it issues a logic update and subtracts 100 from the
bucket. Toss it in a while loop or something to handle if the bucket is at
523 or something. This makes calculating physics equations etc very stable
no matter what the FPS

It would be alot nicer now if i did it that way then, so hoping this helps
you out some (:> ----- Original Message -----

From: gberardi@gbgames.com (Gianfranco Berardi)
To:
Sent: Tuesday, August 30, 2005 8:24 PM
Subject: Re: [SDL] Problem with SDL_GetTicks()

Kolja Kube wrote:

Hello,
I’m currently starting to get into SDL and OpenGL, and ran into a
problem. I wanted to use SDL_GetTicks() for frame rate independent
movement, and I do it similar to this:

for (;:wink:
{
unsigned ticks = SDL_GetTicks();

/* All the movement and drawing here */

unsigned gticks = SDL_GetTicks();
// All movement should be multiplied with this:
double factor = (gticks-ticks) / 1000.0;

}

While debug-outputting this factor I noticed that the factor is very
often 0.001 or 0.000, and then suddenly jumps to ~0.050 at irregular
intervals. When I increase the resolution from 640x480 to 1399x1009, the
frame rate drops from about 1050 FPS to about 200 FPS, but the effect
stays the same, only with a factor of 0.225 instead of 0.050.

The system I’m running this on is a Pentium-M 1.73 GHz with Gentoo Linux
Kernel 2.6.11-r11.

Has anybody an idea why this happens or how I could get around it?

I am currently learning how to do frame rate independent movement, and I
am currently seeing the same issue.

deltaTime: 0
currentTime: 1034
lastTime: 1034
deltaTime: 0
currentTime: 1034
lastTime: 1034
deltaTime: 0
currentTime: 1034
lastTime: 1034
deltaTime: 0.1
currentTime: 1035
lastTime: 1035
deltaTime: 3.1
currentTime: 1066
lastTime: 1066

This is a marked improvement over having deltaTime constantly be 0. I
tried using doubles instead of floats.

I think the main thing is that I don’t do a lot with this new code, so
there isn’t a lot to do between frames in the first place. Perhaps you
also don’t have a lot of things going on?

http://sol.gfxile.net/gp/ch09.html This is a tutorial that I am
following currently, and I can’t readily see why it is able to render
smoothly and quickly while my project sits without doing anything for
some time before jumping. Again, it could just be that I am not doing
enough to make it matter.


GBGames’ Blog: http://www.gbgames.com/blog


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Alan Wolfe wrote:

The way I wish i did it now is where it puts all the dt’s into a "bucket"
variable and when the bucket overfills (lets say at 100ms for 10 updates a
second - slow but just for demonstration purposes), it will preform a logic
update to the game and subtract the “threshhold” value from the bucket.

Ie if the bucket is 115, it issues a logic update and subtracts 100 from the
bucket. Toss it in a while loop or something to handle if the bucket is at
523 or something. This makes calculating physics equations etc very stable
no matter what the FPS

It would be alot nicer now if i did it that way then, so hoping this helps
you out some (:

I think out of all the tutorials or code samples I found yesterday, one
or two might have mentioned something along those lines. I believe it
was one of the tutorials that had psuedocode examples, which was helpful
to a point but hard to visualize if you don’t know what the made up
functions were supposed to do.

Anyway, if I understand you correctly, and the following is off the top
of my head, you would basically have a totalTime variable and do:

totalTime += dt;

Then you would check if totalTime was greater than some threshhold
before you do an update:

while ( totalTime > SOME_CONSTANT )
{
// do some updating
totalTime -= SOME_CONSTANT;
}

Of course, if dt still ends up 0.0 most of the time, I don’t think this
will change anything, but it will likely solve the spikes you are
seeing. Of course, what do you use as the modifier instead of dt since
dt will still have larger than normal values? Normally you would do
something like, player.moveX( moveSpeed * dt ), but you did the above to
compensate for dt’s huge deviations. I think SOME_CONSTANT would just
lock dt to some upper bound, so if it does end up getting too high, it
will still work out. Just subtract the upper bound from dt, use the
upper bound as the modifier, and stick the remaining portion in totalTime.

I haven’t tried it yet as it is 6:30AM and I am getting ready to go to
work, but it makes sense to me this early in the morning.–
GBGames’ Blog: http://www.gbgames.com/blog

Hello,

Did you interpolate between two states, because when I used his 3rd (without
state interpolation) method I got worse results than my original (his 2nd
method).

Unrelated: How do you post to this newsgroup through MS outlook express?

Amir Cicak wrote:

Hello,

Did you interpolate between two states, because when I used his 3rd (without
state interpolation) method I got worse results than my original (his 2nd
method).

Yes I did, but I already solved this using the method proposed by Gerry
(balancing the duration of the frames using SDL_Delay()).