Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Demos / SimplexDemo / SimplexDemo.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         SimplexDemo demonstrated the working of the subdistance algorithm as used in GJK.
18         It draws the simplex, and calculates the closest vector from simplex to the origin
19 */
20
21 #include "GL_Simplex1to4.h"
22 #include "LinearMath/btQuaternion.h"
23 #include "LinearMath/btTransform.h"
24 #include "GL_ShapeDrawer.h"
25
26 #include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
27 #include "SimplexDemo.h"
28 #include "GlutStuff.h"
29
30 btVoronoiSimplexSolver  simplexSolver;
31
32
33
34 float yaw=0.f,pitch=0.f,roll=0.f;
35 const int maxNumObjects = 4;
36 const int numObjects = 1;
37 int screenWidth = 640;
38 int screenHeight = 480;
39 /// simplex contains the vertices, and some extra code to draw and debug
40 GL_Simplex1to4  simplex;
41
42
43 btPolyhedralConvexShape*        shapePtr[maxNumObjects];
44
45
46 ///
47 ///
48 ///
49 int main(int argc,char** argv)
50 {
51
52         SimplexDemo* demo = new SimplexDemo();
53
54         demo->initPhysics();
55         
56         return glutmain(argc, argv,screenWidth,screenHeight,"SimplexDemo",demo);
57 }
58
59 //to be implemented by the demo
60
61 void SimplexDemo::clientMoveAndDisplay()
62 {
63         
64         displayCallback();
65 }
66
67
68
69 void SimplexDemo::displayCallback()
70 {
71         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
72         glDisable(GL_LIGHTING);
73
74         GL_ShapeDrawer::drawCoordSystem();
75
76         btScalar m[16];
77         int i;
78
79         btVector3 worldBoundsMin(-1000,-1000,-1000);
80         btVector3 worldBoundsMax(1000,1000,1000);
81
82         for (i=0;i<numObjects;i++)
83         {
84                 btTransform transA;
85                 transA.setIdentity();
86                 btVector3       dpos(0.f,5.f,0.f);
87                 transA.setOrigin( dpos );
88                 btQuaternion orn;
89                 orn.setEuler(yaw,pitch,roll);
90                 transA.setRotation(orn);
91                 transA.getOpenGLMatrix( m );
92
93                 /// draw the simplex
94                 m_shapeDrawer->drawOpenGL(m,shapePtr[i],btVector3(1,1,1),getDebugMode(),worldBoundsMin,worldBoundsMax);
95
96                 /// calculate closest point from simplex to the origin, and draw this vector
97                 simplex.calcClosest(m);
98
99         }
100         pitch += 0.005f;
101         yaw += 0.01f;
102
103         glFlush();
104     glutSwapBuffers();
105 }
106
107 void    SimplexDemo::initPhysics()
108 {
109
110         simplex.setSimplexSolver(&simplexSolver);
111
112         simplex.addVertex(btVector3(-2,0,-2));
113         simplex.addVertex(btVector3(2,0,-2));
114         simplex.addVertex(btVector3(0,0,2));
115         simplex.addVertex(btVector3(0,2,0));
116
117         shapePtr[0] = &simplex;
118
119         btTransform tr;
120         tr.setIdentity();
121 }
122
123