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.
16 ///-----includes_start-----
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)
24 ///-----includes_end-----
27 ///-----initialization_start-----
29 ///collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration.
30 btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
32 ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
33 btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
35 ///btDbvtBroadphase is a good general purpose broadphase. You can also try out btAxis3Sweep.
36 btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase();
38 ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
39 btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
41 btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver,collisionConfiguration);
43 dynamicsWorld->setGravity(btVector3(0,-10,0));
45 ///-----initialization_end-----
47 ///create a few basic rigid bodies
48 btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
50 //keep track of the shapes, we release memory at exit.
51 //make sure to re-use collision shapes among rigid bodies whenever possible!
52 btAlignedObjectArray<btCollisionShape*> collisionShapes;
54 collisionShapes.push_back(groundShape);
56 btTransform groundTransform;
57 groundTransform.setIdentity();
58 groundTransform.setOrigin(btVector3(0,-56,0));
63 //rigidbody is dynamic if and only if mass is non zero, otherwise static
64 bool isDynamic = (mass != 0.f);
66 btVector3 localInertia(0,0,0);
68 groundShape->calculateLocalInertia(mass,localInertia);
70 //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
71 btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
72 btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
73 btRigidBody* body = new btRigidBody(rbInfo);
75 //add the body to the dynamics world
76 dynamicsWorld->addRigidBody(body);
81 //create a dynamic rigidbody
83 //btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
84 btCollisionShape* colShape = new btSphereShape(btScalar(1.));
85 collisionShapes.push_back(colShape);
87 /// Create Dynamic Objects
88 btTransform startTransform;
89 startTransform.setIdentity();
93 //rigidbody is dynamic if and only if mass is non zero, otherwise static
94 bool isDynamic = (mass != 0.f);
96 btVector3 localInertia(0,0,0);
98 colShape->calculateLocalInertia(mass,localInertia);
100 startTransform.setOrigin(btVector3(2,10,0));
102 //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
103 btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
104 btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
105 btRigidBody* body = new btRigidBody(rbInfo);
107 dynamicsWorld->addRigidBody(body);
112 /// Do some simulation
115 ///-----stepsimulation_start-----
118 dynamicsWorld->stepSimulation(1.f/60.f,10);
120 //print positions of all objects
121 for (int j=dynamicsWorld->getNumCollisionObjects()-1; j>=0 ;j--)
123 btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[j];
124 btRigidBody* body = btRigidBody::upcast(obj);
125 if (body && body->getMotionState())
128 body->getMotionState()->getWorldTransform(trans);
129 printf("world pos = %f,%f,%f\n",float(trans.getOrigin().getX()),float(trans.getOrigin().getY()),float(trans.getOrigin().getZ()));
134 ///-----stepsimulation_end-----
136 //cleanup in the reverse order of creation/initialization
138 ///-----cleanup_start-----
140 //remove the rigidbodies from the dynamics world and delete them
141 for (i=dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
143 btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[i];
144 btRigidBody* body = btRigidBody::upcast(obj);
145 if (body && body->getMotionState())
147 delete body->getMotionState();
149 dynamicsWorld->removeCollisionObject( obj );
153 //delete collision shapes
154 for (int j=0;j<collisionShapes.size();j++)
156 btCollisionShape* shape = collisionShapes[j];
157 collisionShapes[j] = 0;
161 //delete dynamics world
162 delete dynamicsWorld;
168 delete overlappingPairCache;
173 delete collisionConfiguration;
175 //next line is optional: it will be cleared by the destructor when the array goes out of scope
176 collisionShapes.clear();
178 ///-----cleanup_end-----