-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulator.cpp
More file actions
96 lines (81 loc) · 3.61 KB
/
Simulator.cpp
File metadata and controls
96 lines (81 loc) · 3.61 KB
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
#include "Simulator.h"
#include "GameScreen.h"
#include "Ship.h"
#include "PlayerObject.h"
#include <algorithm>
Simulator::Simulator(Ogre::SceneManager* mgr, GameScreen* gs) : gameScreen(gs)
{
///collision configuration contains default setup for memory, collision setup.
collisionConfiguration = new btDefaultCollisionConfiguration();
///use the default collision dispatcher. For parallel processing you can use a different dispatcher
dispatcher = new btCollisionDispatcher(collisionConfiguration);
///btDbvtBroadphase is a good general purpose broadphase. You can also try out btAxis3Sweep.
overlappingPairCache = new btDbvtBroadphase();
///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
solver = new btSequentialImpulseConstraintSolver();
dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver,collisionConfiguration);
dynamicsWorld->setGravity(btVector3(0, 0, 0)); //we're in space
//keep track of the shapes, we release memory at exit.
//make sure to re-use collision shapes among rigid bodies whenever possible!
btAlignedObjectArray<btCollisionShape*> collisionShapes;
sceneMgr = mgr;
mDebugDrawer = new CDebugDraw(mgr, dynamicsWorld);
dynamicsWorld->setDebugDrawer(mDebugDrawer);
}
Simulator::~Simulator()
{
}
void Simulator::addObject (GameObject* o)
{
objList.insert(o);
// use default collision group/mask values (dynamic/kinematic/static)
dynamicsWorld->addRigidBody(o->getBody());
}
void Simulator::removeObject(GameObject* o)
{
objList.erase(o);
dynamicsWorld->removeRigidBody(o->getBody());
}
void Simulator::stepSimulation(const Ogre::Real elapsedTime, int maxSubSteps, const Ogre::Real fixedTimestep)
{
dynamicsWorld->stepSimulation(elapsedTime, maxSubSteps, fixedTimestep);
if (gameScreen->isSinglePlayer() || !gameScreen->getIsClient()) {
std::vector<PlayerObject*> players = gameScreen->getPlayers();
std::vector<Asteroid*> asts = gameScreen->getAsteroids();
for (PlayerObject* obj : players) {
obj->getCollisionCallback()->ctxt.hit = false;
Ship* ship = dynamic_cast<Ship*>(obj);
if (ship != nullptr) {
ship->getPaddle()->getCollisionCallback()->ctxt.hit = false;
}
}
//collision call back
for (PlayerObject* obj : players) {
Ship* ship = dynamic_cast<Ship*>(obj);
if (ship != nullptr) {
GameObject* paddle = ship->getPaddle();
for (Asteroid* ast : asts) {
btRigidBody* astBody = ast->getBody();
dynamicsWorld->contactPairTest(ship->getBody(), astBody, *(ship->getCollisionCallback()));
dynamicsWorld->contactPairTest(paddle->getBody(), astBody, *(paddle->getCollisionCallback()));
}
for (PlayerObject* targetObj : players) {
if (targetObj != obj) {
dynamicsWorld->contactPairTest(targetObj->getBody(), paddle->getBody(), *(targetObj->getCollisionCallback()));
}
}
} else {
for (Asteroid* ast : asts) {
dynamicsWorld->contactPairTest(obj->getBody(), ast->getBody(), *(obj->getCollisionCallback()));
}
}
}
for (GameObject* obj : objList) {
obj->update();
}
}
//mDebugDrawer->Update(); //uncomment to see collision shapes
}
btDiscreteDynamicsWorld* Simulator::getDynamicsWorld() {
return dynamicsWorld;
}