Understanding physics simulation timing

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: Understanding physics simulation timing

Postby microdot » Fri Jun 10, 2011 11:05 am

Break through... :mrgreen:

I've locked fps to 60, and set the NewtonUpdate to 60... now internally I don't know what this is doing but in render, frame rate is a stable 60, and vehicles are moving fluidly, 15 of them. If I revert to 1 vehicle, then way too fast again :twisted: - this is something silly now.
Univerb Gaming Studios // Web: http://www.univerbstudios.co.za
User avatar
microdot
 
Posts: 26
Joined: Thu Oct 25, 2007 12:20 pm
Location: Johannesburg, South Africa

Re: Understanding physics simulation timing

Postby Stucuk » Sat Jun 11, 2011 8:30 am

This is what my Newton Thread looks like:

//Main Thread
Code: Select all
Repeat

  Lock;
   FTimer.Refresh;
   UpdateWorld(FTimer.FrameTime);
   Close := FCloseNow;
  Unlock;

  MySleep;//Sleep(1);
 Until Close;


// UpdateWorld code
Code: Select all
const TimeSlice : Extended = 1/60;

procedure TNewtonThread.UpdateWorld(FrameTime : Extended);
begin
 IncF(FLastFrameTime,FrameTime);
 If FLastFrameTime > 1 then
 FLastFrameTime := 1;

  If FLastFrameTime >= TimeSlice then
  begin
    if not FPaused then
    begin
     NewtonUpdate(FWorld, TimeSlice);
     NewtonManager.UpdateMatrixes;
    end;

    FNewtTimer.Refresh;
    FFPS := FNewtTimer.GetLowFPS;

    DecF(FLastFrameTime,TimeSlice);
    if FLastFrameTime < 0 then
    FLastFrameTime := 0;
  end;
end;
User avatar
Stucuk
 
Posts: 801
Joined: Sat Mar 12, 2005 3:54 pm
Location: Scotland

Re: Understanding physics simulation timing

Postby microdot » Mon Jun 13, 2011 6:45 am

I've re-implimented the logic as follows:

Code: Select all
        const float framerateCost = 1f / 60f;
        float lastFrameTime = 0;
        float frameTime = 0;
        public void Simulate()
        {
            frameTime = _engine.GetTimeElapsed();
            lastFrameTime = lastFrameTime + frameTime;

            if (lastFrameTime > 1)
            {
                lastFrameTime = 1;
            }
            if (lastFrameTime >= framerateCost)
            {
                physics.Simulate(framerateCost);
            }

            lastFrameTime = lastFrameTime - framerateCost;
            if (lastFrameTime < 0)
            {
                lastFrameTime = 0;
            }
        }


Does this look correct to you? I call this from a seperate running thread which is totally independent to the rendering thread. I still have the following problem:

1 vehicle - speed is very fast - fps is stable but simulation rate is like travelling at 1000mph
25 vehicle - speed is very slow - fps is table but simulation rate is like travelling at 5mph

As we can see I've definitely decoupled the render and physics or it would never perform this way (irrespective of seperate thread). If a video would help I'll need to make one sometime?
Univerb Gaming Studios // Web: http://www.univerbstudios.co.za
User avatar
microdot
 
Posts: 26
Joined: Thu Oct 25, 2007 12:20 pm
Location: Johannesburg, South Africa

Re: Understanding physics simulation timing

Postby microdot » Mon Jun 13, 2011 9:25 am

To clarify above, GetTimeElapsed is actually ticks, a bit misleading, I call:

Code: Select all
    return QueryPerformanceCounter(out x);


Which is part of

Code: Select all
    [DllImport("Kernel32.dll")]
    private static extern bool QueryPerformanceCounter(
    out long lpPerformanceCount);
Univerb Gaming Studios // Web: http://www.univerbstudios.co.za
User avatar
microdot
 
Posts: 26
Joined: Thu Oct 25, 2007 12:20 pm
Location: Johannesburg, South Africa

Re: Understanding physics simulation timing

Postby microdot » Mon Jun 13, 2011 10:04 am

Tried QueryPerformanceFrequency to the same end... not making sense why there is such a big differnce here. :?
Univerb Gaming Studios // Web: http://www.univerbstudios.co.za
User avatar
microdot
 
Posts: 26
Joined: Thu Oct 25, 2007 12:20 pm
Location: Johannesburg, South Africa

Re: Understanding physics simulation timing

Postby microdot » Mon Jun 13, 2011 10:35 am

Okay, with some help from Sylvain over at TV related to my sim rate and the way I was using the hi res timers, we've made a bit of progress. 15+ vehicles == smooth, 1 vehicle == stuttery but seems to be same simulation rate as with 15+. We are closer. :twisted:
Univerb Gaming Studios // Web: http://www.univerbstudios.co.za
User avatar
microdot
 
Posts: 26
Joined: Thu Oct 25, 2007 12:20 pm
Location: Johannesburg, South Africa

Re: Understanding physics simulation timing

Postby Julio Jerez » Mon Jun 13, 2011 12:01 pm

The stuttering will not go away unless teh smoth frame inteplation is implemented.

The is a sample of that in the SDK demos.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Understanding physics simulation timing

Postby microdot » Tue Jun 14, 2011 5:37 am

Thanks Julio, will download and look for the demo.

In terms of current code we're sitting with:

Code: Select all
            long freq = 0;
            public double GetFreq()
            {
                QueryPerformanceFrequency(out freq);
                return freq;
            }

            long timer = 0;
            public double GetTicks()
            {
                QueryPerformanceCounter(out timer);
                return ((double)timer / (double)freq);
            }


Code: Select all
const float framerateCost = 1f / 60f;
        double lastFrameTime = 0;
        double frameTime = 0;
        double newGetTicks = 0;
        double oldGetTicks = 0;
        public void Simulate()
        {
            _engine.GetFreq();
            newGetTicks = _engine.GetTicks();
            frameTime = (newGetTicks- oldGetTicks);
            oldGetTicks = newGetTicks;
         
            lastFrameTime = lastFrameTime + frameTime;

            if (lastFrameTime > 1)
            {
                lastFrameTime = 1;
            }
            if (lastFrameTime >= framerateCost)
            {
                physics.Simulate(framerateCost);
            }

            lastFrameTime = lastFrameTime - framerateCost;

            if (lastFrameTime < 0)
            {
                lastFrameTime = 0;
            }
        }


1 vehicle moves with stutter - drive, pause, drive, pause, drive, pause...
15 vehicles moves without visible stutter.

The trouble is still perhaps in my usage of GetTicks()
Univerb Gaming Studios // Web: http://www.univerbstudios.co.za
User avatar
microdot
 
Posts: 26
Joined: Thu Oct 25, 2007 12:20 pm
Location: Johannesburg, South Africa

Re: Understanding physics simulation timing

Postby Stucuk » Tue Jun 14, 2011 8:30 am

I would create a class for it if i was you. You only need to get the Frequency once when the class has been created. Oh and its no longer ticks, so "GetTicks" is an incorrect name, its in seconds once you take the frequency into consideration.

Your usage of the timer looks fine.
User avatar
Stucuk
 
Posts: 801
Joined: Sat Mar 12, 2005 3:54 pm
Location: Scotland

Re: Understanding physics simulation timing

Postby microdot » Wed Jun 15, 2011 2:30 am

Yes you're correct - let me try that and report back.
Univerb Gaming Studios // Web: http://www.univerbstudios.co.za
User avatar
microdot
 
Posts: 26
Joined: Thu Oct 25, 2007 12:20 pm
Location: Johannesburg, South Africa

Re: Understanding physics simulation timing

Postby microdot » Wed Jun 15, 2011 2:39 am

Right, I have a main class that get's the Frequency once and stores it.

I then have a Physics Loop decoupled from the render stream and in it's own thread as (very simple setup for now):
Code: Select all
void physicsLoop()
    {
        int count;
         
        while (true)
        {   
            Simulate();
        }
    }


Simulate now looks like:

Code: Select all
        const float framerateConst = 1f / 60f;
        double lastFrameTime = 0;
        double frameTime = 0;
        double newGetSeconds = 0;
        double oldGetSeconds = 0;
        public void Simulate()
        {
            newGetSeconds = _engine.GetSecondsSimulation();
            frameTime = (newGetSeconds - oldGetSeconds);
            oldGetSeconds = newGetSeconds;
         
            lastFrameTime = lastFrameTime + frameTime;

            if (lastFrameTime > 1)
            {
                lastFrameTime = 1;
            }
            if (lastFrameTime >= framerateConst)
            {
                physics.Simulate(framerateConst);
            }

            lastFrameTime = lastFrameTime - framerateConst;

            if (lastFrameTime < 0)
            {
                lastFrameTime = 0;
            }
        }


physic.Simulate(framerateConst); calls a NewtonUpdate with that value. I've still got 1 vehicle driving, pausing, driving, pausing and then 15+ vehicles working just fine. I really do think that Julio is correct (and you because you mentioned this originally in posting) that I need smooth frame inteplation. Then again, it's massive gaps between point A and point B during the freeze and unfreeze periods of simulation. I don't think it's inteplation only - the gaps between each physics 'refresh' is big
Univerb Gaming Studios // Web: http://www.univerbstudios.co.za
User avatar
microdot
 
Posts: 26
Joined: Thu Oct 25, 2007 12:20 pm
Location: Johannesburg, South Africa

Re: Understanding physics simulation timing

Postby Stucuk » Wed Jun 15, 2011 7:29 pm

I have never used smooth frame interpolation because i have no clue how to implement it. I did try once but it completely failed to have any impact. I was most likely doing something wrong.
User avatar
Stucuk
 
Posts: 801
Joined: Sat Mar 12, 2005 3:54 pm
Location: Scotland

Previous

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron