plCollisionShapeHandle plNewCapsuleShape(plReal radius, plReal height) { //capsule is convex hull of 2 spheres, so use btMultiSphereShape btVector3 inertiaHalfExtents(radius,height,radius); const int numSpheres = 2; btVector3 positions[numSpheres] = {btVector3(0,height,0),btVector3(0,-height,0)}; btScalar radi[numSpheres] = {radius,radius}; void* mem = btAlignedAlloc(sizeof(btMultiSphereShape),16); return (plCollisionShapeHandle) new (mem)btMultiSphereShape(inertiaHalfExtents,positions,radi,numSpheres); }
int main(int argc,char** argv) { raytracePicture = new RenderTexture(screenWidth,screenHeight); myBox.SetMargin(0.02f); myCone.SetMargin(0.2f); simplex.SetSimplexSolver(&simplexSolver); simplex.AddVertex(SimdPoint3(-1,0,-1)); simplex.AddVertex(SimdPoint3(1,0,-1)); simplex.AddVertex(SimdPoint3(0,0,1)); simplex.AddVertex(SimdPoint3(0,1,0)); /// convex hull of 5 spheres #define NUM_SPHERES 5 SimdVector3 inertiaHalfExtents(10.f,10.f,10.f); SimdVector3 positions[NUM_SPHERES] = { SimdVector3(-1.2f, -0.3f, 0.f), SimdVector3(0.8f, -0.3f, 0.f), SimdVector3(0.5f, 0.6f, 0.f), SimdVector3(-0.5f, 0.6f, 0.f), SimdVector3(0.f, 0.f, 0.f) }; SimdScalar radi[NUM_SPHERES] = { 0.35f,0.35f,0.45f,0.40f,0.40f }; MultiSphereShape multiSphereShape(inertiaHalfExtents,positions,radi,NUM_SPHERES); ConvexHullShape convexHullShape(positions,3); //choose shape shapePtr[0] = &myCone; shapePtr[1] =&simplex; shapePtr[2] =&convexHullShape; shapePtr[3] =&myMink;//myBox; simplex.SetMargin(0.3f); setCameraDistance(6.f); return glutmain(argc, argv,screenWidth,screenHeight,"Minkowski-Sum Raytracer Demo"); }
void Raytracer::initPhysics() { raytracePicture = new RenderTexture(screenWidth,screenHeight); myBox.SetMargin(0.02f); myCone.SetMargin(0.2f); simplex.SetSimplexSolver(&simplexSolver); simplex.AddVertex(SimdPoint3(-1,0,-1)); simplex.AddVertex(SimdPoint3(1,0,-1)); simplex.AddVertex(SimdPoint3(0,0,1)); simplex.AddVertex(SimdPoint3(0,1,0)); /// convex hull of 5 spheres #define NUM_SPHERES 5 SimdVector3 inertiaHalfExtents(10.f,10.f,10.f); SimdVector3 positions[NUM_SPHERES] = { SimdVector3(-1.2f, -0.3f, 0.f), SimdVector3(0.8f, -0.3f, 0.f), SimdVector3(0.5f, 0.6f, 0.f), SimdVector3(-0.5f, 0.6f, 0.f), SimdVector3(0.f, 0.f, 0.f) }; // MultiSphereShape* multiSphereShape = new MultiSphereShape(inertiaHalfExtents,positions,radi,NUM_SPHERES); ConvexHullShape* convexHullShape = new ConvexHullShape(positions,3); //choose shape shapePtr[0] = &myCone; shapePtr[1] =&simplex; shapePtr[2] =convexHullShape; shapePtr[3] =&myMink;//myBox;//multiSphereShape simplex.SetMargin(0.3f); }
void BasicDemo::initPhysics() { setTexturing(false); setShadows(false); #if OECAKE_LOADER setCameraDistance(80.); m_cameraTargetPosition.setValue(50, 10, 0); #else #if LARGE_DEMO setCameraDistance(btScalar(SCALING*100.)); #else setCameraDistance(btScalar(SCALING*20.)); #endif m_cameraTargetPosition.setValue(START_POS_X, -START_POS_Y-20, START_POS_Z); #endif m_azi = btScalar(0.f); m_ele = btScalar(0.f); ///collision configuration contains default setup for memory, collision setup btDefaultCollisionConstructionInfo dci; dci.m_defaultMaxPersistentManifoldPoolSize=50000; dci.m_defaultMaxCollisionAlgorithmPoolSize=50000; m_collisionConfiguration = new btDefaultCollisionConfiguration(dci); ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded) m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); m_dispatcher->registerCollisionCreateFunc(BOX_SHAPE_PROXYTYPE,BOX_SHAPE_PROXYTYPE,new btEmptyAlgorithm::CreateFunc); m_dispatcher->setNearCallback(cudaDemoNearCallback); #if USE_CUDA_DEMO_PAIR_CASHE gPairCache = new (btAlignedAlloc(sizeof(btGpuDemoPairCache),16)) btGpuDemoPairCache(MAX_PROXIES, 24, MAX_SMALL_PROXIES); #else gPairCache = new (btAlignedAlloc(sizeof(btHashedOverlappingPairCache),16))btHashedOverlappingPairCache(); #endif // btVector3 numOfCells = (gWorldMax - gWorldMin) / (2. * SCALING); // btVector3 numOfCells = (gWorldMax - gWorldMin) / CELL_SIZE; // int numOfCellsX = (int)numOfCells[0]; // int numOfCellsY = (int)numOfCells[1]; // int numOfCellsZ = (int)numOfCells[2]; // if(!numOfCellsX) numOfCellsX = 1; // if(!numOfCellsY) numOfCellsY = 1; // if(!numOfCellsZ) numOfCellsZ = 1; btScalar maxDiam = 2.0f * SCALING; btVector3 cellSize(maxDiam, maxDiam, maxDiam); btVector3 numOfCells = (gWorldMax - gWorldMin) / cellSize; int numOfCellsX = btGpu3DGridBroadphase::getFloorPowOfTwo((int)numOfCells[0]); int numOfCellsY = btGpu3DGridBroadphase::getFloorPowOfTwo((int)numOfCells[1]); int numOfCellsZ = btGpu3DGridBroadphase::getFloorPowOfTwo((int)numOfCells[2]); // m_broadphase = new btAxisSweep3(gWorldMin, gWorldMax, MAX_PROXIES,gPairCache); // m_broadphase = new btDbvtBroadphase(gPairCache); // m_broadphase = new btGpu3DGridBroadphase(gPairCache, gWorldMin, gWorldMax,numOfCellsX, numOfCellsY, numOfCellsZ,MAX_SMALL_PROXIES,20,24,24, 1.0f/1.5f); // m_broadphase = new btCudaBroadphase(gPairCache, gWorldMin, gWorldMax,numOfCellsX, numOfCellsY, numOfCellsZ,MAX_SMALL_PROXIES,10,24,24); // m_broadphase = new bt3dGridBroadphaseOCL(gPairCache, gWorldMin, gWorldMax,numOfCellsX, numOfCellsY, numOfCellsZ,MAX_SMALL_PROXIES,20,24,24,1./1.5); m_broadphase = new bt3dGridBroadphaseOCL(gPairCache, cellSize,numOfCellsX, numOfCellsY, numOfCellsZ,MAX_SMALL_PROXIES,20,24,10.f, 24); ///the default constraint solver m_solver = new btSequentialImpulseConstraintSolver(); btGpuDemoDynamicsWorld* pDdw = new btGpuDemoDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration, MAX_PROXIES); m_dynamicsWorld = pDdw; m_pWorld = pDdw; pDdw->getSimulationIslandManager()->setSplitIslands(true); pDdw->setObjRad(SCALING); pDdw->setWorldMin(gWorldMin); pDdw->setWorldMax(gWorldMax); // gUseCPUSolver = true; pDdw->setUseCPUSolver(gUseCPUSolver); gUseBulletNarrowphase = false; pDdw->setUseBulletNarrowphase(gUseBulletNarrowphase); // m_dynamicsWorld->setGravity(btVector3(0,0,0)); m_dynamicsWorld->setGravity(btVector3(0,-10.,0)); m_dynamicsWorld->getSolverInfo().m_numIterations = 4; { //create a few dynamic rigidbodies // Re-using the same collision is better for memory usage and performance #if 1 #define SPRADIUS btScalar(SCALING*0.1f) #define SPRPOS btScalar(SCALING*0.05f) static btVector3 sSphPos[8]; for (int k=0;k<8;k++) { sSphPos[k].setValue((k-4)*0.25*SCALING,0,0); } btVector3 inertiaHalfExtents(SPRADIUS, SPRADIUS, SPRADIUS); static btScalar sSphRad[8] = { // SPRADIUS, SPRADIUS, SPRADIUS, SPRADIUS,SPRADIUS, SPRADIUS, SPRADIUS, 0.3 SPRADIUS, SPRADIUS, SPRADIUS, SPRADIUS,SPRADIUS, SPRADIUS, SPRADIUS, SPRADIUS }; // sSphPos[0].setX(sSphPos[0].getX()-0.15); #undef SPR btMultiSphereShape* colShape[2]; colShape[0] = new btMultiSphereShape( sSphPos, sSphRad, 8); colShape[1] = new btMultiSphereShape( sSphPos, sSphRad, 2); //btCollisionShape* colShape = new btSphereShape(btScalar(1.)); m_collisionShapes.push_back(colShape[0]); m_collisionShapes.push_back(colShape[1]); #endif /// Create Dynamic Objects btTransform startTransform; startTransform.setIdentity(); btScalar mass(0.f); //rigidbody is dynamic if and only if mass is non zero, otherwise static bool isDynamic = (mass != 0.f); btVector3 localInertia(0,0,0); #if OECAKE_LOADER BasicDemoOecakeLoader loader(this); if (!loader.processFile("test1.oec")) { loader.processFile("../../../../../Demos/Gpu2dDemo/test.oec"); } #if 0 // perfomance test : work-in-progress { // add more object, but share their shapes int numNewObjects = 500; mass = 1.f; for(int n_obj = 0; n_obj < numNewObjects; n_obj++) { btDefaultMotionState* myMotionState= 0; btVector3 localInertia(0,0,0); btTransform worldTransform; worldTransform.setIdentity(); btScalar fx = fRandMinMax(-30., 30.); btScalar fy = fRandMinMax(5., 30.); worldTransform.setOrigin(btVector3(fx, fy, 0.f)); int sz = m_collisionShapes.size(); btMultiSphereShape* multiSphere = (btMultiSphereShape*)m_collisionShapes[1]; myMotionState = new btDefaultMotionState(worldTransform); multiSphere->calculateLocalInertia(mass, localInertia); btRigidBody* body = new btRigidBody(mass,myMotionState,multiSphere,localInertia); body->setLinearFactor(btVector3(1,1,0)); body->setAngularFactor(btVector3(0,0,1)); body->setWorldTransform(worldTransform); getDynamicsWorld()->addRigidBody(body); } } #endif #else #if (!SPEC_TEST) float start_x = START_POS_X - ARRAY_SIZE_X * SCALING; float start_y = START_POS_Y - ARRAY_SIZE_Y * SCALING; float start_z = START_POS_Z - ARRAY_SIZE_Z * SCALING; int collisionShapeIndex = 0; for (int k=0;k<ARRAY_SIZE_Y;k++) { for (int i=0;i<ARRAY_SIZE_X;i++) { for(int j = 0;j<ARRAY_SIZE_Z;j++) { float offs = (2. * (float)rand() / (float)RAND_MAX - 1.f) * 0.05f; startTransform.setOrigin(SCALING*btVector3( 2.0*SCALING*i + start_x + offs, 2.0*SCALING*k + start_y + offs, 2.0*SCALING*j + start_z)); if (isDynamic) colShape[collisionShapeIndex]->calculateLocalInertia(mass,localInertia); //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects //btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform); btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,0,colShape[collisionShapeIndex],localInertia); collisionShapeIndex = 1 - collisionShapeIndex; rbInfo.m_startWorldTransform=startTransform; btRigidBody* body = new btRigidBody(rbInfo); m_dynamicsWorld->addRigidBody(body); } } } #else//SPEC_TEST // narrowphase test - 2 bodies at the same position float start_x = START_POS_X; // float start_y = START_POS_Y; float start_y = gWorldMin[1] + SCALING * 0.7f + 5.f; float start_z = START_POS_Z; startTransform.setOrigin(SCALING*btVector3(start_x,start_y,start_z)); btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,0,colShape[0],localInertia); rbInfo.m_startWorldTransform=startTransform; btRigidBody* body = new btRigidBody(rbInfo); m_dynamicsWorld->addRigidBody(body); btPoint2PointConstraint * p2pConstr = new btPoint2PointConstraint(*body, btVector3(1., 0., 0.)); m_dynamicsWorld->addConstraint(p2pConstr); startTransform.setOrigin(SCALING*btVector3(start_x-2.f, start_y,start_z)); rbInfo.m_startWorldTransform=startTransform; btRigidBody* body1 = new btRigidBody(rbInfo); m_dynamicsWorld->addRigidBody(body1); p2pConstr = new btPoint2PointConstraint(*body, *body1, btVector3(-1., 0., 0.), btVector3(1., 0., 0.)); m_dynamicsWorld->addConstraint(p2pConstr); #endif//SPEC_TEST #endif //OE_CAKE_LOADER } // now set Ids used by collision detector and constraint solver int numObjects = m_dynamicsWorld->getNumCollisionObjects(); btCollisionObjectArray& collisionObjects = m_dynamicsWorld->getCollisionObjectArray(); for(int i = 0; i < numObjects; i++) { btCollisionObject* colObj = collisionObjects[i]; colObj->setCompanionId(i+1); // 0 reserved for the "world" object btCollisionShape* pShape = colObj->getCollisionShape(); int shapeType = pShape->getShapeType(); if(shapeType == MULTI_SPHERE_SHAPE_PROXYTYPE) { btMultiSphereShape* pMs = (btMultiSphereShape*)pShape; int numSpheres = pMs->getSphereCount(); pDdw->addMultiShereObject(numSpheres, i + 1); for(int j = 0; j < numSpheres; j++) { btVector3 sphPos = pMs->getSpherePosition(j); float sphRad = pMs->getSphereRadius(j); pDdw->addSphere(sphPos, sphRad); } } else { btAssert(0); } } #if OECAKE_LOADER clientResetScene(); #endif }