-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollisionController.cpp
More file actions
386 lines (322 loc) · 11.1 KB
/
CollisionController.cpp
File metadata and controls
386 lines (322 loc) · 11.1 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include <iostream>
#include "CollisionController.hpp"
template <typename T>
b2Vec2
sfVecToB2Vec(sf::Vector2<T> vector)
{
return b2Vec2(vector.x / sfdd::SCALE, vector.y / sfdd::SCALE);
}
CollisionManager::CollisionManager()
: mapX(0)
, mapY(0)
, points(0)
{
gravity.Set(0.0f, 40.0f);
world = std::unique_ptr<b2World>(new b2World(gravity));
world->SetContactListener(&listener);
}
void
CollisionManager::update(sf::Time timeDelta, Agent* agent)
{
sf::FloatRect rect = agent->animatedSprite->getLocalBounds();
b2Vec2 vel = agentBody->GetLinearVelocity();
float desiredVel = 0;
for (auto cmd : agent->eventManager.currentEvent->commands) {
if (cmd == command_e::WALK_LEFT) {
agent->currentDir = Agent::direction::LEFT;
desiredVel = -5.0f;
}
if (cmd == command_e::WALK_RIGHT) {
agent->currentDir = Agent::direction::RIGHT;
desiredVel = 5.0f;
}
if (cmd == command_e::STOP) {
desiredVel = 0.0f;
} else {
updatePaths();
}
if (cmd == command_e::JUMP && agent->phys.isOnGround) {
b2Vec2 vel = agentBody->GetLinearVelocity();
vel.y = -10; // upwards - don't change x velocity
agentBody->SetLinearVelocity(vel);
}
if (cmd == command_e::RESTART) {
agentBody->SetTransform(b2Vec2(rect.width / 2.0f / sfdd::SCALE,
rect.height / 2.0f / sfdd::SCALE),
0);
agentBody->SetLinearVelocity(b2Vec2(0, 0));
agentBody->SetAwake(true);
}
}
float velChange = desiredVel - vel.x;
float impulse = agentBody->GetMass() * velChange;
agentBody->ApplyLinearImpulse(b2Vec2(impulse, 0), agentBody->GetWorldCenter(),
true);
const int velocityIterations = 6;
const int positionIterations = 2;
world->Step(1.0f / 60.0f, velocityIterations, positionIterations);
// Set the position of our agent
b2Vec2 bodyPos = agentBody->GetPosition();
agent->animatedSprite->setPosition(
bodyPos.x * sfdd::SCALE - rect.width / 2.0f,
bodyPos.y * sfdd::SCALE - rect.height / 2.0f);
agent->phys.isOnGround = listener.isOnGround();
for (auto body : bodiesToDelete) {
world->DestroyBody(body);
points++;
}
bodiesToDelete.clear();
}
/**
* Create box2d bodies and insert into world
*/
void
CollisionManager::initPhysics(Agent* agent)
{
pathFinder.setMap(tileMap);
std::vector<bool> tilesVisited(mapWidth * mapHeight, false);
const float tileScale = tileSize / sfdd::SCALE;
for (int in = 0; in < mapWidth; in++) {
sf::Vector2f firstVec(-1.0f, -1.0f);
bool visited = false;
for (int x = 0; x < mapWidth; x++) {
for (int y = 0; y < mapHeight; y++) {
if (!isPassable(x, y) && !tilesVisited[y * mapWidth + x]) {
firstVec.x = x;
firstVec.y = y;
tilesVisited[y * mapWidth + x] = true;
visited = true;
break;
}
}
if (visited)
break;
}
if (!visited)
continue;
tilesVisited[firstVec.y * mapWidth + firstVec.x] = true;
sf::Vector2f secondVec = firstVec;
while ((secondVec.x + 1) < mapWidth &&
!isPassable(secondVec.x + 1, secondVec.y)) {
tilesVisited[secondVec.y * mapWidth + secondVec.x] = true;
secondVec.x++;
}
tilesVisited[secondVec.y * mapWidth + secondVec.x] = true;
sf::FloatRect rect(firstVec, (secondVec - firstVec + sf::Vector2f(1, 1)));
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(
(rect.left + rect.width) * tileScale - (rect.width * tileScale / 2.0f),
(rect.top + rect.height) * tileScale - (rect.height * tileScale / 2.0f));
b2Body* groundBody = world->CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
groundBox.SetAsBox(rect.width * tileScale / 2.0f,
rect.height * tileScale / 2.0f);
groundBody->CreateFixture(&groundBox, 0.0f);
}
// Create agent body
sf::FloatRect bounds = agent->animatedSprite->getGlobalBounds();
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set((bounds.left + bounds.width / 2.0f) / sfdd::SCALE,
(bounds.top + bounds.height / 2.0f) / sfdd::SCALE);
bodyDef.fixedRotation = true;
agentBody = world->CreateBody(&bodyDef);
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(bounds.width / 2.0f / sfdd::SCALE,
bounds.height / 2.0f / sfdd::SCALE);
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
agentBody->CreateFixture(&fixtureDef);
b2PolygonShape rightSideBox;
rightSideBox.SetAsBox(
bounds.width / 8.0f / sfdd::SCALE, bounds.height / 2.0f / sfdd::SCALE,
b2Vec2(
bounds.width / 2.0f / sfdd::SCALE - bounds.width / 8.0f / sfdd::SCALE, 0),
0);
b2FixtureDef rightSideDef;
rightSideDef.shape = &rightSideBox;
rightSideDef.density = 1.0f;
rightSideDef.friction = 0.0f;
agentBody->CreateFixture(&rightSideDef);
b2PolygonShape leftSideBox;
leftSideBox.SetAsBox(bounds.width / 8.0f / sfdd::SCALE,
bounds.height / 2.0f / sfdd::SCALE,
b2Vec2(-bounds.width / 2.0f / sfdd::SCALE, 0), 0);
b2FixtureDef leftSideDef;
leftSideDef.shape = &leftSideBox;
leftSideDef.density = 1.0f;
leftSideDef.friction = 0.0f;
agentBody->CreateFixture(&leftSideDef);
b2PolygonShape polygonShape;
// fixture definition
b2FixtureDef myFixtureDef;
myFixtureDef.shape = &polygonShape;
myFixtureDef.density = 1;
// add foot sensor fixture
polygonShape.SetAsBox(bounds.width / 4.0f / sfdd::SCALE,
bounds.height / 16.0f / sfdd::SCALE,
b2Vec2(0, bounds.height / 2.0f / sfdd::SCALE), 0);
myFixtureDef.isSensor = true;
b2Fixture* footSensorFixture = agentBody->CreateFixture(&myFixtureDef);
footSensorFixture->SetUserData((void*)collisionType_e::FOOT);
// add sensors for coins
b2PolygonShape coinBox;
coinBox.SetAsBox(tileSize / 3.0f / sfdd::SCALE,
tileSize / 3.0f / sfdd::SCALE);
for (int y = 0; y < mapHeight; y++) {
for (int x = 0; x < mapWidth; x++) {
if (tileObjArr[y * mapWidth + x] == collisionType_e::COIN) {
b2BodyDef coinDef;
b2Body* coinBody;
coinDef.type = b2_staticBody;
coinDef.position.Set((x * tileSize + tileSize / 2.0f) / sfdd::SCALE,
(y * tileSize + tileSize / 2.0f) / sfdd::SCALE);
coinBody = world->CreateBody(&coinDef);
b2FixtureDef coinFixtureDef;
coinFixtureDef.shape = &coinBox;
coinFixtureDef.density = 1;
coinFixtureDef.isSensor = true;
b2Fixture* coinSensorFixture = coinBody->CreateFixture(&coinFixtureDef);
coinSensorFixture->SetUserData((void*)collisionType_e::COIN);
listener.tileArr = tileObjArr;
listener.tileMap = objMap;
listener.tileSize = tileSize;
listener.mapWidth = mapWidth;
listener.mapHeight = mapHeight;
listener.controller = this;
}
}
}
}
void
CollisionManager::draw()
{
world->DrawDebugData();
}
bool
CollisionManager::isPassable(int tileX, int tileY)
{
return map[tileY * mapWidth + tileX] == 0;
}
void
CollisionManager::scheduleDelete(b2Body* body)
{
bodiesToDelete.insert(body);
}
int
CollisionManager::getPoints() const
{
return points;
}
std::vector<sf::Vertex>
CollisionManager::getJumpPath(sf::Vector2i start, sf::Vector2i target)
{
sf::Vector2f startPos((start.x) * tileSize + tileSize / 2.0f,
(start.y + 1) * tileSize + tileSize / 2.0f);
sf::Vector2f targetPos((target.x) * tileSize + tileSize / 2.0f,
(target.y + 1) * tileSize + tileSize / 2.0f);
const float gravity = 40.0f;
const float vY = -10.0f;
const float vX = 5.0f;
float vertDist = (targetPos - startPos).y / sfdd::SCALE;
float horzDist = (targetPos - startPos).x / sfdd::SCALE;
float timeToTop = -vY / gravity;
float jumpHeight = -(vY * timeToTop + gravity * timeToTop * timeToTop / 2.0f);
std::vector<sf::Vertex> arcPoints;
if (jumpHeight >= vertDist) {
float fallDistance = jumpHeight - vertDist;
float timeToFall = std::sqrt(2.0f * fallDistance / gravity);
float jumpTime = timeToTop + timeToFall;
float jumpLength = vX * jumpTime;
bool arcSetFirst = false;
float startX = startPos.x;
float startY = startPos.y;
for (float t = 0; t < jumpTime; t += 1.0f / 60.0f) {
float x = vX * t;
float y = vY * t + gravity * t * t / 2.0f;
sf::Vertex vertex(
sf::Vector2f(startX + x * sfdd::SCALE, startY + y * sfdd::SCALE),
sf::Color::Red);
if (arcSetFirst)
arcPoints.push_back(vertex);
arcPoints.push_back(vertex);
arcSetFirst = true;
}
}
return arcPoints;
}
bool
CollisionManager::canJumpBetween(sf::Vector2i start, sf::Vector2i target)
{
sf::Vector2f startPos((start.x) * tileSize + tileSize / 2.0f,
(start.y + 1) * tileSize + tileSize / 2.0f);
sf::Vector2f targetPos((target.x) * tileSize + tileSize / 2.0f,
(target.y + 1) * tileSize + tileSize / 2.0f);
const float gravity = 40.0f;
const float vY = -10.0f;
const float vX = 5.0f;
const sf::Vector2f distanceVec{targetPos - startPos};
float vertDist = distanceVec.y / sfdd::SCALE;
float horzDist = distanceVec.x / sfdd::SCALE;
float timeToTop = -vY / gravity;
float jumpHeight = -(vY * timeToTop + gravity * timeToTop * timeToTop / 2.0f);
std::vector<sf::Vertex> arcPoints;
if (jumpHeight >= vertDist) {
float fallDistance = jumpHeight - vertDist;
float timeToFall = std::sqrt(2.0f * fallDistance / gravity);
float jumpTime = timeToTop + timeToFall;
float jumpLength = vX * jumpTime;
return true;
}
return false;
}
static bool
isValidPoint(sf::Vector2i point, int mapWidth, int mapHeight)
{
if (point.x < 0 || point.y < 0 || point.x > mapWidth || point.y > mapHeight) {
return false;
}
}
std::vector<sf::Vertex>
CollisionManager::getSearchPath(sf::Vector2i start, sf::Vector2i target)
{
if (!isValidPoint(start, mapWidth, mapHeight) ||
!isValidPoint(target, mapWidth, mapHeight)) {
return {};
}
std::vector<Node> path =
pathFinder.getPath(Node(start.x, start.y), Node(target.x, target.y));
std::vector<sf::Vertex> linePoints;
bool setFirst = false;
for (auto node : path) {
sf::Vector2f point((float)node.x * tileSize + tileSize / 2.0f,
(float)node.y * tileSize + tileSize / 2.0f);
sf::Vertex vertex(point, sf::Color::Blue);
linePoints.push_back(sf::Vertex(vertex));
if (setFirst)
linePoints.push_back(vertex);
else
setFirst = true;
}
return linePoints;
}
void
CollisionManager::updatePaths()
{
paths.clear();
for (int y = 0; y < mapHeight; y++) {
for (int x = 0; x < mapWidth; x++) {
if (tileObjArr[y * mapWidth + x]) {
sf::Vector2f pos = agent->animatedSprite->getPosition();
sf::Vector2i start(pos.x / 16, pos.y / 16);
sf::Vector2i end(x, y);
std::vector<sf::Vertex> linePoints = getSearchPath(start, end);
paths.push_back(linePoints);
}
}
}
// std::vector<sf::Vertex> arcPoints = getJumpPath(startTile, targetTile);
// paths.push_back(arcPoints);
}