-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhysSystem.h
101 lines (94 loc) · 2.85 KB
/
PhysSystem.h
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
#pragma once
#include "RigidBody.h"
#include "Collider.h"
#include "Solver.h"
#include <vector>
#include <SFML/Graphics.hpp>
struct PhysSystem
{
RigidBody *AddBody(Coords2f coords, Vector2f size)
{
RigidBody newbie(coords, size, 1e-5f);
bodies.push_back(newbie);
return &(bodies[bodies.size() - 1]);
}
void Update(float dt)
{
collisionTime = mergeTime = solveTime = 0;
sf::Clock clock;
for (size_t bodyIndex = 0; bodyIndex < bodies.size(); bodyIndex++)
{
bodies[bodyIndex].IntegrateVelocity(dt);
}
mergeTime += clock.getElapsedTime().asSeconds();
clock.restart();
collider.FindCollisions(bodies.data(), bodies.size());
collisionTime += clock.getElapsedTime().asSeconds();
clock.restart();
for (size_t jointIndex = 0; jointIndex < solver.contactJoints.size(); jointIndex++)
{
solver.contactJoints[jointIndex].valid = 0;
}
for (Collider::ManifoldMap::iterator man = collider.manifolds.begin(); man != collider.manifolds.end(); man++)
{
for (int collisionIndex = 0; collisionIndex < man->second.collisionsCount; collisionIndex++)
{
Collision &col = man->second.collisions[collisionIndex];
if (!col.userInfo) continue;
ContactJoint::Descriptor desc;
desc.body1 = man->second.body1;
desc.body2 = man->second.body2;
desc.collision = &col;
solver.RefreshContactJoint(desc);
}
}
for (Collider::ManifoldMap::iterator man = collider.manifolds.begin(); man != collider.manifolds.end(); man++)
{
for (int collisionIndex = 0; collisionIndex < man->second.collisionsCount; collisionIndex++)
{
Collision &col = man->second.collisions[collisionIndex];
if (col.userInfo) continue;
ContactJoint::Descriptor desc;
desc.body1 = man->second.body1;
desc.body2 = man->second.body2;
desc.collision = &col;
solver.contactJoints.push_back(ContactJoint(desc));
}
}
solver.RefreshJoints();
mergeTime += clock.getElapsedTime().asSeconds();
clock.restart();
solver.SolveJoints(500, 500);
solveTime += clock.getElapsedTime().asSeconds();
clock.restart();
for (size_t bodyIndex = 0; bodyIndex < bodies.size(); bodyIndex++)
{
bodies[bodyIndex].IntegratePosition(dt);
}
mergeTime += clock.getElapsedTime().asSeconds();
clock.restart();
}
size_t GetBodiesCount()
{
return bodies.size();
}
RigidBody *GetBody(int index)
{
return &(bodies[index]);
}
int GetJointsCount()
{
return solver.contactJoints.size();
}
Collider *GetCollider()
{
return &collider;
}
float collisionTime;
float mergeTime;
float solveTime;
private:
std::vector<RigidBody> bodies;
Collider collider;
Solver solver;
};