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
39 void BasicDemo::clientMoveAndDisplay()
41 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
43 //simple dynamics world doesn't handle fixed-time-stepping
44 float ms = getDeltaTimeMicroseconds();
46 ///step the simulation
49 m_dynamicsWorld->stepSimulation(ms / 1000000.f);
50 //optional but useful: debug drawing
51 m_dynamicsWorld->debugDrawWorld();
64 void BasicDemo::displayCallback(void) {
66 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
70 //optional but useful: debug drawing to detect problems
72 m_dynamicsWorld->debugDrawWorld();
82 void BasicDemo::initPhysics()
87 setCameraDistance(btScalar(SCALING*50.));
89 ///collision configuration contains default setup for memory, collision setup
90 m_collisionConfiguration = new btDefaultCollisionConfiguration();
91 //m_collisionConfiguration->setConvexConvexMultipointIterations();
93 ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
94 m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
96 m_broadphase = new btDbvtBroadphase();
98 ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
99 btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver;
102 m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
104 m_dynamicsWorld->setGravity(btVector3(0,-10,0));
106 ///create a few basic rigid bodies
107 btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
108 // btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),50);
110 m_collisionShapes.push_back(groundShape);
112 btTransform groundTransform;
113 groundTransform.setIdentity();
114 groundTransform.setOrigin(btVector3(0,-50,0));
116 //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
120 //rigidbody is dynamic if and only if mass is non zero, otherwise static
121 bool isDynamic = (mass != 0.f);
123 btVector3 localInertia(0,0,0);
125 groundShape->calculateLocalInertia(mass,localInertia);
127 //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
128 btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
129 btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
130 btRigidBody* body = new btRigidBody(rbInfo);
132 //add the body to the dynamics world
133 m_dynamicsWorld->addRigidBody(body);
138 //create a few dynamic rigidbodies
139 // Re-using the same collision is better for memory usage and performance
141 btCollisionShape* colShape = new btBoxShape(btVector3(SCALING*1,SCALING*1,SCALING*1));
142 //btCollisionShape* colShape = new btSphereShape(btScalar(1.));
143 m_collisionShapes.push_back(colShape);
145 /// Create Dynamic Objects
146 btTransform startTransform;
147 startTransform.setIdentity();
151 //rigidbody is dynamic if and only if mass is non zero, otherwise static
152 bool isDynamic = (mass != 0.f);
154 btVector3 localInertia(0,0,0);
156 colShape->calculateLocalInertia(mass,localInertia);
158 float start_x = START_POS_X - ARRAY_SIZE_X/2;
159 float start_y = START_POS_Y;
160 float start_z = START_POS_Z - ARRAY_SIZE_Z/2;
162 for (int k=0;k<ARRAY_SIZE_Y;k++)
164 for (int i=0;i<ARRAY_SIZE_X;i++)
166 for(int j = 0;j<ARRAY_SIZE_Z;j++)
168 startTransform.setOrigin(SCALING*btVector3(
169 btScalar(2.0*i + start_x),
170 btScalar(20+2.0*k + start_y),
171 btScalar(2.0*j + start_z)));
174 //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
175 btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
176 btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
177 btRigidBody* body = new btRigidBody(rbInfo);
180 m_dynamicsWorld->addRigidBody(body);
188 void BasicDemo::clientResetScene()
195 void BasicDemo::exitPhysics()
198 //cleanup in the reverse order of creation/initialization
200 //remove the rigidbodies from the dynamics world and delete them
202 for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
204 btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i];
205 btRigidBody* body = btRigidBody::upcast(obj);
206 if (body && body->getMotionState())
208 delete body->getMotionState();
210 m_dynamicsWorld->removeCollisionObject( obj );
214 //delete collision shapes
215 for (int j=0;j<m_collisionShapes.size();j++)
217 btCollisionShape* shape = m_collisionShapes[j];
220 m_collisionShapes.clear();
222 delete m_dynamicsWorld;
230 delete m_collisionConfiguration;