diff --git a/Demos/BspDemo/BspConverter.cpp b/Demos/BspDemo/BspConverter.cpp index e3708fd50..aa3530e25 100644 --- a/Demos/BspDemo/BspConverter.cpp +++ b/Demos/BspDemo/BspConverter.cpp @@ -16,6 +16,7 @@ subject to the following restrictions: #include "BspConverter.h" #include "BspLoader.h" #include "LinearMath/btVector3.h" +#include "LinearMath/btGeometryUtil.h" void BspConverter::convertBsp(BspLoader& bspLoader,float scaling) { @@ -83,10 +84,8 @@ void BspConverter::convertBsp(BspLoader& bspLoader,float scaling) { std::vector vertices; - - getVerticesFromPlaneEquations(planeEquations,vertices); - printf("getVerticesFromPlaneEquations returned %i\n",(int)vertices.size()); - + btGeometryUtil::getVerticesFromPlaneEquations(planeEquations,vertices); + bool isEntity = false; btVector3 entityTarget(0.f,0.f,0.f); addConvexVerticesCollider(vertices,isEntity,entityTarget); @@ -160,7 +159,7 @@ void BspConverter::convertBsp(BspLoader& bspLoader,float scaling) { std::vector vertices; - getVerticesFromPlaneEquations(planeEquations,vertices); + btGeometryUtil::getVerticesFromPlaneEquations(planeEquations,vertices); bool isEntity=true; addConvexVerticesCollider(vertices,isEntity,targetLocation); @@ -196,78 +195,3 @@ void BspConverter::convertBsp(BspLoader& bspLoader,float scaling) -void BspConverter::getVerticesFromPlaneEquations(const std::vector& planeEquations , std::vector& verticesOut ) -{ - const int numbrushes = planeEquations.size(); - // brute force: - for (int i=0;i 0.0001f ) && - ( n3n1.length2() > 0.0001f ) && - ( n1n2.length2() > 0.0001f ) ) - { - //point P out of 3 plane equations: - - // d1 ( N2 * N3 ) + d2 ( N3 * N1 ) + d3 ( N1 * N2 ) - //P = ------------------------------------------------------------------------- - // N1 . ( N2 * N3 ) - - - float quotient = (N1.dot(n2n3)); - if (btFabs(quotient) > 0.000001f) - { - quotient = -1.f / quotient; - n2n3 *= N1[3]; - n3n1 *= N2[3]; - n1n2 *= N3[3]; - btVector3 potentialVertex = n2n3; - potentialVertex += n3n1; - potentialVertex += n1n2; - potentialVertex *= quotient; - - //check if inside, and replace supportingVertexOut if needed - if (isInside(planeEquations,potentialVertex,0.1f)) - { - verticesOut.push_back(potentialVertex); - } - } - } - } - } - } -} - - - - -bool BspConverter::isInside(const std::vector& planeEquations, const btVector3& point, float margin) -{ - int numbrushes = planeEquations.size(); - for (int i=0;i0.f) - { - return false; - } - } - return true; - -} diff --git a/Demos/BspDemo/BspConverter.h b/Demos/BspDemo/BspConverter.h index 9616ada1a..e841c1f2d 100644 --- a/Demos/BspDemo/BspConverter.h +++ b/Demos/BspDemo/BspConverter.h @@ -29,10 +29,6 @@ class BspConverter virtual ~BspConverter() { } - ///Utility function to create vertices from a Quake Brush. Brute force but it works. - ///Bit overkill to use QHull package - void getVerticesFromPlaneEquations(const std::vector& planeEquations , std::vector& verticesOut ); - bool isInside(const std::vector& planeEquations, const btVector3& point, float margin); ///this callback is called for each brush that succesfully converted into vertices virtual void addConvexVerticesCollider(std::vector& vertices, bool isEntity, const btVector3& entityTargetLocation) = 0; diff --git a/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp b/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp index 16acde51b..6baf7233d 100644 --- a/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp +++ b/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp @@ -57,6 +57,7 @@ bool createConstraint = true;//false; bool useCompound = false;//true;//false; + #ifdef _DEBUG const int gNumObjects = 120; #else diff --git a/Demos/ConvexDecompositionDemo/ConvexDecompositionDemo.cpp b/Demos/ConvexDecompositionDemo/ConvexDecompositionDemo.cpp index da1296fe4..7989a40cd 100644 --- a/Demos/ConvexDecompositionDemo/ConvexDecompositionDemo.cpp +++ b/Demos/ConvexDecompositionDemo/ConvexDecompositionDemo.cpp @@ -20,10 +20,13 @@ subject to the following restrictions: #include "LinearMath/btQuickprof.h" #include "LinearMath/btIDebugDraw.h" +#include "LinearMath/btGeometryUtil.h" + #include "GLDebugDrawer.h" #include "BMF_Api.h" #include //printf debugging +#include float deltaTime = 1.f/60.f; @@ -89,7 +92,7 @@ void ConvexDecompositionDemo::initPhysics(const char* filename) btTransform startTransform; startTransform.setIdentity(); - startTransform.setOrigin(btVector3(0,-4,0)); + startTransform.setOrigin(btVector3(0,-4.5,0)); localCreateRigidBody(0.f,startTransform,new btBoxShape(btVector3(30,2,30))); @@ -132,28 +135,34 @@ void ConvexDecompositionDemo::initPhysics(const char* filename) //calc centroid, to shift vertices around center of mass centroid.setValue(0,0,0); + std::vector vertices; if ( 1 ) { const unsigned int *src = result.mHullIndices; - for (unsigned int i=0; iaddTriangle(vertex0,vertex1,vertex2); index0+=mBaseCount; @@ -187,7 +197,38 @@ void ConvexDecompositionDemo::initPhysics(const char* filename) } float mass = 1.f; + float collisionMargin = 0.01f; + +//this is a tools issue: due to collision margin, convex objects overlap, compensate for it here: +//#define SHRINK_OBJECT_INWARDS 1 +#ifdef SHRINK_OBJECT_INWARDS + + + std::vector planeEquations; + btGeometryUtil::getPlaneEquationsFromVertices(vertices,planeEquations); + + std::vector shiftedPlaneEquations; + for (int p=0;p shiftedVertices; + btGeometryUtil::getVerticesFromPlaneEquations(shiftedPlaneEquations,shiftedVertices); + + + btCollisionShape* convexShape = new btConvexHullShape(&(shiftedVertices[0].getX()),shiftedVertices.size()); + +#else //SHRINK_OBJECT_INWARDS + + //btCollisionShape* convexShape = new btConvexHullShape(&(vertices[0].getX()),vertices.size()); btCollisionShape* convexShape = new btConvexTriangleMeshShape(trimesh); +#endif + + convexShape->setMargin(0.01); + + btTransform trans; trans.setIdentity(); trans.setOrigin(centroid); @@ -251,11 +292,11 @@ void ConvexDecompositionDemo::initPhysics(const char* filename) strcat(outputFileName,"_convex.obj"); FILE* outputFile = fopen(outputFileName,"wb"); - unsigned int depth = 7; + unsigned int depth = 5; float cpercent = 5; float ppercent = 15; unsigned int maxv = 16; - float skinWidth = 0.01; + float skinWidth = 0.0; printf("WavefrontObj num triangles read %i",tcount); ConvexDecomposition::DecompDesc desc; diff --git a/Demos/EPAPenDepthDemo/PenetrationTestBullet.cpp b/Demos/EPAPenDepthDemo/PenetrationTestBullet.cpp index 3085ff5d3..61dce272a 100644 --- a/Demos/EPAPenDepthDemo/PenetrationTestBullet.cpp +++ b/Demos/EPAPenDepthDemo/PenetrationTestBullet.cpp @@ -1,14 +1,37 @@ -#include -#include -#include "btDiscreteCollisionDetectorInterface.h" -#include "btSimplexSolverInterface.h" -#include "btConvexHullShape.h" -#include "Solid3EpaPenetrationDepth.h" -#include "Solid3JohnsonSimplexSolver.h" -#include "btGjkPairDetector.h" -#include "btMinkowskiPenetrationDepthSolver.h" -#include "EpaPenetrationDepthSolver.h" +///contribution by Pierre Terdiman to check penetration depth solvers +///see http://www.continuousphysics.com/Bullet/phpBB2/viewtopic.php?t=638 + +#ifdef WIN32//for glut.h +#include +#endif + + +//think different +#if defined(__APPLE__) && !defined (VMDMESA) +#include +#include +#include +#else +#include +#endif + +#include +#include +#include + + +#include "btBulletCollisionCommon.h" + +#include "BulletCollision/NarrowPhaseCollision/btDiscreteCollisionDetectorInterface.h" +#include "BulletCollision/NarrowPhaseCollision/btSimplexSolverInterface.h" +#include "BulletCollision/NarrowPhaseCollision/btMinkowskiPenetrationDepthSolver.h" +#include "BulletCollision/NarrowPhaseCollision/btGjkPairDetector.h" +#include "../Extras/ExtraSolid35/Solid3EpaPenetrationDepth.h" +#include "../Extras/ExtraSolid35/Solid3JohnsonSimplexSolver.h" +#include "../Extras/EPA/EpaPenetrationDepthSolver.h" + + static bool gRefMode = false; static int gMethod = 0; @@ -211,7 +234,8 @@ static float gDepth; static bool TestEPA(const MyConvex& hull0, const MyConvex& hull1) { static btSimplexSolverInterface simplexSolver; -// static Solid3JohnsonSimplexSolver simplexSolver; + //static Solid3JohnsonSimplexSolver simplexSolver; + simplexSolver.reset(); btConvexHullShape convexA((float*)hull0.mVerts, hull0.mNbVerts, sizeof(btVector3)); @@ -222,14 +246,17 @@ static bool TestEPA(const MyConvex& hull0, const MyConvex& hull1) static btMinkowskiPenetrationDepthSolver Solver2; btConvexPenetrationDepthSolver* Solver; - if(gMethod==0) Solver = &Solver0; - else if(gMethod==1) Solver = &Solver1; - else Solver = &Solver2; + if(gMethod==0) + Solver = &Solver0; + else if(gMethod==1) + Solver = &Solver1; + else + Solver = &Solver2; btGjkPairDetector GJK(&convexA, &convexB, &simplexSolver, Solver); - GJK.setIgnoreMargin(true); - convexA.setMargin(0.0f); - convexB.setMargin(0.0f); + //GJK.setIgnoreMargin(true); + convexA.setMargin(0.01f); + convexB.setMargin(0.01f); btDiscreteCollisionDetectorInterface::ClosestPointInput input; input.m_transformA = hull0.mTransform; @@ -594,20 +621,20 @@ int main(int argc, char** argv) glEnable(GL_LIGHT0); // - bool Status = gConvex0.LoadFromFile("c:\\convex0.bin"); + bool Status = gConvex0.LoadFromFile("convex0.bin"); if(!Status) { - Status = gConvex0.LoadFromFile("convex0.bin"); + Status = gConvex0.LoadFromFile("../../convex0.bin"); if(!Status) { printf("Failed to load object!\n"); exit(0); } } - Status = gConvex1.LoadFromFile("c:\\convex0.bin"); + Status = gConvex1.LoadFromFile("convex0.bin"); if(!Status) { - Status = gConvex1.LoadFromFile("convex0.bin"); + Status = gConvex1.LoadFromFile("../../convex0.bin"); if(!Status) { printf("Failed to load object!\n"); diff --git a/Extras/EPA/Epa.cpp b/Extras/EPA/Epa.cpp index 44260f769..80baaec6f 100644 --- a/Extras/EPA/Epa.cpp +++ b/Extras/EPA/Epa.cpp @@ -29,13 +29,13 @@ subject to the following restrictions: #include "BulletCollision/NarrowPhaseCollision/btSimplexSolverInterface.h" -#include "NarrowPhaseCollision/EpaCommon.h" +#include "EpaCommon.h" -#include "NarrowPhaseCollision/EpaVertex.h" -#include "NarrowPhaseCollision/EpaHalfEdge.h" -#include "NarrowPhaseCollision/EpaFace.h" -#include "NarrowPhaseCollision/EpaPolyhedron.h" -#include "NarrowPhaseCollision/Epa.h" +#include "EpaVertex.h" +#include "EpaHalfEdge.h" +#include "EpaFace.h" +#include "EpaPolyhedron.h" +#include "Epa.h" const btScalar EPA_MAX_RELATIVE_ERROR = 1e-2f; const btScalar EPA_MAX_RELATIVE_ERROR_SQRD = EPA_MAX_RELATIVE_ERROR * EPA_MAX_RELATIVE_ERROR; diff --git a/Extras/EPA/EpaFace.cpp b/Extras/EPA/EpaFace.cpp index 425009ba9..c74ae1f17 100644 --- a/Extras/EPA/EpaFace.cpp +++ b/Extras/EPA/EpaFace.cpp @@ -18,11 +18,11 @@ subject to the following restrictions: #include "LinearMath/btVector3.h" #include "LinearMath/btPoint3.h" -#include "NarrowPhaseCollision/EpaCommon.h" +#include "EpaCommon.h" -#include "NarrowPhaseCollision/EpaVertex.h" -#include "NarrowPhaseCollision/EpaHalfEdge.h" -#include "NarrowPhaseCollision/EpaFace.h" +#include "EpaVertex.h" +#include "EpaHalfEdge.h" +#include "EpaFace.h" #ifdef EPA_POLYHEDRON_USE_PLANES btScalar PLANE_THICKNESS = 1e-5f; diff --git a/Extras/EPA/EpaPenetrationDepthSolver.cpp b/Extras/EPA/EpaPenetrationDepthSolver.cpp index bfddf762e..1d506d1b0 100644 --- a/Extras/EPA/EpaPenetrationDepthSolver.cpp +++ b/Extras/EPA/EpaPenetrationDepthSolver.cpp @@ -26,15 +26,15 @@ subject to the following restrictions: #include "BulletCollision/NarrowPhaseCollision/btSimplexSolverInterface.h" -#include "NarrowPhaseCollision/EpaCommon.h" +#include "EpaCommon.h" -#include "NarrowPhaseCollision/EpaVertex.h" -#include "NarrowPhaseCollision/EpaHalfEdge.h" -#include "NarrowPhaseCollision/EpaFace.h" -#include "NarrowPhaseCollision/EpaPolyhedron.h" -#include "NarrowPhaseCollision/Epa.h" +#include "EpaVertex.h" +#include "EpaHalfEdge.h" +#include "EpaFace.h" +#include "EpaPolyhedron.h" +#include "Epa.h" #include "BulletCollision/NarrowPhaseCollision/btConvexPenetrationDepthSolver.h" -#include "NarrowPhaseCollision/EpaPenetrationDepthSolver.h" +#include "EpaPenetrationDepthSolver.h" btScalar g_GJKMaxRelError = 1e-3f; btScalar g_GJKMaxRelErrorSqrd = g_GJKMaxRelError * g_GJKMaxRelError; diff --git a/Extras/EPA/EpaPolyhedron.cpp b/Extras/EPA/EpaPolyhedron.cpp index 6aeee94c1..9ae728c88 100644 --- a/Extras/EPA/EpaPolyhedron.cpp +++ b/Extras/EPA/EpaPolyhedron.cpp @@ -17,7 +17,7 @@ subject to the following restrictions: #include "LinearMath/btScalar.h" #include "LinearMath/btVector3.h" #include "LinearMath/btPoint3.h" -#include "Memory2.h" +//#include "Memory2.h" #include #ifdef _DEBUG @@ -25,12 +25,12 @@ subject to the following restrictions: #endif -#include "NarrowPhaseCollision/EpaCommon.h" +#include "EpaCommon.h" -#include "NarrowPhaseCollision/EpaVertex.h" -#include "NarrowPhaseCollision/EpaHalfEdge.h" -#include "NarrowPhaseCollision/EpaFace.h" -#include "NarrowPhaseCollision/EpaPolyhedron.h" +#include "EpaVertex.h" +#include "EpaHalfEdge.h" +#include "EpaFace.h" +#include "EpaPolyhedron.h" EpaPolyhedron::EpaPolyhedron() : m_nbFaces( 0 ) diff --git a/convex0.bin b/convex0.bin new file mode 100644 index 000000000..83493fca3 Binary files /dev/null and b/convex0.bin differ diff --git a/src/BulletCollision/CollisionShapes/btTriangleMesh.h b/src/BulletCollision/CollisionShapes/btTriangleMesh.h index 690d1e849..171dcf33b 100644 --- a/src/BulletCollision/CollisionShapes/btTriangleMesh.h +++ b/src/BulletCollision/CollisionShapes/btTriangleMesh.h @@ -46,6 +46,15 @@ class btTriangleMesh : public btStridingMeshInterface m_triangles.push_back(tri); } + int getNumTriangles() const + { + return m_triangles.size(); + } + + const btMyTriangle& getTriangle(int index) const + { + return m_triangles[index]; + } //StridingMeshInterface interface implementation