2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
17 ///create 125 (5x5x5) dynamic object
18 #define ARRAY_SIZE_X 5
19 #define ARRAY_SIZE_Y 5
20 #define ARRAY_SIZE_Z 5
22 //maximum number of objects (and allow user to shoot additional boxes)
23 #define MAX_PROXIES (ARRAY_SIZE_X*ARRAY_SIZE_Y*ARRAY_SIZE_Z + 1024)
25 ///scaling of the objects (0.1 = 20 centimeter boxes )
27 #define START_POS_X -5
28 #define START_POS_Y -5
29 #define START_POS_Z -3
31 #include "BasicDemo.h"
32 #include "GlutStuff.h"
33 ///btBulletDynamicsCommon.h is the main Bullet include file, contains most common include files.
34 #include "btBulletDynamicsCommon.h"
36 #include <stdio.h> //printf debugging
37 #include "GLDebugDrawer.h"
39 static GLDebugDrawer gDebugDraw;
42 void BasicDemo::clientMoveAndDisplay()
44 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
46 //simple dynamics world doesn't handle fixed-time-stepping
47 float ms = getDeltaTimeMicroseconds();
49 ///step the simulation
52 m_dynamicsWorld->stepSimulation(ms / 1000000.f);
53 //optional but useful: debug drawing
54 m_dynamicsWorld->debugDrawWorld();
67 void BasicDemo::displayCallback(void) {
69 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
73 //optional but useful: debug drawing to detect problems
75 m_dynamicsWorld->debugDrawWorld();
85 void BasicDemo::initPhysics()
90 setCameraDistance(btScalar(SCALING*50.));
92 ///collision configuration contains default setup for memory, collision setup
93 m_collisionConfiguration = new btDefaultCollisionConfiguration();
94 //m_collisionConfiguration->setConvexConvexMultipointIterations();
96 ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
97 m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
99 m_broadphase = new btDbvtBroadphase();
101 ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
102 btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver;
105 m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
106 m_dynamicsWorld->setDebugDrawer(&gDebugDraw);
108 m_dynamicsWorld->setGravity(btVector3(0,-10,0));
110 ///create a few basic rigid bodies
111 btBoxShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
112 //groundShape->initializePolyhedralFeatures();
113 // btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),50);
115 m_collisionShapes.push_back(groundShape);
117 btTransform groundTransform;
118 groundTransform.setIdentity();
119 groundTransform.setOrigin(btVector3(0,-50,0));
121 //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
125 //rigidbody is dynamic if and only if mass is non zero, otherwise static
126 bool isDynamic = (mass != 0.f);
128 btVector3 localInertia(0,0,0);
130 groundShape->calculateLocalInertia(mass,localInertia);
132 //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
133 btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
134 btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
135 btRigidBody* body = new btRigidBody(rbInfo);
137 //add the body to the dynamics world
138 m_dynamicsWorld->addRigidBody(body);
143 //create a few dynamic rigidbodies
144 // Re-using the same collision is better for memory usage and performance
146 btBoxShape* colShape = new btBoxShape(btVector3(SCALING*1,SCALING*1,SCALING*1));
147 //btCollisionShape* colShape = new btSphereShape(btScalar(1.));
148 m_collisionShapes.push_back(colShape);
150 /// Create Dynamic Objects
151 btTransform startTransform;
152 startTransform.setIdentity();
156 //rigidbody is dynamic if and only if mass is non zero, otherwise static
157 bool isDynamic = (mass != 0.f);
159 btVector3 localInertia(0,0,0);
161 colShape->calculateLocalInertia(mass,localInertia);
163 float start_x = START_POS_X - ARRAY_SIZE_X/2;
164 float start_y = START_POS_Y;
165 float start_z = START_POS_Z - ARRAY_SIZE_Z/2;
167 for (int k=0;k<ARRAY_SIZE_Y;k++)
169 for (int i=0;i<ARRAY_SIZE_X;i++)
171 for(int j = 0;j<ARRAY_SIZE_Z;j++)
173 startTransform.setOrigin(SCALING*btVector3(
174 btScalar(2.0*i + start_x),
175 btScalar(20+2.0*k + start_y),
176 btScalar(2.0*j + start_z)));
179 //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
180 btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
181 btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
182 btRigidBody* body = new btRigidBody(rbInfo);
185 m_dynamicsWorld->addRigidBody(body);
193 void BasicDemo::clientResetScene()
200 void BasicDemo::exitPhysics()
203 //cleanup in the reverse order of creation/initialization
205 //remove the rigidbodies from the dynamics world and delete them
207 for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
209 btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i];
210 btRigidBody* body = btRigidBody::upcast(obj);
211 if (body && body->getMotionState())
213 delete body->getMotionState();
215 m_dynamicsWorld->removeCollisionObject( obj );
219 //delete collision shapes
220 for (int j=0;j<m_collisionShapes.size();j++)
222 btCollisionShape* shape = m_collisionShapes[j];
225 m_collisionShapes.clear();
227 delete m_dynamicsWorld;
235 delete m_collisionConfiguration;