Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Demos / HelloWorld / HelloWorld.cpp
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2007 Erwin Coumans  http://continuousphysics.com/Bullet/
4
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:
10
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.
14 */
15
16 ///-----includes_start-----
17 #include "btBulletDynamicsCommon.h"
18 #include <stdio.h>
19
20 /// This is a Hello World program for running a basic Bullet physics simulation
21
22 int main(int argc, char** argv)
23 {
24         ///-----includes_end-----
25
26         int i;
27         ///-----initialization_start-----
28
29         ///collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration.
30         btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
31
32         ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
33         btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
34
35         ///btDbvtBroadphase is a good general purpose broadphase. You can also try out btAxis3Sweep.
36         btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase();
37
38         ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
39         btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
40
41         btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver,collisionConfiguration);
42
43         dynamicsWorld->setGravity(btVector3(0,-10,0));
44
45         ///-----initialization_end-----
46
47         ///create a few basic rigid bodies
48         btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
49
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;
53
54         collisionShapes.push_back(groundShape);
55
56         btTransform groundTransform;
57         groundTransform.setIdentity();
58         groundTransform.setOrigin(btVector3(0,-56,0));
59
60         {
61                 btScalar mass(0.);
62
63                 //rigidbody is dynamic if and only if mass is non zero, otherwise static
64                 bool isDynamic = (mass != 0.f);
65
66                 btVector3 localInertia(0,0,0);
67                 if (isDynamic)
68                         groundShape->calculateLocalInertia(mass,localInertia);
69
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);
74
75                 //add the body to the dynamics world
76                 dynamicsWorld->addRigidBody(body);
77         }
78
79
80         {
81                 //create a dynamic rigidbody
82
83                 //btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
84                 btCollisionShape* colShape = new btSphereShape(btScalar(1.));
85                 collisionShapes.push_back(colShape);
86
87                 /// Create Dynamic Objects
88                 btTransform startTransform;
89                 startTransform.setIdentity();
90
91                 btScalar        mass(1.f);
92
93                 //rigidbody is dynamic if and only if mass is non zero, otherwise static
94                 bool isDynamic = (mass != 0.f);
95
96                 btVector3 localInertia(0,0,0);
97                 if (isDynamic)
98                         colShape->calculateLocalInertia(mass,localInertia);
99
100                         startTransform.setOrigin(btVector3(2,10,0));
101                 
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);
106
107                         dynamicsWorld->addRigidBody(body);
108         }
109
110
111
112 /// Do some simulation
113
114
115         ///-----stepsimulation_start-----
116         for (i=0;i<100;i++)
117         {
118                 dynamicsWorld->stepSimulation(1.f/60.f,10);
119                 
120                 //print positions of all objects
121                 for (int j=dynamicsWorld->getNumCollisionObjects()-1; j>=0 ;j--)
122                 {
123                         btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[j];
124                         btRigidBody* body = btRigidBody::upcast(obj);
125                         if (body && body->getMotionState())
126                         {
127                                 btTransform trans;
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()));
130                         }
131                 }
132         }
133
134         ///-----stepsimulation_end-----
135
136         //cleanup in the reverse order of creation/initialization
137         
138         ///-----cleanup_start-----
139
140         //remove the rigidbodies from the dynamics world and delete them
141         for (i=dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
142         {
143                 btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[i];
144                 btRigidBody* body = btRigidBody::upcast(obj);
145                 if (body && body->getMotionState())
146                 {
147                         delete body->getMotionState();
148                 }
149                 dynamicsWorld->removeCollisionObject( obj );
150                 delete obj;
151         }
152
153         //delete collision shapes
154         for (int j=0;j<collisionShapes.size();j++)
155         {
156                 btCollisionShape* shape = collisionShapes[j];
157                 collisionShapes[j] = 0;
158                 delete shape;
159         }
160
161         //delete dynamics world
162         delete dynamicsWorld;
163
164         //delete solver
165         delete solver;
166
167         //delete broadphase
168         delete overlappingPairCache;
169
170         //delete dispatcher
171         delete dispatcher;
172
173         delete collisionConfiguration;
174
175         //next line is optional: it will be cleared by the destructor when the array goes out of scope
176         collisionShapes.clear();
177
178         ///-----cleanup_end-----
179 }
180