Running Simulations at Faster Rates than Real-Time

Post Reply
KodoCoder
Posts: 10
Joined: Sat Aug 16, 2014 12:40 am

Running Simulations at Faster Rates than Real-Time

Post by KodoCoder »

Hey all,

I'm running an experiment in Bullet Physics, so I was hoping to speeding up the simulation so I can collect all the data in a reasonable time. I've tried to follow the information from here http://gafferongames.com/game-physics/f ... -timestep/, but I'm not sure how to port the format he uses over to the Bullet Physics clientMoveAndDisplay().

Here's my interpretation of what I should be doing:

Code: Select all

void RagdollDemo::clientMoveAndDisplay()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
        bool decouples = true;

	//simple dynamics world doesn't handle fixed-time-stepping
	float ms = getDeltaTimeMicroseconds();
  	float minFPS = 90000.f/60.f;
	const double dt = .01;

	if (ms > minFPS)
		ms = minFPS;

	accumulator += ms;

	if (m_dynamicsWorld)
	{
	  if (!pause || (pause && oneStep))
	    {
	       //reseting touches
	      for (int f=0;f<10;f++)
		 {
		  touches[f] = 0;
 		}
	      // run next step
	      // Code that de-couples rendering and stepping
	       if (decouple)
		{
		   while (accumulator >= dt)
		    {
		      accumulator -= dt;
		      m_dynamicsWorld->stepSimulation(ms/90000.f);
		      if (timeStep%10==0)
			{
			  for (int j=0;j<8;j++)
			    {
			      double motorCommand;
			      motorCommand = 0;

			      for(int i=0;i<4;i++)
 				{
				   int sensor = i+5;
				   motorCommand =+ (touches[sensor] * weights[i][j]);
				   motorCommand = tanh(motorCommand);
				  motorCommand = motorCommand*45;
				  ActuateJoint(j, motorCommand, ms / 90000.f);
				}
			    }
			}
		       timeStep++;
		       if (timeStep==250)
			 {break;}
		     }
 		}
	    }

The problem is that I get different data from that code than from the code below, and I think that if I was doing it right I would be getting the same data if I increased the rate correctly. The faster-than-real-time simulation is consistent to itself, but it doesn't match the real-time simulation.

Code: Select all

 if (!decouple)
	    {
	       if (timeStep%10==0)
	      	{
		  for (int j=0;j<8;j++)
		    {
		      double motorCommand;
		      motorCommand = 0;
		      for(int i=0;i<4;i++)
			{
			  int sensor = i+5;
			  motorCommand =+ (touches[sensor] * weights[i][j]);
			  motorCommand = tanh(motorCommand);
			  motorCommand = motorCommand*45;
			  ActuateJoint(j, motorCommand, ms / 90000.f);
			}
		    }
		}
	      timeStep++;	      
	      //end of coupled code
	    }
	      
          //terminate in 250 timesteps
	  if (timeStep == 250)
	    {
	      Save_Position(0);
	      exit(0);
	    }
Any information you could give me on how to run through the simulation faster than the real-time physics would be greatly appreciated.

Thank you for your time,
Josh
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: Running Simulations at Faster Rates than Real-Time

Post by Basroil »

Bullet physics without user input should be identical regardless of the rate at which you calculate it (barring any pseudo-random number generators you add which are affected by external things), I've had the same results with a simulation regardless of if I was stepping 5 4.33ms frames per graphics frame or ~300 (completely uncapped independent multicore frame-rate for my specific use).

Unless you absolutely need the graphics, I would say to just skip graphics entirely and manually step the simulation in your recording loop. I've been doing some PSO based optimization with Bullet and, with some modifications to make Bullet a bit more thread safe, I've managed to hit ~30k frames/second (multiple simultaneous independent simulations, including PSO overhead) on very simple simulations (two bodies connected by joints with gravity) in a reasonably spec'ed machine.
KodoCoder
Posts: 10
Joined: Sat Aug 16, 2014 12:40 am

Re: Running Simulations at Faster Rates than Real-Time

Post by KodoCoder »

Hey Basroll, thanks for the response!

I do need the graphics in real-time, to make sure the simulation is acting in a manner that makes sense for the experiment. However, I don't need them at the same time that I run the experiment. That's why I want to make sure that the real-time data matched the speeded up version.

I'm rather new to Bullet Physics, and physics engines in general, and haven't had any luck on finding info about using Bullet Physics without the graphics (or speeding up the simulation in general). If you could point me in the direction of some information on that topic, that would be awesome. I'm unsure what "manually step[ping] the simulation means or how I'd do it (if you mean stepping through the simulation without using stepSimulation()), so if it doesn't take up too much time I'd could use some clarification (or links to clarification).

All the best,
Josh
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: Running Simulations at Faster Rates than Real-Time

Post by Basroil »

KodoCoder wrote: I'm rather new to Bullet Physics, and physics engines in general, and haven't had any luck on finding info about using Bullet Physics without the graphics (or speeding up the simulation in general). If you could point me in the direction of some information on that topic, that would be awesome. I'm unsure what "manually step[ping] the simulation means or how I'd do it (if you mean stepping through the simulation without using stepSimulation()), so if it doesn't take up too much time I'd could use some clarification (or links to clarification).
Well, you can just take a look at any of the demos, and it's really easy to switch them from display linked to an external tick, just move m_dynamicsWorld->stepSimulation(btScalar(deltaTime)); to your graphics external synchronization and (not necessary but something you might want to do at first to make sure it works) change deltaTime to a fixed number equal to a multiple of your simulation rate (default of 1/60). It's literally that easy to switch what does the stepping. Adding graphics is a bit more difficult, since you might get data races or even objects disappearing before you access them, but there's a MT bullet fork what solves a lot of those issues for you (though not sure if it's helpful for what you want to do).
KodoCoder
Posts: 10
Joined: Sat Aug 16, 2014 12:40 am

Re: Running Simulations at Faster Rates than Real-Time

Post by KodoCoder »

Thanks for the extra clarification. I got my simulation running without the graphics, and the data lines up with my graphics simulations as well.

Cheers,
Josh
Post Reply