From 682a0a1b908a74b616597432b65c8aa936e0fbfa Mon Sep 17 00:00:00 2001 From: "erwin.coumans" Date: Fri, 26 Sep 2008 21:59:03 +0000 Subject: [PATCH] fixed btSoftBodyConcaveCollisionAlgorithm, wrong bounding box transformation added vertex welding option for btTriangleMesh (brute-force slow) reject appendFace for some degenerate triangles (all 3 vertices/nodes need to be different) add setVelocity method for btSoftBody --- .../CollisionShapes/btTriangleMesh.cpp | 98 +++++++++++-------- .../CollisionShapes/btTriangleMesh.h | 8 +- src/BulletSoftBody/btSoftBody.cpp | 21 ++++ src/BulletSoftBody/btSoftBody.h | 4 + .../btSoftBodyConcaveCollisionAlgorithm.cpp | 40 ++------ 5 files changed, 99 insertions(+), 72 deletions(-) diff --git a/src/BulletCollision/CollisionShapes/btTriangleMesh.cpp b/src/BulletCollision/CollisionShapes/btTriangleMesh.cpp index bcea97052..29d263163 100644 --- a/src/BulletCollision/CollisionShapes/btTriangleMesh.cpp +++ b/src/BulletCollision/CollisionShapes/btTriangleMesh.cpp @@ -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(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 diff --git a/src/BulletCollision/CollisionShapes/btTriangleMesh.h b/src/BulletCollision/CollisionShapes/btTriangleMesh.h index e4d41d5ed..1f51b2f2c 100644 --- a/src/BulletCollision/CollisionShapes/btTriangleMesh.h +++ b/src/BulletCollision/CollisionShapes/btTriangleMesh.h @@ -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 m_4componentVertices; @@ -34,11 +35,16 @@ class btTriangleMesh : public btTriangleIndexVertexArray btAlignedObjectArray 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; diff --git a/src/BulletSoftBody/btSoftBody.cpp b/src/BulletSoftBody/btSoftBody.cpp index 4620001eb..401768e0a 100644 --- a/src/BulletSoftBody/btSoftBody.cpp +++ b/src/BulletSoftBody/btSoftBody.cpp @@ -284,6 +284,13 @@ m_faces.push_back(f); // void btSoftBody::appendFace(int node0,int node1,int node2,Material* mat) { + if (node0==node1) + return; + if (node1==node2) + return; + if (node2==node0) + return; + appendFace(-1,mat); Face& f=m_faces[m_faces.size()-1]; btAssert(node0!=node1); @@ -384,6 +391,20 @@ void btSoftBody::addVelocity(const btVector3& velocity) for(int i=0,ni=m_nodes.size();i0) + { + n.m_v = velocity; + } + } +} + + // void btSoftBody::addVelocity(const btVector3& velocity,int node) { diff --git a/src/BulletSoftBody/btSoftBody.h b/src/BulletSoftBody/btSoftBody.h index 1a10779ad..538383157 100644 --- a/src/BulletSoftBody/btSoftBody.h +++ b/src/BulletSoftBody/btSoftBody.h @@ -677,6 +677,10 @@ public: int node); /* Add velocity to the entire body */ void addVelocity( const btVector3& velocity); + + /* Set velocity for the entire body */ + void setVelocity( const btVector3& velocity); + /* Add velocity to a node of the body */ void addVelocity( const btVector3& velocity, int node); diff --git a/src/BulletSoftBody/btSoftBodyConcaveCollisionAlgorithm.cpp b/src/BulletSoftBody/btSoftBodyConcaveCollisionAlgorithm.cpp index 056befced..dd1f08fd5 100644 --- a/src/BulletSoftBody/btSoftBodyConcaveCollisionAlgorithm.cpp +++ b/src/BulletSoftBody/btSoftBodyConcaveCollisionAlgorithm.cpp @@ -32,7 +32,7 @@ subject to the following restrictions: #include "BulletCollision/NarrowPhaseCollision/btSubSimplexConvexCast.h" #include "BulletSoftBody/btSoftBody.h" -#define BT_SOFTBODY_TRIANGLE_EXTRUSION btScalar(0.3) +#define BT_SOFTBODY_TRIANGLE_EXTRUSION btScalar(0.06)//make this configurable btSoftBodyConcaveCollisionAlgorithm::btSoftBodyConcaveCollisionAlgorithm( const btCollisionAlgorithmConstructionInfo& ci, btCollisionObject* body0,btCollisionObject* body1,bool isSwapped) : btCollisionAlgorithm(ci), @@ -210,44 +210,22 @@ btCollisionObject* ob = static_cast(m_triBody); void btSoftBodyTriangleCallback::setTimeStepAndCounters(btScalar collisionMarginTriangle,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut) { m_dispatchInfoPtr = &dispatchInfo; - m_collisionMarginTriangle = collisionMarginTriangle; + m_collisionMarginTriangle = collisionMarginTriangle+btScalar(BT_SOFTBODY_TRIANGLE_EXTRUSION); m_resultOut = resultOut; - //recalc aabbs -// btTransform softbodyInTriangleSpace; -// softbodyInTriangleSpace = m_triBody->getWorldTransform().inverse() * m_softBody->getWorldTransform(); -// btCollisionShape* convexShape = static_cast(m_convexBody->getCollisionShape()); - //CollisionShape* triangleShape = static_cast(triBody->m_collisionShape); + btVector3 aabbWorldSpaceMin,aabbWorldSpaceMax; m_softBody->getAabb(aabbWorldSpaceMin,aabbWorldSpaceMax); - btVector3 halfExtents = (aabbWorldSpaceMax-aabbWorldSpaceMin)*btScalar(0.5); btVector3 softBodyCenter = (aabbWorldSpaceMax+aabbWorldSpaceMin)*btScalar(0.5); - btTransform triInverse = m_triBody->getWorldTransform().inverse(); - - btMatrix3x3 abs_b = triInverse.getBasis().absolute(); - btPoint3 center = softBodyCenter + triInverse.getOrigin(); - - btVector3 extent = btVector3(abs_b[0].dot(halfExtents), - abs_b[1].dot(halfExtents), - abs_b[2].dot(halfExtents)); -// extent += btVector3(getMargin(),getMargin(),getMargin()); - - m_aabbMin = center - extent; - m_aabbMax = center + extent; - - btScalar extraMargin = collisionMarginTriangle+btScalar(BT_SOFTBODY_TRIANGLE_EXTRUSION); - btVector3 extra(extraMargin,extraMargin,extraMargin); - - m_aabbMax += extra; - m_aabbMin -= extra; - -/* btVector3 extra(2,2,2); - m_aabbMin = aabbWorldSpaceMin-extra; - m_aabbMax = aabbWorldSpaceMax+extra; -*/ + btTransform softTransform; + softTransform.setIdentity(); + softTransform.setOrigin(softBodyCenter); + btTransform convexInTriangleSpace; + convexInTriangleSpace = m_triBody->getWorldTransform().inverse() * softTransform; + btTransformAabb(halfExtents,m_collisionMarginTriangle,convexInTriangleSpace,m_aabbMin,m_aabbMax); } void btSoftBodyConcaveCollisionAlgorithm::clearCache()