123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- /*
- Bullet Continuous Collision Detection and Physics Library
- Copyright (c) 2003-2011 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.
- */
- ///FractureDemo shows how to break objects.
- ///It assumes a btCompoundShaps (where the childshapes are the pre-fractured pieces)
- ///The btFractureBody is a class derived from btRigidBody, dealing with the collision impacts.
- ///Press the F key to toggle between fracture and glue mode
- ///This is preliminary work
- #define CUBE_HALF_EXTENTS 1.f
- #define EXTRA_HEIGHT 1.f
- ///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 "FractureDemo.h"
- ///btBulletDynamicsCommon.h is the main Bullet include file, contains most common include files.
- #include "btBulletDynamicsCommon.h"
- #include <stdio.h> //printf debugging
- int sFrameNumber = 0;
- #include "btFractureBody.h"
- #include "btFractureDynamicsWorld.h"
- #include "LinearMath/btAlignedObjectArray.h"
- #include "../CommonInterfaces/CommonRigidBodyBase.h"
- ///FractureDemo shows basic breaking and glueing of objects
- class FractureDemo : public CommonRigidBodyBase
- {
-
-
- public:
-
- FractureDemo(struct GUIHelperInterface* helper)
- :CommonRigidBodyBase(helper)
- {
- }
- virtual ~FractureDemo()
- {
- }
- void initPhysics();
-
- void exitPhysics();
-
- virtual void stepSimulation(float deltaTime)
- {
- CommonRigidBodyBase::stepSimulation(deltaTime);
-
- {
- BT_PROFILE("recreate graphics");
- //@todo: make this graphics re-creation better
- //right now: brute force remove all graphics objects, and re-create them every frame
- m_guiHelper->getRenderInterface()->removeAllInstances();
- for (int i=0;i<m_dynamicsWorld->getNumCollisionObjects();i++)
- {
- btCollisionObject* colObj = m_dynamicsWorld->getCollisionObjectArray()[i];
- colObj->getCollisionShape()->setUserIndex(-1);
- colObj->setUserIndex(-1);
- }
- m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
- }
- }
-
- virtual bool keyboardCallback(int key, int state);
-
- void resetCamera()
- {
- float dist = 41;
- float pitch = 52;
- float yaw = 35;
- float targetPos[3]={0,0.46,0};
- m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
- }
-
-
- };
- void FractureDemo::initPhysics()
- {
- m_guiHelper->setUpAxis(1);
-
- ///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);
- btFractureDynamicsWorld* fractureWorld = new btFractureDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
- m_dynamicsWorld = fractureWorld;
- m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
-
- //m_splitImpulse removes the penetration resolution from the applied impulse, otherwise objects might fracture due to deep penetrations.
- m_dynamicsWorld->getSolverInfo().m_splitImpulse = true;
- {
- ///create a few basic rigid bodies
- btCollisionShape* groundShape = new btBoxShape(btVector3(50,1,50));
- /// btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),0);
- m_collisionShapes.push_back(groundShape);
- btTransform groundTransform;
- groundTransform.setIdentity();
- groundTransform.setOrigin(btVector3(0,0,0));
- createRigidBody(0.f,groundTransform,groundShape);
- }
- {
- ///create a few basic rigid bodies
- btCollisionShape* shape = new btBoxShape(btVector3(1,1,1));
- m_collisionShapes.push_back(shape);
- btTransform tr;
- tr.setIdentity();
- tr.setOrigin(btVector3(5,2,0));
- createRigidBody(0.f,tr,shape);
- }
- {
- //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 btCapsuleShape(SCALING*0.4,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);
- int gNumObjects = 10;
- for (int i=0;i<gNumObjects;i++)
- {
- btTransform trans;
- trans.setIdentity();
- btVector3 pos(i*2*CUBE_HALF_EXTENTS ,20,0);
- trans.setOrigin(pos);
- //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
- btDefaultMotionState* myMotionState = new btDefaultMotionState(trans);
- btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
- btFractureBody* body = new btFractureBody(rbInfo, m_dynamicsWorld);
- body->setLinearVelocity(btVector3(0,-10,0));
- m_dynamicsWorld->addRigidBody(body);
- }
- }
- fractureWorld->stepSimulation(1./60.,0);
- fractureWorld->glueCallback();
- m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
- }
- #if 0
- void FractureDemo::showMessage()
- {
- if((getDebugMode() & btIDebugDraw::DBG_DrawText))
- {
- setOrthographicProjection();
- glDisable(GL_LIGHTING);
- glColor3f(0, 0, 0);
- char buf[124];
- int lineWidth=380;
- int xStart = m_glutScreenWidth - lineWidth;
- int yStart = 20;
- btFractureDynamicsWorld* world = (btFractureDynamicsWorld*)m_dynamicsWorld;
- if (world->getFractureMode())
- {
- sprintf(buf,"Fracture mode");
- } else
- {
- sprintf(buf,"Glue mode");
- }
- GLDebugDrawString(xStart,yStart,buf);
- sprintf(buf,"f to toggle fracture/glue mode");
- yStart+=20;
- GLDebugDrawString(xStart,yStart,buf);
- sprintf(buf,"space to restart, mouse to pick/shoot");
- yStart+=20;
- GLDebugDrawString(xStart,yStart,buf);
- resetPerspectiveProjection();
- glEnable(GL_LIGHTING);
- }
- }
- #endif
- #if 0
- void FractureDemo::displayCallback(void) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- renderme();
- showMessage();
- //optional but useful: debug drawing to detect problems
- if (m_dynamicsWorld)
- m_dynamicsWorld->debugDrawWorld();
- glFlush();
- swapBuffers();
- }
- #endif
- bool FractureDemo::keyboardCallback(int key, int state)
- {
-
- if (key=='f' && (state==0))
- {
- btFractureDynamicsWorld* world = (btFractureDynamicsWorld*)m_dynamicsWorld;
- world->setFractureMode(!world->getFractureMode());
- if (world->getFractureMode())
- {
- b3Printf("Fracturing mode");
- } else
- {
- b3Printf("Gluing mode");
- }
- return true;
- }
-
- return false;
- }
- #if 0
- void FractureDemo::keyboardUpCallback(unsigned char key, int x, int y)
- {
- if (key=='f')
- {
- btFractureDynamicsWorld* world = (btFractureDynamicsWorld*)m_dynamicsWorld;
- world->setFractureMode(!world->getFractureMode());
- }
- PlatformDemoApplication::keyboardUpCallback(key,x,y);
- }
- #endif
- #if 0
- void FractureDemo::shootBox(const btVector3& destination)
- {
- if (m_dynamicsWorld)
- {
- btScalar mass = 1.f;
- btTransform startTransform;
- startTransform.setIdentity();
- btVector3 camPos = getCameraPosition();
- startTransform.setOrigin(camPos);
- setShootBoxShape ();
- btAssert((!m_shootBoxShape || m_shootBoxShape->getShapeType() != INVALID_SHAPE_PROXYTYPE));
- //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)
- m_shootBoxShape->calculateLocalInertia(mass,localInertia);
- //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
- btFractureBody* body = new btFractureBody(mass,0,m_shootBoxShape,localInertia,&mass,1,m_dynamicsWorld);
- body->setWorldTransform(startTransform);
- m_dynamicsWorld->addRigidBody(body);
- 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->setCcdMotionThreshold(1.);
- body->setCcdSweptSphereRadius(0.2f);
- }
- }
- #endif
- void FractureDemo::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<m_collisionShapes.size();j++)
- {
- btCollisionShape* shape = m_collisionShapes[j];
- delete shape;
- }
- m_collisionShapes.clear();
- delete m_dynamicsWorld;
- m_dynamicsWorld=0;
- delete m_solver;
- m_solver=0;
- delete m_broadphase;
- m_broadphase=0;
- delete m_dispatcher;
- m_dispatcher=0;
- delete m_collisionConfiguration;
- m_collisionConfiguration=0;
- }
- class CommonExampleInterface* FractureDemoCreateFunc(struct CommonExampleOptions& options)
- {
- return new FractureDemo(options.m_guiHelper);
- }
|