Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Demos / GenericJointDemo / GenericJointDemo.cpp
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Ragdoll Demo
4 Copyright (c) 2007 Starbreeze Studios
5
6 This software is provided 'as-is', without any express or implied warranty.
7 In no event will the authors be held liable for any damages arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it freely,
10 subject to the following restrictions:
11
12 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.
13 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
14 3. This notice may not be removed or altered from any source distribution.
15
16 Originally Written by: Marten Svanfeldt
17 ReWritten by: Francisco León
18 */
19
20
21
22 #include "btBulletDynamicsCommon.h"
23 #include "GlutStuff.h"
24 #include "GL_ShapeDrawer.h"
25
26 #include "LinearMath/btIDebugDraw.h"
27
28 #include "GLDebugDrawer.h"
29 #include "GenericJointDemo.h"
30
31
32
33 GLDebugDrawer debugDrawer;
34
35
36
37
38
39
40 void GenericJointDemo::initPhysics()
41 {
42         setTexturing(true);
43         setShadows(true);
44
45         // Setup the basic world
46
47         btDefaultCollisionConfiguration * collision_config = new btDefaultCollisionConfiguration();
48
49         btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collision_config);
50
51         btVector3 worldAabbMin(-10000,-10000,-10000);
52         btVector3 worldAabbMax(10000,10000,10000);
53         btBroadphaseInterface* overlappingPairCache = new btAxisSweep3 (worldAabbMin, worldAabbMax);
54
55         btConstraintSolver* constraintSolver = new btSequentialImpulseConstraintSolver;
56
57
58         m_dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,constraintSolver,collision_config);
59
60         m_dynamicsWorld->setGravity(btVector3(0,-30,0));
61
62         m_dynamicsWorld->setDebugDrawer(&debugDrawer);
63
64         // Setup a big ground box
65         {
66                 btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(200.),btScalar(10.),btScalar(200.)));
67                 btTransform groundTransform;
68                 groundTransform.setIdentity();
69                 groundTransform.setOrigin(btVector3(0,-15,0));
70                 localCreateRigidBody(btScalar(0.),groundTransform,groundShape);
71         }
72
73         // Spawn one ragdoll
74         spawnRagdoll();
75
76         clientResetScene();
77 }
78
79 void GenericJointDemo::spawnRagdoll(bool random)
80 {
81         RagDoll* ragDoll = new RagDoll (m_dynamicsWorld, btVector3 (0,0,10),5.f);
82         m_ragdolls.push_back(ragDoll);
83 }
84
85 void GenericJointDemo::clientMoveAndDisplay()
86 {
87         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
88
89         //simple dynamics world doesn't handle fixed-time-stepping
90         float ms = getDeltaTimeMicroseconds();
91
92         float minFPS = 1000000.f/60.f;
93         if (ms > minFPS)
94                 ms = minFPS;
95
96         if (m_dynamicsWorld)
97         {
98                 m_dynamicsWorld->stepSimulation(ms / 1000000.f);
99                 //optional but useful: debug drawing
100                 m_dynamicsWorld->debugDrawWorld();
101         }
102
103         renderme();
104
105         glFlush();
106
107         swapBuffers();
108 }
109
110 void GenericJointDemo::displayCallback()
111 {
112         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
113
114         if (m_dynamicsWorld)
115                 m_dynamicsWorld->debugDrawWorld();
116
117         renderme();
118
119         glFlush();
120         swapBuffers();
121 }
122
123 void GenericJointDemo::keyboardCallback(unsigned char key, int x, int y)
124 {
125         switch (key)
126         {
127         case 'e':
128                 spawnRagdoll(true);
129                 break;
130         default:
131                 DemoApplication::keyboardCallback(key, x, y);
132         }
133
134
135 }