-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.cpp
80 lines (68 loc) · 2.25 KB
/
world.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
#include <iostream>
#include <bullet/btBulletDynamicsCommon.h>
#include "world.h"
#include "body.h"
#include "abstractGlBody.h"
World :: World(float gravity) {
// Initializing the world.//////////////////////////////////////////////
// Build the broadphase (an algorithm for collision detection).
btBroadphaseInterface* broadphase = new btDbvtBroadphase();
// Build collision config. and dispatcher.
btDefaultCollisionConfiguration* collisionConfiguration =
new btDefaultCollisionConfiguration();
btCollisionDispatcher* dispatcher =
new btCollisionDispatcher(collisionConfiguration);
// Build the actual physics solver.
btSequentialImpulseConstraintSolver* solver =
new btSequentialImpulseConstraintSolver;
// Build the world. All the stuff above are passed to create the world.
this->dynamicsWorld = new btDiscreteDynamicsWorld(
dispatcher,
broadphase,
solver,
collisionConfiguration);
// Set Gravity (Y-axis is "up" here)
dynamicsWorld->setGravity(btVector3(0, -gravity, 0));
////////////////////////////////////////////////////////////////////////
}
World :: ~ World(){
std::cout << std::endl << "....destroy world...." << std::endl;
}
void World :: drawBodies() {
for(auto b : this->bodies) {
b->update();
b->draw();
}
}
void World :: update() {
for(auto b : this->bodies) {
b->update();
}
}
void World :: addBody(Body &body) {
bodies.push_back(&body);
this->dynamicsWorld->addRigidBody(body.getRigidBody());
}
void World :: removeBody(Body &body) {
int removeIndex = -1;
int indexCount = 0;
// Find the index of the body, if it exists.
for(auto b : bodies){
if(b == &body){
removeIndex = indexCount;
break;
}
indexCount++;
}
// Return if the body does not exist.
if(removeIndex == -1){
return;
}
// Remove the body if it exists.
bodies.erase(bodies.begin() + indexCount);
// Remove from Bullet world.
this->dynamicsWorld->removeRigidBody(body.getRigidBody());
}
btDiscreteDynamicsWorld* World :: getDynamicsWorld() const {
return this->dynamicsWorld;
}