Initialize libbullet git in 2.0_beta.
[platform/upstream/libbullet.git] / Demos / BasicDemo / BasicDemo.cpp
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 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 ///create 125 (5x5x5) dynamic object
18 #define ARRAY_SIZE_X 5
19 #define ARRAY_SIZE_Y 5
20 #define ARRAY_SIZE_Z 5
21
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)
24
25 ///scaling of the objects (0.1 = 20 centimeter boxes )
26 #define SCALING 1.
27 #define START_POS_X -5
28 #define START_POS_Y -5
29 #define START_POS_Z -3
30
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"
35
36 #include <stdio.h> //printf debugging
37
38
39 void BasicDemo::clientMoveAndDisplay()
40 {
41         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
42
43         //simple dynamics world doesn't handle fixed-time-stepping
44         float ms = getDeltaTimeMicroseconds();
45         
46         ///step the simulation
47         if (m_dynamicsWorld)
48         {
49                 m_dynamicsWorld->stepSimulation(ms / 1000000.f);
50                 //optional but useful: debug drawing
51                 m_dynamicsWorld->debugDrawWorld();
52         }
53                 
54         renderme(); 
55
56         glFlush();
57
58         swapBuffers();
59
60 }
61
62
63
64 void BasicDemo::displayCallback(void) {
65
66         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
67         
68         renderme();
69
70         //optional but useful: debug drawing to detect problems
71         if (m_dynamicsWorld)
72                 m_dynamicsWorld->debugDrawWorld();
73
74         glFlush();
75         swapBuffers();
76 }
77
78
79
80
81
82 void    BasicDemo::initPhysics()
83 {
84         setTexturing(true);
85         setShadows(true);
86
87         setCameraDistance(btScalar(SCALING*50.));
88
89         ///collision configuration contains default setup for memory, collision setup
90         m_collisionConfiguration = new btDefaultCollisionConfiguration();
91         //m_collisionConfiguration->setConvexConvexMultipointIterations();
92
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);
95
96         m_broadphase = new btDbvtBroadphase();
97
98         ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
99         btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver;
100         m_solver = sol;
101
102         m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
103         
104         m_dynamicsWorld->setGravity(btVector3(0,-10,0));
105
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);
109         
110         m_collisionShapes.push_back(groundShape);
111
112         btTransform groundTransform;
113         groundTransform.setIdentity();
114         groundTransform.setOrigin(btVector3(0,-50,0));
115
116         //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
117         {
118                 btScalar mass(0.);
119
120                 //rigidbody is dynamic if and only if mass is non zero, otherwise static
121                 bool isDynamic = (mass != 0.f);
122
123                 btVector3 localInertia(0,0,0);
124                 if (isDynamic)
125                         groundShape->calculateLocalInertia(mass,localInertia);
126
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);
131
132                 //add the body to the dynamics world
133                 m_dynamicsWorld->addRigidBody(body);
134         }
135
136
137         {
138                 //create a few dynamic rigidbodies
139                 // Re-using the same collision is better for memory usage and performance
140
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);
144
145                 /// Create Dynamic Objects
146                 btTransform startTransform;
147                 startTransform.setIdentity();
148
149                 btScalar        mass(1.f);
150
151                 //rigidbody is dynamic if and only if mass is non zero, otherwise static
152                 bool isDynamic = (mass != 0.f);
153
154                 btVector3 localInertia(0,0,0);
155                 if (isDynamic)
156                         colShape->calculateLocalInertia(mass,localInertia);
157
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;
161
162                 for (int k=0;k<ARRAY_SIZE_Y;k++)
163                 {
164                         for (int i=0;i<ARRAY_SIZE_X;i++)
165                         {
166                                 for(int j = 0;j<ARRAY_SIZE_Z;j++)
167                                 {
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)));
172
173                         
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);
178                                         
179
180                                         m_dynamicsWorld->addRigidBody(body);
181                                 }
182                         }
183                 }
184         }
185
186
187 }
188 void    BasicDemo::clientResetScene()
189 {
190         exitPhysics();
191         initPhysics();
192 }
193         
194
195 void    BasicDemo::exitPhysics()
196 {
197
198         //cleanup in the reverse order of creation/initialization
199
200         //remove the rigidbodies from the dynamics world and delete them
201         int i;
202         for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
203         {
204                 btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i];
205                 btRigidBody* body = btRigidBody::upcast(obj);
206                 if (body && body->getMotionState())
207                 {
208                         delete body->getMotionState();
209                 }
210                 m_dynamicsWorld->removeCollisionObject( obj );
211                 delete obj;
212         }
213
214         //delete collision shapes
215         for (int j=0;j<m_collisionShapes.size();j++)
216         {
217                 btCollisionShape* shape = m_collisionShapes[j];
218                 delete shape;
219         }
220         m_collisionShapes.clear();
221
222         delete m_dynamicsWorld;
223         
224         delete m_solver;
225         
226         delete m_broadphase;
227         
228         delete m_dispatcher;
229
230         delete m_collisionConfiguration;
231
232         
233 }
234
235
236
237