i am currently working on a study project (I have to finish in 3 weeks ) which has a big amount of physics objects.
Now i tried to update the physics in a seperate thread like this:
Code: Select all
void MyGame::PhysicsUpdaterThread()
{
//fist we define some variables we need for calculation of delta time
double lastTime = m_Framework->GetTime();
double deltaTime = 1.0 / 60.0;
double threadtimediff = 0;
while (!m_StopPhysicsUpdate)
{
//Get the current delta time of this thread
double currTime = m_Framework->GetTime();
deltaTime = currTime - lastTime;
lastTime = currTime;
//Then calculate the time difference of the current delta time to the target delta time
//Target delta time is the fixed timestep
threadtimediff = PHYSICS_FIXEDTIMESTEP - deltaTime;
PhysicsMutex.lock();
dynamicsWorld->stepSimulation(deltaTime, PHYSICS_MAX_SUBSTEPS, PHYSICS_FIXEDTIMESTEP);
PhysicsMutex.unlock();
//If the thread needs less time than the fixed timestep targets, we sleep a bit so that the processor has time for other stuff
//if (threadtimediff > 0)
std::this_thread::sleep_for(std::chrono::microseconds((int)(threadtimediff * 1000000)));
}
}
I also look at the new GPU features of bullet but i cant find any basic tutorial or documentation for that and the gpu demo is just too overloaded for me. So i sticked to the multithreading part.
My question now is, what is the best way to get multithreading working with bullet. Am i even calculating the delta time correct when i do it in the thread or do i have to use the deltatime from the main thread, which whouldnt make pretty much sense to me?
greets