Physics simulation is smooth but slow

Post Reply
avithohol
Posts: 34
Joined: Sun Feb 12, 2017 10:22 am

Physics simulation is smooth but slow

Post by avithohol »

Dear Community

My physics simulation runs like a slow motion movie, here are some samples:
https://youtu.be/P8C59huuz5M
https://www.youtube.com/watch?v=u-8Bw3w45R4

It's like the action takes place on the Moon.

I use version bullet-2.81-rev2613.

I have a workaround to this problem by simply multiplying by some number (6,8,10) the elapsed seconds value in the stepSimulation function,
so instead of this:

Code: Select all

	mDynamicsWorld->stepSimulation(static_cast<btScalar>(frameTimer.getElapsedSeconds()), 10);
I use this for faster simulation:

Code: Select all

	mDynamicsWorld->stepSimulation(static_cast<btScalar>(frameTimer.getElapsedSeconds())*10.0f, 10);
I am pretty sure it's a bad solution, but i cannot figure out where else I make mistake in my setup.

Based on the topics in the forum, I think I might have problem around, my main loop (it's based on the Bullett Canonical Loop example)
or the world scale (though I think I am in the proper limits). Gravity as Y -10 should work.

Can't see the forest for the trees, so if anybody could have look on the setup,
and point out anything would be most welcome.




Here is extract of the setup which i think is important:

Code: Select all

//setup of dynamics world
mBroadPhase = new btDbvtBroadphase();
mCollisionConfiguration = new btDefaultCollisionConfiguration();
mDispatcher = new btCollisionDispatcher( mCollisionConfiguration );
mSolver = new btSequentialImpulseConstraintSolver;
mDynamicsWorld = new btDiscreteDynamicsWorld( mDispatcher, mBroadPhase, mSolver, mCollisionConfiguration );
mDynamicsWorld->setGravity( btVector3( 0.0f, -10.0f, 0.0f ) );

Code: Select all

//the main loop
while (!done) 
{
	...
	static cTimer frameTimer;
	mDynamicsWorld->stepSimulation(static_cast<btScalar>(frameTimer.getElapsedSeconds()),10);
	frameTimer.reset();
	...
	renderFrame();
	...
}
frameTimer.getElapsedSeconds() properly measures elapsed seconds between each frame, and return sensible values:
0.0161584
0.0161978
0.0163978
0.016127
0.0164509
0.0158099
0.0161158
0.0163459
0.0160278
0.0172848
0.0151219
0.0162947
0.0164883
0.0157126
0.0165123
0.0159632
0.0165696
0.0160042
0.0159603
0.0158954


Code: Select all

//the rigid body dimensions are like this:
falling boxes:
btVector(5,5,5)	//in other words a box is 10 units wide, 10 units tall, and 10 units deep

Code: Select all

//transformation of meshes
//I use btMotionState to get interpolated values, and use it to transform meshes:
static btTransform trans;
static_cast<btRigidBody*>(mRigidBody)->getMotionState()->getWorldTransform(trans);
trans.getOpenGLMatrix((btScalar*)glm::value_ptr(transformation));

Code: Select all

//rigid bodies are setup like this 
cRigidBody::cRigidBody(const glm::vec3& translation, const glm::quat& orientation, const std::shared_ptr<cHull> parentHull, cPhysicsWorld& dynamicsWorld)
{
	if (parentHull->mMass > 0)
	{
		mMotionState = new btDefaultMotionState(btTransform(btQuaternion(orientation.x, orientation.y, orientation.z, orientation.w),
			btVector3(translation.x, translation.y, translation.z)));
		
		btVector3 btInertia = btVector3((btScalar)parentHull->mInertia.x, (btScalar)parentHull->mInertia.y, (btScalar)parentHull->mInertia.z);
		parentHull->getCollisionShape()->calculateLocalInertia((btScalar)parentHull->mMass, btInertia);

		btRigidBody::btRigidBodyConstructionInfo rigidBodyConstructionInfo((btScalar)parentHull->mMass, mMotionState, parentHull->getCollisionShape(), btInertia);

		mRigidBody = new btRigidBody(rigidBodyConstructionInfo);

		mRigidBody->setFriction(parentHull->mFriction);
		
		static_cast<btRigidBody*>(mRigidBody)->setAngularFactor(btVector3(parentHull->mAngularVelocity.x, parentHull->mAngularVelocity.y, parentHull->mAngularVelocity.z));

		dynamicsWorld.getPhysicsWorld()->addRigidBody(static_cast<btRigidBody*>(mRigidBody));
	}
	...
}
lunkhound
Posts: 99
Joined: Thu Nov 21, 2013 8:57 pm

Re: Physics simulation is smooth but slow

Post by lunkhound »

I think it is a scale problem. Your gravity is set to -10 which is about right for a world where 1 unit = 1 meter.
However your boxes are 10 x 10 x 10 which in meters is the size of a 3 story building!
avithohol
Posts: 34
Joined: Sun Feb 12, 2017 10:22 am

Re: Physics simulation is smooth but slow

Post by avithohol »

lunkhound wrote:I think it is a scale problem. Your gravity is set to -10 which is about right for a world where 1 unit = 1 meter.
However your boxes are 10 x 10 x 10 which in meters is the size of a 3 story building!


Spot on, I scaled everything down by 10, and everything is sped up to normal speed.
Shame on me it's written on the very beginning of wiki:
http://www.bulletphysics.org/mediawiki- ... _The_World


Now I have some clip through problem (margin issue maybe),
but i will research first.

Thank you Lunkhound, this is resolved!
Post Reply