[167] | 1 | ///create 125 (5x5x5) dynamic object |
---|
| 2 | #include "SimpleDemo.h" |
---|
| 3 | #include "btBulletDynamicsCommon.h" |
---|
| 4 | #include "GLDebugDrawer.h" |
---|
| 5 | |
---|
| 6 | #include <mpi.h> |
---|
| 7 | #include <stdio.h> |
---|
| 8 | #include <map> |
---|
| 9 | #include <string> |
---|
| 10 | #include <vector> |
---|
| 11 | |
---|
| 12 | using namespace std; |
---|
| 13 | |
---|
| 14 | btCollisionShape* groundShape; |
---|
| 15 | btTransform groundTransform; |
---|
| 16 | btCollisionShape* colShape; |
---|
| 17 | |
---|
| 18 | bool enable1, enable2; |
---|
| 19 | bool simpleSimulation; |
---|
| 20 | bool debugConstraints; |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | void SimpleDemo::clientMoveAndDisplay() |
---|
| 24 | { |
---|
| 25 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
| 26 | |
---|
| 27 | //simple dynamics world doesn't handle fixed-time-stepping |
---|
| 28 | float ms = getDeltaTimeMicroseconds(); |
---|
| 29 | ///step the simulation |
---|
| 30 | if (m_dynamicsWorld) |
---|
| 31 | { |
---|
| 32 | m_dynamicsWorld->stepSimulation(ms / 1000000.f); |
---|
| 33 | //optional but useful: debug drawing |
---|
| 34 | m_dynamicsWorld->debugDrawWorld(); |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | renderme(); |
---|
| 38 | |
---|
| 39 | glFlush(); |
---|
| 40 | glutSwapBuffers(); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | void SimpleDemo::displayCallback(void) { |
---|
| 45 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
| 46 | renderme(); |
---|
| 47 | //optional but useful: debug drawing to detect problems |
---|
| 48 | if (m_dynamicsWorld) |
---|
| 49 | m_dynamicsWorld->debugDrawWorld(); |
---|
| 50 | glFlush(); |
---|
| 51 | glutSwapBuffers(); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | void SimpleDemo::initPhysics() { |
---|
| 55 | |
---|
| 56 | m_ele = 75; |
---|
| 57 | updateCamera(); |
---|
| 58 | |
---|
| 59 | setTexturing(true); |
---|
| 60 | setShadows(true); |
---|
| 61 | setCameraDistance(btScalar(40.)); |
---|
| 62 | |
---|
| 63 | m_collisionConfiguration = new btDefaultCollisionConfiguration(); |
---|
| 64 | m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); |
---|
| 65 | m_broadphase = new btDbvtBroadphase(); |
---|
| 66 | btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver; |
---|
| 67 | m_solver = sol; |
---|
| 68 | |
---|
| 69 | m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration); |
---|
| 70 | m_dynamicsWorld->setGravity(btVector3(0,-10,0)); |
---|
| 71 | |
---|
| 72 | groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.))); |
---|
| 73 | m_collisionShapes.push_back(groundShape); |
---|
| 74 | |
---|
| 75 | groundTransform.setIdentity(); |
---|
| 76 | groundTransform.setOrigin(btVector3(0,-50,0)); |
---|
| 77 | |
---|
| 78 | btScalar mass(0.); |
---|
| 79 | //rigidbody is dynamic if and only if mass is non zero, otherwise static |
---|
| 80 | bool isDynamic = (mass != 0.f); |
---|
| 81 | btVector3 localInertia(0,0,0); |
---|
| 82 | if (isDynamic) |
---|
| 83 | groundShape->calculateLocalInertia(mass,localInertia); |
---|
| 84 | |
---|
| 85 | //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects |
---|
| 86 | btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform); |
---|
| 87 | btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia); |
---|
| 88 | btRigidBody* body = new btRigidBody(rbInfo); |
---|
| 89 | |
---|
| 90 | //add the body to the dynamics world |
---|
| 91 | m_dynamicsWorld->addRigidBody(body); |
---|
| 92 | |
---|
| 93 | m_dynamicsWorld->setDebugDrawer(new GLDebugDrawer()); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | void SimpleDemo::exitPhysics() |
---|
| 97 | { |
---|
| 98 | int i; |
---|
| 99 | for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--) |
---|
| 100 | { |
---|
| 101 | btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i]; |
---|
| 102 | btRigidBody* body = btRigidBody::upcast(obj); |
---|
| 103 | if (body && body->getMotionState()) |
---|
| 104 | { |
---|
| 105 | delete body->getMotionState(); |
---|
| 106 | } |
---|
| 107 | m_dynamicsWorld->removeCollisionObject( obj ); |
---|
| 108 | delete obj; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | //delete collision shapes |
---|
| 112 | for (int j=0;j<m_collisionShapes.size();j++) |
---|
| 113 | { |
---|
| 114 | btCollisionShape* shape = m_collisionShapes[j]; |
---|
| 115 | delete shape; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | delete m_dynamicsWorld; |
---|
| 119 | delete m_solver; |
---|
| 120 | delete m_broadphase; |
---|
| 121 | delete m_dispatcher; |
---|
| 122 | delete m_collisionConfiguration; |
---|
| 123 | } |
---|
| 124 | |
---|