Soft body vanishes

silenthunter
Posts: 8
Joined: Wed Apr 06, 2011 7:48 pm

Soft body vanishes

Post by silenthunter »

I'm trying to create a soft body sphere using code from the Soft Body Demo. I got it loaded and could see it in the debug drawer, but once I started stepping the simulation it vanished. I've tried disabling the simulation on "brush", my soft body variable, using setActivationState but that didn't seem to change anything.

I'm fairly certain that "brush" isn't moving anywhere since my simulation has no gravity and it's the only object in the world, but I'm not sure how to check its position since getWorldTransform().getOrigin() always returns btVector3(0, 0, 0).

Loading code

Code: Select all

void loadObj(const char* fileName, btVector3 &position, btScalar scaling)
{
	ConvexDecomposition::WavefrontObj wo;
	int loadedWO = wo.loadObj(fileName);

	if(loadedWO)
	{
		btVector3* d = new btVector3[wo.mTriCount * 3];

		btVector3 localScaling(scaling, scaling, scaling);
		
		int i;
		for ( i = 0; i < wo.mTriCount;i++)
		{
			int index0 = wo.mIndices[i*3];
			int index1 = wo.mIndices[i*3+1];
			int index2 = wo.mIndices[i*3+2];

			btVector3 vertex0(wo.mVertices[index0*3], wo.mVertices[index0*3+1],wo.mVertices[index0*3+2]);
			btVector3 vertex1(wo.mVertices[index1*3], wo.mVertices[index1*3+1],wo.mVertices[index1*3+2]);
			btVector3 vertex2(wo.mVertices[index2*3], wo.mVertices[index2*3+1],wo.mVertices[index2*3+2]);
			
			vertex0 *= localScaling;
			vertex1 *= localScaling;
			vertex2 *= localScaling;

			d[i * 3] = vertex0;
			d[i * 3 + 1] = vertex1;
			d[i * 3 + 2] = vertex2;

		}

		brush = btSoftBodyHelpers::CreateFromConvexHull(worldInfo, d, wo.mTriCount * 3);
		brush->generateBendingConstraints(2);
		btTransform trans;
		trans.setIdentity();
		trans.setOrigin(position);
		brush->transform(trans);

		dynamicsWorld->addSoftBody(brush);

	}
}
Update method

Code: Select all

void update(double elapsed)
{
	dynamicsWorld->stepSimulation(elapsed);
	btVector3 pos = brush->getWorldTransform().getOrigin();
	std::cout << pos.x() << " " << pos.y() << " " << pos.z() << std::endl;
}
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: Soft body vanishes

Post by dphil »

I'm not sure why your soft body is disappearing, but as for the issue with printing the origin... From my experience (I had the same problem with the softBody origin returning (0,0,0)), and from what I understand from soft body source code, soft bodies do not do anything with their transform. Origin and orientation are both zero-initialized, and that's it. This is because the soft body does not have the same notion of a single transform as a rigid body. Instead, its nodes all just store world coordinates, and any time transforms are applied to a soft body, it just directly modifies the nodes' world coordinates (see the softBody transform(...) function). To check node positions, I do this:

Code: Select all

btSoftBody::tNodeArray& btNodes = softBody->m_nodes;
for (int i = 0; i < btNodes.size(); ++i)
{
	printf("Soft body node %d: (%f, %f, %f)\n", i, btNodes[i].m_x.x(), btNodes[i].m_x.y(), btNodes[i].m_x.z());
}
So you could try that to see if the soft body nodes are at least where they should be.
silenthunter
Posts: 8
Joined: Wed Apr 06, 2011 7:48 pm

Re: Soft body vanishes

Post by silenthunter »

Thanks, that showed I was wrong about it "vanishing". It's just moving out of the scene at an extremely high velocity for some reason. Now to figure out why :?.

My first thought was self-collision, but changing "m_cfg.collisions" doesn't seem to do much.
silenthunter
Posts: 8
Joined: Wed Apr 06, 2011 7:48 pm

Re: Soft body vanishes

Post by silenthunter »

Alright I fixed the problem. Turns out while I set the gravity to 0 in the dynamicsWorld, I hadn't set it in my world info variable as well. One added line was all I needed :roll:

Code: Select all

worldInfo.m_gravity.setValue(0, 0, 0);
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: Soft body vanishes

Post by dphil »

Ah :) , yeah soft bodies have their own gravity separate from the dynamics world for some reason. This duplication of gravities tripped me up a little while ago as well. I don't see why soft bodies don't just use the dynamicsWorld gravity like the other collision object types. Maybe I'll file a bug report about it since I'm patching some other soft body stuff at the same time.
rookie
Posts: 12
Joined: Mon Dec 05, 2011 8:36 pm

Re: Soft body vanishes

Post by rookie »

Can anyone tell me how to update texture coordinates for soft body?? I am rendering a customized .obj file which falls down due to gravity but the texture is not at all as it is supposed to be. I am passing the texture coordinates but they are not updated since world transform doesn't work for soft body. I am using the soft bodys node structure to render the soft body. All this is done using OpenGL. Please any help would be appreciated.