From 71fb40b7751c9b506b91f1bf71e31a3d00b4c447 Mon Sep 17 00:00:00 2001 From: ejcoumans Date: Thu, 6 Dec 2007 23:58:50 +0000 Subject: [PATCH] some demo cleanup, part 0 --- Demos/BasicDemo/BasicDemo.cpp | 133 +++------------- .../ConcaveConvexcastDemo.cpp | 150 +----------------- .../ConcaveRaycastDemo/ConcaveRaycastDemo.cpp | 32 +--- Demos/DynamicControlDemo/MotorDemo.cpp | 5 +- .../Dynamics/btDiscreteDynamicsWorld.h | 2 +- src/BulletDynamics/Dynamics/btDynamicsWorld.h | 2 + 6 files changed, 39 insertions(+), 285 deletions(-) diff --git a/Demos/BasicDemo/BasicDemo.cpp b/Demos/BasicDemo/BasicDemo.cpp index 9faa6b1c8..7b7c10074 100644 --- a/Demos/BasicDemo/BasicDemo.cpp +++ b/Demos/BasicDemo/BasicDemo.cpp @@ -13,62 +13,24 @@ subject to the following restrictions: 3. This notice may not be removed or altered from any source distribution. */ - -#define USE_GROUND_BOX 1 -//#define PRINT_CONTACT_STATISTICS 1 -//#define USE_PARALLEL_DISPATCHER 1 - -#define START_POS_X -5 -#define START_POS_Y -5 -#define START_POS_Z -3 - - ///create 125 (5x5x5) dynamic object #define ARRAY_SIZE_X 5 #define ARRAY_SIZE_Y 5 #define ARRAY_SIZE_Z 5 +//maximum number of objects (and allow user to shoot additional boxes) +#define MAX_PROXIES (ARRAY_SIZE_X*ARRAY_SIZE_Y*ARRAY_SIZE_Z + 1024) -//#define USE_SIMPLE_DYNAMICS_WORLD 1 +#define START_POS_X -5 +#define START_POS_Y -5 +#define START_POS_Z -3 -int gNumObjects = 120; -#define HALF_EXTENTS btScalar(1.) -#include "btBulletDynamicsCommon.h" -#include "BulletCollision/BroadphaseCollision/btMultiSapBroadphase.h" -#include "LinearMath/btIDebugDraw.h" -#include "GLDebugDrawer.h" -#include //printf debugging -btScalar deltaTime = btScalar(1./60.); #include "BasicDemo.h" -#include "GL_ShapeDrawer.h" #include "GlutStuff.h" +///btBulletDynamicsCommon.h is the main Bullet include file, contains most common include files. +#include "btBulletDynamicsCommon.h" +#include //printf debugging -#ifdef USE_PARALLEL_DISPATCHER -#include "../../Extras/BulletMultiThreaded/SpuGatheringCollisionDispatcher.h" -#include "../../Extras/BulletMultiThreaded/Win32ThreadSupport.h" -#include "../../Extras/BulletMultiThreaded/SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h" -#endif//USE_PARALLEL_DISPATCHER - - -#include - -//////////////////////////////////// - - -class myTest -{ - -}; - - - - - - - -extern int gNumManifold; -extern int gOverlappingPairs; -extern int gTotalContactPoints; void BasicDemo::clientMoveAndDisplay() { @@ -77,24 +39,19 @@ void BasicDemo::clientMoveAndDisplay() //simple dynamics world doesn't handle fixed-time-stepping float ms = m_clock.getTimeMicroseconds(); m_clock.reset(); - float minFPS = 1000000.f/60.f; - if (ms > minFPS) - ms = minFPS; - + + ///step the simulation if (m_dynamicsWorld) + { m_dynamicsWorld->stepSimulation(ms / 1000000.f); + //optional but useful: debug drawing + m_dynamicsWorld->debugDrawWorld(); + } renderme(); glFlush(); - //some additional debugging info -#ifdef PRINT_CONTACT_STATISTICS - printf("num manifolds: %i\n",gNumManifold); - printf("num gOverlappingPairs: %i\n",gOverlappingPairs); - printf("num gTotalContactPoints : %i\n",gTotalContactPoints ); -#endif //PRINT_CONTACT_STATISTICS - gTotalContactPoints = 0; glutSwapBuffers(); } @@ -107,6 +64,10 @@ void BasicDemo::displayCallback(void) { renderme(); + //optional but useful: debug drawing to detect problems + if (m_dynamicsWorld) + m_dynamicsWorld->debugDrawWorld(); + glFlush(); glutSwapBuffers(); } @@ -120,73 +81,29 @@ void BasicDemo::initPhysics() setCameraDistance(btScalar(50.)); - + ///collision configuration contains default setup for memory, collision setup m_collisionConfiguration = new btDefaultCollisionConfiguration(); -#ifdef USE_PARALLEL_DISPATCHER - -#ifdef USE_WIN32_THREADING - - int maxNumOutstandingTasks = 4;//number of maximum outstanding tasks - Win32ThreadSupport* threadSupport = new Win32ThreadSupport(Win32ThreadSupport::Win32ThreadConstructionInfo( - "collision", - processCollisionTask, - createCollisionLocalStoreMemory, - maxNumOutstandingTasks)); -#else -///todo other platform threading -///Playstation 3 SPU (SPURS) version is available through PS3 Devnet -///Libspe2 SPU support will be available soon -///pthreads version -///you can hook it up to your custom task scheduler by deriving from btThreadSupportInterface -#endif - - - m_dispatcher = new SpuGatheringCollisionDispatcher(threadSupport,maxNumOutstandingTasks,collisionConfiguration); -#else + ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded) m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); -#endif //USE_PARALLEL_DISPATCHER -#define USE_SWEEP_AND_PRUNE 1 -#ifdef USE_SWEEP_AND_PRUNE -#define maxProxies 8192 + ///the maximum size of the collision world. Make sure objects stay within these boundaries + ///Don't make the world AABB size too large, it will harm simulation quality and performance btVector3 worldAabbMin(-10000,-10000,-10000); btVector3 worldAabbMax(10000,10000,10000); - m_overlappingPairCache = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies); - - - -#else - m_overlappingPairCache = new btSimpleBroadphase; -#endif //USE_SWEEP_AND_PRUNE - - + m_overlappingPairCache = new btAxisSweep3(worldAabbMin,worldAabbMax,MAX_PROXIES); + ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded) btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver; m_solver = sol; -#ifdef USE_SIMPLE_DYNAMICS_WORLD - //btSimpleDynamicsWorld doesn't support 'cache friendly' optimization, so disable this - sol->setSolverMode(btSequentialImpulseConstraintSolver::SOLVER_RANDMIZE_ORDER); - m_dynamicsWorld = new btSimpleDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_solver,m_collisionConfiguration); -#else m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_solver,m_collisionConfiguration); -#endif //USE_SIMPLE_DYNAMICS_WORLD - m_dynamicsWorld->getDispatchInfo().m_enableSPU = true; m_dynamicsWorld->setGravity(btVector3(0,-10,0)); - ///create a few basic rigid bodies - - - //static ground -#ifdef USE_GROUND_BOX btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.))); -#else - btCollisionShape* groundShape = new btSphereShape(btScalar(50.)); -#endif//USE_GROUND_BOX m_collisionShapes.push_back(groundShape); @@ -200,7 +117,7 @@ void BasicDemo::initPhysics() btCollisionShape* colShape = new btSphereShape(btScalar(1.)); m_collisionShapes.push_back(colShape); - /// Create Dynamic Objects + /// Create Dynamic Objects btTransform startTransform; startTransform.setIdentity(); diff --git a/Demos/ConcaveConvexcastDemo/ConcaveConvexcastDemo.cpp b/Demos/ConcaveConvexcastDemo/ConcaveConvexcastDemo.cpp index eca4477f4..d70110e52 100644 --- a/Demos/ConcaveConvexcastDemo/ConcaveConvexcastDemo.cpp +++ b/Demos/ConcaveConvexcastDemo/ConcaveConvexcastDemo.cpp @@ -21,15 +21,6 @@ subject to the following restrictions: #include "GL_ShapeDrawer.h" #include "GlutStuff.h" -//#define USE_PARALLEL_DISPATCHER 1 -#ifdef USE_PARALLEL_DISPATCHER -#include "../../Extras/BulletMultiThreaded/SpuGatheringCollisionDispatcher.h" -#include "../../Extras/BulletMultiThreaded/Win32ThreadSupport.h" -#include "../../Extras/BulletMultiThreaded/SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h" -#endif//USE_PARALLEL_DISPATCHER - - - static btVector3* gVertices=0; static int* gIndices=0; @@ -226,64 +217,13 @@ public: static btConvexcastBatch convexcastBatch; -///User can override this material combiner by implementing gContactAddedCallback and setting body0->m_collisionFlags |= btCollisionObject::customMaterialCallback; -inline btScalar calculateCombinedFriction(float friction0,float friction1) -{ - btScalar friction = friction0 * friction1; - - const btScalar MAX_FRICTION = 10.f; - if (friction < -MAX_FRICTION) - friction = -MAX_FRICTION; - if (friction > MAX_FRICTION) - friction = MAX_FRICTION; - return friction; - -} - -inline btScalar calculateCombinedRestitution(float restitution0,float restitution1) -{ - return restitution0 * restitution1; -} -static bool CustomMaterialCombinerCallback(btManifoldPoint& cp, const btCollisionObject* colObj0,int partId0,int index0,const btCollisionObject* colObj1,int partId1,int index1) -{ - float friction0 = colObj0->getFriction(); - float friction1 = colObj1->getFriction(); - float restitution0 = colObj0->getRestitution(); - float restitution1 = colObj1->getRestitution(); - - if (colObj0->getCollisionFlags() & btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK) - { - friction0 = 1.0;//partId0,index0 - restitution0 = 0.f; - } - if (colObj1->getCollisionFlags() & btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK) - { - if (index1&1) - { - friction1 = 1.0f;//partId1,index1 - } else - { - friction1 = 0.f; - } - restitution1 = 0.f; - } - - cp.m_combinedFriction = calculateCombinedFriction(friction0,friction1); - cp.m_combinedRestitution = calculateCombinedRestitution(restitution0,restitution1); - - //this return value is currently ignored, but to be on the safe side: return false if you don't calculate friction - return true; -} - -extern ContactAddedCallback gContactAddedCallback; - - const int NUM_VERTS_X = 30; - const int NUM_VERTS_Y = 30; - const int totalVerts = NUM_VERTS_X*NUM_VERTS_Y; +const int NUM_VERTS_X = 30; +const int NUM_VERTS_Y = 30; +const int totalVerts = NUM_VERTS_X*NUM_VERTS_Y; void ConcaveConvexcastDemo::setVertexPositions(float waveheight, float offset) { @@ -326,10 +266,7 @@ void ConcaveConvexcastDemo::initPhysics() { #define TRISIZE 10.f - gContactAddedCallback = CustomMaterialCombinerCallback; - -#define USE_TRIMESH_SHAPE 1 -#ifdef USE_TRIMESH_SHAPE + int vertStride = sizeof(btVector3); int indexStride = 3*sizeof(int); @@ -367,99 +304,22 @@ void ConcaveConvexcastDemo::initPhysics() bool useQuantizedAabbCompression = true; -//comment out the next line to read the BVH from disk (first run the demo once to create the BVH) -#define SERIALIZE_TO_DISK 1 -#ifdef SERIALIZE_TO_DISK trimeshShape = new btBvhTriangleMeshShape(m_indexVertexArrays,useQuantizedAabbCompression); + m_collisionShapes.push_back(trimeshShape); - - - ///we can serialize the BVH data - void* buffer = 0; - int numBytes = trimeshShape->getOptimizedBvh()->calculateSerializeBufferSize(); - buffer = btAlignedAlloc(numBytes,16); - bool swapEndian = false; - trimeshShape->getOptimizedBvh()->serialize(buffer,numBytes,swapEndian); - FILE* file = fopen("bvh.bin","wb"); - fwrite(buffer,1,numBytes,file); - fclose(file); - btAlignedFree(buffer); - - - -#else - - trimeshShape = new btBvhTriangleMeshShape(m_indexVertexArrays,useQuantizedAabbCompression,false); - - char* fileName = "bvh.bin"; - - FILE* file = fopen(fileName,"rb"); - int size=0; - btOptimizedBvh* bvh = 0; - - if (fseek(file, 0, SEEK_END) || (size = ftell(file)) == EOF || fseek(file, 0, SEEK_SET)) { /* File operations denied? ok, just close and return failure */ - printf("Error: cannot get filesize from %s\n", fileName); - exit(0); - } else - { - - fseek(file, 0, SEEK_SET); - - int buffersize = size+btOptimizedBvh::getAlignmentSerializationPadding(); - - void* buffer = btAlignedAlloc(buffersize,16); - int read = fread(buffer,1,size,file); - fclose(file); - bool swapEndian = false; - bvh = btOptimizedBvh::deSerializeInPlace(buffer,buffersize,swapEndian); - } - - trimeshShape->setOptimizedBvh(bvh); - -#endif btCollisionShape* groundShape = trimeshShape; -#else - btCollisionShape* groundShape = new btBoxShape(btVector3(50,3,50)); - m_collisionShapes.push_back(groundShape); - -#endif //USE_TRIMESH_SHAPE m_collisionConfiguration = new btDefaultCollisionConfiguration(); -#ifdef USE_PARALLEL_DISPATCHER - -#ifdef USE_WIN32_THREADING - - int maxNumOutstandingTasks = 4;//number of maximum outstanding tasks - Win32ThreadSupport* threadSupport = new Win32ThreadSupport(Win32ThreadSupport::Win32ThreadConstructionInfo( - "collision", - processCollisionTask, - createCollisionLocalStoreMemory, - maxNumOutstandingTasks)); -#else -///todo other platform threading -///Playstation 3 SPU (SPURS) version is available through PS3 Devnet -///Libspe2 SPU support will be available soon -///pthreads version -///you can hook it up to your custom task scheduler by deriving from btThreadSupportInterface -#endif - - m_dispatcher = new SpuGatheringCollisionDispatcher(threadSupport,maxNumOutstandingTasks,m_collisionConfiguration); -#else m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); -#endif//USE_PARALLEL_DISPATCHER - btVector3 worldMin(-1000,-1000,-1000); btVector3 worldMax(1000,1000,1000); m_broadphase = new btAxisSweep3(worldMin,worldMax); m_solver = new btSequentialImpulseConstraintSolver(); m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration); -#ifdef USE_PARALLEL_DISPATCHER - m_dynamicsWorld->getDispatchInfo().m_enableSPU=true; -#endif //USE_PARALLEL_DISPATCHER float mass = 0.f; btTransform startTransform; diff --git a/Demos/ConcaveRaycastDemo/ConcaveRaycastDemo.cpp b/Demos/ConcaveRaycastDemo/ConcaveRaycastDemo.cpp index 4bbb6ff20..d71721c7a 100644 --- a/Demos/ConcaveRaycastDemo/ConcaveRaycastDemo.cpp +++ b/Demos/ConcaveRaycastDemo/ConcaveRaycastDemo.cpp @@ -20,15 +20,6 @@ subject to the following restrictions: #include "GL_ShapeDrawer.h" #include "GlutStuff.h" -//#define USE_PARALLEL_DISPATCHER 1 -#ifdef USE_PARALLEL_DISPATCHER -#include "../../Extras/BulletMultiThreaded/SpuGatheringCollisionDispatcher.h" -#include "../../Extras/BulletMultiThreaded/Win32ThreadSupport.h" -#include "../../Extras/BulletMultiThreaded/SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h" -#endif//USE_PARALLEL_DISPATCHER - - - static btVector3* gVertices=0; static int* gIndices=0; @@ -211,27 +202,6 @@ public: static btRaycastBar raycastBar; -///User can override this material combiner by implementing gContactAddedCallback and setting body0->m_collisionFlags |= btCollisionObject::customMaterialCallback; -inline btScalar calculateCombinedFriction(float friction0,float friction1) -{ - btScalar friction = friction0 * friction1; - - const btScalar MAX_FRICTION = 10.f; - if (friction < -MAX_FRICTION) - friction = -MAX_FRICTION; - if (friction > MAX_FRICTION) - friction = MAX_FRICTION; - return friction; - -} - -inline btScalar calculateCombinedRestitution(float restitution0,float restitution1) -{ - return restitution0 * restitution1; -} - - - const int NUM_VERTS_X = 30; const int NUM_VERTS_Y = 30; @@ -316,6 +286,7 @@ void ConcaveRaycastDemo::initPhysics() bool useQuantizedAabbCompression = true; trimeshShape = new btBvhTriangleMeshShape(m_indexVertexArrays,useQuantizedAabbCompression); + m_collisionShapes.push_back(trimeshShape); btCollisionShape* groundShape = trimeshShape; @@ -449,6 +420,7 @@ void ConcaveRaycastDemo::exitPhysics() delete m_collisionConfiguration; + delete[] gVertices; } diff --git a/Demos/DynamicControlDemo/MotorDemo.cpp b/Demos/DynamicControlDemo/MotorDemo.cpp index d8e8aa92b..11acc3fbc 100644 --- a/Demos/DynamicControlDemo/MotorDemo.cpp +++ b/Demos/DynamicControlDemo/MotorDemo.cpp @@ -328,7 +328,10 @@ void MotorDemo::clientMoveAndDisplay() if (m_dynamicsWorld) + { m_dynamicsWorld->stepSimulation(ms / 1000000.f); + m_dynamicsWorld->debugDrawWorld(); + } renderme(); @@ -349,7 +352,7 @@ void MotorDemo::displayCallback() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (m_dynamicsWorld) - m_dynamicsWorld->updateAabbs(); + m_dynamicsWorld->debugDrawWorld(); renderme(); diff --git a/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h b/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h index 2de68f964..8e3b0c7aa 100644 --- a/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h +++ b/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h @@ -138,7 +138,7 @@ public: void debugDrawObject(const btTransform& worldTransform, const btCollisionShape* shape, const btVector3& color); - void debugDrawWorld(); + virtual void debugDrawWorld(); virtual void setConstraintSolver(btConstraintSolver* solver); diff --git a/src/BulletDynamics/Dynamics/btDynamicsWorld.h b/src/BulletDynamics/Dynamics/btDynamicsWorld.h index aaf734a09..a628a13c8 100644 --- a/src/BulletDynamics/Dynamics/btDynamicsWorld.h +++ b/src/BulletDynamics/Dynamics/btDynamicsWorld.h @@ -49,6 +49,8 @@ class btDynamicsWorld : public btCollisionWorld virtual int stepSimulation( btScalar timeStep,int maxSubSteps=1, btScalar fixedTimeStep=btScalar(1.)/btScalar(60.))=0; virtual void updateAabbs() = 0; + + virtual void debugDrawWorld() = 0; virtual void addConstraint(btTypedConstraint* constraint, bool disableCollisionsBetweenLinkedBodies=false) { (void)constraint;};