-
Notifications
You must be signed in to change notification settings - Fork 1
/
Swarm.cpp
61 lines (51 loc) · 1.88 KB
/
Swarm.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
//
// Created by Alexandra Zaharia on 20/12/18.
//
#include <math.h>
#include <iostream> // TODO
#include "Swarm.h"
namespace particlesim {
Swarm::Swarm(): lastTime(0), color(ImVec4(1.0f, 1.0f, 1.0f, 1.0f)) {
for (int i = 0; i < 2500; i++)
m_pParticles.push_back(new Particle());
}
void Swarm::updateSettings(SimulatorSettings settings) {
this->settings.nParticles = settings.nParticles;
this->settings.cycleColors = settings.cycleColors;
this->settings.particleColor = settings.particleColor;
this->settings.particleRadius = settings.particleRadius;
this->settings.blurRadius = settings.blurRadius;
}
void Swarm::update(int elapsed, SimulatorSettings settings) {
updateSettings(settings);
int interval = elapsed - lastTime;
lastTime = elapsed;
for (Particle* particle : m_pParticles) {
particle->update(interval, settings.particleSpin, settings.particleSpeed);
}
if (settings.cycleColors) {
color.x = (float) ((1 + sin(elapsed * settings.colorCyclingSpeed * 2)) * 0.5);
color.y = (float) ((1 + sin(elapsed * settings.colorCyclingSpeed)) * 0.5);
color.z = (float) ((1 + sin(elapsed * settings.colorCyclingSpeed * 3)) * 0.5);
} else {
color.x = settings.particleColor.x;
color.y = settings.particleColor.y;
color.z = settings.particleColor.z;
}
if (m_pParticles.size() != settings.nParticles) {
int oldNParticles = (int) m_pParticles.size();
m_pParticles.resize((size_t) settings.nParticles);
if (oldNParticles < settings.nParticles) {
int count = 0;
for (int i = oldNParticles; i < settings.nParticles; i++) {
m_pParticles[i] = new Particle();
count++;
}
}
}
}
void Swarm::draw() {
for (Particle* particle : m_pParticles)
particle->draw(color, settings.particleRadius);
}
}