support concave soft bodies, preliminary. could be used for cloth too. need vertex...
authorErwin Coumans <blender@erwincoumans.com>
Fri, 26 Sep 2008 06:25:35 +0000 (06:25 +0000)
committerErwin Coumans <blender@erwincoumans.com>
Fri, 26 Sep 2008 06:25:35 +0000 (06:25 +0000)
extern/bullet2/src/BulletCollision/CollisionShapes/btTriangleMesh.cpp
extern/bullet2/src/BulletCollision/CollisionShapes/btTriangleMesh.h
source/gameengine/Ketsji/KX_ConvertPhysicsObjects.cpp
source/gameengine/Physics/Bullet/CcdPhysicsController.cpp
source/gameengine/Physics/Bullet/CcdPhysicsController.h

index bcea97052d616e27c0dc9cabf382c013c048a55c..29d263163167e684256222665011948abc2f3723 100644 (file)
@@ -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::addTriangle(const btVector3& vertex0,const btVector3& vertex1,const btVector3& vertex2)
+void   btTriangleMesh::addIndex(int index)
 {
-       m_indexedMeshes[0].m_numTriangles++;
-       m_indexedMeshes[0].m_numVertices+=3;
-
-       if (m_use4componentVertices)
+       if (m_use32bitIndices)
        {
-               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];
+               m_32bitIndices.push_back(index);
+               m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_32bitIndices[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];
+               m_16bitIndices.push_back(index);
+               m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_16bitIndices[0];
        }
+}
 
-       if (m_use32bitIndices)
+int    btTriangleMesh::findOrAddVertex(const btVector3& vertex)
+{
+       //return index of new/existing vertex
+       //todo: could use acceleration structure for this
+       if (m_use4componentVertices)
        {
-               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];
+               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
        {
-               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];
+               
+               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++;
+               
+       addIndex(findOrAddVertex(vertex0));
+       addIndex(findOrAddVertex(vertex1));
+       addIndex(findOrAddVertex(vertex2));
 }
 
 int btTriangleMesh::getNumTriangles() const
index e4d41d5ede061faad1175150c8a970ba3ebad5f2..1f51b2f2c8730cef821526c890f509b62f348e2d 100644 (file)
@@ -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;
index ddf1f4024319496c6b394d6a8fe9d8f8433b94f5..c5dbabe24fc6d684bf1f31ddfaf7a2fe56e11115 100644 (file)
@@ -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);
index d552cb3dc9eeb6d2d5b1673a211fa9f8c73d50dd..f1e30b4a1e3093ea1aafdb334e39399d714458dc 100644 (file)
@@ -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(); )
                                {
index 5d35482dd461fc57cdf25cd33a0a0274f1efa77c..60c734838f36e164e0117c9e0941e90b57a906a7 100644 (file)
@@ -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