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

Miguel Morales therevoltingx@REDACTED
Tue Aug 24 13:13:05 CEST 2010


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.

On Tue, Aug 24, 2010 at 4:06 AM, Roberto Ostinelli <roberto@REDACTED> wrote:
> 2010/8/24 Mazen Harake <mazen.harake@REDACTED>:
>>  Be cautious,
>>
>> Using send messages in an interval is dangerous if compute() can take more
>> then the interval. It can build up message queues on a congested system;
>> especially if what compute() does has side effects.
>>
>> Just a word of advise.
>>
>
> you are absolutely right, and i'm aware of the issue.
>
> it's a game engine which needs to detect collision between particles,
> and thus it needs to periodically compute the position of all elements
> and see if these collide or not.
>
> what would you recommend to ensure avoiding this issue?
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
>



-- 
http://developingthedream.blogspot.com/,
http://diastrofunk.com,
http://www.youtube.com/user/revoltingx, ~Isaiah 55:8-9


More information about the erlang-questions mailing list