Multithreading stepSimulation

0xFF
Posts: 2
Joined: Mon Nov 03, 2014 9:09 pm

Multithreading stepSimulation

Post by 0xFF »

Hi,

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)));
	}
}
But now the simulation looks pretty slow motion and if i add more objects the simulation gets slower even more. Now comes the interesting part: When i use stepSimulation(deltaTime*2, 0, PHYSICS_FIXEDTIMESTEP) the simulation looks pretty smooth and not slow motion but when i add more objects the simulation gets messed and objects are just exploding around (i assume the simulation is getting too unaccurate and objects are glitching around).

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
Tassim
Posts: 2
Joined: Sun Nov 02, 2014 5:11 pm

Re: Multithreading stepSimulation

Post by Tassim »

I'm trying to use parallel processing too...
There is a demo "Demos/MultiThreadedDemo" wich uses parrallel Solver & dispatcher I think.
I realy don't understand it though.

On the other side, i'm using C++11/14 wich implements a "thread" class. I'm actualy trying use it for a btThreadSupportInterface, so it can be crossplatform. (I dont want to use all those #define / #ifdef things...)

If someone has any information we could use, would be realy nice !
0xFF
Posts: 2
Joined: Mon Nov 03, 2014 9:09 pm

Re: Multithreading stepSimulation

Post by 0xFF »

Tassim wrote:I'm trying to use parallel processing too...
There is a demo "Demos/MultiThreadedDemo" wich uses parrallel Solver & dispatcher I think.
I realy don't understand it though.

On the other side, i'm using C++11/14 wich implements a "thread" class. I'm actualy trying use it for a btThreadSupportInterface, so it can be crossplatform. (I dont want to use all those #define / #ifdef things...)

If someone has any information we could use, would be realy nice !

Yes, i also looked at the MultIThreadedDemo but i have the same problem, its totally overloaded. I think this is a problem with nearly every bullet demo, there is so much stuff in it that you cant easily understand it as beginner.
But as i researched a bit i think that i need to use the new OpenCL functionality of bullet, since i need to simulate a pretty big amount of objects (mostly cubes). But since i dont have really that knowledge of opengl i try to stick with multithreading first.

//Edit: Ok i managed to get the multithreading working like in the MultithreadedDemo. but i think its still not enough for my project (still get unprecise simulation and therefore objects stucking through others). So it seems that i need to use the new opencl functions. Is there any tutorial or document to get a starting point for this?