Softbody rendering. (Slow)

Raven
Posts: 5
Joined: Wed May 04, 2011 5:49 pm

Softbody rendering. (Slow)

Post by Raven »

I'm trying to reproduce the Softbody demo. I got it to run, but it's painfully slow. I'm getting something like 1-2 frames persecond here. Below is the code, can anyone tell me what I'm doing wrong?

main body.

Code: Select all

int main(int argc, char** argv)
{
	btDefaultMotionState *motionState;
	btBroadphaseInterface* broadphase;
	btDefaultCollisionConfiguration* collisionConfiguration = new  btSoftBodyRigidBodyCollisionConfiguration();
	
	btSoftBodyWorldInfo	m_softBodyWorldInfo;
	btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
	m_softBodyWorldInfo.m_dispatcher = dispatcher;
	
	btVector3 worldAabbMin(-1000,-1000,-1000);
	btVector3 worldAabbMax(1000,1000,1000);
	broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax);
	m_softBodyWorldInfo.m_broadphase = broadphase;


	btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
	dynamicsWorld = new btSoftRigidDynamicsWorld (dispatcher, broadphase, solver,collisionConfiguration);
	dynamicsWorld->setGravity(btVector3(0,-0.1,0));
	m_softBodyWorldInfo.m_gravity.setValue(0,-0.1,0);
	m_softBodyWorldInfo.m_sparsesdf.Initialize();

	//create Object
	btScalar	mass(0.010f);
	bool isDynamic = (mass != 0.f);	
	btVector3 localInertia(1,1,1);

	psb=btSoftBodyHelpers::CreateFromTriMesh(m_softBodyWorldInfo,gVerticesBunny, &gIndicesBunny[0][0], BUNNY_NUM_TRIANGLES);
	btSoftBody::Material*	pm=psb->appendMaterial();
	pm->m_kLST = 0.01;
	pm->m_kAST = 0.01;
	pm->m_kVST = 0.01;
	psb->generateBendingConstraints(2,pm);
	psb->m_cfg.piterations	=	2;
	psb->m_cfg.kDF			=	0.5;
	psb->m_cfg.collisions	|=	btSoftBody::fCollision::VF_SS;
	psb->randomizeConstraints();
	btMatrix3x3	m;
	m.setEulerZYX(0,-SIMD_PI/2,0);
	psb->transform(btTransform(m,btVector3(2,4,0)));
	psb->scale(btVector3(20,20,20));
	psb->setTotalMass(0.02,true);
	psb->setVelocity(btVector3(-10,0,0));
	dynamicsWorld->addSoftBody(psb);

	


	//OpenGL init
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
	glutInitWindowSize(1024, 1024);
	glutCreateWindow("PROJECT");
	myInit();
        glutReshapeFunc(myReshape);
	glutDisplayFunc(draw);
	glutIdleFunc(timer);
	glutMainLoop();

}
As you can see it's just using the Bunny mesh from the demo. At first I thought maybe because of my rendering routine is bad, but disable it (just let the collision run) doesn't help.

The only part I find question is the timestep, but I don't think that's the case. Here is the timer:

Code: Select all

void timer(void)
{
	float dtime = time;
	time = glutGet(GLUT_ELAPSED_TIME) / 500.0;
	dtime = time - dtime;
	if(dynamicsWorld)
	dynamicsWorld->stepSimulation(dtime, 100);
}
So anyone know what is wrong with this code?
bitshifternz
Posts: 6
Joined: Sun Apr 17, 2011 11:19 pm

Re: Softbody rendering. (Slow)

Post by bitshifternz »

Code: Select all

dynamicsWorld->stepSimulation(dtime, 100);
Max sub steps of 100 is huge. Perhaps you are getting a large time delta on your first frame and get into a loop of doing a large number of sub steps each frame which it never recovers from. Try the default of 1 sub step and see if it behaves.
Raven
Posts: 5
Joined: Wed May 04, 2011 5:49 pm

Re: Softbody rendering. (Slow)

Post by Raven »

yep that does it. Thanks.