source: proiecte/SIMEO/Simeo/src/SimeoEngine/Environment.cpp @ 167

Last change on this file since 167 was 167, checked in by (none), 14 years ago

Simeo: added final project and also older proof of concept code.

We used Git for version control, so look at the Git repo
in SIMEO/Simeo/ for more info.

File size: 6.8 KB
Line 
1#include <stdio.h>
2#include "Environment.h"
3#include "BulletObject.h"
4
5Environment::Environment()
6: m_dynamicsWorld(0)
7{
8}
9
10/** Initialize the environment of the virtual world.
11        Create de ground and add it as an object to the virtual world
12*/
13void Environment::setupEnvironment()
14{
15        btScalar mass(0.);
16        bool isDynamic = (mass != 0.f);
17        btVector3 localInertia(0,0,0);
18        btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver;
19
20        m_collisionConfiguration = new btDefaultCollisionConfiguration();
21        m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
22        m_broadphase = new btDbvtBroadphase();
23        m_solver = sol;
24
25        m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
26        m_dynamicsWorld->setGravity(btVector3(0,-10,0));
27
28        groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
29        m_collisionShapes.push_back(groundShape);
30        groundTransform.setIdentity();
31        groundTransform.setOrigin(btVector3(0,-50,0));
32
33        //rigidbody is dynamic if and only if mass is non zero, otherwise static
34        if (isDynamic)
35                groundShape->calculateLocalInertia(mass, localInertia);
36
37        //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
38        btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
39        btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, myMotionState, groundShape, localInertia);
40        btRigidBody* body = new btRigidBody(rbInfo);
41        //add the body to the dynamics world
42        m_dynamicsWorld->addRigidBody(body);
43//      m_dynamicsWorld->setDebugDrawer(new GLDebugDrawer());
44}
45
46
47void Environment::addAgent(Agent *agent)
48{
49}
50
51btDynamicsWorld* Environment::getDynamicsWorld()
52{
53        return m_dynamicsWorld;
54}
55
56btAlignedObjectArray<btCollisionShape*> Environment::getCollisionShapes()
57{
58        return m_collisionShapes;
59}
60
61/// Simulation 2D
62void Environment::createMagicCarpet(int n, int m) {
63        int i, j, base = m_dynamicsWorld->getNumCollisionObjects();
64        btCollisionShape *shape;
65        btRigidBody *body, *body1, *body2;
66        btVector3 pivotA, pivotB, axisA, axisB;
67        btHingeConstraint* hinge;
68        btTransform transform;
69        btTransform frameA, frameB;
70
71        BulletObject *object = new BulletObject(this);
72
73        for(i = 0; i < n; i++) {
74                for(j = 0; j < m; j++) {
75                        transform = btTransform();
76                        transform.setIdentity();
77                        transform.setOrigin(SCALING*btVector3(i*1.0,HEIGHT,j*1.0));
78                        transform.setRotation(btQuaternion(0, 0, 0));
79
80                        shape = new btSphereShape(SCALING*btScalar(0.1));
81                        body = object->createBulletObject(shape, transform, (!i && !j) ? 1.f : 1.f);
82
83                        btAlignedObjectArray<int> tmp;
84                        tmp.resize(4);
85                        m_matrix.push_back(tmp);
86                }
87        }
88
89        for(i = 0; i < n; i++) {
90                for(j = 0; j < m; j++) {
91                        if (i > 0) {
92                                transform = btTransform();
93                                transform.setIdentity();
94                                transform.setOrigin(SCALING*btVector3(i*1.0-0.5,HEIGHT,j*1.0));
95                                transform.setRotation(btQuaternion(0, 0, -M_PI/2));
96
97                                shape = new btCapsuleShape(SCALING*btScalar(0.05),SCALING*btScalar(0.6));
98                                body = object->createBulletObject(shape, transform, 1.f);
99
100                                body1 = btRigidBody::upcast(m_dynamicsWorld->getCollisionObjectArray()[base + (i - 1) * m + j]);
101                                body2 = btRigidBody::upcast(m_dynamicsWorld->getCollisionObjectArray()[base + i * m + j]);
102
103                                frameA = btTransform();
104                                frameA.setIdentity();
105                                frameA.setOrigin(SCALING*btVector3(0, -0.5, 0));
106                                frameA.setRotation(btQuaternion(0, 0, M_PI/2));
107                                frameB = btTransform();
108                                frameB.setIdentity();
109                                frameB.setOrigin(SCALING*btVector3(0, 0, 0));
110                                frameB.setRotation(btQuaternion(0, 0, 0));
111                                hinge = new btHingeConstraint(*body, *body1, frameA, frameB);
112                                hinge->setLimit(btScalar(- M_PI/2), btScalar(M_PI/2));
113                                hinge->setDbgDrawSize(btScalar(1.f));
114                                m_matrix[(i - 1) * m + j][RIGHT] = m_joints.size();
115                                m_joints.push_back(hinge);
116                                m_dynamicsWorld->addConstraint(hinge, true);
117
118                                frameA = btTransform();
119                                frameA.setIdentity();
120                                frameA.setOrigin(SCALING*btVector3(0, 0.5, 0));
121                                frameA.setRotation(btQuaternion(0, 0, M_PI/2));
122                                frameB = btTransform();
123                                frameB.setIdentity();
124                                frameB.setOrigin(SCALING*btVector3(0, 0, 0));
125                                frameB.setRotation(btQuaternion(0, 0, 0));
126                                hinge = new btHingeConstraint(*body, *body2, frameA, frameB);
127                                hinge->setLimit(btScalar(- M_PI/2), btScalar(M_PI/2));
128                                hinge->setDbgDrawSize(btScalar(1.f));
129                                m_matrix[i * m + j][LEFT] = m_joints.size();
130                                m_joints.push_back(hinge);
131                                m_dynamicsWorld->addConstraint(hinge, true);
132                        }
133                        if (j > 0) {
134                                transform = btTransform();
135                                transform.setIdentity();
136                                transform.setOrigin(SCALING*btVector3(i*1.0,HEIGHT,j*1.0-0.5));
137                                transform.setRotation(btQuaternion(-M_PI/2, 0, -M_PI/2));
138
139                                shape = new btCapsuleShape(SCALING*btScalar(0.05),SCALING*btScalar(0.6));
140                                body = object->createBulletObject(shape, transform, 1.f);
141
142                                body1 = btRigidBody::upcast(m_dynamicsWorld->getCollisionObjectArray()[base + i * m + (j - 1)]);
143                                body2 = btRigidBody::upcast(m_dynamicsWorld->getCollisionObjectArray()[base + i * m + j]);
144
145                                frameA = btTransform();
146                                frameA.setIdentity();
147                                frameA.setOrigin(SCALING*btVector3(0, -0.5, 0));
148                                frameA.setRotation(btQuaternion(0, 0, 0));
149                                frameB = btTransform();
150                                frameB.setIdentity();
151                                frameB.setOrigin(SCALING*btVector3(0, 0, 0));
152                                frameB.setRotation(btQuaternion(-M_PI/2, 0, -M_PI/2));
153                                hinge = new btHingeConstraint(*body, *body1, frameA, frameB);
154                                hinge->setLimit(btScalar(- M_PI/2), btScalar(M_PI/2));
155                                hinge->setDbgDrawSize(btScalar(1.f));
156                                m_matrix[i * m + (j - 1)][DOWN] = m_joints.size();
157                                m_joints.push_back(hinge);
158                                m_dynamicsWorld->addConstraint(hinge, true);
159
160                                frameA = btTransform();
161                                frameA.setIdentity();
162                                frameA.setOrigin(SCALING*btVector3(0, 0.5, 0));
163                                frameA.setRotation(btQuaternion(0, 0, 0));
164                                frameB = btTransform();
165                                frameB.setIdentity();
166                                frameB.setOrigin(SCALING*btVector3(0, 0, 0));
167                                frameB.setRotation(btQuaternion(-M_PI/2, 0, -M_PI/2));
168                                hinge = new btHingeConstraint(*body, *body2, frameA, frameB);
169                                hinge->setLimit(btScalar(- M_PI/2), btScalar(M_PI/2));
170                                hinge->setDbgDrawSize(btScalar(1.f));
171                                m_matrix[i * m + j][UP] = m_joints.size();
172                                m_joints.push_back(hinge);
173                                m_dynamicsWorld->addConstraint(hinge, true);
174                        }
175                }
176        }
177}
178
179
180/** Remove all objects from the virtual world */
181void Environment::exitEnvironment()
182{
183        int i;
184        for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
185        {
186                btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i];
187                btRigidBody* body = btRigidBody::upcast(obj);
188                if (body && body->getMotionState())
189                {
190                        delete body->getMotionState();
191                }
192                m_dynamicsWorld->removeCollisionObject( obj );
193                delete obj;
194        }
195
196        //delete collision shapes
197        for (int j=0;j<m_collisionShapes.size();j++)
198        {
199                btCollisionShape* shape = m_collisionShapes[j];
200                delete shape;
201        }
202
203        delete m_dynamicsWorld;
204        delete m_solver;
205        delete m_broadphase;
206        delete m_dispatcher;
207        delete m_collisionConfiguration;
208}
Note: See TracBrowser for help on using the repository browser.