[erlang-questions] perform a function on a fixed time interval

Roberto Ostinelli roberto@REDACTED
Tue Aug 24 13:35:41 CEST 2010


2010/8/24 Miguel Morales <therevoltingx@REDACTED>:
> Well, you'll want to learn about keeping a steady frame rate when
> doing calculations.
> Some of my bookmarks: http://gafferongames.com/game-physics/fix-your-timestep/
> http://developer.valvesoftware.com/wiki/Source_Multiplayer_Networking
>
> What you'll want to do is something like this:
>
> -define(FRAMES_PER_SECOND, 17).
>
> get_timestamp() ->
>    {Mega,Sec,Micro} = erlang:now(),
>    (Mega*1000000+Sec)*1000000+Micro.
>
> loop() ->
>    Before = trunc(get_timestamp() / 1000000),
>    update_physics(),
>    TimeTakenMS = (get_timestamp() - Before),
>    SleepTime = trunc(?FRAMES_PER_SECOND - (TimeTakenMS / 1000)),
>
>   if
>        SleepTime > 1 ->
>            {ok, TRef} = timer:send_after(SleepTime, self(), loop);
>        true ->  %%we're running behind...
>            loop() ! tick
>    end,
>
> Pretty bad cut & paste from my code.  But like Max said, just
> calculate how much you need to sleep before the next loop.

max, miguel,

thank you i get your point.

since the movements of my particles are defined in meters/second, i
will therefore need to use the effective time passed since last
computation, instead of a fixed variable, to determine current
particle positions.

since the top speed of a particle is 150m/s, and since the smallest
object is of 10m in diameter, this means that i need to have a
framerate less of 10/150 i.e. around 67ms, otherwise i could get the
famous 'ghosts' [i.e. collisions not being detected, so it looks like
if particles can get past one another].

67 seconds should be enough of a time for these computations?..


More information about the erlang-questions mailing list