/* Bullet Continuous Collision Detection and Physics Library Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/ This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ ///create 125 (5x5x5) dynamic object #define ARRAY_SIZE_X 5 #define ARRAY_SIZE_Y 5 #define ARRAY_SIZE_Z 5 //maximum number of objects (and allow user to shoot additional boxes) #define MAX_PROXIES (ARRAY_SIZE_X*ARRAY_SIZE_Y*ARRAY_SIZE_Z + 1024) ///scaling of the objects (0.1 = 20 centimeter boxes ) #define SCALING 1. #define START_POS_X -5 #define START_POS_Y -5 #define START_POS_Z -3 #include "BasicDemo.h" #include "GlutStuff.h" #include "GLDebugFont.h" ///btBulletDynamicsCommon.h is the main Bullet include file, contains most common include files. #include "btBulletDynamicsCommon.h" #include //printf debugging #include "GLDebugDrawer.h" static GLDebugDrawer sDebugDrawer; BasicDemo::BasicDemo() :m_ccdMode(USE_SPECULULATIVE_CONTACTS) { setDebugMode(btIDebugDraw::DBG_DrawText); setCameraDistance(btScalar(SCALING*50.)); } void BasicDemo::clientMoveAndDisplay() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //simple dynamics world doesn't handle fixed-time-stepping float ms = getDeltaTimeMicroseconds(); ///step the simulation if (m_dynamicsWorld) { m_dynamicsWorld->stepSimulation(1./60.);//ms / 1000000.f); //optional but useful: debug drawing m_dynamicsWorld->debugDrawWorld(); } renderme(); displayText(); glFlush(); swapBuffers(); } void BasicDemo::displayText() { int lineWidth=400; int xStart = m_glutScreenWidth - lineWidth; int yStart = 20; if((getDebugMode() & btIDebugDraw::DBG_DrawText)!=0) { setOrthographicProjection(); glDisable(GL_LIGHTING); glColor3f(0, 0, 0); char buf[124]; glRasterPos3f(xStart, yStart, 0); switch (m_ccdMode) { case USE_SPECULULATIVE_CONTACTS: { sprintf(buf,"Predictive contacts enabled"); break; } case USE_CONSERVATIVE_ADVANCEMENT: { sprintf(buf,"Conservative advancement enabled"); break; }; case USE_NO_CCD: { sprintf(buf,"CCD disabled"); break; } default: { sprintf(buf,"unknown CCD setting"); }; }; GLDebugDrawString(xStart,20,buf); glRasterPos3f(xStart, yStart, 0); sprintf(buf,"Press 'p' to change CCD mode"); yStart+=20; GLDebugDrawString(xStart,yStart,buf); glRasterPos3f(xStart, yStart, 0); sprintf(buf,"Press '.' or right mouse to shoot boxes"); yStart+=20; GLDebugDrawString(xStart,yStart,buf); glRasterPos3f(xStart, yStart, 0); sprintf(buf,"space to restart, h(elp), t(ext), w(ire)"); yStart+=20; GLDebugDrawString(xStart,yStart,buf); resetPerspectiveProjection(); glEnable(GL_LIGHTING); } } void BasicDemo::displayCallback(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); renderme(); displayText(); //optional but useful: debug drawing to detect problems if (m_dynamicsWorld) { m_dynamicsWorld->debugDrawWorld(); } glFlush(); swapBuffers(); } void BasicDemo::initPhysics() { setTexturing(true); setShadows(true); m_ShootBoxInitialSpeed = 1000.f; ///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_dispatcher->registerCollisionCreateFunc(BOX_SHAPE_PROXYTYPE,BOX_SHAPE_PROXYTYPE,m_collisionConfiguration->getCollisionAlgorithmCreateFunc(CONVEX_SHAPE_PROXYTYPE,CONVEX_SHAPE_PROXYTYPE)); 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 ->setDebugDrawer(&sDebugDrawer); if (m_ccdMode==USE_SPECULULATIVE_CONTACTS) { m_dynamicsWorld->getDispatchInfo().m_convexMaxDistanceUseCPT = true; } 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;ksetContactProcessingThreshold(1e30); m_dynamicsWorld->addRigidBody(body); } } } } } void BasicDemo::clientResetScene() { exitPhysics(); initPhysics(); } void BasicDemo::keyboardCallback(unsigned char key, int x, int y) { if (key=='p') { switch (m_ccdMode) { case USE_SPECULULATIVE_CONTACTS: { m_ccdMode = USE_CONSERVATIVE_ADVANCEMENT; break; } case USE_CONSERVATIVE_ADVANCEMENT: { m_ccdMode = USE_NO_CCD; break; }; case USE_NO_CCD: default: { m_ccdMode = USE_SPECULULATIVE_CONTACTS; } }; clientResetScene(); } else { DemoApplication::keyboardCallback(key,x,y); } } void BasicDemo::shootBox(const btVector3& destination) { if (m_dynamicsWorld) { float mass = 1.f; btTransform startTransform; startTransform.setIdentity(); btVector3 camPos = getCameraPosition(); startTransform.setOrigin(camPos); setShootBoxShape (); btRigidBody* body = this->localCreateRigidBody(mass, startTransform,m_shootBoxShape); body->setLinearFactor(btVector3(1,1,1)); //body->setRestitution(1); btVector3 linVel(destination[0]-camPos[0],destination[1]-camPos[1],destination[2]-camPos[2]); linVel.normalize(); linVel*=m_ShootBoxInitialSpeed; body->getWorldTransform().setOrigin(camPos); body->getWorldTransform().setRotation(btQuaternion(0,0,0,1)); body->setLinearVelocity(linVel); body->setAngularVelocity(btVector3(0,0,0)); body->setContactProcessingThreshold(1e30); ///when using m_ccdMode, disable regular CCD if (m_ccdMode==USE_CONSERVATIVE_ADVANCEMENT) { body->setCcdMotionThreshold(1.); body->setCcdSweptSphereRadius(0.5f*SCALING); } } } void BasicDemo::exitPhysics() { //cleanup in the reverse order of creation/initialization //remove the rigidbodies from the dynamics world and delete them int i; for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--) { btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i]; btRigidBody* body = btRigidBody::upcast(obj); if (body && body->getMotionState()) { delete body->getMotionState(); } m_dynamicsWorld->removeCollisionObject( obj ); delete obj; } //delete collision shapes for (int j=0;j