improved game soft bodies, works for objects that are using 'set smooth'

use shape matching by default for game soft bodies
store soft body index for game vertices
This commit is contained in:
Erwin Coumans
2008-09-25 03:02:30 +00:00
parent 38a80ff9a5
commit 718e2bf74f
8 changed files with 219 additions and 108 deletions

View File

@@ -222,14 +222,57 @@ void CcdPhysicsController::CreateRigidbody()
psb->appendFace(idx[0],idx[1],idx[2]);
}
///create a mapping between graphics mesh vertices and soft body vertices
{
for (int i=0;i<hlib.m_vertexIndexMapping.size();i++)
psb->m_userIndexMapping.push_back(hlib.m_vertexIndexMapping[i]);
//psb->m_userIndexMapping.push_back(hres.m_Indices[i]);
RAS_MeshObject* rasMesh= GetShapeInfo()->GetMesh();
if (rasMesh)
{
//printf("apply\n");
RAS_MeshSlot::iterator it;
RAS_MeshMaterial *mmat;
RAS_MeshSlot *slot;
size_t i;
//for each material
for (int m=0;m<rasMesh->NumMaterials();m++)
{
// The vertex cache can only be updated for this deformer:
// Duplicated objects with more than one ploymaterial (=multiple mesh slot per object)
// share the same mesh (=the same cache). As the rendering is done per polymaterial
// cycling through the objects, the entire mesh cache cannot be updated in one shot.
mmat = rasMesh->GetMeshMaterial(m);
slot = mmat->m_baseslot;
for(slot->begin(it); !slot->end(it); slot->next(it))
{
int index = 0;
for(i=it.startvertex; i<it.endvertex; i++,index++)
{
RAS_TexVert* vertex = &it.vertex[i];
//search closest index, and store it in vertex
vertex->setSoftBodyIndex(0);
btScalar maxDistSqr = 1e30;
btSoftBody::tNodeArray& nodes(psb->m_nodes);
btVector3 xyz = trans(btVector3(vertex->getXYZ()[0],vertex->getXYZ()[1],vertex->getXYZ()[2]));
for (int n=0;n<nodes.size();n++)
{
btScalar distSqr = (nodes[n].m_x - xyz).length2();
if (distSqr<maxDistSqr)
{
maxDistSqr = distSqr;
vertex->setSoftBodyIndex(n);
}
}
}
}
}
}
}
hlib.ReleaseResult(hres);
psb->randomizeConstraints();
}
@@ -292,13 +335,42 @@ void CcdPhysicsController::CreateRigidbody()
//psb->m_cfg.collisions = btSoftBody::fCollision::SDF_RS;//btSoftBody::fCollision::CL_SS+ btSoftBody::fCollision::CL_RS;
psb->m_cfg.collisions = btSoftBody::fCollision::SDF_RS + btSoftBody::fCollision::CL_SS;
//psb->m_cfg.collisions = btSoftBody::fCollision::CL_SS + btSoftBody::fCollision::CL_RS;
//btSoftBody::Material* pm=psb->appendMaterial();
btSoftBody::Material* pm=psb->m_materials[0];
pm->m_kLST = 0.1f;
pm->m_kLST = m_cci.m_linearStiffness;
pm->m_kAST = m_cci.m_angularStiffness;
pm->m_kVST = m_cci.m_volumePreservation;
//pm->m_kAST = 0.01f;
//pm->m_kVST = 0.001f;
psb->generateBendingConstraints(2,pm);
//psb->m_cfg.piterations = 4;
//psb->m_cfg.viterations = 4;
//psb->m_cfg.diterations = 4;
//psb->m_cfg.citerations = 4;
if (m_cci.m_gamesoftFlag & 1)///OB_SOFT_SHAPE_MATCHING)
{
psb->setPose(false,true);//
} else
{
psb->setPose(true,false);
}
psb->m_cfg.kDF = 0.5;
psb->m_cfg.kMT = 0.05;
psb->m_cfg.piterations = 5;
psb->m_cfg.piterations = 5;
//psb->m_cfg.kVC = 20;
psb->randomizeConstraints();
/*
psb->m_cfg.kDF = 0.1f;//1.f;
psb->m_cfg.kDP = 0.0001;
@@ -315,10 +387,10 @@ void CcdPhysicsController::CreateRigidbody()
// psb->activate();
// psb->setActivationState(1);
// psb->setDeactivationTime(1.f);
//psb->m_cfg.piterations = 4;
//psb->m_materials[0]->m_kLST = 0.1+(i/(btScalar)(n-1))*0.9;
psb->setTotalMass(m_cci.m_mass);
psb->generateClusters(64);
//psb->generateClusters(8);//(64);
psb->setCollisionFlags(0);
// m_object->setCollisionShape(rbci.m_collisionShape);
btTransform startTrans;
@@ -1147,7 +1219,19 @@ bool CcdShapeConstructionInfo::SetMesh(RAS_MeshObject* meshobj, bool polytope)
{
const float* vtx = poly->GetVertex(i)->getXYZ();
btPoint3 point(vtx[0],vtx[1],vtx[2]);
m_vertexArray.push_back(point);
//avoid duplicates (could better directly use vertex offsets, rather than a vertex compare)
bool found = false;
for (int j=0;j<m_vertexArray.size();j++)
{
if (m_vertexArray[j]==point)
{
found = true;
break;
}
}
if (!found)
m_vertexArray.push_back(point);
numvalidpolys++;
}
} else