-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticles.cpp
181 lines (144 loc) · 4.36 KB
/
particles.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
174
175
176
177
178
179
180
181
#include "particles.h"
#include <random>
#include <exception>
namespace Simulation
{
std::vector<Particle> particles = {};
}
using namespace Simulation;
const sf::Color Simulation::GetRandColor() noexcept(false)
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dist(1, 7);
switch (dist(gen)) {
case White:
return sf::Color::White;
case Red:
return sf::Color::Red;
case Green:
return sf::Color::Green;
case Blue:
return sf::Color::Blue;
case Yellow:
return sf::Color::Yellow;
case Magenta:
return sf::Color::Magenta;
case Cyan:
return sf::Color::Cyan;
default:
case Black:
throw std::exception("Unexpected value!");
}
}
const Particle Simulation::GenParticle() noexcept(false)
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<size_t> dist(0, 100);
float x = (float)dist(gen) * 10, y = (float)dist(gen) * 10;
y = y > window_height ? (y - window_height) : y;
Particle particle;
particle.position = sf::Vector2f(x, y);
particle.velocity = sf::Vector2f((float)dist(gen), -150);
particle.acceleration = sf::Vector2f(0, 98);
particle.color = GetRandColor();
return particle;
}
void Simulation::DrawParticle(const Particle& particle, const SWindow& window) noexcept(true)
{
sf::CircleShape ptc(5);
ptc.setFillColor(particle.color);
ptc.setPosition(particle.position);
window->draw(ptc);
}
void Simulation::ApplyPhysics(Particle& particle, float dTime, sf::Vector2u windowSize) noexcept(true)
{
particle.velocity += particle.acceleration * dTime;
particle.position += particle.velocity * dTime;
if (particle.position.x <= 0) {
particle.position.x = 0;
particle.velocity.x = -(particle.velocity.x / 2);
}
if (particle.position.x + 10 >= windowSize.x) {
particle.position.x = (float)windowSize.x - 10;
particle.velocity.x = -(particle.velocity.x / 2);
}
if (particle.position.y <= 0) {
particle.position.y = 0;
particle.velocity.y = -(particle.velocity.y / 2);
}
if (particle.position.y + 10 >= windowSize.y) {
particle.position.y = (float)windowSize.y - 10;
particle.velocity.y = -(particle.velocity.y / 2);
}
particle.acceleration = sf::Vector2f(0, 98);
}
void Simulation::DealWithCollisions(Particle& particle) noexcept(true)
{
for (Particle& other : particles) {
if (particle != other) {
sf::Vector2f delta = particle.position - other.position;
float distance = std::sqrt(delta.x * delta.x + delta.y * delta.y);
if (distance < 10) {
sf::Vector2f normal = delta / distance;
float relativeVelocity = (particle.velocity - other.velocity).x * normal.x + (particle.velocity - other.velocity).y * normal.y;
if (relativeVelocity < 0) {
float coefficientOfRestitution = 0.8f;
sf::Vector2f impulse = (1 + coefficientOfRestitution) * relativeVelocity / 2 * normal;
particle.velocity -= impulse;
other.velocity += impulse;
}
}
}
}
}
SWindow Simulation::GetWindow() noexcept(true)
{
auto window = std::make_shared<sf::RenderWindow>(
sf::VideoMode(window_width, window_height),
"Particle Simulation",
sf::Style::Titlebar | sf::Style::Close);
return window;
}
void Simulation::AttractParticles(Particle& particle, sf::Vector2i mPos) noexcept(true)
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<size_t> dist(1, 10);
sf::Vector2f direction = (sf::Vector2f)mPos - particle.position;
float distance = std::sqrt(direction.x * direction.x + direction.y * direction.y);
if (distance > 0) {
direction /= distance;
float factor = dist(gen);
particle.acceleration = direction * 200.0f * factor;
}
}
void Simulation::Run(const SWindow& window) noexcept(false)
{
if (particles.empty()) {
for (int i = 0; i < PARTICLE_AMOUNT; ++i)
particles.push_back(GenParticle());
}
sf::Clock clock;
while (window->isOpen()) {
sf::Event event;
while (window->pollEvent(event)) {
if (event.type == sf::Event::Closed)
window->close();
else if (event.type == sf::Event::MouseButtonPressed) {
auto pos = sf::Mouse::getPosition(*window.get());
for (Particle& particle : particles)
AttractParticles(particle, pos);
}
}
float deltaTime = clock.restart().asSeconds();
window->clear(sf::Color::Black);
for (Particle& particle : particles) {
ApplyPhysics(particle, deltaTime, window->getSize());
DealWithCollisions(particle);
DrawParticle(particle, window);
}
window->display();
}
}