Simulation run time and bodies' velocities in freefall

gm09
Posts: 9
Joined: Fri Aug 05, 2011 1:27 am

Simulation run time and bodies' velocities in freefall

Post by gm09 »

Hi, I have a couple of questions regarding simulations:

1. Is there a way to set the simulation run time (run the simulation for a certain amount of time)? For instance, if I want to run a demo simulation for X seconds or Y frames, where/how can I set this?
2. How can I stop a simulation of rigid bodies in freefall, then retrieve their velocities and positions at this point?

Any help would be greatly appreciated! Thank you in advance :)
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: Simulation run time and bodies' velocities in freefall

Post by xexuxjy »

There's no way to directly do this, but it's easy enough to do.
You just need to do the counting in your main update loop, once you've reached your target time or frame just stop further calls to stepSimulation. You can then get the list of rigidBodies from the collisionWorld and just go through them and find out what their state is.
gm09
Posts: 9
Joined: Fri Aug 05, 2011 1:27 am

Re: Simulation run time and bodies' velocities in freefall

Post by gm09 »

Thanks for the reply. But what I don't know is where/how to set my target frame or time. For instance, I'm looking at the HelloWorld demo as reference, and I see:

Code: Select all

for (i=0;i<100;i++)
{
            dynamicsWorld->stepSimulation(1.f/60);
	
		//print positions of all objects
		for (int j=dynamicsWorld->getNumCollisionObjects()-1; j>=0 ;j--)
		{
			btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[j];
			btRigidBody* body = btRigidBody::upcast(obj);
			if (body && body->getMotionState())
			{
				btTransform trans;
				body->getMotionState()->getWorldTransform(trans);
				//printf("world pos = %f,%f,%f\n",float(trans.getOrigin().getX()),float(trans.getOrigin().getY()),float(trans.getOrigin().getZ()));
			}
		}
}
When running this app, I type "time ./AppHelloWorld" in the command line and receive .004s for real time it took to run this sim, but I can't make the connection between this and the number of iterations and timestep. Does this mean that the simulation runs for a total of 100*(1/60) seconds? I looked at the stepSimulation code but am still lost. Could you be more specific please?
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: Simulation run time and bodies' velocities in freefall

Post by xexuxjy »

The step simulation call takes in a time delta for each call (and an option fixed time step (default 60Hz)) . This is what Bullet will use to progress the simulation, so yes in your example below you're simulating a run time of 100 * (1/60) but are doing it it in .004s. Having it run at a fixed rate makes the simulation a great deal more stable / predictable. Normally you'd have the stepSimulation as part of your update call with other work going on so wouldn't have all the time available to Bullet. If you wanted to use the real-elapsed time for the simulation you can do that, but you'll have to have a timer to provide the value to pass into stepSimulation for you , you'll also need to pass in maxSubSteps as 0 to allow variable timestamp (have a look at the stepSimulation call in btDiscreteDynamicsWorld.cpp)

Hope this makes some sort of sense.