Making 200Hz timer for Physics simulation code

Can anyone suggest me how to make a timer which runs at 200Hz my simple
physics simulation code?

Don’t tell me to lower the frequency, I’m only interested in replies which
actually let me know how to do it.

Will SDL Timer with interval of 5 ms do? Which platforms can/can not do
this?

– Timo Suoranta – @Timo_K_Suoranta

SDL_Delay()? Check the docs

-bill!On Thu, Jan 17, 2002 at 09:42:08PM +0200, Timo K Suoranta wrote:

Can anyone suggest me how to make a timer which runs at 200Hz my simple
physics simulation code?

Will SDL Timer with interval of 5 ms do? Which platforms can/can not do
this?

None, that I know of. I think the minimum is 10 ms.

Might be worth checking SDL_GetTicks() in a tight loop and seeing if it’s
time to call your handler function yet.

–ryan.

First, you are going to need some custom interrupt code to make it exact.

You could average out the error by having a timer go off every 10ms and do
something once and then do the math for again for somethign 5 ms later
(assume nothing will change in the next 5 ms). (probably not a good option
from the way you sound.)

I don’t think any OS under the SDL will support that (I may be wrong). You
should probably look for a different library if your constraints are that
high. I could hack something for that in Windows, but it wouldn’t be pretty
=) Actually, if you wouldn’t mind it going off every 4ms, I could do that
in a fairly simple way.

SDL is for games. I don’t think it would be what you are looking for.
Actually, if you want such a precise measurement, you should look into a
RTOS that could probably keep up with that.

So I guess my answers are:
Try a RTOS
Make special hardware
I could HACK something for you… keyword: HACK> ----- Original Message -----

From: tksuoran@cc.helsinki.fi (Timo K Suoranta)
To:
Sent: Thursday, January 17, 2002 1:42 PM
Subject: [SDL] Making 200Hz timer for Physics simulation code

Can anyone suggest me how to make a timer which runs at 200Hz my simple
physics simulation code?

Don’t tell me to lower the frequency, I’m only interested in replies which
actually let me know how to do it.

Will SDL Timer with interval of 5 ms do? Which platforms can/can not do
this?

– Timo Suoranta – tksuoran at cc.helsinki.fi


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

once again, SDL_GetTicks is only accurate to 10ms anyways, so that’s not a
good option either> ----- Original Message -----

From: icculus@icculus.org (Ryan C. Gordon)
To:
Sent: Thursday, January 17, 2002 1:52 PM
Subject: Re: [SDL] Making 200Hz timer for Physics simulation code

Will SDL Timer with interval of 5 ms do? Which platforms can/can not do
this?

None, that I know of. I think the minimum is 10 ms.

Might be worth checking SDL_GetTicks() in a tight loop and seeing if it’s
time to call your handler function yet.

–ryan.


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

once again, SDL_GetTicks is only accurate to 10ms anyways, so that’s not a
good option either

It’s not supposed to be, is it? I know we had this accuracy issue on
WinNT, and made changes to fix it.

Not that it changes the fact that you can’t guarantee you’ll hit the loop
every five milliseconds, or that the system won’t preempt you, anyhow…I
tend to agree, SDL isn’t the right choice for such a simulation (or at
least, for getting timer interrupts…)

–ryan.

Timo K Suoranta wrote:

Can anyone suggest me how to make a timer which runs at 200Hz my simple
physics simulation code?

Just run your update loop multiple times per timestep.

Float leftover = 0;
Float myStep = 0.005;

void onUpdate(Float timestep)
{
leftover += timestep;
while (leftover > myStep)
{
leftover -= myStep;
// update physics
}
}

Jason
379

—end quoted text—

I don’t know it’s precision, but you should try calling gettimeofday() in a loop and only execute the code when 5ms passed…
But note that this will take 99% of your processor, since you cannot add any sleep here… (none FWIK have this precision…) so I guess you can lower the priority without any problem if you are not really going to use the 99%…On Thu, Jan 17, 2002 at 09:42:08PM +0200, Timo K Suoranta wrote:

Can anyone suggest me how to make a timer which runs at 200Hz my simple
physics simulation code?

Don’t tell me to lower the frequency, I’m only interested in replies which
actually let me know how to do it.

Will SDL Timer with interval of 5 ms do? Which platforms can/can not do
this?

Marcelo R Leitner <@Marcelo_Ricardo_Leit>
ICQ #: 29966851

I think the whole problem is thinking about these “200 Hz” in the wrong
way. A simulation is not a hard real time application, unless you
really need real time animated output - in which case it still isn’t the
simulation frame rate that matters, but the rendering frame rate.

For example, Kobo Deluxe runs the control and enemy “AI” at a 30 ms
period. Does that mean that the code is actually executed once every 30
ms?

No! The engine only guarantees that the average control system frame
rate will be 1000/30 Hz, and it synchronizes control system time with
real world time only just before calculating the graphics coordinates
for each video frame.

The timing is entirely derived from SDL_GetTicks(), and it’s updated only
once per rendered frame. No busy waiting, no control system threads, no
SDL_Delay()s or anything like that.

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Thursday 17 January 2002 22:07, Ryan C. Gordon wrote:

once again, SDL_GetTicks is only accurate to 10ms anyways, so that’s
not a good option either

It’s not supposed to be, is it? I know we had this accuracy issue on
WinNT, and made changes to fix it.

Not that it changes the fact that you can’t guarantee you’ll hit the
loop every five milliseconds, or that the system won’t preempt you,
anyhow…I tend to agree, SDL isn’t the right choice for such a
simulation (or at least, for getting timer interrupts…)

“Ryan C. Gordon” wrote:

once again, SDL_GetTicks is only accurate to 10ms anyways, so that’s not a
good option either

It’s not supposed to be, is it? I know we had this accuracy issue on
WinNT, and made changes to fix it.

the accuracy should be 1 ms almost everywhere. If not, and the
underlying platform allows 1 ms accuracy, feel free to report it as a
bug.

It is unclear what the original poster wanted to accomplish, however

Just run your update loop multiple times per timestep.

This never works in practice as well as it should. You tend to get choppy
performance; the app seems to run faster while the timer is catching up,
and slower once it does, and back again.

At least, that’s been my experience.

–ryan.

Of course! :slight_smile: It sucks, just as resampling audio without any form of
interpolation or filtering. Timing jitter translates into noise - or
unsmooth animation. It’s to be expected.

Kobo Deluxe runs the whole “world” through a linear interpolation filter
when generating graphics coords. (It can be disabled in the options menu
if you want to see the difference.) Of course, that requires that the
filter knows exactly which two frames it’s interpolating between, but
that’s not an issue, as everything is running cooperatively in the same
thread.

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Thursday 17 January 2002 23:27, Ryan C. Gordon wrote:

Just run your update loop multiple times per timestep.

This never works in practice as well as it should. You tend to get
choppy performance; the app seems to run faster while the timer is
catching up, and slower once it does, and back again.

once again, SDL_GetTicks is only accurate to 10ms anyways, so that’s not a
good option either

It’s not supposed to be, is it? I know we had this accuracy issue on
WinNT, and made changes to fix it.

That’s right. SDL_GetTicks() should be accurate to 1 ms.
It’s the timer callbacks that are only accurate to 10 ms, and part of that
is an operating system scheduling timeslice of 10 ms (on Windows and Linux,
barring real-time extensions)

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

I can’t get SDL_Delay() to work with an interval this small in Linux. I’m
trying to get a framerate of 60 fps in an emulator I’m working on, and the
best it will do is 50 (or 100), because of the granularity of the timer.

Linux 2.4.16, SDL 1.2.3

SteveOn January 17, 2002 04:12 pm, you wrote:

Can anyone suggest me how to make a timer which runs at 200Hz my simple
physics simulation code?

Don’t tell me to lower the frequency, I’m only interested in replies which
actually let me know how to do it.

Will SDL Timer with interval of 5 ms do? Which platforms can/can not do
this?

I can’t get SDL_Delay() to work with an interval this small in Linux. I’m
trying to get a framerate of 60 fps in an emulator I’m working on, and the
best it will do is 50 (or 100), because of the granularity of the timer.

This is because of the OS scheduling timeslice of 10 ms. If you run an OS
with real-time extensions, then you’ll be fine.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment