-
Notifications
You must be signed in to change notification settings - Fork 1
/
BulletManager.cpp
111 lines (103 loc) · 3.85 KB
/
BulletManager.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
#include "BulletManager.h"
#include "WorldState.h"
#include "Profiler.h"
#include "GameState.h"
#include "InputManager.h"
BulletManager::BulletManager()
{
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase();
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
m_dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver,collisionConfiguration);
m_dynamicsWorld->setGravity(btVector3(0,-10,0));
m_playerBody = 0;
}
void BulletManager::tick(int nFps)
{
if (m_playerBody == 0)
{
createPlayerBody();
}
}
void BulletManager::addPhysicsShape(PhysicsShape *shape)
{
btCollisionShape* bodyShape = shape->getCollisionShape();
if (bodyShape == 0)
{
return;
}
btTransform bodyTransform;
bodyTransform.setIdentity();
bodyTransform.setFromOpenGLMatrix(&shape->matrix[0][0]);
btScalar mass(0.);
btVector3 localInertia(0,0,0);
btDefaultMotionState* myMotionState = new btDefaultMotionState(bodyTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,bodyShape,localInertia);
btRigidBody *body = new btRigidBody(rbInfo);
m_dynamicsWorld->addRigidBody(body);
}
void BulletManager::clearDynamicsWorld()
{
delete m_dynamicsWorld;
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase();
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
m_dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver,collisionConfiguration);
m_playerBody = 0;
}
void BulletManager::createPlayerBody()
{
btCollisionShape* bodyShape = new btCapsuleShape(.3f,2.0f);
WorldState *worldState = (WorldState *) GameState::GAMESTATE;
Camera *camera = worldState->getPhysicsManager()->getWorldCameras()->getPlayerCamera();
btTransform bodyTransform;
bodyTransform.setIdentity();
float *translate = camera->getEye();
bodyTransform.setOrigin(btVector3(translate[0],translate[1]-0.79f,translate[2]));
btScalar mass(10.);
btVector3 localInertia(0,0,0);
bodyShape->calculateLocalInertia(mass,localInertia);
btDefaultMotionState* myMotionState = new btDefaultMotionState(bodyTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,bodyShape,localInertia);
m_playerBody = new btRigidBody(rbInfo);
m_playerBody->setAngularFactor(0);
m_dynamicsWorld->addRigidBody(m_playerBody);
}
void BulletManager::updateDynamicsWorld(float nSpeed)
{
Profiler::getInstance()->startProfile("Update Dynamics World");
// move camera
WorldState *worldState = (WorldState *) GameState::GAMESTATE;
WorldCamera *camera = worldState->getPhysicsManager()->getWorldCameras()->getPlayerCamera();
float nOldEye[3];
nOldEye[0] = camera->getEyeX();
nOldEye[1] = camera->getEyeY();
nOldEye[2] = camera->getEyeZ();
if (InputManager::getInstance()->isKeyDown('w'))
{
camera->moveForward(nSpeed*0.1f);
}
if (InputManager::getInstance()->isKeyDown('s'))
{
camera->moveBackward(nSpeed*0.1f);
}
if (InputManager::getInstance()->isKeyDown('a'))
{
camera->moveLeft(nSpeed*0.1f);
}
if (InputManager::getInstance()->isKeyDown('d'))
{
camera->moveRight(nSpeed*0.1f);
}
float *nNewEye = camera->getEye();
m_playerBody->activate(true);
m_playerBody->setLinearVelocity(100.0f*btVector3(nNewEye[0]-nOldEye[0],-0.04f,nNewEye[2]-nOldEye[2]));
// check physics
m_dynamicsWorld->stepSimulation(1.f/60.f,10);
// update camera
btVector3 camPos = m_playerBody->getWorldTransform().getOrigin();
camera->setPosition(camPos[0],camPos[1]+0.8,camPos[2]);
Profiler::getInstance()->endProfile();
}