Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Demos / UserCollisionAlgorithm / UserCollisionAlgorithm.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 #include "btBulletDynamicsCommon.h"
17 #include "LinearMath/btIDebugDraw.h"
18 #include "GLDebugDrawer.h"
19 #include "UserCollisionAlgorithm.h"
20 #include "GL_ShapeDrawer.h"
21 #include "GlutStuff.h"
22
23 //The user defined collision algorithm
24 #include "BulletCollision/CollisionDispatch/btSphereSphereCollisionAlgorithm.h"
25
26 GLDebugDrawer   debugDrawer;
27
28 static const int NUM_VERTICES = 5;
29 static const int NUM_TRIANGLES=4;
30
31 btVector3       gVertices[NUM_VERTICES];
32 int     gIndices[NUM_TRIANGLES*3];
33 const float TRIANGLE_SIZE=10.f;
34
35
36 ///User can override this material combiner by implementing gContactAddedCallback and setting body0->m_collisionFlags |= btCollisionObject::customMaterialCallback;
37 inline btScalar calculateCombinedFriction(float friction0,float friction1)
38 {
39         btScalar friction = friction0 * friction1;
40
41         const btScalar MAX_FRICTION  = 10.f;
42         if (friction < -MAX_FRICTION)
43                 friction = -MAX_FRICTION;
44         if (friction > MAX_FRICTION)
45                 friction = MAX_FRICTION;
46         return friction;
47
48 }
49
50 inline btScalar calculateCombinedRestitution(float restitution0,float restitution1)
51 {
52         return restitution0 * restitution1;
53 }
54
55
56
57
58
59
60 int main(int argc,char** argv)
61 {
62
63         UserCollisionAlgorithm* userCollisionAlgorithm = new UserCollisionAlgorithm;
64         userCollisionAlgorithm->initPhysics();
65         userCollisionAlgorithm->setCameraDistance(30.f);
66
67         return glutmain(argc, argv,640,480,"Static Concave Mesh Demo",userCollisionAlgorithm);
68 }
69
70 void    UserCollisionAlgorithm::initPhysics()
71 {
72         #define TRISIZE 5.f
73
74
75         const int NUM_VERTS_X = 50;
76         const int NUM_VERTS_Y = 50;
77         const int totalVerts = NUM_VERTS_X*NUM_VERTS_Y;
78
79         btVector3*      gVertices = new btVector3[totalVerts];
80         
81         int i;
82         for ( i=0;i<NUM_VERTS_X;i++)
83         {
84                 for (int j=0;j<NUM_VERTS_Y;j++)
85                 {
86                         gVertices[i+j*NUM_VERTS_X].setValue((i-NUM_VERTS_X*0.5f)*TRIANGLE_SIZE,2.f*sinf((float)i)*cosf((float)j),(j-NUM_VERTS_Y*0.5f)*TRIANGLE_SIZE);
87                 }
88         }
89
90         btTriangleMesh* trimesh = new btTriangleMesh();
91
92         for ( i=0;i<NUM_VERTS_X-1;i++)
93         {
94                 for (int j=0;j<NUM_VERTS_Y-1;j++)
95                 {
96                         trimesh->addTriangle(gVertices[j*NUM_VERTS_X+i],gVertices[j*NUM_VERTS_X+i+1],gVertices[(j+1)*NUM_VERTS_X+i+1]);
97                         trimesh->addTriangle(gVertices[j*NUM_VERTS_X+i],gVertices[(j+1)*NUM_VERTS_X+i+1],gVertices[(j+1)*NUM_VERTS_X+i]);
98                 }
99         }
100         
101         delete[] gVertices;
102
103         bool useQuantizedBvhTree = true;
104         btCollisionShape* trimeshShape  = new btBvhTriangleMeshShape(trimesh,useQuantizedBvhTree);
105                 
106         //ConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
107         btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
108         btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
109                 
110         btVector3 maxAabb(10000,10000,10000);
111         btBroadphaseInterface* broadphase = new btAxisSweep3(-maxAabb,maxAabb);//SimpleBroadphase();
112         dispatcher->registerCollisionCreateFunc(GIMPACT_SHAPE_PROXYTYPE,GIMPACT_SHAPE_PROXYTYPE,new btSphereSphereCollisionAlgorithm::CreateFunc);
113         
114         btConstraintSolver* constraintSolver = new btSequentialImpulseConstraintSolver();
115         m_dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,constraintSolver,collisionConfiguration);
116         
117         float mass = 0.f;
118         btTransform     startTransform;
119         startTransform.setIdentity();
120         startTransform.setOrigin(btVector3(0,-2,0));
121
122         btRigidBody* staticBody= localCreateRigidBody(mass, startTransform,trimeshShape);
123         //enable custom material callback
124         staticBody->setCollisionFlags(staticBody->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
125
126         {
127                 for (int i=0;i<10;i++)
128                 {
129                         btCollisionShape* sphereShape = new btSphereShape(1);
130                         startTransform.setOrigin(btVector3(1,2*i,1));
131                         //btRigidBody* body = localCreateRigidBody(1, startTransform,sphereShape);
132                         localCreateRigidBody(1, startTransform,sphereShape);
133                 }
134         }
135         
136         
137
138         m_dynamicsWorld->setDebugDrawer(&debugDrawer);
139 }
140
141 void UserCollisionAlgorithm::clientMoveAndDisplay()
142 {
143          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
144
145         float dt = getDeltaTimeMicroseconds() * 0.000001f;
146         
147
148         m_dynamicsWorld->stepSimulation(dt);
149         
150         //optional but useful: debug drawing
151         m_dynamicsWorld->debugDrawWorld();
152
153         renderme();
154
155     glFlush();
156     glutSwapBuffers();
157
158 }
159
160
161
162
163
164
165 void UserCollisionAlgorithm::displayCallback(void) {
166
167     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
168
169         renderme();
170
171     glFlush();
172     glutSwapBuffers();
173 }
174
175