support concave soft bodies, preliminary. could be used for cloth too. need vertex pinning/constraint attach to other objects.

This commit is contained in:
Erwin Coumans 2008-09-26 06:25:35 +00:00
parent a1bef84ea8
commit 9d3c77ec62
5 changed files with 168 additions and 109 deletions

@ -19,7 +19,8 @@ subject to the following restrictions:
btTriangleMesh::btTriangleMesh (bool use32bitIndices,bool use4componentVertices)
:m_use32bitIndices(use32bitIndices),
m_use4componentVertices(use4componentVertices)
m_use4componentVertices(use4componentVertices),
m_weldingThreshold(0.0)
{
btIndexedMesh meshIndex;
meshIndex.m_numTriangles = 0;
@ -60,49 +61,66 @@ m_use4componentVertices(use4componentVertices)
}
void btTriangleMesh::addIndex(int index)
{
if (m_use32bitIndices)
{
m_32bitIndices.push_back(index);
m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_32bitIndices[0];
} else
{
m_16bitIndices.push_back(index);
m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_16bitIndices[0];
}
}
int btTriangleMesh::findOrAddVertex(const btVector3& vertex)
{
//return index of new/existing vertex
//todo: could use acceleration structure for this
if (m_use4componentVertices)
{
for (int i=0;i< m_4componentVertices.size();i++)
{
if ((m_4componentVertices[i]-vertex).length2() <= m_weldingThreshold)
{
return i;
}
}
m_indexedMeshes[0].m_numVertices++;
m_4componentVertices.push_back(vertex);
m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_4componentVertices[0];
return m_4componentVertices.size()-1;
} else
{
for (int i=0;i< m_3componentVertices.size();i+=3)
{
btVector3 vtx(m_3componentVertices[i],m_3componentVertices[i+1],m_3componentVertices[i+2]);
if ((vtx-vertex).length2() <= m_weldingThreshold)
{
return i/3;
}
}
m_3componentVertices.push_back(vertex.getX());
m_3componentVertices.push_back(vertex.getY());
m_3componentVertices.push_back(vertex.getZ());
m_indexedMeshes[0].m_numVertices++;
m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_3componentVertices[0];
return (m_3componentVertices.size()/3)-1;
}
}
void btTriangleMesh::addTriangle(const btVector3& vertex0,const btVector3& vertex1,const btVector3& vertex2)
{
m_indexedMeshes[0].m_numTriangles++;
m_indexedMeshes[0].m_numVertices+=3;
if (m_use4componentVertices)
{
m_4componentVertices.push_back(vertex0);
m_4componentVertices.push_back(vertex1);
m_4componentVertices.push_back(vertex2);
m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_4componentVertices[0];
} else
{
m_3componentVertices.push_back(vertex0.getX());
m_3componentVertices.push_back(vertex0.getY());
m_3componentVertices.push_back(vertex0.getZ());
m_3componentVertices.push_back(vertex1.getX());
m_3componentVertices.push_back(vertex1.getY());
m_3componentVertices.push_back(vertex1.getZ());
m_3componentVertices.push_back(vertex2.getX());
m_3componentVertices.push_back(vertex2.getY());
m_3componentVertices.push_back(vertex2.getZ());
m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_3componentVertices[0];
}
if (m_use32bitIndices)
{
int curIndex = m_32bitIndices.size();
m_32bitIndices.push_back(curIndex++);
m_32bitIndices.push_back(curIndex++);
m_32bitIndices.push_back(curIndex++);
m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_32bitIndices[0];
} else
{
short curIndex = static_cast<short>(m_16bitIndices.size());
m_16bitIndices.push_back(curIndex++);
m_16bitIndices.push_back(curIndex++);
m_16bitIndices.push_back(curIndex++);
m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_16bitIndices[0];
}
addIndex(findOrAddVertex(vertex0));
addIndex(findOrAddVertex(vertex1));
addIndex(findOrAddVertex(vertex2));
}
int btTriangleMesh::getNumTriangles() const

@ -25,6 +25,7 @@ subject to the following restrictions:
///It allows either 32bit or 16bit indices, and 4 (x-y-z-w) or 3 (x-y-z) component vertices.
///If you want to share triangle/index data between graphics mesh and collision mesh (btBvhTriangleMeshShape), you can directly use btTriangleIndexVertexArray or derive your own class from btStridingMeshInterface.
///Performance of btTriangleMesh and btTriangleIndexVertexArray used in a btBvhTriangleMeshShape is the same.
///It has a brute-force option to weld together closeby vertices.
class btTriangleMesh : public btTriangleIndexVertexArray
{
btAlignedObjectArray<btVector3> m_4componentVertices;
@ -34,11 +35,16 @@ class btTriangleMesh : public btTriangleIndexVertexArray
btAlignedObjectArray<unsigned short int> m_16bitIndices;
bool m_use32bitIndices;
bool m_use4componentVertices;
public:
btScalar m_weldingThreshold;
btTriangleMesh (bool use32bitIndices=true,bool use4componentVertices=true);
int findOrAddVertex(const btVector3& vertex);
void addIndex(int index);
bool getUse32bitIndices() const
{
return m_use32bitIndices;

@ -881,6 +881,9 @@ void KX_ConvertBulletObject( class KX_GameObject* gameobj,
{
shapeInfo->SetMesh(meshobj, false,false);
}
if (objprop->m_softbody)
shapeInfo->setVertexWeldingThreshold(0.01f); //todo: expose this to the UI
bm = shapeInfo->CreateBulletShape();
//no moving concave meshes, so don't bother calculating inertia
//bm->calculateLocalInertia(ci.m_mass,ci.m_localInertiaTensor);

@ -160,9 +160,9 @@ void CcdPhysicsController::CreateRigidbody()
//disable soft body until first sneak preview is ready
if (m_cci.m_bSoft && m_cci.m_collisionShape &&
(shapeType == CONVEX_HULL_SHAPE_PROXYTYPE))
//(shapeType == TRIANGLE_MESH_SHAPE_PROXYTYPE) |
//(shapeType == SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE)))
(shapeType == CONVEX_HULL_SHAPE_PROXYTYPE)|
(shapeType == TRIANGLE_MESH_SHAPE_PROXYTYPE) |
(shapeType == SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE))
{
btRigidBody::btRigidBodyConstructionInfo rbci(m_cci.m_mass,m_bulletMotionState,m_collisionShape,m_cci.m_localInertiaTensor * m_cci.m_inertiaFactor);
rbci.m_linearDamping = m_cci.m_linearDamping;
@ -221,57 +221,7 @@ void CcdPhysicsController::CreateRigidbody()
psb->appendFace(idx[0],idx[1],idx[2]);
}
///create a mapping between graphics mesh vertices and soft body vertices
{
RAS_MeshObject* rasMesh= GetShapeInfo()->GetMesh();
if (rasMesh && !m_softbodyMappingDone)
{
//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 = 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);
@ -285,7 +235,9 @@ void CcdPhysicsController::CreateRigidbody()
} else
{
/*
btSoftBodyWorldInfo& sbi= softDynaWorld->getWorldInfo();
if (m_cci.m_collisionShape->getShapeType() ==SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE)
{
btScaledBvhTriangleMeshShape* scaledtrimeshshape = (btScaledBvhTriangleMeshShape*) m_cci.m_collisionShape;
@ -328,16 +280,15 @@ void CcdPhysicsController::CreateRigidbody()
//psb = btSoftBodyHelpers::CreateFromTriMesh(sbi,&pts[0].getX(),triangles,numtriangles);
}
*/
}
m_softbodyMappingDone = true;
m_object = psb;
//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::SDF_RS + btSoftBody::fCollision::VF_SS;//CL_SS;
//psb->m_cfg.collisions = btSoftBody::fCollision::CL_SS + btSoftBody::fCollision::CL_RS;
//btSoftBody::Material* pm=psb->appendMaterial();
@ -352,10 +303,10 @@ void CcdPhysicsController::CreateRigidbody()
//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;
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 & 2)//OB_SB_GOAL)
{
psb->setPose(false,true);//
@ -365,7 +316,7 @@ void CcdPhysicsController::CreateRigidbody()
}
psb->m_cfg.kDF = 0.5;
psb->m_cfg.kMT = 0.05;
//psb->m_cfg.kMT = 0.05;
psb->m_cfg.piterations = 5;
psb->m_cfg.piterations = 5;
@ -392,8 +343,72 @@ void CcdPhysicsController::CreateRigidbody()
//psb->m_materials[0]->m_kLST = 0.1+(i/(btScalar)(n-1))*0.9;
psb->setTotalMass(m_cci.m_mass);
psb->generateClusters(8);//(64);
psb->generateClusters(64);
psb->setCollisionFlags(0);
///create a mapping between graphics mesh vertices and soft body vertices
{
RAS_MeshObject* rasMesh= GetShapeInfo()->GetMesh();
if (rasMesh && !m_softbodyMappingDone)
{
//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 = 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);
}
}
}
}
}
}
}
m_softbodyMappingDone = true;
// m_object->setCollisionShape(rbci.m_collisionShape);
btTransform startTrans;
@ -1391,6 +1406,8 @@ btCollisionShape* CcdShapeConstructionInfo::CreateBulletShape()
if (m_useGimpact)
{
collisionMeshData = new btTriangleMesh();
// m_vertexArray is necessarily a multiple of 3
for (std::vector<btPoint3>::iterator it=m_vertexArray.begin(); it != m_vertexArray.end(); )
{
@ -1405,7 +1422,9 @@ btCollisionShape* CcdShapeConstructionInfo::CreateBulletShape()
{
if (!m_unscaledShape)
{
collisionMeshData = new btTriangleMesh();
collisionMeshData = new btTriangleMesh(true,false);
collisionMeshData->m_weldingThreshold = m_weldingThreshold;
// m_vertexArray is necessarily a multiple of 3
for (std::vector<btPoint3>::iterator it=m_vertexArray.begin(); it != m_vertexArray.end(); )
{

@ -43,6 +43,8 @@ class btCollisionShape;
class CcdShapeConstructionInfo
{
public:
static CcdShapeConstructionInfo* FindMesh(RAS_MeshObject* mesh, bool polytope);
CcdShapeConstructionInfo() :
@ -54,7 +56,8 @@ public:
m_refCount(1),
m_meshObject(NULL),
m_unscaledShape(NULL),
m_useGimpact(false)
m_useGimpact(false),
m_weldingThreshold(0.f)
{
m_childTrans.setIdentity();
}
@ -111,6 +114,14 @@ public:
// original mesh that correspond to shape triangles.
// only set for concave mesh shape.
void setVertexWeldingThreshold(float threshold)
{
m_weldingThreshold = threshold;
}
float getVertexWeldingThreshold() const
{
return m_weldingThreshold;
}
protected:
static std::map<RAS_MeshObject*, CcdShapeConstructionInfo*> m_meshShapeMap;
int m_refCount; // this class is shared between replicas
@ -119,7 +130,9 @@ protected:
btBvhTriangleMeshShape* m_unscaledShape;// holds the shared unscale BVH mesh shape,
// the actual shape is of type btScaledBvhTriangleMeshShape
std::vector<CcdShapeConstructionInfo*> m_shapeArray; // for compound shapes
bool m_useGimpact;
bool m_useGimpact; //use gimpact for concave dynamic/moving collision detection
float m_weldingThreshold; //welding closeby vertices together can improve softbody stability etc.
};
struct CcdConstructionInfo