int main(int argc,char** argv) { clientResetScene(); btMatrix3x3 basisA; basisA.setIdentity(); btMatrix3x3 basisB; basisB.setIdentity(); tr[0].setBasis(basisA); tr[1].setBasis(basisB); btVector3 points0[3]={btVector3(1,0,0),btVector3(0,1,0),btVector3(0,0,1)}; //btVector3 points1[5]={btVector3(1,0,0),btVector3(0,1,0),btVector3(0,0,1),btVector3(0,0,-1),btVector3(-1,-1,0)}; btConvexHullShape hullA(&points0[0].getX(),3); btConvexHullShape hullB(TaruVtx,TaruVtxCount,3*sizeof(btScalar)); shapePtr[0] = &hullA; shapePtr[1] = &hullB; btTransform tr; tr.setIdentity(); return myglutmain(argc, argv,screenWidth,screenHeight,"Convex Hull Distance Demo"); }
void ConvexHullDistanceDemo::initPhysics() { #endif clientResetScene(); btMatrix3x3 basisA; basisA.setIdentity(); btMatrix3x3 basisB; basisB.setIdentity(); tr[0].setBasis(basisA); tr[1].setBasis(basisB); btVector3 points0[3]={btVector3(1,0,0),btVector3(0,1,0),btVector3(0,0,1)}; //btVector3 points1[5]={btVector3(1,0,0),btVector3(0,1,0),btVector3(0,0,1),btVector3(0,0,-1),btVector3(-1,-1,0)}; #ifdef __QNX__ shapePtr[0] = new btConvexHullShape(&points0[0].getX(),3); shapePtr[1] = new btConvexHullShape(TaruVtx,TaruVtxCount,3*sizeof(btScalar)); #else btConvexHullShape hullA(&points0[0].getX(),3); btConvexHullShape hullB(TaruVtx,TaruVtxCount,3*sizeof(btScalar)); shapePtr[0] = &hullA; shapePtr[1] = &hullB; #endif btTransform tr; tr.setIdentity(); #ifdef __QNX__ }
void FluidHfDemo::keyboardCallback(unsigned char key, int x, int y) { switch(key) { case ']': if(current_demo < NUM_DEMOS-1) { ++current_demo; clientResetScene(); } break; case '[': if(current_demo >= 1) { --current_demo; clientResetScene(); } break; case '/': current_draw_mode = (current_draw_mode+1) % DRAWMODE_MAX; getFluidHfDynamicsWorld()->setDrawMode (current_draw_mode); break; case 'v': current_body_draw_mode = (current_body_draw_mode+1) % BODY_DRAWMODE_MAX; getFluidHfDynamicsWorld()->setBodyDrawMode (current_body_draw_mode); break; case 'k': m_hfFluidShapeDrawer->m_drawHfFluidWithTriangles = !m_hfFluidShapeDrawer->m_drawHfFluidWithTriangles; break; case 'l': m_hfFluidShapeDrawer->m_drawHfGroundWithTriangles = !m_hfFluidShapeDrawer->m_drawHfGroundWithTriangles; break; case ';': m_hfFluidShapeDrawer->m_drawHfFluidAsColumns = !m_hfFluidShapeDrawer->m_drawHfFluidAsColumns; break; case '\'': m_hfFluidShapeDrawer->m_drawHfGroundAsColumns = !m_hfFluidShapeDrawer->m_drawHfGroundAsColumns; break; default: DemoApplication::keyboardCallback(key,x,y); break; } }
void RagdollDemo::initPhysics() { // Setup the basic world setTexturing(true); setShadows(true); setCameraDistance(btScalar(5.)); m_collisionConfiguration = new btDefaultCollisionConfiguration(); m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); btVector3 worldAabbMin(-10000,-10000,-10000); btVector3 worldAabbMax(10000,10000,10000); m_broadphase = new btAxisSweep3 (worldAabbMin, worldAabbMax); m_solver = new btSequentialImpulseConstraintSolver; m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration); //m_dynamicsWorld->getDispatchInfo().m_useConvexConservativeDistanceUtil = true; //m_dynamicsWorld->getDispatchInfo().m_convexConservativeDistanceThreshold = 0.01f; // Setup a big ground box { btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(200.),btScalar(10.),btScalar(200.))); m_collisionShapes.push_back(groundShape); btTransform groundTransform; groundTransform.setIdentity(); groundTransform.setOrigin(btVector3(0,-10,0)); #define CREATE_GROUND_COLLISION_OBJECT 1 #ifdef CREATE_GROUND_COLLISION_OBJECT btCollisionObject* fixedGround = new btCollisionObject(); fixedGround->setCollisionShape(groundShape); fixedGround->setWorldTransform(groundTransform); m_dynamicsWorld->addCollisionObject(fixedGround); #else localCreateRigidBody(btScalar(0.),groundTransform,groundShape); #endif //CREATE_GROUND_COLLISION_OBJECT } // Spawn one ragdoll btVector3 startOffset(1,0.5,0); spawnRagdoll(startOffset); startOffset.setValue(-1,0.5,0); spawnRagdoll(startOffset); clientResetScene(); }
void ArtificialBirdsDemoApp::initPhysics() { // Setup the basic world setTexturing(true); setShadows(true); setCameraDistance(btScalar(5.)); m_collisionConfiguration = new btDefaultCollisionConfiguration(); m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); btVector3 worldAabbMin(-10000,-10000,-10000); btVector3 worldAabbMax(10000,10000,10000); m_broadphase = new btAxisSweep3 (worldAabbMin, worldAabbMax); m_solver = new btSequentialImpulseConstraintSolver; m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration); //m_dynamicsWorld->getDispatchInfo().m_useConvexConservativeDistanceUtil = true; //m_dynamicsWorld->getDispatchInfo().m_convexConservativeDistanceThreshold = 0.01f; m_dynamicsWorld->setInternalTickCallback(pickingPreTickCallback, this, true); m_dynamicsWorld->getSolverInfo().m_numIterations = kSolverNumIterations; m_dynamicsWorld->setGravity(btVector3(0,kGravity,0)); // Setup a big ground box { btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(200.),btScalar(10.),btScalar(200.))); m_collisionShapes.push_back(groundShape); btTransform groundTransform; groundTransform.setIdentity(); groundTransform.setOrigin(btVector3(0,-10,0)); #define CREATE_GROUND_COLLISION_OBJECT 1 #ifdef CREATE_GROUND_COLLISION_OBJECT btCollisionObject* fixedGround = new btCollisionObject(); fixedGround->setCollisionShape(groundShape); fixedGround->setWorldTransform(groundTransform); m_dynamicsWorld->addCollisionObject(fixedGround); #else localCreateRigidBody(btScalar(0.),groundTransform,groundShape); #endif //CREATE_GROUND_COLLISION_OBJECT } m_birdOpt = 0; m_birdDemo = 0; //m_birdOpt = new BirdOptimizer(m_dynamicsWorld); m_birdDemo = new BirdDemo(m_dynamicsWorld); clientResetScene(); }
void RagdollApp::initPhysics() { // Setup the basic world m_time = 0; m_CycPerKnee = 2000.f; // in milliseconds m_CycPerHip = 3000.f; // in milliseconds m_fMuscleStrength = 0.5f; setTexturing(true); setShadows(true); setCameraDistance(btScalar(5.)); m_collisionConfiguration = new btDefaultCollisionConfiguration(); m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); btVector3 worldAabbMin(-10000,-10000,-10000); btVector3 worldAabbMax(10000,10000,10000); m_broadphase = new btAxisSweep3 (worldAabbMin, worldAabbMax); m_solver = new btSequentialImpulseConstraintSolver; m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration); //m_dynamicsWorld->getDispatchInfo().m_useConvexConservativeDistanceUtil = true; //m_dynamicsWorld->getDispatchInfo().m_convexConservativeDistanceThreshold = 0.01f; m_dynamicsWorld->setInternalTickCallback( forcePreTickCB, this, true ); m_dynamicsWorld->setGravity( btVector3(0,-0,0) ); // create surface //createSurface(); //// Setup a big ground box btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(200.),btScalar(10.),btScalar(200.))); m_collisionShapes.push_back(groundShape); btTransform groundTransform; groundTransform.setIdentity(); groundTransform.setOrigin(btVector3(0,-10,0)); btCollisionObject* fixedGround = new btCollisionObject(); fixedGround->setCollisionShape(groundShape); fixedGround->setWorldTransform(groundTransform); m_dynamicsWorld->addCollisionObject(fixedGround); // Spawn one ragdoll btVector3 startOffset(1,0.5,0); spawnRagdoll(startOffset); startOffset.setValue(-1,0.1,0); spawnRagdoll(startOffset); clientResetScene(); }
void MotorDemo::initPhysics() { setTexturing(true); setShadows(true); // Setup the basic world m_Time = 0; m_fCyclePeriod = 2000.f; // in milliseconds // m_fMuscleStrength = 0.05f; // new SIMD solver for joints clips accumulated impulse, so the new limits for the motor // should be (numberOfsolverIterations * oldLimits) // currently solver uses 10 iterations, so: m_fMuscleStrength = 0.5f; setCameraDistance(btScalar(5.)); m_collisionConfiguration = new btDefaultCollisionConfiguration(); m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); btVector3 worldAabbMin(-10000,-10000,-10000); btVector3 worldAabbMax(10000,10000,10000); m_broadphase = new btAxisSweep3 (worldAabbMin, worldAabbMax); m_solver = new btSequentialImpulseConstraintSolver; m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration); m_dynamicsWorld->setInternalTickCallback(motorPreTickCallback,this,true); // Setup a big ground box { btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(200.),btScalar(10.),btScalar(200.))); m_collisionShapes.push_back(groundShape); btTransform groundTransform; groundTransform.setIdentity(); groundTransform.setOrigin(btVector3(0,-10,0)); localCreateRigidBody(btScalar(0.),groundTransform,groundShape); } // Spawn one ragdoll btVector3 startOffset(1,0.5,0); spawnTestRig(startOffset, false); startOffset.setValue(-2,0.5,0); spawnTestRig(startOffset, true); clientResetScene(); }
void ChainBtDynamics::initPhysics() { // Setup the basic world m_collisionConfiguration = new btDefaultCollisionConfiguration(); m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); btVector3 worldAabbMin(-10000,-10000,-10000); btVector3 worldAabbMax(10000,10000,10000); m_broadphase = new btAxisSweep3 (worldAabbMin, worldAabbMax); m_solver = new btSequentialImpulseConstraintSolver; m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration); //m_dynamicsWorld->setGravity(btVector3(0.0f, -9.8f*GRAVITY_SCALE, 0.0f)); //m_dynamicsWorld->setGravity(btVector3(0.0f, 0.1f*GRAVITY_SCALE, 0.0f)); //m_dynamicsWorld->setGravity(btVector3(0.0f, -3.0f*GRAVITY_SCALE, 0.0f)); m_dynamicsWorld->setGravity(btVector3(0.0f, 0.00f*GRAVITY_SCALE, 0.0f)); //m_dynamicsWorld->getDispatchInfo().m_useConvexConservativeDistanceUtil = true; //m_dynamicsWorld->getDispatchInfo().m_convexConservativeDistanceThreshold = 0.01f; // Setup a big ground box //{ // //btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(1.5),btScalar(0.1),btScalar(1.5))); // // btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(3000.0f), // btScalar(600.0f), // btScalar(3000.0f))); // m_collisionShapes.push_back(groundShape); // btTransform groundTransform; // groundTransform.setIdentity(); // //groundTransform.setOrigin(btVector3(0,-10,0)); // groundTransform.setOrigin(btVector3(0,-600.0f,0)); // // btCollisionObject* fixedGround = new btCollisionObject(); // fixedGround->setCollisionShape(groundShape); // fixedGround->setWorldTransform(groundTransform); // m_groundInfo.isGround = true; // fixedGround->setUserPointer(&m_groundInfo); // // m_dynamicsWorld->addCollisionObject(fixedGround); //} btVector3 startOffset(0,100,0); clientResetScene(); }
int main(int argc,char** argv) { clientResetScene(); SimdMatrix3x3 basisA; basisA.setIdentity(); SimdMatrix3x3 basisB; basisB.setIdentity(); objects[0].m_worldTransform.setBasis(basisA); objects[1].m_worldTransform.setBasis(basisB); SimdPoint3 points0[3]={SimdPoint3(1,0,0),SimdPoint3(0,1,0),SimdPoint3(0,0,1)}; SimdPoint3 points1[5]={SimdPoint3(1,0,0),SimdPoint3(0,1,0),SimdPoint3(0,0,1),SimdPoint3(0,0,-1),SimdPoint3(-1,-1,0)}; BoxShape boxA(SimdVector3(1,1,1)); BoxShape boxB(SimdVector3(0.5,0.5,0.5)); //ConvexHullShape hullA(points0,3); //hullA.setLocalScaling(SimdVector3(3,3,3)); //ConvexHullShape hullB(points1,4); //hullB.setLocalScaling(SimdVector3(4,4,4)); objects[0].m_collisionShape = &boxA;//&hullA; objects[1].m_collisionShape = &boxB;//&hullB; CollisionDispatcher dispatcher; //SimpleBroadphase broadphase; SimdVector3 worldAabbMin(-1000,-1000,-1000); SimdVector3 worldAabbMax(1000,1000,1000); AxisSweep3 broadphase(worldAabbMin,worldAabbMax); collisionWorld = new CollisionWorld(&dispatcher,&broadphase); collisionWorld->AddCollisionObject(&objects[0]); collisionWorld->AddCollisionObject(&objects[1]); return glutmain(argc, argv,screenWidth,screenHeight,"Collision Interface Demo"); }
void GenericJointDemo::initPhysics() { setTexturing(true); setShadows(true); // Setup the basic world btDefaultCollisionConfiguration * collision_config = new btDefaultCollisionConfiguration(); btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collision_config); btVector3 worldAabbMin(-10000,-10000,-10000); btVector3 worldAabbMax(10000,10000,10000); btBroadphaseInterface* overlappingPairCache = new btAxisSweep3 (worldAabbMin, worldAabbMax); btConstraintSolver* constraintSolver = new btSequentialImpulseConstraintSolver; m_dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,constraintSolver,collision_config); m_dynamicsWorld->setGravity(btVector3(0,-10,0)); m_dynamicsWorld->setDebugDrawer(&debugDrawer); //m_dynamicsWorld->getSolverInfo().m_restingContactRestitutionThreshold = 0.f; // m_singleAxisRollingFrictionThreshold = 0.f;//faster but lower quality // Setup a big ground box { btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(200.),btScalar(0.),btScalar(200.))); //! 0 thickness btTransform groundTransform; groundTransform.setIdentity(); groundTransform.setOrigin(btVector3(0,-8,0)); m_ground = localCreateRigidBody(btScalar(0.),groundTransform,groundShape); } Swing* swing1 = new Swing( m_dynamicsWorld, btVector3(0,9,0), 4.0f, m_ground ); Swing* swing2 = new Swing( m_dynamicsWorld, btVector3(4,9,0), 4.0f, m_ground ); clientResetScene(); }
void GenericJointDemo::initPhysics() { setTexturing(true); setShadows(true); // Setup the basic world btDefaultCollisionConfiguration * collision_config = new btDefaultCollisionConfiguration(); btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collision_config); btVector3 worldAabbMin(-10000,-10000,-10000); btVector3 worldAabbMax(10000,10000,10000); btBroadphaseInterface* overlappingPairCache = new btAxisSweep3 (worldAabbMin, worldAabbMax); btConstraintSolver* constraintSolver = new btSequentialImpulseConstraintSolver; m_dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,constraintSolver,collision_config); m_dynamicsWorld->setGravity(btVector3(0,-30,0)); m_dynamicsWorld->setDebugDrawer(&debugDrawer); // Setup a big ground box { btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(200.),btScalar(10.),btScalar(200.))); btTransform groundTransform; groundTransform.setIdentity(); groundTransform.setOrigin(btVector3(0,-15,0)); localCreateRigidBody(btScalar(0.),groundTransform,groundShape); } // Spawn one ragdoll spawnRagdoll(); clientResetScene(); }
void CcdPhysicsDemo::keyboardCallback(unsigned char key, int x, int y) { if (key=='p') { switch (m_ccdMode) { case USE_CCD: { m_ccdMode = USE_NO_CCD; break; } case USE_NO_CCD: default: { m_ccdMode = USE_CCD; } }; clientResetScene(); } else { DemoApplication::keyboardCallback(key,x,y); } }
void FluidHfDemo::initPhysics() { m_collisionConfiguration = new btFluidHfRigidCollisionConfiguration(); m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); m_broadphase = new btDbvtBroadphase(); m_solver = new btSequentialImpulseConstraintSolver(); m_dynamicsWorld = new btFluidHfRigidDynamicsWorld(m_dispatcher, m_broadphase, m_solver, m_collisionConfiguration); m_dynamicsWorld->setGravity( btVector3(0, -10, 0) ); // const btScalar CUBE_HALF_EXTENTS = btScalar(1.5); btCollisionShape* groundShape = new btBoxShape( btVector3(200, CUBE_HALF_EXTENTS, 200) ); { m_collisionShapes.push_back(groundShape); btTransform transform( btQuaternion::getIdentity(), btVector3(0, -12, 0) ); localCreateRigidBody(0.f, transform, groundShape); } // clientResetScene(); }
BulletOpenGLViewer::BulletOpenGLViewer(DynamicsWorldPtr world) { SIMDYNAMICS_ASSERT(world); m_sundirection = btVector3(1, 1, -2) * 1000; bulletEngine = boost::dynamic_pointer_cast<BulletEngine>(world->getEngine()); SIMDYNAMICS_ASSERT(bulletEngine); setTexturing(true); setShadows(true); // set Bullet world (defined in bullet's OpenGL helpers) m_dynamicsWorld = bulletEngine->getBulletWorld(); m_dynamicsWorld->setDebugDrawer(&debugDrawer); // set up vector for camera setCameraDistance(btScalar(3.)); setCameraForwardAxis(1); btVector3 up(0, 0, btScalar(1.)); setCameraUp(up); clientResetScene(); }
void BasicDemo::initPhysics() { setTexturing(true); setShadows(true); setCameraDistance(btScalar(10.0)); ///collision configuration contains default setup for memory, collision setup m_collisionConfiguration = new btDefaultCollisionConfiguration(); ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded) m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); m_broadphase = new btDbvtBroadphase(); ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded) btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver; m_solver = sol; m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration); m_dynamicsWorld->setGravity(btVector3(0,-10,0)); ///create a few basic rigid bodies btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.))); m_collisionShapes.push_back(groundShape); btTransform groundTransform; groundTransform.setIdentity(); groundTransform.setOrigin(btVector3(0,-50,0)); //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here: { btScalar mass(0.); //rigidbody is dynamic if and only if mass is non zero, otherwise static bool isDynamic = (mass != 0.f); btVector3 localInertia(0,0,0); if (isDynamic) groundShape->calculateLocalInertia(mass,localInertia); //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform); btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia); btRigidBody* body = new btRigidBody(rbInfo); body->setRestitution(btScalar(0.8f)); //add the body to the dynamics world m_dynamicsWorld->addRigidBody(body); } // Here are the dynamic bodies { btCollisionShape* colShape = new btSphereShape(1); m_collisionShapes.push_back(colShape); /// Create Dynamic Objects btTransform startTransform; startTransform.setIdentity(); btScalar mass(fMass); //rigidbody is dynamic if and only if mass is non zero, otherwise static bool isDynamic = (mass != 0.f); btVector3 localInertia(0,0,0); if (isDynamic) colShape->calculateLocalInertia(mass,localInertia); float start_x[] = {0.0, 1.9}; // collision angle float start_y[] = {1.0, 1.0}; float start_z[] = {-4.0, 4.0}; // set up 2 rigid bodies and put them into the btAlignedObjectArray called m_rigidBody for(int ii = 0; ii < 2; ii++){ startTransform.setOrigin(btVector3( start_x[ii], start_y[ii], start_z[ii] )); //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform); btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia); btRigidBody* ball = new btRigidBody(rbInfo); ball->setActivationState(ISLAND_SLEEPING); ball->setRestitution(fCoefficientOfRestitution); m_dynamicsWorld->addRigidBody(ball); ball->setActivationState(ISLAND_SLEEPING); m_rigidBody.push_back(ball); } for(int i = 0; i < 2; i++) { float start_velx[] = {0,0}; float start_vely[] = {0,0}; float start_velz[] = {3,0}; m_vAcceleration[i] = btVector3(); m_vAngularVelocity[i] = btVector3(); m_vPosition[i] = btVector3(); m_vTorque[i] = btVector3(); m_vVelocity[i] = btVector3(); m_qOrientation[i] = btQuaternion(); m_qOrientation[i].setValue(0.0,0.0,0.0,1.0); m_vAcceleration[i].setValue(0.0,0.0,0.0); m_vAngularVelocity[i].setValue(0.0,0.0,0.0); m_vVelocity[i].setValue(start_velx[i],start_vely[i], start_velz[i]); m_vPosition[i].setValue(start_x[i],start_y[i],start_z[i]); m_vTorque[i].setValue(0.0,0.0,0.0); } } clientResetScene(); }
void DemoApplication::keyboardCallback(unsigned char key, int x, int y) { (void)x; (void)y; m_lastKey = 0; #ifndef BT_NO_PROFILE if (key >= 0x31 && key <= 0x39) { int child = key-0x31; m_profileIterator->Enter_Child(child); } if (key==0x30) { m_profileIterator->Enter_Parent(); } #endif //BT_NO_PROFILE switch (key) { case 'q' : #ifdef BT_USE_FREEGLUT //return from glutMainLoop(), detect memory leaks etc. glutLeaveMainLoop(); #else exit(0); #endif break; case 'l' : stepLeft(); break; case 'r' : stepRight(); break; case 'f' : stepFront(); break; case 'b' : stepBack(); break; case 'z' : zoomIn(); break; case 'x' : zoomOut(); break; case 'i' : toggleIdle(); break; case 'g' : m_enableshadows=!m_enableshadows;break; case 'u' : m_shapeDrawer->enableTexture(!m_shapeDrawer->enableTexture(false));break; case 'h': if (m_debugMode & btIDebugDraw::DBG_NoHelpText) m_debugMode = m_debugMode & (~btIDebugDraw::DBG_NoHelpText); else m_debugMode |= btIDebugDraw::DBG_NoHelpText; break; case 'w': if (m_debugMode & btIDebugDraw::DBG_DrawWireframe) m_debugMode = m_debugMode & (~btIDebugDraw::DBG_DrawWireframe); else m_debugMode |= btIDebugDraw::DBG_DrawWireframe; break; case 'p': if (m_debugMode & btIDebugDraw::DBG_ProfileTimings) m_debugMode = m_debugMode & (~btIDebugDraw::DBG_ProfileTimings); else m_debugMode |= btIDebugDraw::DBG_ProfileTimings; break; case '=': { int maxSerializeBufferSize = 1024*1024*5; btDefaultSerializer* serializer = new btDefaultSerializer(maxSerializeBufferSize); //serializer->setSerializationFlags(BT_SERIALIZE_NO_DUPLICATE_ASSERT); m_dynamicsWorld->serialize(serializer); FILE* f2 = fopen("testFile.bullet","wb"); fwrite(serializer->getBufferPointer(),serializer->getCurrentBufferSize(),1,f2); fclose(f2); delete serializer; break; } case 'm': if (m_debugMode & btIDebugDraw::DBG_EnableSatComparison) m_debugMode = m_debugMode & (~btIDebugDraw::DBG_EnableSatComparison); else m_debugMode |= btIDebugDraw::DBG_EnableSatComparison; break; case 'n': if (m_debugMode & btIDebugDraw::DBG_DisableBulletLCP) m_debugMode = m_debugMode & (~btIDebugDraw::DBG_DisableBulletLCP); else m_debugMode |= btIDebugDraw::DBG_DisableBulletLCP; break; case 't' : if (m_debugMode & btIDebugDraw::DBG_DrawText) m_debugMode = m_debugMode & (~btIDebugDraw::DBG_DrawText); else m_debugMode |= btIDebugDraw::DBG_DrawText; break; case 'y': if (m_debugMode & btIDebugDraw::DBG_DrawFeaturesText) m_debugMode = m_debugMode & (~btIDebugDraw::DBG_DrawFeaturesText); else m_debugMode |= btIDebugDraw::DBG_DrawFeaturesText; break; case 'a': if (m_debugMode & btIDebugDraw::DBG_DrawAabb) m_debugMode = m_debugMode & (~btIDebugDraw::DBG_DrawAabb); else m_debugMode |= btIDebugDraw::DBG_DrawAabb; break; case 'c' : if (m_debugMode & btIDebugDraw::DBG_DrawContactPoints) m_debugMode = m_debugMode & (~btIDebugDraw::DBG_DrawContactPoints); else m_debugMode |= btIDebugDraw::DBG_DrawContactPoints; break; case 'C' : if (m_debugMode & btIDebugDraw::DBG_DrawConstraints) m_debugMode = m_debugMode & (~btIDebugDraw::DBG_DrawConstraints); else m_debugMode |= btIDebugDraw::DBG_DrawConstraints; break; case 'L' : if (m_debugMode & btIDebugDraw::DBG_DrawConstraintLimits) m_debugMode = m_debugMode & (~btIDebugDraw::DBG_DrawConstraintLimits); else m_debugMode |= btIDebugDraw::DBG_DrawConstraintLimits; break; case 'd' : if (m_debugMode & btIDebugDraw::DBG_NoDeactivation) m_debugMode = m_debugMode & (~btIDebugDraw::DBG_NoDeactivation); else m_debugMode |= btIDebugDraw::DBG_NoDeactivation; if (m_debugMode & btIDebugDraw::DBG_NoDeactivation) { gDisableDeactivation = true; } else { gDisableDeactivation = false; } break; case 'o' : { m_ortho = !m_ortho;//m_stepping = !m_stepping; break; } case 's' : clientMoveAndDisplay(); break; // case ' ' : newRandom(); break; case ' ': clientResetScene(); break; case '1': { if (m_debugMode & btIDebugDraw::DBG_EnableCCD) m_debugMode = m_debugMode & (~btIDebugDraw::DBG_EnableCCD); else m_debugMode |= btIDebugDraw::DBG_EnableCCD; break; } case '.': { shootBox(getRayTo(x,y));//getCameraTargetPosition()); break; } case '+': { m_ShootBoxInitialSpeed += 10.f; break; } case '-': { m_ShootBoxInitialSpeed -= 10.f; break; } default: // std::cout << "unused key : " << key << std::endl; break; } if (getDynamicsWorld() && getDynamicsWorld()->getDebugDrawer()) getDynamicsWorld()->getDebugDrawer()->setDebugMode(m_debugMode); }
void RagdollDemo::initPhysics() { if (create_draw_file) { draw_fh.open(DRAW_FILE); } std::ifstream input_stream; input_stream.open(TARGET_FILENAME); input_stream >> target_a_sx; input_stream >> target_b_sx; input_stream >> target_a_dx; input_stream >> target_b_dx; input_stream >> target_a_dz; input_stream >> target_b_dz; input_stream.close(); if (!keep_input_files) { if (remove(TARGET_FILENAME) != 0) { perror( "Error deleting file" ); } } srand((unsigned)time(NULL)); target_a_passed = false; target_b_passed = false; pause = false; ragdollDemo = this; frame_count = 0; brain = new Ctrnn(); brain->keep_input_files = keep_input_files; brain->load_genome(GENOME_FILE); //brain->print_weights(); //exit(1); for (int i = 0; i < NUM_RAYS; i++) { brain->set_sensor(i, &ray_sensors[i]); } // Setup the basic world #ifdef GRAPHICS setTexturing(true); setShadows(true); setCameraDistance(btScalar(5.)); #endif m_collisionConfiguration = new btDefaultCollisionConfiguration(); m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); btVector3 worldAabbMin(-10000,-10000,-10000); btVector3 worldAabbMax(10000,10000,10000); m_broadphase = new btAxisSweep3 (worldAabbMin, worldAabbMax); m_solver = new btSequentialImpulseConstraintSolver; m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration); #ifdef TEST_SPEED //agent = create_kinematic_box(MIN_X, BODY_ELEVATION, AGENT_Z, BODY_WIDTH, BODY_WIDTH, BODY_WIDTH); agent = create_kinematic_cylinder(MIN_X, BODY_ELEVATION, AGENT_Z, BODY_HEIGHT, BODY_WIDTH, 0, 0, 0); #else //agent = create_kinematic_box(0., BODY_ELEVATION, AGENT_Z, BODY_WIDTH, BODY_WIDTH, BODY_WIDTH); agent = create_kinematic_cylinder(0, BODY_ELEVATION, AGENT_Z, BODY_HEIGHT, BODY_WIDTH, 0, 0, 0); #endif //target_a = create_kinematic_box(target_a_sx, BODY_ELEVATION, TARGET_Z, BODY_WIDTH, BODY_WIDTH, BODY_WIDTH); target_a = create_kinematic_cylinder(target_a_sx, BODY_ELEVATION, TARGET_Z, BODY_HEIGHT, BODY_WIDTH, 0, 0, 0); //target_b = create_kinematic_box(target_b_sx, BODY_ELEVATION, TARGET_Z, BODY_WIDTH, BODY_WIDTH, BODY_WIDTH); target_b = create_kinematic_cylinder(target_b_sx, BODY_ELEVATION, TARGET_Z, BODY_HEIGHT, BODY_WIDTH, 0, 0, 0); #ifdef GRAPHICS clientResetScene(); //m_azi = 0; m_ele = 100; m_cameraDistance = 130; updateCamera(); #endif }
void Box2dDemo::initPhysics() { m_dialogDynamicsWorld = new GL_DialogDynamicsWorld(); //m_dialogDynamicsWorld->createDialog(100,110,200,50); //m_dialogDynamicsWorld->createDialog(100,00,100,100); //m_dialogDynamicsWorld->createDialog(0,0,100,100); GL_DialogWindow* settings = m_dialogDynamicsWorld->createDialog(50,0,200,120,"Settings"); GL_ToggleControl* toggle = m_dialogDynamicsWorld->createToggle(settings,"Toggle 1"); toggle = m_dialogDynamicsWorld->createToggle(settings,"Toggle 2"); toggle ->m_active = true; toggle = m_dialogDynamicsWorld->createToggle(settings,"Toggle 3"); //GL_SliderControl* slider = m_dialogDynamicsWorld->createSlider(settings,"Slider"); GL_DialogWindow* dialog = m_dialogDynamicsWorld->createDialog(0,200,420,300,"Help"); GL_TextControl* txt = new GL_TextControl; dialog->addControl(txt); txt->m_textLines.push_back("Mouse to move"); txt->m_textLines.push_back("Test 2"); txt->m_textLines.push_back("mouse to interact"); txt->m_textLines.push_back("ALT + mouse to move camera"); txt->m_textLines.push_back("space to reset"); txt->m_textLines.push_back("cursor keys and z,x to navigate"); txt->m_textLines.push_back("i to toggle simulation, s single step"); txt->m_textLines.push_back("q to quit"); txt->m_textLines.push_back(". to shoot box"); txt->m_textLines.push_back("d to toggle deactivation"); txt->m_textLines.push_back("g to toggle mesh animation (ConcaveDemo)"); txt->m_textLines.push_back("h to toggle help text"); txt->m_textLines.push_back("o to toggle orthogonal/perspective view"); //txt->m_textLines.push_back("+- shooting speed = %10.2f",m_ShootBoxInitialSpeed); setTexturing(true); setShadows(true); setCameraDistance(btScalar(SCALING*50.)); m_cameraTargetPosition.setValue(0,0,0);//0, ARRAY_SIZE_Y, 0); ///collision configuration contains default setup for memory, collision setup m_collisionConfiguration = new btDefaultCollisionConfiguration(); //m_collisionConfiguration->setConvexConvexMultipointIterations(); ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded) m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); btVoronoiSimplexSolver* simplex = new btVoronoiSimplexSolver(); btMinkowskiPenetrationDepthSolver* pdSolver = new btMinkowskiPenetrationDepthSolver(); btConvex2dConvex2dAlgorithm::CreateFunc* convexAlgo2d = new btConvex2dConvex2dAlgorithm::CreateFunc(simplex,pdSolver); m_dispatcher->registerCollisionCreateFunc(CONVEX_2D_SHAPE_PROXYTYPE,CONVEX_2D_SHAPE_PROXYTYPE,convexAlgo2d); m_dispatcher->registerCollisionCreateFunc(BOX_2D_SHAPE_PROXYTYPE,CONVEX_2D_SHAPE_PROXYTYPE,convexAlgo2d); m_dispatcher->registerCollisionCreateFunc(CONVEX_2D_SHAPE_PROXYTYPE,BOX_2D_SHAPE_PROXYTYPE,convexAlgo2d); m_dispatcher->registerCollisionCreateFunc(BOX_2D_SHAPE_PROXYTYPE,BOX_2D_SHAPE_PROXYTYPE,new btBox2dBox2dCollisionAlgorithm::CreateFunc()); m_broadphase = new btDbvtBroadphase(); //m_broadphase = new btSimpleBroadphase(); ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded) btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver; m_solver = sol; m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration); //m_dynamicsWorld->getSolverInfo().m_erp = 1.f; //m_dynamicsWorld->getSolverInfo().m_numIterations = 4; m_dynamicsWorld->setGravity(btVector3(0,-10,0)); ///create a few basic rigid bodies btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(150.),btScalar(50.),btScalar(150.))); // btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),50); m_collisionShapes.push_back(groundShape); btTransform groundTransform; groundTransform.setIdentity(); groundTransform.setOrigin(btVector3(0,-43,0)); //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here: { btScalar mass(0.); //rigidbody is dynamic if and only if mass is non zero, otherwise static bool isDynamic = (mass != 0.f); btVector3 localInertia(0,0,0); if (isDynamic) groundShape->calculateLocalInertia(mass,localInertia); //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform); btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia); btRigidBody* body = new btRigidBody(rbInfo); //add the body to the dynamics world m_dynamicsWorld->addRigidBody(body); } { //create a few dynamic rigidbodies // Re-using the same collision is better for memory usage and performance btScalar u= btScalar(1*SCALING-0.04); btVector3 points[3] = {btVector3(0,u,0),btVector3(-u,-u,0),btVector3(u,-u,0)}; btConvexShape* childShape0 = new btBoxShape(btVector3(btScalar(SCALING*1),btScalar(SCALING*1),btScalar(0.04))); btConvexShape* colShape= new btConvex2dShape(childShape0); //btCollisionShape* colShape = new btBox2dShape(btVector3(SCALING*1,SCALING*1,0.04)); btConvexShape* childShape1 = new btConvexHullShape(&points[0].getX(),3); btConvexShape* colShape2= new btConvex2dShape(childShape1); btConvexShape* childShape2 = new btCylinderShapeZ(btVector3(btScalar(SCALING*1),btScalar(SCALING*1),btScalar(0.04))); btConvexShape* colShape3= new btConvex2dShape(childShape2); m_collisionShapes.push_back(colShape); m_collisionShapes.push_back(colShape2); m_collisionShapes.push_back(colShape3); m_collisionShapes.push_back(childShape0); m_collisionShapes.push_back(childShape1); m_collisionShapes.push_back(childShape2); //btUniformScalingShape* colShape = new btUniformScalingShape(convexColShape,1.f); colShape->setMargin(btScalar(0.03)); //btCollisionShape* colShape = new btSphereShape(btScalar(1.)); /// Create Dynamic Objects btTransform startTransform; startTransform.setIdentity(); btScalar mass(1.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 (isDynamic) colShape->calculateLocalInertia(mass,localInertia); // float start_x = START_POS_X - ARRAY_SIZE_X/2; // float start_y = START_POS_Y; // float start_z = START_POS_Z - ARRAY_SIZE_Z/2; btVector3 x(-ARRAY_SIZE_X, 8.0f,-20.f); btVector3 y; btVector3 deltaX(SCALING*1, SCALING*2,0.f); btVector3 deltaY(SCALING*2, 0.0f,0.f); for (int i = 0; i < ARRAY_SIZE_X; ++i) { y = x; for (int j = i; j < ARRAY_SIZE_Y; ++j) { startTransform.setOrigin(y-btVector3(-10,0,0)); //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform); btRigidBody::btRigidBodyConstructionInfo rbInfo(0,0,0); switch (j%3) { #if 1 case 0: rbInfo = btRigidBody::btRigidBodyConstructionInfo(mass,myMotionState,colShape,localInertia); break; case 1: rbInfo = btRigidBody::btRigidBodyConstructionInfo(mass,myMotionState,colShape3,localInertia); break; #endif default: rbInfo = btRigidBody::btRigidBodyConstructionInfo(mass,myMotionState,colShape2,localInertia); } btRigidBody* body = new btRigidBody(rbInfo); //body->setContactProcessingThreshold(colShape->getContactBreakingThreshold()); body->setActivationState(ISLAND_SLEEPING); body->setLinearFactor(btVector3(1,1,0)); body->setAngularFactor(btVector3(0,0,1)); m_dynamicsWorld->addRigidBody(body); body->setActivationState(ISLAND_SLEEPING); // y += -0.8*deltaY; y += deltaY; } x += deltaX; } } clientResetScene(); }
void DefracDemo::initPhysics() { m_dispatcher=0; m_collisionConfiguration = new btDefaultCollisionConfiguration(); m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); btVector3 worldAabbMin(-1000,-1000,-1000); btVector3 worldAabbMax(1000,1000,1000); m_broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies); btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver(); m_solver = solver; btDiscreteDynamicsWorld* world = new btDefracDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration); m_dynamicsWorld = world; m_dynamicsWorld->setGravity(btVector3(0,-10,0)); ///create concave ground mesh m_azi = 0; btCollisionShape* groundShape = 0; { int i; int j; const int NUM_VERTS_X = 30; const int NUM_VERTS_Y = 30; const int totalVerts = NUM_VERTS_X*NUM_VERTS_Y; const int totalTriangles = 2*(NUM_VERTS_X-1)*(NUM_VERTS_Y-1); gGroundVertices = new btVector3[totalVerts]; gGroundIndices = new int[totalTriangles*3]; btScalar offset(-50); for ( i=0;i<NUM_VERTS_X;i++) { for (j=0;j<NUM_VERTS_Y;j++) { gGroundVertices[i+j*NUM_VERTS_X].setValue((i-NUM_VERTS_X*0.5f)*TRIANGLE_SIZE, //0.f, waveheight*sinf((float)i)*cosf((float)j+offset), (j-NUM_VERTS_Y*0.5f)*TRIANGLE_SIZE); } } int vertStride = sizeof(btVector3); int indexStride = 3*sizeof(int); int index=0; for ( i=0;i<NUM_VERTS_X-1;i++) { for (int j=0;j<NUM_VERTS_Y-1;j++) { gGroundIndices[index++] = j*NUM_VERTS_X+i; gGroundIndices[index++] = j*NUM_VERTS_X+i+1; gGroundIndices[index++] = (j+1)*NUM_VERTS_X+i+1; gGroundIndices[index++] = j*NUM_VERTS_X+i; gGroundIndices[index++] = (j+1)*NUM_VERTS_X+i+1; gGroundIndices[index++] = (j+1)*NUM_VERTS_X+i; } } btTriangleIndexVertexArray* indexVertexArrays = new btTriangleIndexVertexArray(totalTriangles, gGroundIndices, indexStride, totalVerts,(btScalar*) &gGroundVertices[0].x(),vertStride); bool useQuantizedAabbCompression = true; groundShape = new btBvhTriangleMeshShape(indexVertexArrays,useQuantizedAabbCompression); groundShape->setMargin(0.5); } m_collisionShapes.push_back(groundShape); btCollisionShape* groundBox = new btBoxShape (btVector3(100,CUBE_HALF_EXTENTS,100)); m_collisionShapes.push_back(groundBox); btTransform tr; tr.setIdentity(); tr.setOrigin(btVector3(0,-30.f,0)); localCreateRigidBody(0, tr, groundBox); // clientResetScene(); initDemo(); clientResetScene(); }
int main(int argc,char** argv) { int i; for (i=0;i<numObjects;i++) { if (i>0) { shapePtr[i] = prebuildShapePtr[1]; shapeIndex[i] = 1;//sphere } else { shapeIndex[i] = 0; shapePtr[i] = prebuildShapePtr[0]; } } ConvexDecomposition::WavefrontObj wo; char* filename = "file.obj"; tcount = wo.loadObj(filename); class MyConvexDecomposition : public ConvexDecomposition::ConvexDecompInterface { public: MyConvexDecomposition (FILE* outputFile) :mBaseCount(0), mHullCount(0), mOutputFile(outputFile) { } virtual void ConvexDecompResult(ConvexDecomposition::ConvexResult &result) { TriangleMesh* trimesh = new TriangleMesh(); SimdVector3 localScaling(6.f,6.f,6.f); //export data to .obj printf("ConvexResult\n"); if (mOutputFile) { fprintf(mOutputFile,"## Hull Piece %d with %d vertices and %d triangles.\r\n", mHullCount, result.mHullVcount, result.mHullTcount ); fprintf(mOutputFile,"usemtl Material%i\r\n",mBaseCount); fprintf(mOutputFile,"o Object%i\r\n",mBaseCount); for (unsigned int i=0; i<result.mHullVcount; i++) { const float *p = &result.mHullVertices[i*3]; fprintf(mOutputFile,"v %0.9f %0.9f %0.9f\r\n", p[0], p[1], p[2] ); } //calc centroid, to shift vertices around center of mass centroids[numObjects] = SimdVector3(0,0,0); if ( 1 ) { const unsigned int *src = result.mHullIndices; for (unsigned int i=0; i<result.mHullTcount; i++) { unsigned int index0 = *src++; unsigned int index1 = *src++; unsigned int index2 = *src++; SimdVector3 vertex0(result.mHullVertices[index0*3], result.mHullVertices[index0*3+1],result.mHullVertices[index0*3+2]); SimdVector3 vertex1(result.mHullVertices[index1*3], result.mHullVertices[index1*3+1],result.mHullVertices[index1*3+2]); SimdVector3 vertex2(result.mHullVertices[index2*3], result.mHullVertices[index2*3+1],result.mHullVertices[index2*3+2]); vertex0 *= localScaling; vertex1 *= localScaling; vertex2 *= localScaling; centroids[numObjects] += vertex0; centroids[numObjects]+= vertex1; centroids[numObjects]+= vertex2; } } centroids[numObjects] *= 1.f/(float(result.mHullTcount) * 3); if ( 1 ) { const unsigned int *src = result.mHullIndices; for (unsigned int i=0; i<result.mHullTcount; i++) { unsigned int index0 = *src++; unsigned int index1 = *src++; unsigned int index2 = *src++; SimdVector3 vertex0(result.mHullVertices[index0*3], result.mHullVertices[index0*3+1],result.mHullVertices[index0*3+2]); SimdVector3 vertex1(result.mHullVertices[index1*3], result.mHullVertices[index1*3+1],result.mHullVertices[index1*3+2]); SimdVector3 vertex2(result.mHullVertices[index2*3], result.mHullVertices[index2*3+1],result.mHullVertices[index2*3+2]); vertex0 *= localScaling; vertex1 *= localScaling; vertex2 *= localScaling; vertex0 -= centroids[numObjects]; vertex1 -= centroids[numObjects]; vertex2 -= centroids[numObjects]; trimesh->AddTriangle(vertex0,vertex1,vertex2); index0+=mBaseCount; index1+=mBaseCount; index2+=mBaseCount; fprintf(mOutputFile,"f %d %d %d\r\n", index0+1, index1+1, index2+1 ); } } shapeIndex[numObjects] = numObjects; shapePtr[numObjects++] = new ConvexTriangleMeshShape(trimesh); mBaseCount+=result.mHullVcount; // advance the 'base index' counter. } } int mBaseCount; int mHullCount; FILE* mOutputFile; }; if (tcount) { numObjects = 1; //always have the ground object first TriangleMesh* trimesh = new TriangleMesh(); SimdVector3 localScaling(6.f,6.f,6.f); for (int i=0;i<wo.mTriCount;i++) { int index0 = wo.mIndices[i*3]; int index1 = wo.mIndices[i*3+1]; int index2 = wo.mIndices[i*3+2]; SimdVector3 vertex0(wo.mVertices[index0*3], wo.mVertices[index0*3+1],wo.mVertices[index0*3+2]); SimdVector3 vertex1(wo.mVertices[index1*3], wo.mVertices[index1*3+1],wo.mVertices[index1*3+2]); SimdVector3 vertex2(wo.mVertices[index2*3], wo.mVertices[index2*3+1],wo.mVertices[index2*3+2]); vertex0 *= localScaling; vertex1 *= localScaling; vertex2 *= localScaling; trimesh->AddTriangle(vertex0,vertex1,vertex2); } shapePtr[numObjects++] = new ConvexTriangleMeshShape(trimesh); } if (tcount) { char outputFileName[512]; strcpy(outputFileName,filename); char *dot = strstr(outputFileName,"."); if ( dot ) *dot = 0; strcat(outputFileName,"_convex.obj"); FILE* outputFile = fopen(outputFileName,"wb"); unsigned int depth = 7; float cpercent = 5; float ppercent = 15; unsigned int maxv = 16; float skinWidth = 0.01; printf("WavefrontObj num triangles read %i",tcount); ConvexDecomposition::DecompDesc desc; desc.mVcount = wo.mVertexCount; desc.mVertices = wo.mVertices; desc.mTcount = wo.mTriCount; desc.mIndices = (unsigned int *)wo.mIndices; desc.mDepth = depth; desc.mCpercent = cpercent; desc.mPpercent = ppercent; desc.mMaxVertices = maxv; desc.mSkinWidth = skinWidth; MyConvexDecomposition convexDecomposition(outputFile); desc.mCallback = &convexDecomposition; //convexDecomposition.performConvexDecomposition(desc); ConvexBuilder cb(desc.mCallback); int ret = cb.process(desc); if (outputFile) fclose(outputFile); } CollisionDispatcher* dispatcher = new CollisionDispatcher(); SimdVector3 worldAabbMin(-10000,-10000,-10000); SimdVector3 worldAabbMax(10000,10000,10000); OverlappingPairCache* broadphase = new AxisSweep3(worldAabbMin,worldAabbMax); //OverlappingPairCache* broadphase = new SimpleBroadphase(); physicsEnvironmentPtr = new CcdPhysicsEnvironment(dispatcher,broadphase); physicsEnvironmentPtr->setDeactivationTime(2.f); physicsEnvironmentPtr->setGravity(0,-10,0); PHY_ShapeProps shapeProps; shapeProps.m_do_anisotropic = false; shapeProps.m_do_fh = false; shapeProps.m_do_rot_fh = false; shapeProps.m_friction_scaling[0] = 1.; shapeProps.m_friction_scaling[1] = 1.; shapeProps.m_friction_scaling[2] = 1.; shapeProps.m_inertia = 1.f; shapeProps.m_lin_drag = 0.2f; shapeProps.m_ang_drag = 0.1f; shapeProps.m_mass = 10.0f; PHY_MaterialProps materialProps; materialProps.m_friction = 10.5f; materialProps.m_restitution = 0.0f; CcdConstructionInfo ccdObjectCi; ccdObjectCi.m_friction = 0.5f; ccdObjectCi.m_linearDamping = shapeProps.m_lin_drag; ccdObjectCi.m_angularDamping = shapeProps.m_ang_drag; SimdTransform tr; tr.setIdentity(); for (i=0;i<numObjects;i++) { shapeProps.m_shape = shapePtr[shapeIndex[i]]; shapeProps.m_shape->SetMargin(0.05f); bool isDyna = i>0; //if (i==1) // isDyna=false; if (0)//i==1) { SimdQuaternion orn(0,0,0.1*SIMD_HALF_PI); ms[i].setWorldOrientation(orn.x(),orn.y(),orn.z(),orn[3]); } if (i>0) { switch (i) { case 1: { ms[i].setWorldPosition(0,10,0); //for testing, rotate the ground cube so the stack has to recover a bit break; } case 2: { ms[i].setWorldPosition(0,8,2); break; } default: ms[i].setWorldPosition(0,i*CUBE_HALF_EXTENTS*2 - CUBE_HALF_EXTENTS,0); } float quatIma0,quatIma1,quatIma2,quatReal; SimdQuaternion quat; SimdVector3 axis(0,0,1); SimdScalar angle=0.5f; quat.setRotation(axis,angle); ms[i].setWorldOrientation(quat.getX(),quat.getY(),quat.getZ(),quat[3]); } else { ms[i].setWorldPosition(0,-10+EXTRA_HEIGHT,0); } ccdObjectCi.m_MotionState = &ms[i]; ccdObjectCi.m_gravity = SimdVector3(0,0,0); ccdObjectCi.m_localInertiaTensor =SimdVector3(0,0,0); if (!isDyna) { shapeProps.m_mass = 0.f; ccdObjectCi.m_mass = shapeProps.m_mass; ccdObjectCi.m_collisionFlags = CollisionObject::isStatic; } else { shapeProps.m_mass = 1.f; ccdObjectCi.m_mass = shapeProps.m_mass; ccdObjectCi.m_collisionFlags = 0; } SimdVector3 localInertia; if (shapePtr[shapeIndex[i]]->GetShapeType() == EMPTY_SHAPE_PROXYTYPE) { //take inertia from first shape shapePtr[1]->CalculateLocalInertia(shapeProps.m_mass,localInertia); } else { shapePtr[shapeIndex[i]]->CalculateLocalInertia(shapeProps.m_mass,localInertia); } ccdObjectCi.m_localInertiaTensor = localInertia; ccdObjectCi.m_collisionShape = shapePtr[shapeIndex[i]]; physObjects[i]= new CcdPhysicsController( ccdObjectCi); // Only do CCD if motion in one timestep (1.f/60.f) exceeds CUBE_HALF_EXTENTS physObjects[i]->GetRigidBody()->m_ccdSquareMotionTreshold = CUBE_HALF_EXTENTS; //Experimental: better estimation of CCD Time of Impact: //physObjects[i]->GetRigidBody()->m_ccdSweptShereRadius = 0.5*CUBE_HALF_EXTENTS; physicsEnvironmentPtr->addCcdPhysicsController( physObjects[i]); if (i==1) { //physObjects[i]->SetAngularVelocity(0,0,-2,true); } physicsEnvironmentPtr->setDebugDrawer(&debugDrawer); } //create a constraint if (createConstraint) { //physObjects[i]->SetAngularVelocity(0,0,-2,true); int constraintId; float pivotX=CUBE_HALF_EXTENTS, pivotY=-CUBE_HALF_EXTENTS, pivotZ=CUBE_HALF_EXTENTS; float axisX=1,axisY=0,axisZ=0; HingeConstraint* hinge = 0; SimdVector3 pivotInA(CUBE_HALF_EXTENTS,-CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS); SimdVector3 pivotInB(-CUBE_HALF_EXTENTS,-CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS); SimdVector3 axisInA(0,1,0); SimdVector3 axisInB(0,-1,0); RigidBody* rb0 = physObjects[1]->GetRigidBody(); RigidBody* rb1 = physObjects[2]->GetRigidBody(); hinge = new HingeConstraint( *rb0, *rb1,pivotInA,pivotInB,axisInA,axisInB); physicsEnvironmentPtr->m_constraints.push_back(hinge); hinge->SetUserConstraintId(100); hinge->SetUserConstraintType(PHY_LINEHINGE_CONSTRAINT); } clientResetScene(); setCameraDistance(26.f); return glutmain(argc, argv,640,480,"Bullet Physics Demo. http://www.continuousphysics.com/Bullet/phpBB2/"); }
void ParticlesDemo::initPhysics() { setTexturing(false); setShadows(false); // setCameraDistance(80.f); setCameraDistance(3.0f); // m_cameraTargetPosition.setValue(50, 10, 0); m_cameraTargetPosition.setValue(0, 0, 0); // m_azi = btScalar(0.f); // m_ele = btScalar(0.f); m_azi = btScalar(45.f); m_ele = btScalar(30.f); setFrustumZPlanes(0.1f, 10.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_pairCache = new (btAlignedAlloc(sizeof(btHashedOverlappingPairCache),16))btHashedOverlappingPairCache(); // m_broadphase = new btDbvtBroadphase(m_pairCache); m_broadphase = new btNullBroadphase(); ///the default constraint solver m_solver = new btSequentialImpulseConstraintSolver(); m_pWorld = new btParticlesDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration, 65536); m_dialogDynamicsWorld = new GL_DialogDynamicsWorld(); GL_DialogWindow* settings = m_dialogDynamicsWorld->createDialog(50,0,280,280,"CPU fallback"); m_pWorld->m_useCpuControls[0] = 0; GL_ToggleControl* ctrl = 0; m_pWorld->m_useCpuControls[SIMSTAGE_INTEGRATE_MOTION] = m_dialogDynamicsWorld->createToggle(settings,"Integrate Motion"); m_pWorld->m_useCpuControls[SIMSTAGE_COMPUTE_CELL_ID] = m_dialogDynamicsWorld->createToggle(settings,"Compute Cell ID"); m_pWorld->m_useCpuControls[SIMSTAGE_SORT_CELL_ID] = m_dialogDynamicsWorld->createToggle(settings,"Sort Cell ID"); m_pWorld->m_useCpuControls[SIMSTAGE_FIND_CELL_START] = m_dialogDynamicsWorld->createToggle(settings,"Find Cell Start"); m_pWorld->m_useCpuControls[SIMSTAGE_COLLIDE_PARTICLES] = m_dialogDynamicsWorld->createToggle(settings,"Collide Particles"); for(int i = 1; i < SIMSTAGE_TOTAL; i++) { m_pWorld->m_useCpuControls[i]->m_active = false; } #if defined(CL_PLATFORM_MINI_CL) // these kernels use barrier() m_pWorld->m_useCpuControls[SIMSTAGE_SORT_CELL_ID]->m_active = true; m_pWorld->m_useCpuControls[SIMSTAGE_FIND_CELL_START]->m_active = true; #endif #if defined(CL_PLATFORM_AMD) // these kernels use barrier() m_pWorld->m_useCpuControls[SIMSTAGE_SORT_CELL_ID]->m_active = true; m_pWorld->m_useCpuControls[SIMSTAGE_FIND_CELL_START]->m_active = true; #endif m_dynamicsWorld = m_pWorld; m_pWorld->getSimulationIslandManager()->setSplitIslands(true); m_pWorld->setGravity(btVector3(0,-10.,0)); m_pWorld->getSolverInfo().m_numIterations = 4; { // btCollisionShape* colShape = new btSphereShape(btScalar(1.0f)); /* btCollisionShape* colShape = new btSphereShape(DEF_PARTICLE_RADIUS); m_collisionShapes.push_back(colShape); btTransform startTransform; startTransform.setIdentity(); btScalar mass(1.f); btVector3 localInertia(0,0,0); colShape->calculateLocalInertia(mass,localInertia); float start_x = START_POS_X - ARRAY_SIZE_X * DIST * btScalar(0.5f); float start_y = START_POS_Y - ARRAY_SIZE_Y * DIST * btScalar(0.5f); float start_z = START_POS_Z - ARRAY_SIZE_Z * DIST * btScalar(0.5f); startTransform.setOrigin(btVector3(start_x, start_y, start_z)); btRigidBodyConstructionInfo rbInfo(mass,0,colShape,localInertia); rbInfo.m_startWorldTransform = startTransform; btRigidBody* body = new btRigidBody(rbInfo); m_pWorld->addRigidBody(body); */ init_scene_directly(); } clientResetScene(); m_pWorld->initDeviceData(); }
void VehicleDemo::initPhysics() { #ifdef FORCE_ZAXIS_UP m_cameraUp = btVector3(0,0,1); m_forwardAxis = 1; #endif btCollisionShape* groundShape = new btBoxShape(btVector3(50,3,50)); m_collisionShapes.push_back(groundShape); m_collisionConfiguration = new btDefaultCollisionConfiguration(); m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); btVector3 worldMin(-1000,-1000,-1000); btVector3 worldMax(1000,1000,1000); m_overlappingPairCache = new btAxisSweep3(worldMin,worldMax); m_constraintSolver = new btSequentialImpulseConstraintSolver(); m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_constraintSolver,m_collisionConfiguration); #ifdef FORCE_ZAXIS_UP m_dynamicsWorld->setGravity(btVector3(0,0,-10)); #endif //m_dynamicsWorld->setGravity(btVector3(0,0,0)); btTransform tr; tr.setIdentity(); //either use heightfield or triangle mesh //#define USE_TRIMESH_GROUND 1 #ifdef USE_TRIMESH_GROUND int i; const float TRIANGLE_SIZE=20.f; //create a triangle-mesh ground int vertStride = sizeof(btVector3); int indexStride = 3*sizeof(int); const int NUM_VERTS_X = 20; const int NUM_VERTS_Y = 20; const int totalVerts = NUM_VERTS_X*NUM_VERTS_Y; const int totalTriangles = 2*(NUM_VERTS_X-1)*(NUM_VERTS_Y-1); m_vertices = new btVector3[totalVerts]; int* gIndices = new int[totalTriangles*3]; for ( i=0;i<NUM_VERTS_X;i++) { for (int j=0;j<NUM_VERTS_Y;j++) { float wl = .2f; //height set to zero, but can also use curved landscape, just uncomment out the code float height = 0.f;//20.f*sinf(float(i)*wl)*cosf(float(j)*wl); #ifdef FORCE_ZAXIS_UP m_vertices[i+j*NUM_VERTS_X].setValue( (i-NUM_VERTS_X*0.5f)*TRIANGLE_SIZE, (j-NUM_VERTS_Y*0.5f)*TRIANGLE_SIZE, height ); #else m_vertices[i+j*NUM_VERTS_X].setValue( (i-NUM_VERTS_X*0.5f)*TRIANGLE_SIZE, height, (j-NUM_VERTS_Y*0.5f)*TRIANGLE_SIZE); #endif } } int index=0; for ( i=0;i<NUM_VERTS_X-1;i++) { for (int j=0;j<NUM_VERTS_Y-1;j++) { gIndices[index++] = j*NUM_VERTS_X+i; gIndices[index++] = j*NUM_VERTS_X+i+1; gIndices[index++] = (j+1)*NUM_VERTS_X+i+1; gIndices[index++] = j*NUM_VERTS_X+i; gIndices[index++] = (j+1)*NUM_VERTS_X+i+1; gIndices[index++] = (j+1)*NUM_VERTS_X+i; } } m_indexVertexArrays = new btTriangleIndexVertexArray(totalTriangles, gIndices, indexStride, totalVerts,(btScalar*) &m_vertices[0].x(),vertStride); bool useQuantizedAabbCompression = true; groundShape = new btBvhTriangleMeshShape(m_indexVertexArrays,useQuantizedAabbCompression); tr.setOrigin(btVector3(0,-4.5f,0)); #else //testing btHeightfieldTerrainShape int width=128; int length=128; #ifdef LOAD_FROM_FILE unsigned char* heightfieldData = new unsigned char[width*length]; { for (int i=0;i<width*length;i++) { heightfieldData[i]=0; } } char* filename="heightfield128x128.raw"; FILE* heightfieldFile = fopen(filename,"r"); if (!heightfieldFile) { filename="../../heightfield128x128.raw"; heightfieldFile = fopen(filename,"r"); } if (heightfieldFile) { int numBytes =fread(heightfieldData,1,width*length,heightfieldFile); //btAssert(numBytes); if (!numBytes) { printf("couldn't read heightfield at %s\n",filename); } fclose (heightfieldFile); } #else char* heightfieldData = MyHeightfield; #endif //btScalar maxHeight = 20000.f;//exposes a bug btScalar maxHeight = 100; bool useFloatDatam=false; bool flipQuadEdges=false; btHeightfieldTerrainShape* heightFieldShape = new btHeightfieldTerrainShape(width,length,heightfieldData,maxHeight,upIndex,useFloatDatam,flipQuadEdges);; btVector3 mmin,mmax; heightFieldShape->getAabb(btTransform::getIdentity(),mmin,mmax); groundShape = heightFieldShape; heightFieldShape->setUseDiamondSubdivision(true); btVector3 localScaling(100,1,100); localScaling[upIndex]=1.f; groundShape->setLocalScaling(localScaling); //tr.setOrigin(btVector3(0,9940,0)); tr.setOrigin(btVector3(0,49.4,0)); #endif // m_collisionShapes.push_back(groundShape); //create ground object localCreateRigidBody(0,tr,groundShape); tr.setOrigin(btVector3(0,0,0));//-64.5f,0)); #ifdef FORCE_ZAXIS_UP // indexRightAxis = 0; // indexUpAxis = 2; // indexForwardAxis = 1; btCollisionShape* chassisShape = new btBoxShape(btVector3(1.f,2.f, 0.5f)); btCompoundShape* compound = new btCompoundShape(); btTransform localTrans; localTrans.setIdentity(); //localTrans effectively shifts the center of mass with respect to the chassis localTrans.setOrigin(btVector3(0,0,1)); #else btCollisionShape* chassisShape = new btBoxShape(btVector3(1.f,0.5f,2.f)); m_collisionShapes.push_back(chassisShape); btCompoundShape* compound = new btCompoundShape(); m_collisionShapes.push_back(compound); btTransform localTrans; localTrans.setIdentity(); //localTrans effectively shifts the center of mass with respect to the chassis localTrans.setOrigin(btVector3(0,1,0)); #endif compound->addChildShape(localTrans,chassisShape); tr.setOrigin(btVector3(0,0.f,0)); m_carChassis = localCreateRigidBody(800,tr,compound);//chassisShape); //m_carChassis->setDamping(0.2,0.2); m_wheelShape = new btCylinderShapeX(btVector3(wheelWidth,wheelRadius,wheelRadius)); clientResetScene(); /// create vehicle { m_vehicleRayCaster = new btDefaultVehicleRaycaster(m_dynamicsWorld); m_vehicle = new btRaycastVehicle(m_tuning,m_carChassis,m_vehicleRayCaster); ///never deactivate the vehicle m_carChassis->setActivationState(DISABLE_DEACTIVATION); m_dynamicsWorld->addVehicle(m_vehicle); float connectionHeight = 1.2f; bool isFrontWheel=true; //choose coordinate system m_vehicle->setCoordinateSystem(rightIndex,upIndex,forwardIndex); #ifdef FORCE_ZAXIS_UP btVector3 connectionPointCS0(CUBE_HALF_EXTENTS-(0.3*wheelWidth),2*CUBE_HALF_EXTENTS-wheelRadius, connectionHeight); #else btVector3 connectionPointCS0(CUBE_HALF_EXTENTS-(0.3*wheelWidth),connectionHeight,2*CUBE_HALF_EXTENTS-wheelRadius); #endif m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel); #ifdef FORCE_ZAXIS_UP connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),2*CUBE_HALF_EXTENTS-wheelRadius, connectionHeight); #else connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),connectionHeight,2*CUBE_HALF_EXTENTS-wheelRadius); #endif m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel); #ifdef FORCE_ZAXIS_UP connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),-2*CUBE_HALF_EXTENTS+wheelRadius, connectionHeight); #else connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),connectionHeight,-2*CUBE_HALF_EXTENTS+wheelRadius); #endif //FORCE_ZAXIS_UP isFrontWheel = false; m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel); #ifdef FORCE_ZAXIS_UP connectionPointCS0 = btVector3(CUBE_HALF_EXTENTS-(0.3*wheelWidth),-2*CUBE_HALF_EXTENTS+wheelRadius, connectionHeight); #else connectionPointCS0 = btVector3(CUBE_HALF_EXTENTS-(0.3*wheelWidth),connectionHeight,-2*CUBE_HALF_EXTENTS+wheelRadius); #endif m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel); for (int i=0;i<m_vehicle->getNumWheels();i++) { btWheelInfo& wheel = m_vehicle->getWheelInfo(i); wheel.m_suspensionStiffness = suspensionStiffness; wheel.m_wheelsDampingRelaxation = suspensionDamping; wheel.m_wheelsDampingCompression = suspensionCompression; wheel.m_frictionSlip = wheelFriction; wheel.m_rollInfluence = rollInfluence; } } setCameraDistance(26.f); }
void CharacterDemo::initPhysics() { btCollisionShape* groundShape = new btBoxShape(btVector3(50,3,50)); m_collisionShapes.push_back(groundShape); m_collisionConfiguration = new btDefaultCollisionConfiguration(); m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); btVector3 worldMin(-1000,-1000,-1000); btVector3 worldMax(1000,1000,1000); btAxisSweep3* sweepBP = new btAxisSweep3(worldMin,worldMax); m_overlappingPairCache = sweepBP; m_constraintSolver = new btSequentialImpulseConstraintSolver(); m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_constraintSolver,m_collisionConfiguration); m_dynamicsWorld->getDispatchInfo().m_allowedCcdPenetration=0.0001f; #ifdef DYNAMIC_CHARACTER_CONTROLLER m_character = new DynamicCharacterController (); #else btTransform startTransform; startTransform.setIdentity (); //startTransform.setOrigin (btVector3(0.0, 4.0, 0.0)); startTransform.setOrigin (btVector3(10.210098,-1.6433364,16.453260)); m_ghostObject = new btPairCachingGhostObject(); m_ghostObject->setWorldTransform(startTransform); sweepBP->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback()); btScalar characterHeight=1.75; btScalar characterWidth =1.75; btConvexShape* capsule = new btCapsuleShape(characterWidth,characterHeight); m_ghostObject->setCollisionShape (capsule); m_ghostObject->setCollisionFlags (btCollisionObject::CF_CHARACTER_OBJECT); btScalar stepHeight = btScalar(0.35); m_character = new btKinematicCharacterController (m_ghostObject,capsule,stepHeight); #endif //////////////// /// Create some basic environment from a Quake level //m_dynamicsWorld->setGravity(btVector3(0,0,0)); btTransform tr; tr.setIdentity(); const char* bspfilename = "BspDemo.bsp"; void* memoryBuffer = 0; FILE* file = fopen(bspfilename,"r"); if (!file) { //cmake generated visual studio projects need 4 levels back bspfilename = "../../../../BspDemo.bsp"; file = fopen(bspfilename,"r"); } if (!file) { //visual studio leaves the current working directory in the projectfiles folder bspfilename = "../../BspDemo.bsp"; file = fopen(bspfilename,"r"); } if (!file) { //visual studio leaves the current working directory in the projectfiles folder bspfilename = "BspDemo.bsp"; file = fopen(bspfilename,"r"); } if (file) { BspLoader bspLoader; int size=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", bspfilename); } else { //how to detect file size? memoryBuffer = malloc(size+1); fread(memoryBuffer,1,size,file); bspLoader.loadBSPFile( memoryBuffer); BspToBulletConverter bsp2bullet(this); float bspScaling = 0.1f; bsp2bullet.convertBsp(bspLoader,bspScaling); } fclose(file); } ///only collide with static for now (no interaction with dynamic objects) m_dynamicsWorld->addCollisionObject(m_ghostObject,btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter); m_dynamicsWorld->addAction(m_character); /////////////// clientResetScene(); setCameraDistance(56.f); }
void BasicDemo::initPhysics() { setTexturing(true); setShadows(true); setCameraDistance(btScalar(SCALING*50.)); ///collision configuration contains default setup for memory, collision setup m_collisionConfiguration = new btDefaultCollisionConfiguration(); //m_collisionConfiguration->setConvexConvexMultipointIterations(); ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded) m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); m_broadphase = new btDbvtBroadphase(); ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded) btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver; m_solver = sol; m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration); m_dynamicsWorld->setGravity(btVector3(0,-10,0)); ///create a few basic rigid bodies btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.))); // btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),50); m_collisionShapes.push_back(groundShape); btTransform groundTransform; groundTransform.setIdentity(); groundTransform.setOrigin(btVector3(0,-50,0)); //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here: { btScalar mass(0.); //rigidbody is dynamic if and only if mass is non zero, otherwise static bool isDynamic = (mass != 0.f); btVector3 localInertia(0,0,0); if (isDynamic) groundShape->calculateLocalInertia(mass,localInertia); //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform); btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia); btRigidBody* body = new btRigidBody(rbInfo); //add the body to the dynamics world m_dynamicsWorld->addRigidBody(body); } { //create a few dynamic rigidbodies // Re-using the same collision is better for memory usage and performance btCollisionShape* colShape = new btBoxShape(btVector3(SCALING*1,SCALING*1,SCALING*1)); //btCollisionShape* colShape = new btSphereShape(btScalar(1.)); m_collisionShapes.push_back(colShape); /// Create Dynamic Objects btTransform startTransform; startTransform.setIdentity(); btScalar mass(1.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 (isDynamic) colShape->calculateLocalInertia(mass,localInertia); float start_x = START_POS_X - ARRAY_SIZE_X/2; float start_y = START_POS_Y; float start_z = START_POS_Z - ARRAY_SIZE_Z/2; 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++) { startTransform.setOrigin(SCALING*btVector3( btScalar(2.0*i + start_x), btScalar(20+2.0*k + start_y), btScalar(2.0*j + start_z))); //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform); btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia); btRigidBody* body = new btRigidBody(rbInfo); body->setActivationState(ISLAND_SLEEPING); m_dynamicsWorld->addRigidBody(body); body->setActivationState(ISLAND_SLEEPING); } } } } clientResetScene(); #ifdef TEST_SERIALIZATION //test serializing this int maxSerializeBufferSize = 1024*1024*5; btDefaultSerializer* serializer = new btDefaultSerializer(maxSerializeBufferSize); m_dynamicsWorld->serialize(serializer); FILE* f2 = fopen("testFile.bullet","wb"); fwrite(serializer->m_buffer,serializer->m_currentSize,1,f2); fclose(f2); #endif #if 0 bParse::btBulletFile* bulletFile2 = new bParse::btBulletFile("testFile.bullet"); bool ok = (bulletFile2->getFlags()& bParse::FD_OK)!=0; bool verboseDumpAllTypes = true; if (ok) bulletFile2->parse(verboseDumpAllTypes); if (verboseDumpAllTypes) { bulletFile2->dumpChunks(bulletFile2->getFileDNA()); } #endif //TEST_SERIALIZATION }
void BasicDemo::initPhysics() { #ifdef FORCE_ZAXIS_UP m_cameraUp = btVector3(0,0,1); m_forwardAxis = 1; #endif gContactAddedCallback = CustomMaterialCombinerCallback; setTexturing(true); setShadows(false); setCameraDistance(btScalar(SCALING*20.)); this->m_azi = 90; ///collision configuration contains default setup for memory, collision setup m_collisionConfiguration = new btDefaultCollisionConfiguration(); //m_collisionConfiguration->setConvexConvexMultipointIterations(); ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded) m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); m_broadphase = new btDbvtBroadphase(); ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded) btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver; m_solver = sol; m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration); #ifdef FORCE_ZAXIS_UP m_dynamicsWorld->setGravity(btVector3(0,0,-10)); #else m_dynamicsWorld->setGravity(btVector3(0,-10,0)); #endif m_dynamicsWorld->getSolverInfo().m_solverMode |= SOLVER_DISABLE_VELOCITY_DEPENDENT_FRICTION_DIRECTION+SOLVER_USE_2_FRICTION_DIRECTIONS; m_dynamicsWorld->getSolverInfo().m_solverMode |= SOLVER_ENABLE_FRICTION_DIRECTION_CACHING; #if 1 m_blendReader = new BasicBlendReader(m_dynamicsWorld,this); //const char* fileName = "clubsilo_packed.blend"; const char* fileName = "PhysicsAnimationBakingDemo.blend"; char fullPath[512]; if(!m_blendReader->openFile(fileName)) { sprintf(fullPath,"../../%s",fileName); m_blendReader->openFile(fullPath); } if (m_blendReader) { m_blendReader->convertAllObjects(); } else { printf("file not found\n"); } #endif ///create a few basic rigid bodies #if 0 btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.))); // btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),50); m_collisionShapes.push_back(groundShape); btTransform groundTransform; groundTransform.setIdentity(); groundTransform.setOrigin(btVector3(0,-60,0)); //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here: { btScalar mass(0.); //rigidbody is dynamic if and only if mass is non zero, otherwise static bool isDynamic = (mass != 0.f); btVector3 localInertia(0,0,0); if (isDynamic) groundShape->calculateLocalInertia(mass,localInertia); //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform); btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia); btRigidBody* body = new btRigidBody(rbInfo); //enable custom material callback body->setCollisionFlags(body->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK); //add the body to the dynamics world m_dynamicsWorld->addRigidBody(body); } #endif #if 0 { //create a few dynamic rigidbodies // Re-using the same collision is better for memory usage and performance //btCollisionShape* colShape = new btBoxShape(btVector3(SCALING*1,SCALING*1,SCALING*1)); //btCollisionShape* colShape = new btBoxShape(btVector3(SCALING*.1,SCALING*.1,SCALING*.1)); btCollisionShape* colShape = new btSphereShape(SCALING*btScalar(1.)); m_collisionShapes.push_back(colShape); /// Create Dynamic Objects btTransform startTransform; startTransform.setIdentity(); btScalar mass(1.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 (isDynamic) colShape->calculateLocalInertia(mass,localInertia); float start_x = -ARRAY_SIZE_X; float start_y = -ARRAY_SIZE_Y; float start_z = - ARRAY_SIZE_Z; 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++) { startTransform.setOrigin(1.*SCALING*btVector3( 2.0*i + start_x, 2.0*k + start_y, 2.0*j + start_z)); //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform); btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia); btRigidBody* body = new btRigidBody(rbInfo); //body->setContactProcessingThreshold(colShape->getContactBreakingThreshold()); body->setActivationState(ISLAND_SLEEPING); body->setCollisionFlags(btCollisionObject::CF_NO_CONTACT_RESPONSE); // m_dynamicsWorld->addRigidBody(body); body->setActivationState(ISLAND_SLEEPING); } } } } btTriangleIndexVertexArray* meshInterface = new btTriangleIndexVertexArray(); btIndexedMesh indexMesh; indexMesh.m_numTriangles = BUNNY_NUM_TRIANGLES ; indexMesh.m_numVertices = BUNNY_NUM_VERTICES; indexMesh.m_vertexBase = (const unsigned char*) &gVerticesBunny[0]; indexMesh.m_vertexStride = 3*sizeof(REAL); indexMesh.m_triangleIndexBase = (const unsigned char*)&gIndicesBunny[0]; indexMesh.m_triangleIndexStride = 3*sizeof(int); meshInterface->addIndexedMesh(indexMesh); btBvhTriangleMeshShape* bunny = new btBvhTriangleMeshShape(meshInterface,true); bunny->setLocalScaling(btVector3(2,2,2)); btCollisionObject* obj = new btCollisionObject(); btTransform tr; tr.setIdentity(); tr.setOrigin(btVector3(0,2,-20)); obj ->setWorldTransform(tr); obj->setCollisionShape(bunny); m_dynamicsWorld->addCollisionObject(obj); #endif #if 0 btConvexTriangleMeshShape* convexBun = new btConvexTriangleMeshShape(meshInterface); obj = new btCollisionObject(); tr.setOrigin(btVector3(0,2,-14)); obj ->setWorldTransform(tr); obj->setCollisionShape(convexBun); m_dynamicsWorld->addCollisionObject(obj); #endif #if 0 btConvexTriangleMeshShape* convexBun = new btConvexTriangleMeshShape(meshInterface); obj = new btCollisionObject(); tr.setOrigin(btVector3(0,2,-14)); obj ->setWorldTransform(tr); obj->setCollisionShape(convexBun); m_dynamicsWorld->addCollisionObject(obj); //btDiscreteCollisionDetectorInterface::ClosestPointInput input; //input.m_maximumDistanceSquared = btScalar(BT_LARGE_FLOAT);///@todo: tighter bounds //input.m_transformA = sphereObj->getWorldTransform(); //input.m_transformB = triObj->getWorldTransform(); //bool swapResults = m_swapped; //detector.getClosestPoints(input,*resultOut,dispatchInfo.m_debugDraw,swapResults); for (int v=1;v<10;v++) { float VOXEL_SIZE = VOXEL_SIZE_START * v; btVoxelizationCallback voxelizationCallback; btCompoundShape* compoundBunny = new btCompoundShape(); voxelizationCallback.m_bunnyCompound = compoundBunny; voxelizationCallback.m_sphereChildShape = new btSphereShape(VOXEL_SIZE); #if 1 float start_x = -ARRAY_SIZE_X; float start_y = -ARRAY_SIZE_Y; float start_z = - ARRAY_SIZE_Z; 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++) { btVector3 pos =VOXEL_SIZE*SCALING*btVector3( 2.0*i + start_x, 2.0*k + start_y, 2.0*j + start_z); btVector3 aabbMin(pos-btVector3(VOXEL_SIZE,VOXEL_SIZE,VOXEL_SIZE)); btVector3 aabbMax(pos+btVector3(VOXEL_SIZE,VOXEL_SIZE,VOXEL_SIZE)); voxelizationCallback.m_curSpherePos = pos; voxelizationCallback.m_oncePerSphere = false; bunny->processAllTriangles(&voxelizationCallback,aabbMin,aabbMax); } } } //btCollisionObject* obj2 = new btCollisionObject(); //obj2->setCollisionShape(compoundBunny); //m_dynamicsWorld->addCollisionObject(obj2); btVector3 localInertia; compoundBunny->calculateLocalInertia(1,localInertia); btRigidBody* body = new btRigidBody(1,0,compoundBunny,localInertia); //m_dynamicsWorld->addRigidBody(body); btTransform start; start.setIdentity(); start.setOrigin(btVector3(0,2,-12+6*v)); localCreateRigidBody(1.,start,compoundBunny); printf("compoundBunny with %d spheres\n",compoundBunny->getNumChildShapes()); #endif } #endif clientResetScene(); }
int main(int argc,char** argv) { CollisionDispatcher* dispatcher = new CollisionDispatcher(); SimdVector3 worldAabbMin(-10000,-10000,-10000); SimdVector3 worldAabbMax(10000,10000,10000); BroadphaseInterface* broadphase = new AxisSweep3(worldAabbMin,worldAabbMax); //BroadphaseInterface* broadphase = new SimpleBroadphase(); physicsEnvironmentPtr = new CcdPhysicsEnvironment(dispatcher,broadphase); physicsEnvironmentPtr->setDeactivationTime(2.f); physicsEnvironmentPtr->setGravity(0,-10,0); PHY_ShapeProps shapeProps; shapeProps.m_do_anisotropic = false; shapeProps.m_do_fh = false; shapeProps.m_do_rot_fh = false; shapeProps.m_friction_scaling[0] = 1.; shapeProps.m_friction_scaling[1] = 1.; shapeProps.m_friction_scaling[2] = 1.; shapeProps.m_inertia = 1.f; shapeProps.m_lin_drag = 0.2f; shapeProps.m_ang_drag = 0.1f; shapeProps.m_mass = 10.0f; PHY_MaterialProps materialProps; materialProps.m_friction = 10.5f; materialProps.m_restitution = 0.0f; CcdConstructionInfo ccdObjectCi; ccdObjectCi.m_friction = 0.5f; ccdObjectCi.m_linearDamping = shapeProps.m_lin_drag; ccdObjectCi.m_angularDamping = shapeProps.m_ang_drag; SimdTransform tr; tr.setIdentity(); int i; for (i=0;i<numObjects;i++) { if (i>0) { shapeIndex[i] = 1;//sphere } else shapeIndex[i] = 0; } for (i=0;i<numObjects;i++) { shapeProps.m_shape = shapePtr[shapeIndex[i]]; shapeProps.m_shape->SetMargin(0.05f); bool isDyna = i>0; //if (i==1) // isDyna=false; if (0)//i==1) { SimdQuaternion orn(0,0,0.1*SIMD_HALF_PI); ms[i].setWorldOrientation(orn.x(),orn.y(),orn.z(),orn[3]); } if (i>0) { switch (i) { case 1: { ms[i].setWorldPosition(0,10,0); //for testing, rotate the ground cube so the stack has to recover a bit break; } case 2: { ms[i].setWorldPosition(0,8,2); break; } default: ms[i].setWorldPosition(0,i*CUBE_HALF_EXTENTS*2 - CUBE_HALF_EXTENTS,0); } float quatIma0,quatIma1,quatIma2,quatReal; SimdQuaternion quat; SimdVector3 axis(0,0,1); SimdScalar angle=0.5f; quat.setRotation(axis,angle); ms[i].setWorldOrientation(quat.getX(),quat.getY(),quat.getZ(),quat[3]); } else { ms[i].setWorldPosition(0,-10+EXTRA_HEIGHT,0); } ccdObjectCi.m_MotionState = &ms[i]; ccdObjectCi.m_gravity = SimdVector3(0,0,0); ccdObjectCi.m_localInertiaTensor =SimdVector3(0,0,0); if (!isDyna) { shapeProps.m_mass = 0.f; ccdObjectCi.m_mass = shapeProps.m_mass; ccdObjectCi.m_collisionFlags = CollisionObject::isStatic; } else { shapeProps.m_mass = 1.f; ccdObjectCi.m_mass = shapeProps.m_mass; ccdObjectCi.m_collisionFlags = 0; } SimdVector3 localInertia; if (shapePtr[shapeIndex[i]]->GetShapeType() == EMPTY_SHAPE_PROXYTYPE) { //take inertia from first shape shapePtr[1]->CalculateLocalInertia(shapeProps.m_mass,localInertia); } else { shapePtr[shapeIndex[i]]->CalculateLocalInertia(shapeProps.m_mass,localInertia); } ccdObjectCi.m_localInertiaTensor = localInertia; ccdObjectCi.m_collisionShape = shapePtr[shapeIndex[i]]; physObjects[i]= new CcdPhysicsController( ccdObjectCi); // Only do CCD if motion in one timestep (1.f/60.f) exceeds CUBE_HALF_EXTENTS physObjects[i]->GetRigidBody()->m_ccdSquareMotionTreshold = CUBE_HALF_EXTENTS; //Experimental: better estimation of CCD Time of Impact: //physObjects[i]->GetRigidBody()->m_ccdSweptShereRadius = 0.5*CUBE_HALF_EXTENTS; physicsEnvironmentPtr->addCcdPhysicsController( physObjects[i]); if (i==1) { //physObjects[i]->SetAngularVelocity(0,0,-2,true); } physicsEnvironmentPtr->setDebugDrawer(&debugDrawer); } clientResetScene(); { //physObjects[i]->SetAngularVelocity(0,0,-2,true); int constraintId; float pivotX=CUBE_HALF_EXTENTS, pivotY=CUBE_HALF_EXTENTS, pivotZ=CUBE_HALF_EXTENTS; float axisX=0,axisY=1,axisZ=0; constraintId =physicsEnvironmentPtr->createConstraint( physObjects[1], //0, physObjects[2], ////PHY_POINT2POINT_CONSTRAINT, PHY_GENERIC_6DOF_CONSTRAINT,//can leave any of the 6 degree of freedom 'free' or 'locked' //PHY_LINEHINGE_CONSTRAINT, pivotX,pivotY,pivotZ, axisX,axisY,axisZ ); } setCameraDistance(26.f); return glutmain(argc, argv,640,480,"Bullet Physics Demo. http://www.continuousphysics.com/Bullet/phpBB2/"); }
void RagdollDemo::initPhysics() { // Setup the basic world setTexturing(true); setShadows(true); setCameraDistance(btScalar(5.)); m_collisionConfiguration = new btDefaultCollisionConfiguration(); m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); btVector3 worldAabbMin(-10000,-10000,-10000); btVector3 worldAabbMax(10000,10000,10000); m_broadphase = new btAxisSweep3 (worldAabbMin, worldAabbMax); m_solver = new btSequentialImpulseConstraintSolver; m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration); //m_dynamicsWorld->getDispatchInfo().m_useConvexConservativeDistanceUtil = true; //m_dynamicsWorld->getDispatchInfo().m_convexConservativeDistanceThreshold = 0.01f; // Setup a big ground box { btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(200.),btScalar(10.),btScalar(200.))); m_collisionShapes.push_back(groundShape); btTransform groundTransform; groundTransform.setIdentity(); groundTransform.setOrigin(btVector3(0,-10,0)); #define CREATE_GROUND_COLLISION_OBJECT 1 #ifdef CREATE_GROUND_COLLISION_OBJECT btCollisionObject* fixedGround = new btCollisionObject(); fixedGround->setCollisionShape(groundShape); fixedGround->setWorldTransform(groundTransform); m_dynamicsWorld->addCollisionObject(fixedGround); #else localCreateRigidBody(btScalar(0.),groundTransform,groundShape); #endif //CREATE_GROUND_COLLISION_OBJECT } // Spawn one ragdoll btVector3 startOffset(1,0.5,0); //spawnRagdoll(startOffset); startOffset.setValue(-1,0.5,0); //spawnRagdoll(startOffset); CreateBox(0, 0., 2., 0., 1., 0.2, 1.); // Create the box //leg components CreateCylinder(1, 2. , 2., 0., .2, 1., 0., 0., M_PI_2); //xleft horizontal CreateCylinder(2, -2., 2., 0., .2, 1., 0., 0., M_PI_2); //xright (negative) horizontal CreateCylinder(3, 0, 2., 2., .2, 1., M_PI_2, 0., 0.); //zpositive (into screen) CreateCylinder(4, 0, 2., -2., .2, 1., M_PI_2, 0., 0.); //znegative (out of screen) CreateCylinder(5, 3.0, 1., 0., .2, 1., 0., 0., 0.); //xleft vertical CreateCylinder(6, -3.0, 1., 0., .2, 1., 0., 0., 0.); //xright vertical CreateCylinder(7, 0., 1., 3.0, .2, 1., 0., 0., 0.); //zpositive vertical CreateCylinder(8, 0., 1., -3.0, .2, 1., 0., 0., 0.); //znegative vertical //The axisworldtolocal defines a vector perpendicular to the plane that you want the bodies to rotate in //hinge the legs together //xnegative -- right //two bodies should rotate in y-plane through x--give axisworldtolocal it a z vector btVector3 local2 = PointWorldToLocal(2, btVector3(-3., 2., 0)); btVector3 local6 = PointWorldToLocal(6, btVector3(-3., 2., 0)); btVector3 axis2 = AxisWorldToLocal(2, btVector3(0., 0., 1.)); btVector3 axis6 = AxisWorldToLocal(6, btVector3( 0., 0., 1.)); CreateHinge(0, *body[2], *body[6], local2, local6, axis2, axis6); joints[0]->setLimit(btScalar(0.), btScalar(M_PI_2+M_PI_4)); //positive -- left //give axisworldtolocal a z vector btVector3 local1 = PointWorldToLocal(1, btVector3(3., 2., 0)); btVector3 local5 = PointWorldToLocal(5, btVector3(3., 2., 0)); btVector3 axis1 = AxisWorldToLocal(1, btVector3( 0., 0., 1.)); btVector3 axis5 = AxisWorldToLocal(5, btVector3(0., 0., 1.)); CreateHinge(1, *body[1], *body[5], local1, local5, axis1, axis5); joints[1]->setLimit(btScalar(M_PI_4), btScalar(M_PI_4)); //zpositive -- back //rotates in y-plane through z--give it an x vector btVector3 local3 = PointWorldToLocal(3, btVector3(0, 2., 3.)); btVector3 local7 = PointWorldToLocal(7, btVector3(0, 2., 3.)); btVector3 axis3 = AxisWorldToLocal(3, btVector3(1., 0., 0.)); btVector3 axis7 = AxisWorldToLocal(7, btVector3(1., 0., 0.)); CreateHinge(2, *body[3], *body[7], local3, local7, axis3, axis7); joints[2]->setLimit(btScalar(0.), btScalar(M_PI_2+M_PI_4)); //znegative -- front btVector3 local4 = PointWorldToLocal(4, btVector3(0, 2., -3.)); btVector3 local8 = PointWorldToLocal(8, btVector3(0, 2., -3.)); btVector3 axis4 = AxisWorldToLocal(4, btVector3(1., 0., 0.)); btVector3 axis8 = AxisWorldToLocal(8, btVector3(1., 0., 0.)); CreateHinge(3, *body[4], *body[8], local4, local8, axis4, axis8); joints[3]->setLimit(btScalar(M_PI_4), btScalar(M_PI_4)); //hinge the legs to the body //xright to main btVector3 localHinge2 = PointWorldToLocal(2, btVector3(-1, 2., 0)); btVector3 mainHinge2 = PointWorldToLocal(0, btVector3(-1, 2., 0)); btVector3 localAxis2 = AxisWorldToLocal(2, btVector3(0., 0., 1.)); btVector3 mainAxis2 = AxisWorldToLocal(0, btVector3( 0., 0., 1.)); CreateHinge(4, *body[0], *body[2], mainHinge2, localHinge2, mainAxis2, localAxis2); //joints[4]->setLimit(btScalar(-M_PI_2-M_PI_4), btScalar(M_PI_4)); //xleft to main btVector3 localHinge1 = PointWorldToLocal(1, btVector3(1, 2., 0)); btVector3 mainHinge1 = PointWorldToLocal(0, btVector3(1, 2., 0)); btVector3 localAxis1 = AxisWorldToLocal(1, btVector3(0., 0., 1.)); btVector3 mainAxis1 = AxisWorldToLocal(0, btVector3( 0., 0., 1.)); CreateHinge(5, *body[0], *body[1], mainHinge1, localHinge1, mainAxis1, localAxis1); //joints[5]->setLimit(btScalar(-M_PI_2), btScalar(M_PI_2)); //zpositive (back) to main btVector3 localHinge3 = PointWorldToLocal(3, btVector3(0., 2., 1)); btVector3 mainHinge3 = PointWorldToLocal(0, btVector3(0., 2., 1)); btVector3 localAxis3 = AxisWorldToLocal(3, btVector3(1., 0., 0.)); btVector3 mainAxis3 = AxisWorldToLocal(0, btVector3( 1., 0., 0.)); CreateHinge(6, *body[0], *body[3], mainHinge3, localHinge3, mainAxis3, localAxis3); //joints[6]->setLimit(btScalar(-M_PI_2-M_PI_4), btScalar(M_PI_4)); btVector3 localHinge4 = PointWorldToLocal(4, btVector3(0., 2., -1)); btVector3 mainHinge4= PointWorldToLocal(0, btVector3(0., 2., -1)); btVector3 localAxis4 = AxisWorldToLocal(4, btVector3(1., 0., 0. )); btVector3 mainAxis4 = AxisWorldToLocal(0, btVector3( 1., 0., 0.)); CreateHinge(7, *body[0], *body[4], mainHinge4, localHinge4, mainAxis4, localAxis4); //joints[7]->setLimit(btScalar(-M_PI_2), btScalar(M_PI_2)); clientResetScene(); }
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 }
void ForkLiftDemo::initPhysics() { #ifdef FORCE_ZAXIS_UP m_cameraUp = btVector3(0,0,1); m_forwardAxis = 1; #endif btCollisionShape* groundShape = new btBoxShape(btVector3(50,3,50)); m_collisionShapes.push_back(groundShape); m_collisionConfiguration = new btDefaultCollisionConfiguration(); m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); btVector3 worldMin(-1000,-1000,-1000); btVector3 worldMax(1000,1000,1000); m_overlappingPairCache = new btAxisSweep3(worldMin,worldMax); m_constraintSolver = new btSequentialImpulseConstraintSolver(); m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_constraintSolver,m_collisionConfiguration); #ifdef FORCE_ZAXIS_UP m_dynamicsWorld->setGravity(btVector3(0,0,-10)); #endif //m_dynamicsWorld->setGravity(btVector3(0,0,0)); btTransform tr; tr.setIdentity(); tr.setOrigin(btVector3(0,-10,0)); //either use heightfield or triangle mesh //create ground object localCreateRigidBody(0,tr,groundShape); #ifdef FORCE_ZAXIS_UP // indexRightAxis = 0; // indexUpAxis = 2; // indexForwardAxis = 1; btCollisionShape* chassisShape = new btBoxShape(btVector3(1.f,2.f, 0.5f)); btCompoundShape* compound = new btCompoundShape(); btTransform localTrans; localTrans.setIdentity(); //localTrans effectively shifts the center of mass with respect to the chassis localTrans.setOrigin(btVector3(0,0,1)); #else btCollisionShape* chassisShape = new btBoxShape(btVector3(1.f,0.5f,2.f)); m_collisionShapes.push_back(chassisShape); btCompoundShape* compound = new btCompoundShape(); m_collisionShapes.push_back(compound); btTransform localTrans; localTrans.setIdentity(); //localTrans effectively shifts the center of mass with respect to the chassis localTrans.setOrigin(btVector3(0,1,0)); #endif compound->addChildShape(localTrans,chassisShape); { btCollisionShape* suppShape = new btBoxShape(btVector3(0.5f,0.1f,0.5f)); btTransform suppLocalTrans; suppLocalTrans.setIdentity(); //localTrans effectively shifts the center of mass with respect to the chassis suppLocalTrans.setOrigin(btVector3(0,1.0,2.5)); compound->addChildShape(suppLocalTrans, suppShape); } tr.setOrigin(btVector3(0,0.f,0)); m_carChassis = localCreateRigidBody(800,tr,compound);//chassisShape); //m_carChassis->setDamping(0.2,0.2); m_wheelShape = new btCylinderShapeX(btVector3(wheelWidth,wheelRadius,wheelRadius)); { btCollisionShape* liftShape = new btBoxShape(btVector3(0.5f,2.0f,0.05f)); m_collisionShapes.push_back(liftShape); btTransform liftTrans; m_liftStartPos = btVector3(0.0f, 2.5f, 3.05f); liftTrans.setIdentity(); liftTrans.setOrigin(m_liftStartPos); m_liftBody = localCreateRigidBody(10,liftTrans, liftShape); btTransform localA, localB; localA.setIdentity(); localB.setIdentity(); localA.getBasis().setEulerZYX(0, M_PI_2, 0); localA.setOrigin(btVector3(0.0, 1.0, 3.05)); localB.getBasis().setEulerZYX(0, M_PI_2, 0); localB.setOrigin(btVector3(0.0, -1.5, -0.05)); m_liftHinge = new btHingeConstraint(*m_carChassis,*m_liftBody, localA, localB); // m_liftHinge->setLimit(-LIFT_EPS, LIFT_EPS); m_liftHinge->setLimit(0.0f, 0.0f); m_dynamicsWorld->addConstraint(m_liftHinge, true); btCollisionShape* forkShapeA = new btBoxShape(btVector3(1.0f,0.1f,0.1f)); m_collisionShapes.push_back(forkShapeA); btCompoundShape* forkCompound = new btCompoundShape(); m_collisionShapes.push_back(forkCompound); btTransform forkLocalTrans; forkLocalTrans.setIdentity(); forkCompound->addChildShape(forkLocalTrans, forkShapeA); btCollisionShape* forkShapeB = new btBoxShape(btVector3(0.1f,0.02f,0.6f)); m_collisionShapes.push_back(forkShapeB); forkLocalTrans.setIdentity(); forkLocalTrans.setOrigin(btVector3(-0.9f, -0.08f, 0.7f)); forkCompound->addChildShape(forkLocalTrans, forkShapeB); btCollisionShape* forkShapeC = new btBoxShape(btVector3(0.1f,0.02f,0.6f)); m_collisionShapes.push_back(forkShapeC); forkLocalTrans.setIdentity(); forkLocalTrans.setOrigin(btVector3(0.9f, -0.08f, 0.7f)); forkCompound->addChildShape(forkLocalTrans, forkShapeC); btTransform forkTrans; m_forkStartPos = btVector3(0.0f, 0.6f, 3.2f); forkTrans.setIdentity(); forkTrans.setOrigin(m_forkStartPos); m_forkBody = localCreateRigidBody(5, forkTrans, forkCompound); localA.setIdentity(); localB.setIdentity(); localA.getBasis().setEulerZYX(0, 0, M_PI_2); localA.setOrigin(btVector3(0.0f, -1.9f, 0.05f)); localB.getBasis().setEulerZYX(0, 0, M_PI_2); localB.setOrigin(btVector3(0.0, 0.0, -0.1)); m_forkSlider = new btSliderConstraint(*m_liftBody, *m_forkBody, localA, localB, true); m_forkSlider->setLowerLinLimit(0.1f); m_forkSlider->setUpperLinLimit(0.1f); // m_forkSlider->setLowerAngLimit(-LIFT_EPS); // m_forkSlider->setUpperAngLimit(LIFT_EPS); m_forkSlider->setLowerAngLimit(0.0f); m_forkSlider->setUpperAngLimit(0.0f); m_dynamicsWorld->addConstraint(m_forkSlider, true); btCompoundShape* loadCompound = new btCompoundShape(); m_collisionShapes.push_back(loadCompound); btCollisionShape* loadShapeA = new btBoxShape(btVector3(2.0f,0.5f,0.5f)); m_collisionShapes.push_back(loadShapeA); btTransform loadTrans; loadTrans.setIdentity(); loadCompound->addChildShape(loadTrans, loadShapeA); btCollisionShape* loadShapeB = new btBoxShape(btVector3(0.1f,1.0f,1.0f)); m_collisionShapes.push_back(loadShapeB); loadTrans.setIdentity(); loadTrans.setOrigin(btVector3(2.1f, 0.0f, 0.0f)); loadCompound->addChildShape(loadTrans, loadShapeB); btCollisionShape* loadShapeC = new btBoxShape(btVector3(0.1f,1.0f,1.0f)); m_collisionShapes.push_back(loadShapeC); loadTrans.setIdentity(); loadTrans.setOrigin(btVector3(-2.1f, 0.0f, 0.0f)); loadCompound->addChildShape(loadTrans, loadShapeC); loadTrans.setIdentity(); m_loadStartPos = btVector3(0.0f, -3.5f, 7.0f); loadTrans.setOrigin(m_loadStartPos); m_loadBody = localCreateRigidBody(4, loadTrans, loadCompound); } clientResetScene(); /// create vehicle { m_vehicleRayCaster = new btDefaultVehicleRaycaster(m_dynamicsWorld); m_vehicle = new btRaycastVehicle(m_tuning,m_carChassis,m_vehicleRayCaster); ///never deactivate the vehicle m_carChassis->setActivationState(DISABLE_DEACTIVATION); m_dynamicsWorld->addVehicle(m_vehicle); float connectionHeight = 1.2f; bool isFrontWheel=true; //choose coordinate system m_vehicle->setCoordinateSystem(rightIndex,upIndex,forwardIndex); #ifdef FORCE_ZAXIS_UP btVector3 connectionPointCS0(CUBE_HALF_EXTENTS-(0.3*wheelWidth),2*CUBE_HALF_EXTENTS-wheelRadius, connectionHeight); #else btVector3 connectionPointCS0(CUBE_HALF_EXTENTS-(0.3*wheelWidth),connectionHeight,2*CUBE_HALF_EXTENTS-wheelRadius); #endif m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel); #ifdef FORCE_ZAXIS_UP connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),2*CUBE_HALF_EXTENTS-wheelRadius, connectionHeight); #else connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),connectionHeight,2*CUBE_HALF_EXTENTS-wheelRadius); #endif m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel); #ifdef FORCE_ZAXIS_UP connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),-2*CUBE_HALF_EXTENTS+wheelRadius, connectionHeight); #else connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),connectionHeight,-2*CUBE_HALF_EXTENTS+wheelRadius); #endif //FORCE_ZAXIS_UP isFrontWheel = false; m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel); #ifdef FORCE_ZAXIS_UP connectionPointCS0 = btVector3(CUBE_HALF_EXTENTS-(0.3*wheelWidth),-2*CUBE_HALF_EXTENTS+wheelRadius, connectionHeight); #else connectionPointCS0 = btVector3(CUBE_HALF_EXTENTS-(0.3*wheelWidth),connectionHeight,-2*CUBE_HALF_EXTENTS+wheelRadius); #endif m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel); for (int i=0;i<m_vehicle->getNumWheels();i++) { btWheelInfo& wheel = m_vehicle->getWheelInfo(i); wheel.m_suspensionStiffness = suspensionStiffness; wheel.m_wheelsDampingRelaxation = suspensionDamping; wheel.m_wheelsDampingCompression = suspensionCompression; wheel.m_frictionSlip = wheelFriction; wheel.m_rollInfluence = rollInfluence; } } setCameraDistance(26.f); }
void RagdollDemo::initPhysics() { //ASSIGNMENT 8 ragdolldemo = this; //ASSIGNMENT 9 srand(time(NULL)); //ASSIGNMENT 10 pause = false; //ragdolldemo->touches = new btVector3* // Setup the basic world //setTexturing(true); //setShadows(true); setCameraDistance(btScalar(5.)); m_collisionConfiguration = new btDefaultCollisionConfiguration(); m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); btVector3 worldAabbMin(-10000,-10000,-10000); btVector3 worldAabbMax(10000,10000,10000); m_broadphase = new btAxisSweep3 (worldAabbMin, worldAabbMax); m_solver = new btSequentialImpulseConstraintSolver; m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration); //m_dynamicsWorld->getDispatchInfo().m_useConvexConservativeDistanceUtil = true; //m_dynamicsWorld->getDispatchInfo().m_convexConservativeDistanceThreshold = 0.01f; //ASSIGNMENT 10 string line; ifstream myfile("../../RAGDOLL_DATA/weights.txt"); cout << "NORMAL SYNAPSE" << endl; for(int i = 0; i < 4; i++) { for(int k = 0; k < 8; k++) { getline(myfile,line); stringstream convert(line); convert >> synapseWeights[i][k]; cout << synapseWeights[i][k] << " | "; } cout << endl; } myfile.close(); //GET RECURRENT SYNAPSE line = ""; ifstream myfile3("../../RAGDOLL_DATA/recurrent.txt"); cout << "RECURRNT SYNAPSE" << endl; for(int i = 0; i < 4; i++) { for(int k = 0; k < 8; k++) { getline(myfile3,line); stringstream convert(line); convert >> synapseRecurrent[i][k]; cout << synapseRecurrent[i][k] << " | "; } cout << endl; } myfile.close(); // GET MASK recurrentOn = false; line = ""; cout << "PRINTING RECURRENT" << endl; ifstream myfile2("../../RAGDOLL_DATA/mask.txt"); for(int i = 0; i < 4; i++) { for(int k = 0; k < 8; k++) { getline(myfile2,line); stringstream convert(line); convert >> mask[i][k]; cout << mask[i][k] << " | "; if(mask[i][k] == 1) { recurrentOn = true; } } cout << endl; } myfile2.close(); cout << "INITIAL PEVIOUS TOUCH" << endl; //INITIAL PREVIOUS STEP = 0 for(int i = 0; i < 4; i++) { previousTouch[i] = 0; cout << previousTouch[i] << " | "; } cout << endl; // Setup a big ground box { btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(200.),btScalar(10.),btScalar(200.))); m_collisionShapes.push_back(groundShape); btTransform groundTransform; groundTransform.setIdentity(); groundTransform.setOrigin(btVector3(0,-10,0)); #define CREATE_GROUND_COLLISION_OBJECT 1 #ifdef CREATE_GROUND_COLLISION_OBJECT //ASSIGNMENT 8 /* btTransform offset; offset.setIdentity(); offset.setOrigin(btVector3(btScalar(0),btScalar(-10),btScalar(0))); btRigidBody* fixedGround = localCreateRigidBody(btScalar(1.0),offset, groundShape); fixedGround->setCollisionFlags(3); */ btCollisionObject* fixedGround = new btCollisionObject(); fixedGround->setFriction(btScalar(10000.0)); fixedGround->setCollisionShape(groundShape); fixedGround->setWorldTransform(groundTransform); m_dynamicsWorld->addCollisionObject(fixedGround); currentIndex = 0; /* fixedGround->setCollisionFlags(fixedGround->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK); fixedGround->setCenterOfMassTransform(offset); */ collisionID[currentIndex] = new collisionObject(currentIndex, (btRigidBody *)groundShape); fixedGround->setUserPointer(collisionID[currentIndex]); cout << "OBJECT ID: " << collisionID[currentIndex]->id; cout << " | POINTER ID: " << ((collisionObject*)fixedGround->getUserPointer())->id; cout << " | ADDRESS " << collisionID[currentIndex]; cout << " | FLAGS " << collisionID[currentIndex]->body->getCollisionFlags() << endl; currentIndex++; #else localCreateRigidBody(btScalar(0.),groundTransform,groundShape); #endif //CREATE_GROUND_COLLISION_OBJECT } gContactAddedCallback = callBackFunc; //BODY ------------------------------------------------------------------ //(index, x, y, z, length, width, height) RagdollDemo::CreateBox(0, 0.0, 1.8, 0.0, 1.0, 0.2, 1.0);//WORKING //FEET RagdollDemo::CreateCylinder(3, 3.2, 0, 0, 1, .2, 0, M_PI_2, 0); //WORKING -> NORMAL RagdollDemo::CreateCylinder(4, -3.2, 0, 0, 1, .2, 0, -M_PI_2, 0); //WORKING -> NORMAL RagdollDemo::CreateCylinder(8, 0, 0, -3.2, 1, .2, 0, M_PI_2, 0); //WORKING -> ROTATED RagdollDemo::CreateCylinder(6, 0, 0, 3.2, 1, .2, 0, M_PI_2, 0); //WORKING -> NORMAL -> //ARM RagdollDemo::CreateCylinder(5, 0.0, 0.8, 2.0, 1.0, 0.2, M_PI_2, 0.0, 0.0);//WORKING -> ROTATED RagdollDemo::CreateCylinder(7, 0.0, 0.8, -2.0, 1.0, 0.2, M_PI_2, 0.0, 0.0);//WORKIN G-> ROTATED RagdollDemo::CreateCylinder(1, 2.0, .8, 0.0, 1.0, 0.2, 0.0, 0.0, M_PI_2);//WORKING -> NORMAL RagdollDemo::CreateCylinder(2, -2.0, .8, 0.0, 1.0, 0.2, 0, M_PI_2, M_PI_2);//WORKING -> NORMAL //FEET HINGE RagdollDemo::CreateHinge(2,3,1, btVector3(0.0, 0.0, 1.0), btVector3(0.0, 0.0, 1.0),btVector3(0.0, 1.0, 0.0), btVector3(0, -1.0, 0.0));//WORKING RagdollDemo::CreateHinge(3,4,2, btVector3(1.0, 0.0, 0.0), btVector3(0.0, 0.0, 1.0),btVector3(0.0, 1.0, 0.0), btVector3(0, 1.0, 0.0)); //WORKIN RagdollDemo::CreateHinge(6,7,8, btVector3(1.0, 0.0, 0.0), btVector3(1.0, 0.0, 0.0),btVector3(0.0, -1.0, 0.0), btVector3(0, 1.0, 0.0));//WORKING -> ROTATED RagdollDemo::CreateHinge(5,5,6, btVector3(1.0, 0.0, 0.0), btVector3(1.0, 0.0, 0.0),btVector3(0.0, 1.0, 0.0), btVector3(0, 1.0, 0.0));//WORKING -> ROTATED // //ARM HINGE RagdollDemo::CreateHinge(4,0,5, btVector3(1.0, 0.0, 0.0), btVector3(1.0, 0.0, 0.0),btVector3(0.0, 0.0, 1.0), btVector3(0, -1.0, 0.0));//WORKING -> ROTATED RagdollDemo::CreateHinge(7,0,7, btVector3(1.0, 0.0, 0.0), btVector3(1.0, 0.0, 0.0),btVector3(0.0, 0.0, -1.0), btVector3(0, 1.0, 0.0));//WORKING -> ROTATED RagdollDemo::CreateHinge(0,0,1, btVector3(0.0, 0.0, 1.0), btVector3(0.0, 0.0, 1.0), btVector3(1.0, 0.0, 0.0), btVector3(0.0, 1.0, 0.0));//WORKING RagdollDemo::CreateHinge(1,0,2, btVector3(0.0, 0.0, 1.0), btVector3(0.0, 0.0, 1.0),btVector3(-1.0, 0.0, 0.0), btVector3(0.0, -1.0, 0.0));//WORKING clientResetScene(); }