2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2007 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 #include "btBulletDynamicsCommon.h"
20 /// This is a Hello World program for running a basic Bullet physics simulation
22 int main(int argc, char** argv)
27 ///collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration.
28 btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
30 ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
31 btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
33 ///btDbvtBroadphase is a good general purpose broadphase. You can also try out btAxis3Sweep.
34 btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase();
36 ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
37 btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
39 btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver,collisionConfiguration);
41 dynamicsWorld->setGravity(btVector3(0,-10,0));
43 ///create a few basic rigid bodies
44 btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
46 //keep track of the shapes, we release memory at exit.
47 //make sure to re-use collision shapes among rigid bodies whenever possible!
48 btAlignedObjectArray<btCollisionShape*> collisionShapes;
50 collisionShapes.push_back(groundShape);
52 btTransform groundTransform;
53 groundTransform.setIdentity();
54 groundTransform.setOrigin(btVector3(0,-56,0));
59 //rigidbody is dynamic if and only if mass is non zero, otherwise static
60 bool isDynamic = (mass != 0.f);
62 btVector3 localInertia(0,0,0);
64 groundShape->calculateLocalInertia(mass,localInertia);
66 //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
67 btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
68 btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
69 btRigidBody* body = new btRigidBody(rbInfo);
71 //add the body to the dynamics world
72 dynamicsWorld->addRigidBody(body);
77 //create a dynamic rigidbody
79 //btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
80 btCollisionShape* colShape = new btSphereShape(btScalar(1.));
81 collisionShapes.push_back(colShape);
83 /// Create Dynamic Objects
84 btTransform startTransform;
85 startTransform.setIdentity();
89 //rigidbody is dynamic if and only if mass is non zero, otherwise static
90 bool isDynamic = (mass != 0.f);
92 btVector3 localInertia(0,0,0);
94 colShape->calculateLocalInertia(mass,localInertia);
96 startTransform.setOrigin(btVector3(2,10,0));
98 //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
99 btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
100 btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
101 btRigidBody* body = new btRigidBody(rbInfo);
103 dynamicsWorld->addRigidBody(body);
108 /// Do some simulation
114 dynamicsWorld->stepSimulation(1.f/60.f,10);
116 //print positions of all objects
117 for (int j=dynamicsWorld->getNumCollisionObjects()-1; j>=0 ;j--)
119 btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[j];
120 btRigidBody* body = btRigidBody::upcast(obj);
121 if (body && body->getMotionState())
124 body->getMotionState()->getWorldTransform(trans);
125 printf("world pos = %f,%f,%f\n",float(trans.getOrigin().getX()),float(trans.getOrigin().getY()),float(trans.getOrigin().getZ()));
131 //cleanup in the reverse order of creation/initialization
133 //remove the rigidbodies from the dynamics world and delete them
134 for (i=dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
136 btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[i];
137 btRigidBody* body = btRigidBody::upcast(obj);
138 if (body && body->getMotionState())
140 delete body->getMotionState();
142 dynamicsWorld->removeCollisionObject( obj );
146 //delete collision shapes
147 for (int j=0;j<collisionShapes.size();j++)
149 btCollisionShape* shape = collisionShapes[j];
150 collisionShapes[j] = 0;
154 //delete dynamics world
155 delete dynamicsWorld;
161 delete overlappingPairCache;
166 delete collisionConfiguration;
168 //next line is optional: it will be cleared by the destructor when the array goes out of scope
169 collisionShapes.clear();