Imported Upstream version 2.81
[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 #include "GLDebugDrawer.h"
38
39 static GLDebugDrawer gDebugDraw;
40
41
42 void BasicDemo::clientMoveAndDisplay()
43 {
44         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
45
46         //simple dynamics world doesn't handle fixed-time-stepping
47         float ms = getDeltaTimeMicroseconds();
48         
49         ///step the simulation
50         if (m_dynamicsWorld)
51         {
52                 m_dynamicsWorld->stepSimulation(ms / 1000000.f);
53                 //optional but useful: debug drawing
54                 m_dynamicsWorld->debugDrawWorld();
55         }
56                 
57         renderme(); 
58
59         glFlush();
60
61         swapBuffers();
62
63 }
64
65
66
67 void BasicDemo::displayCallback(void) {
68
69         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
70         
71         renderme();
72
73         //optional but useful: debug drawing to detect problems
74         if (m_dynamicsWorld)
75                 m_dynamicsWorld->debugDrawWorld();
76
77         glFlush();
78         swapBuffers();
79 }
80
81
82
83
84
85 void    BasicDemo::initPhysics()
86 {
87         setTexturing(true);
88         setShadows(true);
89
90         setCameraDistance(btScalar(SCALING*50.));
91
92         ///collision configuration contains default setup for memory, collision setup
93         m_collisionConfiguration = new btDefaultCollisionConfiguration();
94         //m_collisionConfiguration->setConvexConvexMultipointIterations();
95
96         ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
97         m_dispatcher = new      btCollisionDispatcher(m_collisionConfiguration);
98
99         m_broadphase = new btDbvtBroadphase();
100
101         ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
102         btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver;
103         m_solver = sol;
104
105         m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
106         m_dynamicsWorld->setDebugDrawer(&gDebugDraw);
107         
108         m_dynamicsWorld->setGravity(btVector3(0,-10,0));
109
110         ///create a few basic rigid bodies
111         btBoxShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
112         //groundShape->initializePolyhedralFeatures();
113 //      btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),50);
114         
115         m_collisionShapes.push_back(groundShape);
116
117         btTransform groundTransform;
118         groundTransform.setIdentity();
119         groundTransform.setOrigin(btVector3(0,-50,0));
120
121         //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
122         {
123                 btScalar mass(0.);
124
125                 //rigidbody is dynamic if and only if mass is non zero, otherwise static
126                 bool isDynamic = (mass != 0.f);
127
128                 btVector3 localInertia(0,0,0);
129                 if (isDynamic)
130                         groundShape->calculateLocalInertia(mass,localInertia);
131
132                 //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
133                 btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
134                 btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
135                 btRigidBody* body = new btRigidBody(rbInfo);
136
137                 //add the body to the dynamics world
138                 m_dynamicsWorld->addRigidBody(body);
139         }
140
141
142         {
143                 //create a few dynamic rigidbodies
144                 // Re-using the same collision is better for memory usage and performance
145
146                 btBoxShape* colShape = new btBoxShape(btVector3(SCALING*1,SCALING*1,SCALING*1));
147                 //btCollisionShape* colShape = new btSphereShape(btScalar(1.));
148                 m_collisionShapes.push_back(colShape);
149
150                 /// Create Dynamic Objects
151                 btTransform startTransform;
152                 startTransform.setIdentity();
153
154                 btScalar        mass(1.f);
155
156                 //rigidbody is dynamic if and only if mass is non zero, otherwise static
157                 bool isDynamic = (mass != 0.f);
158
159                 btVector3 localInertia(0,0,0);
160                 if (isDynamic)
161                         colShape->calculateLocalInertia(mass,localInertia);
162
163                 float start_x = START_POS_X - ARRAY_SIZE_X/2;
164                 float start_y = START_POS_Y;
165                 float start_z = START_POS_Z - ARRAY_SIZE_Z/2;
166
167                 for (int k=0;k<ARRAY_SIZE_Y;k++)
168                 {
169                         for (int i=0;i<ARRAY_SIZE_X;i++)
170                         {
171                                 for(int j = 0;j<ARRAY_SIZE_Z;j++)
172                                 {
173                                         startTransform.setOrigin(SCALING*btVector3(
174                                                                                 btScalar(2.0*i + start_x),
175                                                                                 btScalar(20+2.0*k + start_y),
176                                                                                 btScalar(2.0*j + start_z)));
177
178                         
179                                         //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
180                                         btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
181                                         btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
182                                         btRigidBody* body = new btRigidBody(rbInfo);
183                                         
184
185                                         m_dynamicsWorld->addRigidBody(body);
186                                 }
187                         }
188                 }
189         }
190
191
192 }
193 void    BasicDemo::clientResetScene()
194 {
195         exitPhysics();
196         initPhysics();
197 }
198         
199
200 void    BasicDemo::exitPhysics()
201 {
202
203         //cleanup in the reverse order of creation/initialization
204
205         //remove the rigidbodies from the dynamics world and delete them
206         int i;
207         for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
208         {
209                 btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i];
210                 btRigidBody* body = btRigidBody::upcast(obj);
211                 if (body && body->getMotionState())
212                 {
213                         delete body->getMotionState();
214                 }
215                 m_dynamicsWorld->removeCollisionObject( obj );
216                 delete obj;
217         }
218
219         //delete collision shapes
220         for (int j=0;j<m_collisionShapes.size();j++)
221         {
222                 btCollisionShape* shape = m_collisionShapes[j];
223                 delete shape;
224         }
225         m_collisionShapes.clear();
226
227         delete m_dynamicsWorld;
228         
229         delete m_solver;
230         
231         delete m_broadphase;
232         
233         delete m_dispatcher;
234
235         delete m_collisionConfiguration;
236
237         
238 }
239
240
241
242