Fixed Timestep and bullet

jazztickets
Posts: 21
Joined: Tue Dec 29, 2009 12:48 am

Fixed Timestep and bullet

Post by jazztickets »

Is there a way to manually interpolate between simulation steps without passing a variable time step to stepSimulation?

Say you are doing your own fixed time step code running at 100HZ.

Code: Select all

TimeStepAccumulator += FrameTime;
while(TimeStepAccumulator >= TimeStep) {
	UpdateGameLogic(TimeStep);
	TimeStepAccumulator -= TimeStep;
}
And inside UpdateGameLogic you are calling Bullet's World->stepSimulation() function. Now obviously stepSimulation will only see a time step of 0.01 seconds. Is there a way to take advantage of Bullet's interpolation code in this way? Bullet I think uses the private variable m_localTime to blend between simulation steps.

Ideally there would be another function called World->stepSimulationFixed(float FixedTimeStep, float BlendFactor). Blendfactor being TimeStepAccumulator / TimeStep.
jazztickets
Posts: 21
Joined: Tue Dec 29, 2009 12:48 am

Re: Fixed Timestep and bullet

Post by jazztickets »

Meh, decided that creating a derived custom world was the way to go.

Code: Select all

// Custom world that overrides stepSimulation for applications that implement their own fixed time step calculations
class btFixedWorld : public btDiscreteDynamicsWorld {

public:

	btFixedWorld(btDispatcher *dispatcher, btBroadphaseInterface *pairCache, btConstraintSolver *constraintSolver, btCollisionConfiguration *collisionConfiguration)
		: btDiscreteDynamicsWorld(dispatcher, pairCache, constraintSolver, collisionConfiguration) { }
	~btFixedWorld() { }

	void setTimeStepRemainder(btScalar time) { m_localTime = time; }
	int	stepSimulation(btScalar timeStep, int maxSubSteps=0, btScalar fixedTimeStep=0) {
		
		saveKinematicState(timeStep);
		applyGravity();
		internalSingleStepSimulation(timeStep);
		clearForces();
		
		return 1;
	}

};
Then, in your application's update code, something like this would work:

Code: Select all


// Accumulate time and apply fixed timestep
TimeStepAccumulator += FrameTime;
while(TimeStepAccumulator >= TimeStep) {
	Update(TimeStep); // update game logic and call stepSimulation(TimeStep)
	TimeStepAccumulator -= TimeStep;
}
World->setTimeStepRemainder(TimeStepAccumulator);
World->synchronizeMotionStates();
Buttery smooth interpolation ensues.