# Physx fixed time step (solved)

This topic is 2075 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi everyone,

I am trying to port some older physx 2.8 code to the newer physx 3.2 and i am having a hard time getting the graphics and physics to sync properly.

With the old physx 2.8 it had the scene->setTiming method which has been removed in physx 3.

The engine has a preTickSingal and a postTickSignal, with physx 2.8 i would do this and it worked perfectly (that is using the default vales in setTiming function).

void preTickSignal()
{
mScene->fetchResults(NX_RIGID_BODY_FINISHED,true);
mSimulating=false;
}

void postTickSignal(elapsed)
{
mScene->simulate(elapsed);
mSimulating=true;
}


I am trying to emulate the behavior of the old physx fixed time step but i just can't get it to work. What exactly is the old physx 2.8 doing under the hood when setTiming is set with NX_TIMESTEP_FIXED? Is it internally doing something like this (i have tried this in the postTickSignal and the simulation is very unstable and i am using the exact same default values the physx 2.8 used by default but it's not using the maxTimeStep, i wasn't sure about this variable):

	if(mAccumulator > FixedSubStepSize)
mAccumulator = 0.0f;

mAccumulator  += elapsed;
if(mAccumulator < FixedSubStepSize)
{
mNbSubSteps = 0;
return;
}

mNbSubSteps= physx::PxMin(physx::PxU32(mAccumulator/FixedSubStepSize), MaxSubSteps);

mAccumulator -= F32(mNbSubSteps)*FixedSubStepSize;

if(mNbSubSteps ==0)
return;

for(int i=0;i<mNbSubSteps;i++)
{
mScene->fetchResults( true );
mScene->simulate(FixedSubStepSize);
}


Forgive my noobness on this, it's something i never investigated before because the old physx 2.8 way just worked.

##### Share on other sites

No need to apologize for a question you don't know the answer to...lol..we all have to ask at one time or another. I don't have access to the code right now, but I"m using PhysX 3.2 and using the update strategy laid out in the Guide should give the appropriate behavior. Basically you want to increment a timer by the amount time that has elapse since the last call to your physic update method. As long as that timer is less than a const stepSize, do not call simulate...
Ex.

const float simStepSize = 1.0f/60.0f; //60 Hz update
float simTimer = 0.0f;
void update( float dt )
{
simTimer+=dt;

if( simTimer < simStepSize )
return;

simTimer-=simStepSize;

//if the previous call to fetch result has been completed
PxScene::simulate( simStepSize );

}

##### Share on other sites

Thanks cgrant got it working perfectly now, i don't mind this new physx actually, it's kinda feels like the switch from OpenGL 2 to OpenGL 3.

*Edit:

Ooops no i didn't  . It is actually getting pretty close and is a lot more stable using the above code but still not 100%. I'm still curious what physx 2.8 where doing because they are using two additional parameters one is maxTimestep and the other maxIter.

I changed the simulate to run at 1.f/32.f because according to the engine documentation the postTickSignal(elapsed) function is called every 32ms and checking the elapsed it does seem to apart from the occasional spike. The fully working physx 2.8 plugin is using 1.f/32.f too.

Edited by reducer

##### Share on other sites

Here are two pvd's files: http://www.filedropper.com/tests_1

test.pvd = 1.f/32.f

test2.pvd = 1.f/60.f

test2 seems better.

The official documentation states to use at least 50 for simulating although the physx 2.8 plugin does not and behaves perfectly

Edited by reducer

##### Share on other sites

Im pretty sure i got it all sussed out now.

• 18
• 18
• 11
• 21
• 16