Rigid bodies not colliding with soft bodies

Physics APIs, Physics file formats, Maya, Max, XSI, Cinema 4D, Lightwave, Blender, thinkingParticles™ and other simulation tools, exporters and importers
Post Reply
Maximalist
Posts: 5
Joined: Sun Nov 13, 2011 10:41 pm

Rigid bodies not colliding with soft bodies

Post by Maximalist »

Hello all,

I'm currently adding Bullet soft bodies to Dynamica as a personal learning exercise (I'll release the code when it's done, along with any documentation that comes out of it). I've made good progress but I'm currently blocked by the fact that soft bodies do not seem to interact with rigid bodies at all inside the plugin. Soft bodies do collide with each other, but not rigid bodies. I was wondering if anyone more familiar with the code might know any "obvious" reasons why this would happen. My soft body object (class bt_soft_body) construction code looks something like this:

Code: Select all

bt_soft_body::bt_soft_body(btSoftBodyWorldInfo &worldInfo, const std::vector<float> &triVertexCoords,
						   const std::vector<int> &triVertexIndices  )
{
	this->numTriangles = triVertexIndices.size() / 3;
	this->numVertices = triVertexCoords.size() / 3;
	this->triVertCoords = new btScalar[triVertexCoords.size()];
	this->triVertIndices = new int[triVertexIndices.size()];

	for(int i = 0; i < triVertexCoords.size(); i ++)
	{
		this->triVertCoords[i] = static_cast<btScalar>(triVertexCoords[i]);

	}
	
	for(int i = 0; i < triVertexIndices.size(); i ++)
	{
		this->triVertIndices[i] = triVertexIndices[i];
	}
	this->m_body.reset( btSoftBodyHelpers::CreateFromTriMesh(worldInfo, this->triVertCoords, this->triVertIndices, this->numTriangles ));
	
	btSoftBody::Material* pm = this->m_body->appendMaterial();
	pm->m_kLST = 0.5;
	
	this->m_body->scale(btVector3(1,1,1));
	this->m_body->setTotalMass(50);
	this->m_body->generateBendingConstraints(2, pm);
	this->m_body->m_cfg.piterations=2;
	this->m_body->m_cfg.kDF = 0.5;
	this->m_body->generateClusters(50); 
	this->m_body->m_cfg.collisions += btSoftBody::fCollision::CL_RS;
	this->m_body->m_cfg.collisions += btSoftBody::fCollision::CL_SS;
	this->m_body->randomizeConstraints();

}
I've modified the bt_solver_t constructor code to this:

Code: Select all

bt_solver_t::bt_solver_t():
	m_broadphase(new btDbvtBroadphase()),  
          m_solver(new btSequentialImpulseConstraintSolver),
            m_collisionConfiguration(new btSoftBodyRigidBodyCollisionConfiguration()),
            m_dispatcher(new btCollisionDispatcher(m_collisionConfiguration.get())),     
			 m_dynamicsWorld(new btSoftRigidDynamicsWorld(m_dispatcher.get(),
                                                        m_broadphase.get(),
                                                        m_solver.get(),
                                                        m_collisionConfiguration.get())
														
														),
			m_worldInfo()
{

    //register algorithm for concave meshes
    btGImpactCollisionAlgorithm::registerAlgorithm(m_dispatcher.get());

    m_dynamicsWorld->setGravity(btVector3(0, -9.81f, 0));
	
	m_dynamicsWorld->getDispatchInfo().m_enableSPU = true;
	
	bt_debug_draw* dbgDraw = new bt_debug_draw();
	m_dynamicsWorld->setDebugDrawer(dbgDraw);
	
	m_worldInfo.m_broadphase = m_broadphase.get();
	m_worldInfo.m_dispatcher = m_dispatcher.get();
	m_worldInfo.m_gravity = m_dynamicsWorld->getGravity();
	m_worldInfo.m_sparsesdf.Initialize();
	
}
m_worldInfo is passed in to the bt_soft_body constructor when adding new soft bodies to the solver.

Thanks in advance for any help.
Maximalist
Posts: 5
Joined: Sun Nov 13, 2011 10:41 pm

Re: Rigid bodies not colliding with soft bodies

Post by Maximalist »

I've run a few more searches on this topic in the forum archives and it has come up a few times in the past. See the following for example:

http://bulletphysics.org/Bullet/phpBB3/ ... +collision
http://bulletphysics.org/Bullet/phpBB3/ ... 169#p23169

I've played with some of the ideas in the second URL to no avail (but have not tried changing the collision margins yet). Other posts suggest that a problem may arise if a rigid body's position is set either at constructor time or if it is updated later - but which one of these is "correct" is not clear to me. I will try playing with the way object positions are set but there's no cause for optimism so far - hopefully I won't have to halt development :(.
Maximalist
Posts: 5
Joined: Sun Nov 13, 2011 10:41 pm

Re: Rigid bodies not colliding with soft bodies

Post by Maximalist »

It looks like I've managed to fix this. I changed the soft-body creation code to match that in the demo bunny constructor exactly. It now looks like this:

Code: Select all

bt_soft_body::bt_soft_body(btSoftBodyWorldInfo &worldInfo, const std::vector<float> &triVertexCoords,
						   const std::vector<int> &triVertexIndices  )
{
	this->numTriangles = triVertexIndices.size() / 3;
	this->numVertices = triVertexCoords.size() / 3;
	this->triVertCoords = new btScalar[triVertexCoords.size()];
	this->triVertIndices = new int[triVertexIndices.size()];

	for(int i = 0; i < triVertexCoords.size(); i ++)
	{
		this->triVertCoords[i] = static_cast<btScalar>(triVertexCoords[i]);
	

	}

	
	for(int i = 0; i < triVertexIndices.size(); i ++)
	{
		this->triVertIndices[i] = triVertexIndices[i];
	}
	this->m_body.reset( btSoftBodyHelpers::CreateFromTriMesh(worldInfo, this->triVertCoords, this->triVertIndices, this->numTriangles));
	
	btSoftBody::Material*	pm=this->m_body->appendMaterial();
	pm->m_kLST				=	0.5;
	pm->m_flags				-=	btSoftBody::fMaterial::DebugDraw;
	this->m_body->generateBendingConstraints(2,pm);
	this->m_body->m_cfg.piterations	=	3;
	this->m_body->m_cfg.kDF			=	0.5;
	this->m_body->scale(btVector3(1,1,1));	
	this->m_body->setTotalMass(100, true);
	this->m_body->randomizeConstraints();
	
}
I've also updated to the latest Bullet source (as of yesterday). Soft bodies, passive rigid bodies, and active rigid bodies now all seem to be interacting correctly :D.
Maximalist
Posts: 5
Joined: Sun Nov 13, 2011 10:41 pm

Re: Rigid bodies not colliding with soft bodies

Post by Maximalist »

Final word on this :):

The "fix" I posted before does not allow soft bodies to interact with each other. After further experimentation I found that the original cause of my problems was the btSoftBody::fCollision::CL_RS flag. When this is added soft bodies actually stop interacting with rigid bodies entirely (I'm guessing that this interacts badly with some other setting somewhere else), and as a result I cannot seem to use soft body - rigid body cluster collisions, though other collision detection methods do work (albeit imperfectly). So I added the cluster generation code along with the btSoftBody::fCollision::CL_SS flag back in. Things seem fine now except that soft-bodies tend to pass through planes after prolonged contact.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Rigid bodies not colliding with soft bodies

Post by Erwin Coumans »

The Bullet soft body implementation has several collision options, and not all combinations are compatible/fully implemented.

Generally it is best to check out the demos in Bullet/Demos/SoftDemo.

Thanks a lot for your contribution, your soft body integration has been added to Dynamica here:
http://code.google.com/p/dynamica/source/detail?r=129
Post Reply