-
Notifications
You must be signed in to change notification settings - Fork 0
/
SMovementSystem.cpp
173 lines (154 loc) · 7.14 KB
/
SMovementSystem.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
#include "SMovementSystem.h"
using namespace lf;
SMovementSystem::SMovementSystem()
{
//ctor
}
SMovementSystem::~SMovementSystem()
{
//dtor
}
void SMovementSystem::configure(entityx::ptr<EventManager> event_manager)
{
events = event_manager;
}
void SMovementSystem::update(entityx::ptr<EntityManager> es, entityx::ptr<EventManager> events, double dt)
{
for (auto entity : es->entities_with_components<PositionComponent>())
{
entityx::ptr<PositionComponent> pos = entity.component<PositionComponent>();
if(pos)
{
entityx::ptr<InputComponent> input = entity.component<InputComponent>();
if(input)
{
entityx::ptr<ModelComponent> model = entity.component<ModelComponent>();
entityx::ptr<PhysicsComponent> physics = entity.component<PhysicsComponent>();
entityx::ptr<PhysicsGhostComponent> ghost = entity.component<PhysicsGhostComponent>();
if(model && physics)
{
btTransform transform;
physics->rigidBody->getMotionState()->getWorldTransform(transform);
btQuaternion rotation = transform.getRotation();
core::quaternion lfrot = gravutil::getLFQuatFromBT(rotation);
// create orientation vectors
// btVector3 up(0, 1, 0);
// btVector3 lookat = quatRotate(rotation, btVector3(0, 0, 1));
// btVector3 forward = btVector3(lookat.getX(), 0, lookat.getZ()).normalize();
// btVector3 side = btCross(up, forward);
core::vector3df lfup = lfrot * core::vector3df(0, 1, 0);
core::vector3df lfforward = lfrot * core::vector3df(0, 0, 1);
core::vector3df lfside = lfrot * core::vector3df(1, 0, 0);
btVector3 up = gravutil::getBTVecFromLF(lfup);
btVector3 forward = gravutil::getBTVecFromLF(lfforward);
btVector3 side = gravutil::getBTVecFromLF(lfside);
if(!input->shift)
{
if(input->up == true)
{
physics->rigidBody->applyCentralImpulse(forward * 100);
input->up = false;
}
if(input->down == true)
{
physics->rigidBody->applyCentralImpulse(forward * -100);
input->down = false;
}
}
else
{
if(input->up == true)
{
physics->rigidBody->applyCentralImpulse(up * 100);
input->up = false;
}
if(input->down == true)
{
physics->rigidBody->applyCentralImpulse(up * -100);
input->down = false;
}
}
if(input->left == true)
{
physics->rigidBody->applyCentralImpulse(side * -100);
input->left = false;
}
if(input->right == true)
{
physics->rigidBody->applyCentralImpulse(side * 100);
input->right = false;
}
if(input->yaw != 0 || input->pitch != 0)
{
//std::cout << "Yaw : " << input->yaw << " Pitch : " << input->pitch << " DT : " << dt << std::endl;
btVector3 Amount(input->yaw * dt , input->pitch * dt, 0);
input->yaw = 0;
input->pitch = 0;
// rotate camera with quaternions created from axis and angle
//rotation.setRotation(up, 10);
rotation = btQuaternion(up, Amount.getX()) * rotation;
rotation = btQuaternion(side, Amount.getY()) * rotation;
//rotation = btQuaternion(forward, Amount.getZ()) * rotation;
// set new rotation
//std::cout << "Yaw : " << input->yaw << " Pitch : " << input->pitch << std::endl;
//std::cout << "Rot. Angle : " << rotation.getAngle() << std::endl;
//std::cout << "Rot. X : " << rotation.getX() << " Rot. X : " << rotation.getY() << " Rot. X : " << rotation.getZ() << std::endl;
//std::cout << "UP. X : " << up.getX() << " UP. X : " << up.getY() << " UP. X : " << up.getZ() << std::endl;
//std::cout << "FWD. X : " << forward.getX() << " FWD. X : " << forward.getY() << " FWD. X : " << forward.getZ() << std::endl;
//std::cout << "SIDE. X : " << side.getX() << " SIDE. X : " << side.getY() << " SIDE. X : " << side.getZ() << std::endl;
transform.setRotation(rotation);
physics->rigidBody->clearForces();
//physics->rigidBody->setLinearVelocity (btVector3(0,0,0));
physics->rigidBody->setAngularVelocity (btVector3(0,0,0));
physics->rigidBody->setWorldTransform(transform);
}
}
else if(model && !physics)
{
if(input->up == true)
{
pos->y += 1.0f;
}
if(input->down == true)
{
pos->y -= 1.0f;
}
if(input->left == true)
{
pos->z += 1.0f;
}
if(input->right == true)
{
pos->z -= 1.0f;
}
this->events->emit<ListenerPositionChangedEvent>(pos->getPositionLF());
}
else
{
if(input->up == true)
{
btVector3 vec = btVector3(0,1,0);
vec = vec.normalized();
physics->rigidBody->applyCentralImpulse(vec * 100);
input->up = false;
}
}
}
}
}
}
/*
for (auto entity : es->entities_with_components<InputComponent, PhysicsGhostComponent>())
{
entityx::ptr<InputComponent> inp = entity.component<InputComponent>();
if(inp->up == true)
{
entityx::ptr<PhysicsGhostComponent> gho = entity.component<PhysicsGhostComponent>();
btVector3 upDir = gho->ghost->getWorldTransform().getBasis()[1];
upDir.normalize();
btVector3 oldpos = gho->ghost->getWorldTransform().getOrigin();
gho->ghost->getWorldTransform().setOrigin(oldpos + upDir);
inp->up = false;
}
}
*/