-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathsolver.hpp
194 lines (167 loc) · 5.2 KB
/
solver.hpp
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
#pragma once
#include <vector>
#include <cmath>
#include <SFML/Graphics.hpp>
#include "utils/math.hpp"
struct VerletObject
{
sf::Vector2f position;
sf::Vector2f position_last;
sf::Vector2f acceleration;
float radius = 10.0f;
sf::Color color = sf::Color::White;
VerletObject() = default;
VerletObject(sf::Vector2f position_, float radius_)
: position{position_}
, position_last{position_}
, acceleration{0.0f, 0.0f}
, radius{radius_}
{}
void update(float dt)
{
// Compute how much we moved
const sf::Vector2f displacement = position - position_last;
// Update position
position_last = position;
position = position + displacement + acceleration * (dt * dt);
// Reset acceleration
acceleration = {};
}
void accelerate(sf::Vector2f a)
{
acceleration += a;
}
void setVelocity(sf::Vector2f v, float dt)
{
position_last = position - (v * dt);
}
void addVelocity(sf::Vector2f v, float dt)
{
position_last -= v * dt;
}
[[nodiscard]]
sf::Vector2f getVelocity(float dt) const
{
return (position - position_last) / dt;
}
};
class Solver
{
public:
Solver() = default;
VerletObject& addObject(sf::Vector2f position, float radius)
{
return m_objects.emplace_back(position, radius);
}
void update()
{
m_time += m_frame_dt;
const float step_dt = getStepDt();
for (uint32_t i{m_sub_steps}; i--;) {
applyGravity();
checkCollisions(step_dt);
applyConstraint();
updateObjects(step_dt);
}
}
void setSimulationUpdateRate(uint32_t rate)
{
m_frame_dt = 1.0f / static_cast<float>(rate);
}
void setConstraint(sf::Vector2f position, float radius)
{
m_constraint_center = position;
m_constraint_radius = radius;
}
void setSubStepsCount(uint32_t sub_steps)
{
m_sub_steps = sub_steps;
}
void setObjectVelocity(VerletObject& object, sf::Vector2f v)
{
object.setVelocity(v, getStepDt());
}
[[nodiscard]]
const std::vector<VerletObject>& getObjects() const
{
return m_objects;
}
[[nodiscard]]
sf::Vector3f getConstraint() const
{
return {m_constraint_center.x, m_constraint_center.y, m_constraint_radius};
}
[[nodiscard]]
uint64_t getObjectsCount() const
{
return m_objects.size();
}
[[nodiscard]]
float getTime() const
{
return m_time;
}
[[nodiscard]]
float getStepDt() const
{
return m_frame_dt / static_cast<float>(m_sub_steps);
}
private:
uint32_t m_sub_steps = 1;
sf::Vector2f m_gravity = {0.0f, 1000.0f};
sf::Vector2f m_constraint_center;
float m_constraint_radius = 100.0f;
std::vector<VerletObject> m_objects;
float m_time = 0.0f;
float m_frame_dt = 0.0f;
void applyGravity()
{
for (auto& obj : m_objects) {
obj.accelerate(m_gravity);
}
}
void checkCollisions(float dt)
{
const float response_coef = 0.75f;
const uint64_t objects_count = m_objects.size();
// Iterate on all objects
for (uint64_t i{0}; i < objects_count; ++i) {
VerletObject& object_1 = m_objects[i];
// Iterate on object involved in new collision pairs
for (uint64_t k{i + 1}; k < objects_count; ++k) {
VerletObject& object_2 = m_objects[k];
const sf::Vector2f v = object_1.position - object_2.position;
const float dist2 = v.x * v.x + v.y * v.y;
const float min_dist = object_1.radius + object_2.radius;
// Check overlapping
if (dist2 < min_dist * min_dist) {
const float dist = sqrt(dist2);
const sf::Vector2f n = v / dist;
const float mass_ratio_1 = object_1.radius / (object_1.radius + object_2.radius);
const float mass_ratio_2 = object_2.radius / (object_1.radius + object_2.radius);
const float delta = 0.5f * response_coef * (dist - min_dist);
// Update positions
object_1.position -= n * (mass_ratio_2 * delta);
object_2.position += n * (mass_ratio_1 * delta);
}
}
}
}
void applyConstraint()
{
for (auto& obj : m_objects) {
const sf::Vector2f v = m_constraint_center - obj.position;
const float dist = sqrt(v.x * v.x + v.y * v.y);
if (dist > (m_constraint_radius - obj.radius)) {
const sf::Vector2f n = v / dist;
obj.position = m_constraint_center - n * (m_constraint_radius - obj.radius);
}
}
}
void updateObjects(float dt)
{
for (auto& obj : m_objects) {
obj.update(dt);
}
}
};