Help with timestep

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Help with timestep

Postby KingSnail » Tue Apr 26, 2011 1:30 pm

Whenever my application reaches 30 fps and below the simulation becomes slower than normal.
I think there might be something wrong with my timestep right now.
I am running NewtonUpdate within the main thread for now.

AdvanceSimulation function:
Code: Select all
#define DEMO_PHYSICS_FPS        120.0f
#define DEMO_FPS_IN_MICROSECUNDS  (int (1000000.0f/DEMO_PHYSICS_FPS))
#define MAX_PHYSICS_LOOPS        1

static int g_currentTime;
static int g_physicTime;
static int g_timeAccumulator = DEMO_FPS_IN_MICROSECUNDS;

unsigned GetTimeInMicrosenconds()
{
   unsigned ticks;
   LARGE_INTEGER count;
   static bool firstTime = true;
   static bool haveTimer = false;
   static LARGE_INTEGER frequency;
   static LARGE_INTEGER baseCount;

   if (firstTime) {
      firstTime = false;
      haveTimer = QueryPerformanceFrequency(&frequency) ? true : false;
      QueryPerformanceCounter (&baseCount);
   }

   QueryPerformanceCounter (&count);
   count.QuadPart -= baseCount.QuadPart;
   ticks = unsigned (count.QuadPart * LONGLONG (1000000) / frequency.QuadPart);
   return ticks;
}

void AdvanceSimulation(int timeInMilisecunds, dFloat& param)
{
  // do the physics simulation here
  int deltaTime;
  int physicLoopsTimeAcc;
  dFloat fps;
  dFloat physicTime;
 
  // get the time step
  deltaTime = timeInMilisecunds - g_currentTime;
  g_currentTime = timeInMilisecunds;
  g_timeAccumulator += deltaTime;
 
  physicTime = 0;
  // advance the simulation at a fix step
  int loops = 0;
  physicLoopsTimeAcc = 0;

  while ((loops < MAX_PHYSICS_LOOPS) && (g_timeAccumulator >= DEMO_FPS_IN_MICROSECUNDS))
  {
    loops ++;
 
    // Process incoming events.
    //ProcessEvents (g_world);
 
    // sample time before the Update
    g_physicTime = GetTimeInMicrosenconds ();

    // run the newton update function
   NewtonUpdate (gT->GetWorld(), (1.0f / DEMO_PHYSICS_FPS));
 
    // calculate the time spent in the physical Simulation
    g_physicTime = GetTimeInMicrosenconds () - g_physicTime;

    // subtract time from time accumulator
    g_timeAccumulator -= DEMO_FPS_IN_MICROSECUNDS;
    physicTime ++;

    physicLoopsTimeAcc += g_physicTime;
  }
  if (loops > MAX_PHYSICS_LOOPS)
  {
    g_physicTime = physicLoopsTimeAcc;
    g_timeAccumulator = DEMO_FPS_IN_MICROSECUNDS;
  }

  param = dFloat ( (g_timeAccumulator) / dFloat(DEMO_FPS_IN_MICROSECUNDS) );
  if(param > 1.0) param = 0.5f;
}


Main Loop is like this:
Code: Select all
while(game_running)
{
      ProcessEvents();
      AdvanceSimulation(GetTimeInMicrosenconds(), InterpolationParam);

               render here...
}
Working on an MMORPG powered by Newton Dynamics.
User avatar
KingSnail
 
Posts: 112
Joined: Sat Jan 02, 2010 9:55 pm

Re: Help with timestep

Postby Stucuk » Tue Apr 26, 2011 4:23 pm

I gave up on timestep solutions and shoved newton in its own thread in my app. That way it doesn't have to wait on the rendering/whatever so you never get delays building up, it always(Well close enough) gives a stable "Frame Rate"(Newton Rate?).

Though you do have the extra work of redesigning your application so that its thread safe.
User avatar
Stucuk
 
Posts: 801
Joined: Sat Mar 12, 2005 3:54 pm
Location: Scotland

Re: Help with timestep

Postby KingSnail » Tue Apr 26, 2011 5:06 pm

Hmm thanks for reply Stucuk. Actually I originally had newton running its own thread which worked ok
but caused visual lag and sometimes raycast crash.

Does raycast have to be on same thread as newton?
I dont see how I can do that since my camera update function is on the main thread.
Working on an MMORPG powered by Newton Dynamics.
User avatar
KingSnail
 
Posts: 112
Joined: Sat Jan 02, 2010 9:55 pm

Re: Help with timestep

Postby Stucuk » Tue Apr 26, 2011 6:54 pm

I haven't used the raycast while newton is in a separate thread. Critical sections are how i access newton stuff from the main thread. No clue if there is a better way but it works.

Newton Thread
Code: Select all
EnterCriticalSection(Newton); NewtonUpdate; LeaveCriticalSection(Newton)


Main Thread
Code: Select all
EnterCriticalSection(Newton); Do something; LeaveCriticalSection(Newton)


Every time the main thread does rendering i get the matrix's of every newton object from newton before rendering and store them. That way each frame they are upto date(I basically keep 2 copies of the matrix, one for the newton thread to use in callbacks and one for the rendering thread. I do the same with anything that both could use.). There doesn't seem to be any visual lag.
User avatar
Stucuk
 
Posts: 801
Joined: Sat Mar 12, 2005 3:54 pm
Location: Scotland

Re: Help with timestep

Postby Julio Jerez » Tue Apr 26, 2011 10:37 pm

see the dThread, and Dsymulation, it run the update in a seprate thread at a fixed FPS with smooth inteplaotion.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests