Tizen 2.1 base
[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
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
25         int i;
26
27         ///collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration.
28         btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
29
30         ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
31         btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
32
33         ///btDbvtBroadphase is a good general purpose broadphase. You can also try out btAxis3Sweep.
34         btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase();
35
36         ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
37         btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
38
39         btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver,collisionConfiguration);
40
41         dynamicsWorld->setGravity(btVector3(0,-10,0));
42
43         ///create a few basic rigid bodies
44         btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
45
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;
49
50         collisionShapes.push_back(groundShape);
51
52         btTransform groundTransform;
53         groundTransform.setIdentity();
54         groundTransform.setOrigin(btVector3(0,-56,0));
55
56         {
57                 btScalar mass(0.);
58
59                 //rigidbody is dynamic if and only if mass is non zero, otherwise static
60                 bool isDynamic = (mass != 0.f);
61
62                 btVector3 localInertia(0,0,0);
63                 if (isDynamic)
64                         groundShape->calculateLocalInertia(mass,localInertia);
65
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);
70
71                 //add the body to the dynamics world
72                 dynamicsWorld->addRigidBody(body);
73         }
74
75
76         {
77                 //create a dynamic rigidbody
78
79                 //btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
80                 btCollisionShape* colShape = new btSphereShape(btScalar(1.));
81                 collisionShapes.push_back(colShape);
82
83                 /// Create Dynamic Objects
84                 btTransform startTransform;
85                 startTransform.setIdentity();
86
87                 btScalar        mass(1.f);
88
89                 //rigidbody is dynamic if and only if mass is non zero, otherwise static
90                 bool isDynamic = (mass != 0.f);
91
92                 btVector3 localInertia(0,0,0);
93                 if (isDynamic)
94                         colShape->calculateLocalInertia(mass,localInertia);
95
96                         startTransform.setOrigin(btVector3(2,10,0));
97                 
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);
102
103                         dynamicsWorld->addRigidBody(body);
104         }
105
106
107
108 /// Do some simulation
109
110
111
112         for (i=0;i<100;i++)
113         {
114                 dynamicsWorld->stepSimulation(1.f/60.f,10);
115                 
116                 //print positions of all objects
117                 for (int j=dynamicsWorld->getNumCollisionObjects()-1; j>=0 ;j--)
118                 {
119                         btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[j];
120                         btRigidBody* body = btRigidBody::upcast(obj);
121                         if (body && body->getMotionState())
122                         {
123                                 btTransform trans;
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()));
126                         }
127                 }
128         }
129
130
131         //cleanup in the reverse order of creation/initialization
132
133         //remove the rigidbodies from the dynamics world and delete them
134         for (i=dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
135         {
136                 btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[i];
137                 btRigidBody* body = btRigidBody::upcast(obj);
138                 if (body && body->getMotionState())
139                 {
140                         delete body->getMotionState();
141                 }
142                 dynamicsWorld->removeCollisionObject( obj );
143                 delete obj;
144         }
145
146         //delete collision shapes
147         for (int j=0;j<collisionShapes.size();j++)
148         {
149                 btCollisionShape* shape = collisionShapes[j];
150                 collisionShapes[j] = 0;
151                 delete shape;
152         }
153
154         //delete dynamics world
155         delete dynamicsWorld;
156
157         //delete solver
158         delete solver;
159
160         //delete broadphase
161         delete overlappingPairCache;
162
163         //delete dispatcher
164         delete dispatcher;
165
166         delete collisionConfiguration;
167
168         //next line is optional: it will be cleared by the destructor when the array goes out of scope
169         collisionShapes.clear();
170
171 }
172