-
Notifications
You must be signed in to change notification settings - Fork 8
/
PhysicsCheckpoint.cpp
86 lines (69 loc) · 2.67 KB
/
PhysicsCheckpoint.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
// +------------------------------------------------------------+
// | University Racer |
// | Projekt do PGR a GMU, FIT VUT v Brne, 2011 |
// +------------------------------------------------------------+
// | Autori: Tomáš Kimer, [email protected] |
// | Tomáš Sychra, [email protected] |
// | David Šabata, [email protected] |
// +------------------------------------------------------------+
#include "PhysicsCheckpoint.h"
void PhysicsCheckpoint::Initialize(btDiscreteDynamicsWorld *refWorld)
{
m_ghostPairCallback = new btGhostPairCallback();
refWorld->getPairCache()->setInternalGhostPairCallback(m_ghostPairCallback);
m_ghostObject = new btGhostObject();
m_checkpointShape = new btBoxShape(btVector3(CHECKPOINT_SIZE_X, CHECKPOINT_SIZE_Y, CHECKPOINT_SIZE_Z));
m_ghostObject->setCollisionShape(m_checkpointShape);
m_ghostObject->setCollisionFlags(btCollisionObject::CF_NO_CONTACT_RESPONSE);
refWorld->addCollisionObject(m_ghostObject);
}
void PhysicsCheckpoint::Deinitialize()
{
delete m_checkpointShape;
delete m_ghostPairCallback;
}
bool PhysicsCheckpoint::Collision(btRigidBody *body, const GameTime & gameTime)
{
if (PassedFinish())
return false;
for(int i = 0; i < m_ghostObject->getNumOverlappingObjects(); i++)
{
btRigidBody *rigidBody = static_cast<btRigidBody *>(m_ghostObject->getOverlappingObject(i));
if (rigidBody == body)
{
if (m_passedNum+1 == m_transforms.size())
{
m_round++;
if (PassedFinish())
m_finishTime = gameTime.Current();
}
m_passedNum = (m_passedNum+1) % m_transforms.size();
m_ghostObject->setWorldTransform(m_transforms[(m_passedNum+1) % m_transforms.size()]);
if (PassedStart() && !PassedFinish())
m_startTime = gameTime.Current();
return true;
}
}
return false;
}
void PhysicsCheckpoint::Add(const btTransform & trans)
{
if (m_transforms.size() == 0)
m_ghostObject->setWorldTransform(trans);
m_transforms.push_back(trans);
}
void PhysicsCheckpoint::Reset()
{
m_passedNum = -1;
m_round = 0;
m_ghostObject->setWorldTransform(m_transforms[0]);
}
TimeSpan PhysicsCheckpoint::GetTime(const GameTime & gameTime)
{
if (PassedFinish())
return m_finishTime - m_startTime;
else if (FirstPassed())
return gameTime.Current() - m_startTime;
else
return TimeSpan::Zero();
}