Properly Stepping PhysX 3.2 ?

Hello, I’m reading the PhysX guide, and I can’t seem to figure out how to properly step the simulation…

Could anyone please provide me with example code to run the simulation in a single thread ?

What I have now is:

SceneVector::iterator it;

	mAccumulator  += timeSinceLastUpdate;
	if(mAccumulator < mStepSize)
		return true;

	mAccumulator -= mStepSize;

	for (it = mScenes.begin(); it != mScenes.end(); ++it)
	{
		(*it)->simulate(mStepSize);
		(*it)->fetchResults(true);
	}

But, as you can see I couldn’t figure out how to properly sub-step the simulation, and the result is a very slow simulation.

It would help if you would provide information about current step size. I have 0.05 for mine and it works fine.

There are so many ways you could try stepping your simulation. You can look at http://gafferongames.com/game-physics/fix-your-timestep/ for some more info on this subject.

First off, if timeSinceLastUpdate is larger than mStepSize, you will be lagging behind. So mAccumulator will increase too quickly and never be less than mStepSize. Depending on how you implemented your frame timer, the first few frames might be longer than normal due to loading assets and whatnot. If you don’t care about determinism (assuming timeSinceLastUpdate is variable), try performing multiple simulation steps.

You can try changing it to something like this:

mAccumulator += timeSinceLastUpdate;

while( mAccumulator > mStepSize )
{
    mAccumulator -= mStepSize;
    for (it = mScenes.begin(); it != mScenes.end(); ++it)
    {
        (*it)->simulate(mStepSize);
        (*it)->fetchResults(true);
    }
}

This will let your accumulator shrink back down to keep up with your timeSinceLastUpdate. It will perform a variable amount of steps (none if accumulator is small enough) in order to keep your accumulator as small as possible.

If your timeSinceLastUpdate is very large, you should clamp it to something more reasonable like 5 * mStepSize (which is arbitrary, so play around with it). In a typical game engine, this clamp should be done for the other systems that use this time (AI, Graphics, etc) so everything is kept in sync with physics.

Hope that helps.

Thanks for the reply, what was going on was, since I had nothing running except PhysX - the loop was running too fast for single-precision timing.

I already fixed the problem.

void PhysicsSystem::step(Real stepSize)
{
	SceneVector::iterator it;
	for (it = mScenes.begin(); it != mScenes.end(); ++it)
	{
		(*it)->simulate(stepSize);
		(*it)->fetchResults(true);
	}
}

bool PhysicsSystem::update(const Real& timeSinceLastUpdate)
{	
	mAccumulator += timeSinceLastUpdate; 

	while (mAccumulator >= mStepSize) 
	{ 
		mAccumulator -= mStepSize; 
		step(mStepSize);
	}

	return true;
}

Now it’s like that, and works pretty well.

Sorry I forgot to post earlier.

And thanks for the tips :)