Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8623627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:19:40+00:00 2026-06-12T07:19:40+00:00

This question is specific to reactive-banana and real-time simulations with a physical and visual

  • 0

This question is specific to reactive-banana and real-time simulations with a physical and visual component (eg., games).

According to Fix Your Timestep! the ideal way to setup a game loop (assuming physics that needs to be reproducible), you need a fixed timestep between frames. After considering a number of real complications , the author arrives at this game loop:

double t = 0.0;
const double dt = 0.01;

double currentTime = hires_time_in_seconds();
double accumulator = 0.0;

State previous;
State current;

while ( !quit )
{
     double newTime = time();
     double frameTime = newTime - currentTime;
     if ( frameTime > 0.25 )
          frameTime = 0.25;   // note: max frame time to avoid spiral of death
     currentTime = newTime;

     accumulator += frameTime;

     while ( accumulator >= dt )
     {
          previousState = currentState;
          integrate( currentState, t, dt );
          t += dt;
          accumulator -= dt;
     }

     const double alpha = accumulator / dt;

     State state = currentState*alpha + previousState * ( 1.0 - alpha );

     render( state );
}

The synopsis is that the physics simulation is always fed the same increment of time (dt) for numerical stability. Arranging for that must consider that physics and visuals may update at different frequencies and you don’t want to get too far behind.

For example, you may want updates at a frequency of 20hz, but a visual update with a framerate of 60hz. This loop does linear interpolation of the physics to make up the difference between physics updates and graphical updates.

Additionally, when the difference in time between frames is much larger than dt there is a loop to handle stepping the updates in chunks of dt. The note about the spiral of death just refers to a case when your physics calculation simply can’t keep up with the desired frequency of updates, so you allow it to skip some updates.

For this discussion, the part I’m most interested in is arranging so that the call to the physics engine (the call to integrate) is always stepped by dt. Does reactive-banana allow the user to write this style loop? If so how? Perhaps an example doing real-time physics simulation is in order (or already exists)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:19:42+00:00Added an answer on June 12, 2026 at 7:19 am

    For this discussion, the part I’m most interested in is arranging so that the call to the physics engine (the call to integrate) is always stepped by dt. Does reactive-banana allow the user to write this style loop?

    Yes, reactive-banana can do that.

    The idea is that you write a custom event loop and hook reactive-banana into that. The library doesn’t make any assumptions as to where you get your events from, it “only” solves the problem of neatly describing new events in terms of existing ones. In particular, you can use the newAddHandler function to create two callback functions that are called in the appropriate places in the event loop. Essentially, reactive-banana is just a mind-boggling method to write ordinary callback functions that maintain state. When and how you call these functions is up to you.

    Here a general outline:

    -- set up callback functions
    (renderEvent, render) <- newAddHandler
    (stateUpdateEvent, stateUpdate) <- newAddHandler
    
    -- make the callback functions do something interesting
    let networkDescription = do
        eRender      <- fromAddHandler renderEvent
        eStateUpdate <- fromAddHandler stateUpdateEvent
        ...
        -- functionality here
    
    actuate =<< compile networkDescription
    
    -- event loop
    while (! quit)
    {
        ...
        while (accumulator >= dt)
        {
            stateUpdate (t,dt)      -- call one callback
            t += dt
            accumulator -= dt
        }
        ...
        render ()                   -- call another callback
    }
    

    In fact, I have written a game loop example in this style for an older version of reactive-banana, but haven’t gotten around to polishing and publishing it on hackage. The important things that I would like to see completed are:

    • Pick a graphics engine that is easy to install and works in GHCi. The concept uses SDL, but this is really quite awkward as it cannot be used from GHCi. Something like OpenGL + GLFW would be nice.
    • Offer a small abstraction to make it easier to write the interpolation phase. Probably just two things: an event eTimer :: Event t () that represents the regular physics updates and a behavior bSinceLastTimer :: Behavior t TimeDiff that measures the time since the last physics updates, which can be used for doing the interpolation. (It’s a behavior instead of an event, so the internal “draw this!” updates are transparent.)

    Andreas Bernstein’s blackout clone using reactive-banana may be an excellent example to implement in this style.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a question about how to use Reactive Extensions (Rx) in a specific
This question is specific to Adobe AIR ActionScript/Flash applications. I've spend quite some time
This question is specific to the iMacros suite in particular. What is the best
Sorry since this question is specific to my problem. While learning reflections i did
i have this specific question to do to you, i have a database from
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
(This question does not rely on a specific IoC framework, so the interfaces and
This question is kind of related to another question but I have a specific
This question is a duplicate of 1042830 , but MonoTouch-specific. Is there a way
I'm reviving this question, and making it more specific: Is there a .NET framework

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.