-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSandbox.cpp
More file actions
241 lines (220 loc) · 7.71 KB
/
Sandbox.cpp
File metadata and controls
241 lines (220 loc) · 7.71 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
#include "Sandbox.hpp"
#include <iostream>
#include <iterator>
#include <map>
#include <utility>
#include <cmath>
Sandbox::Sandbox(long i_NumberOfParticles, float i_globalRadius, int i_Width, int i_Height, int i_variety)
{
Sandbox::m_number_of_particles = i_NumberOfParticles;
Sandbox::m_width = i_Width;
Sandbox::m_height = i_Height;
m_particles.resize(i_NumberOfParticles);
m_global_radius = i_globalRadius;
m_center_x = m_width * 0.5f;
m_center_y = m_height * 0.5f;
CreateColourMap();
RandomizeParticles(i_variety);
}
void Sandbox::CreateColourMap()
{
// White
m_colour_map.insert(std::pair<int, sf::Color>(0, sf::Color(255, 255, 255)));
// Red
m_colour_map.insert(std::pair<int, sf::Color>(1, sf::Color(255, 0, 0)));
// Green
m_colour_map.insert(std::pair<int, sf::Color>(2, sf::Color(0, 255, 0)));
// Blue
m_colour_map.insert(std::pair<int, sf::Color>(3, sf::Color(0, 0, 255)));
//Yellow
m_colour_map.insert(std::pair<int, sf::Color>(4, sf::Color(255, 255, 0)));
// Pink
m_colour_map.insert(std::pair<int, sf::Color>(5, sf::Color(255, 0, 255)));
// Cyan
m_colour_map.insert(std::pair<int, sf::Color>(6, sf::Color(0, 255, 255)));
// Orange
m_colour_map.insert(std::pair<int, sf::Color>(7, sf::Color(255, 127, 0)));
// Violet
m_colour_map.insert(std::pair<int, sf::Color>(8, sf::Color(148, 0, 211)));
}
void Sandbox::ToCenter(int x, int y, float& cx, float& cy) const
{
cx = m_center_x + float(x - m_width / 2) / m_zoom;
cy = m_center_y + float(y - m_height / 2) / m_zoom;
}
void Sandbox::Zoom(float cx, float cy, float zoom)
{
//Apply the zoom
m_center_x = cx;
m_center_y = cy;
m_zoom = std::max(1.0f, zoom);
//Clamp to make sure camera doesn't go out of bounds
m_center_x = std::min(m_center_x, float(m_width) * (1.0f - 0.5f / m_zoom));
m_center_y = std::min(m_center_y, float(m_height) * (1.0f - 0.5f / m_zoom));
m_center_x = std::max(m_center_x, float(m_width) * (0.5f / m_zoom));
m_center_y = std::max(m_center_y, float(m_height) * (0.5f / m_zoom));
}
void Sandbox::RandomizeParticles(int i_variety)
{
int i;
for (i = 0; i < m_particles.size(); i++)
{
// Set the position of particles from 0 - width/height and further check for boundary
position pos;
pos.x = rand() % m_width;
pos.y = rand() % m_height;
// Adjust to boundary
if (pos.x <= m_global_radius)
{
pos.x = pos.x + m_global_radius - pos.x + 1;
}
if (pos.y <= m_global_radius)
{
pos.y = pos.y + m_global_radius - pos.y + 1;
}
if (pos.x >= (m_width - m_global_radius))
{
pos.x = pos.x - (m_width - m_global_radius) - 1;
}
if (pos.y >= (m_height - m_global_radius))
{
pos.y = pos.y - (m_height - m_global_radius) - 1;
}
// Generate random colour based of variety
int col_index = rand() % i_variety;
Particle particle(pos.x, pos.y, m_colour_map[col_index], m_global_radius, col_index);
// Assign random path to the particle:
// The paths are: -1, 0, 1
// -1 is the negative on that axis, 1 is positive and 0 is no change
position path;
int temp_rand = rand() % 3;
if (temp_rand % 3 == 0)
path.x = 1;
else if(temp_rand % 3 == 1)
path.x = 0;
else
path.x = -1;
if (path.x == 0)
{
temp_rand = rand() % 2;
if (temp_rand % 2 == 0)
{
path.y = 1;
}
else
{
path.y = -1;
}
}
else
{
temp_rand = rand() % 3;
if (temp_rand % 3 == 0)
path.y = -1;
else if (temp_rand % 3 == 1)
path.y = 0;
else
path.y = 1;
}
particle.SetPath(path);
m_particles[i] = particle;
}
}
void Sandbox::ComputePath()
{
int i;
for (i = 0; i < m_particles.size(); i++)
{
Particle& particle = m_particles[i];
position& path = particle.GetPath();
position& pos = particle.GetPosition();
// Check the boundaries and if hit: flip the path info
// For example, a particle in straight line, when collides with wall will continue in opposite
if (((pos.x - Sandbox::m_global_radius) <= 0) || ((pos.x + Sandbox::m_global_radius) >= Sandbox::m_width))
{
path.x *= -1;
}
if (((pos.y - Sandbox::m_global_radius) <= 0) || ((pos.y + Sandbox::m_global_radius) >= Sandbox::m_height))
{
path.y *= -1;
}
// After calculation: add to position after checking path
if (path.x == -1)
{
pos.x = pos.x - 1;
}
else
{
pos.x = pos.x + 1;
}
if (path.y == -1)
{
pos.y = pos.y - 1;
}
else
{
pos.y = pos.y + 1;
}
}
}
void Sandbox::DrawLines(sf::RenderWindow& window, float opacity)
{
// Draw lines
int i;
for (i = 0; i < m_particles.size(); i++)
{
Particle& particle = m_particles[i];
position& path = particle.GetPath();
position& pos = particle.GetPosition();
int j;
for (j = 0; j < m_particles.size(); j++)
{
if (i == j)
{
// Cannot draw with itself
continue;
}
else
{
Particle& next_particle = m_particles[j];
position& next_pos = next_particle.GetPosition();
position& next_path = next_particle.GetPath();
// We are interested if the particles are of the same colour, so we want to draw lines
if (next_particle.GetColourTag() == particle.GetColourTag())
{
// Calculate distance:
int dx = pos.x - next_pos.x;
int dy = pos.y - next_pos.y;
float distance = sqrt((dx * dx) + (dy * dy));
// If distance is less than or equal to 50px
if ((ceil(distance) - (2.0f * Sandbox::m_global_radius)) <= 50.0f)
{
sf::Vertex line[] =
{
sf::Vertex(sf::Vector2f((pos.x - m_center_x) * m_zoom + float(m_width / 2) + m_global_radius, (pos.y - m_center_y) * m_zoom + float(m_height / 2) + m_global_radius), sf::Color(m_colour_map[particle.GetColourTag()])),
sf::Vertex(sf::Vector2f((next_pos.x - m_center_x) * m_zoom + float(m_width / 2) + m_global_radius, ( next_pos.y - m_center_y) * m_zoom + float(m_height / 2) + m_global_radius), sf::Color(m_colour_map[particle.GetColourTag()]))
};
window.draw(line, 2, sf::Lines);
}
}
}
}
}
}
void Sandbox::DrawParticles(sf::RenderWindow& window, float opacity)
{
sf::CircleShape circle;
circle.setOrigin(circle.getRadius(), circle.getRadius());
for (size_t i = 0; i < m_particles.size(); ++i)
{
Particle& particle = m_particles[i];
circle.setRadius(m_global_radius);
const float x = (particle.GetX() - m_center_x)* m_zoom + float(m_width / 2);
const float y = (particle.GetY() - m_center_y)* m_zoom + float(m_height / 2);
circle.setPosition(x, y);
sf::Color col = particle.GetColour();
col.a = uint8_t(opacity * 255);
circle.setFillColor(col);
window.draw(circle);
}
}