-
Notifications
You must be signed in to change notification settings - Fork 2
/
BodyEmitter.cpp
269 lines (214 loc) · 6.81 KB
/
BodyEmitter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "BodyEmitter.h"
#include "Camera.h"
#include "Gate.h"
#include "Ship.h"
#include "SpaceBat.h"
#define BOUNDARY_X 50.0
#define BOUNDARY_Y 44.0
#define BOUNDARY_Z -1000.0
const int BODIES_TO_EMIT = 15;
using namespace std;
BodyEmitter::BodyEmitter(btDiscreteDynamicsWorld *world) {
this->world = world;
accum = 0.0;
boostMode = false;
wallShape = new btStaticPlaneShape(btVector3(0, 0, -1), -80);
wall = new btCollisionObject();
wall->setCollisionShape(wallShape);
contactCallback = new ContactCallback(this);
emitSpeed = 1;
}
BodyEmitter::~BodyEmitter() {
delete wall;
delete wallShape;
delete contactCallback;
}
void BodyEmitter::emit(BodyType type, ParticleEngine* pEngine){
btVector3 pos = getPositionForType(type);
btDefaultMotionState *motionState =
new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), pos));
btScalar mass = getMassForType(type);
btRigidBody::btRigidBodyConstructionInfo
constructionInfo(mass, motionState, models[type].getCollisionShape());
Body* newBody;
if(type == GATE){
newBody = new Gate(&models[type], constructionInfo, type, pEngine );
}else if(type == SPACEBAT){
newBody = new SpaceBat(&models[type], constructionInfo, type, pEngine);
}else{
newBody = new Body(&models[type], constructionInfo, type);
}
newBody->setLinearVelocity(getLinearVelocityForType(type));
newBody->setAngularVelocity(getAngularVelocityForType(type));
world->addRigidBody(newBody);
bodies.push_back(newBody);
}
void BodyEmitter::clear(){
bodies.clear();
}
void BodyEmitter::setSpeed(float speed){
list<Body*>::iterator it;
for(it = bodies.begin(); it != bodies.end(); it++){
Body* b = *it;
btVector3 vel = b->getLinearVelocity();
vel.setZ(speed);
b->setLinearVelocity(vel);
}
}
void BodyEmitter::boostSpeed(){
setSpeed(BOOST_SPEED);
}
void BodyEmitter::resetSpeed(){
setSpeed(NORMAL_SPEED);
}
void BodyEmitter::setBoostMode(bool boost){
boostMode = boost;
}
float RandomFloat(float min, float max){
float t = (float)rand() / RAND_MAX;
return min + t * (max - min);
}
btVector3 BodyEmitter::getAngularVelocityForType(BodyType type){
switch (type){
case PEPSI:
return btVector3(RandomFloat(2,4), RandomFloat(1,2), RandomFloat(2,4));
//case MARS:
//case APPLE:
// return btVector3(0,1,0);
case GATE:
case SPACEBAT:
case BIGBAT:
case JUPITER:
return btVector3(0,0,0);
//case VENUS:
case LUSH:
return btVector3(0,0.1,0);
default:
return btVector3(RandomFloat(-1,1), RandomFloat(-1,1), RandomFloat(-1,1));
}
}
btVector3 BodyEmitter::getPositionForType(BodyType type){
switch (type) {
case JUPITER:
case END:
case BIGRED:
case BIGBAT:
return btVector3(0,0, BOUNDARY_Z);
case GATE:
//case APPLE:
case PEPSI:
return btVector3(
((float)rand() / RAND_MAX * 2.0 * BOUNDARY_X - BOUNDARY_X) / 2.0,
((float)rand() / RAND_MAX * 2.0 * BOUNDARY_Y - BOUNDARY_Y) / 2.0,
BOUNDARY_Z );
default:
return btVector3(
(float)rand() / RAND_MAX * 2.0 * BOUNDARY_X - BOUNDARY_X,
(float)rand() / RAND_MAX * 2.0 * BOUNDARY_Y - BOUNDARY_Y,
BOUNDARY_Z );
}
}
btVector3 BodyEmitter::getLinearVelocityForType(BodyType type){
float speed = boostMode ? BOOST_SPEED : NORMAL_SPEED;
switch(type){
case GATE:
case JUPITER:
return btVector3(0,0, speed);
//case VENUS:
//case APPLE:
case PEPSI:
return btVector3(0,0, 25);
case LUSH:
return btVector3(0.2,0.02, 0.03);
default:
// return btVector3(RandomFloat(-1,1),RandomFloat(-1,1),speed);
return btVector3(0,0, speed);
}
}
btScalar BodyEmitter::getMassForType(BodyType type){
switch(type){
case GATE:
return 10;
//case VENUS:
case LUSH:
return 1000;
case END:
case BIGRED:
case BIGBAT:
return 10000;
default:
return 4;
}
}
void BodyEmitter::setEmitSpeed(float speed){
emitSpeed = speed;
}
void BodyEmitter::emitBodies(float tstep, ParticleEngine* pEngine, Level* level) {
accum += tstep;
world->contactTest(wall, *contactCallback);
if(emitSpeed == 0) return;
if (accum > emitSpeed) {
accum = 0.0;
if(level->levelTypes.size() > 0){
for(int i = 0; i < level->count; i++){
int index = rand() % level->levelTypes.size();
BodyType type = level->levelTypes[index];
emit(type, NULL);
}
}
if(level->hasGates)
emit(GATE, pEngine);
}
}
void BodyEmitter::drawBodies(RenderPass pass) {
list<Body*>::iterator it;
for(it = bodies.begin(); it != bodies.end(); it++)
(*it)->draw(pass);
}
void BodyEmitter::loadModels() {
//models[MARS].loadFromFile("models/mars", "mars.3ds", importers[MARS]);
models[ASTEROID].loadFromFile("models/ass", "asteroid.obj", importers[ASTEROID]);
models[ASTEROID].setScaleFactor(2);
//models[GATE].loadFromFile("models/aster", "roid.obj", importers[GATE]);
models[EROS].loadFromFile("models/eros", "eros.3ds", importers[EROS]);
models[EROS].setScaleFactor(0.7);
models[GOLEVKA].loadFromFile("models/golevka", "golevka.3ds", importers[GOLEVKA]);
models[GOLEVKA].setScaleFactor(0.05);
models[JUNO].loadFromFile("models/juno", "juno.3ds", importers[JUNO]);
models[JUNO].setScaleFactor(0.05);
models[GATE].loadFromFile("models/gayte", "gate.obj", importers[GATE]);
models[GATE].setScaleFactor(0.5);
//models[SPACEBAT].loadFromFile("models/spacebat", "batty.obj", importers[SPACEBAT]);
models[SPACEBAT].loadFromFile("models/spacebat", "LOWRESBAT.obj", importers[SPACEBAT]);
models[SPACEBAT].setScaleFactor(4);
models[LUSH].loadFromFile("models/lush", "lush.3DS", importers[LUSH]);
models[JUPITER].loadFromFile("models/jupiter", "jupiter.3ds", importers[JUPITER]);
models[JUPITER].setScaleFactor(50);
models[PEPSI].loadFromFile("models/pepsi", "pepsi.3ds", importers[PEPSI]);
models[PEPSI].setScaleFactor(4);
models[END].loadFromFile("models/ass", "asteroid.obj", importers[END]);
models[END].setScaleFactor(20);
models[MINE].loadFromFile("models/mine", "mine.obj", importers[MINE]);
models[MINE].setScaleFactor(2);
models[BIGRED].loadFromFile("models/mine", "mine.obj", importers[BIGRED]);
models[BIGRED].setScaleFactor(12);
models[BIGBAT].loadFromFile("models/spacebat", "LOWRESBAT.obj", importers[BIGBAT]);
models[BIGBAT].setScaleFactor(20);
}
BodyEmitter::ContactCallback::ContactCallback(BodyEmitter *bodyEmitter)
: btCollisionWorld::ContactResultCallback(), bodyEmitter(bodyEmitter) {}
btScalar BodyEmitter::ContactCallback::addSingleResult(btManifoldPoint &cp,
const btCollisionObject *colObj0, int partId0, int index0,
const btCollisionObject *colObj1, int partId1, int index1) {
Body *body = (colObj0 == bodyEmitter->wall)? (Body *)colObj1 : (Body *)colObj0;
bodyEmitter->world->removeRigidBody(body);
list<Body*>::iterator it;
for (it = bodyEmitter->bodies.begin(); it != bodyEmitter->bodies.end(); it++) {
if (*it == body) {
bodyEmitter->bodies.erase(it);
delete body;
break;
}
}
return 0.0;
}