-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparticleSystem.cpp
More file actions
216 lines (172 loc) · 4.45 KB
/
particleSystem.cpp
File metadata and controls
216 lines (172 loc) · 4.45 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
#pragma warning(disable : 4786)
#include "particleSystem.h"
#include "modelerapp.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <limits.h>
/***************
* Constructors
***************/
ParticleSystem::ParticleSystem() : bake_fps(30), simulate(false), dirty(false), m_gravity(0, -10, 0), m_groundY(-10.0)
{
// TODO
}
/*************
* Destructor
*************/
ParticleSystem::~ParticleSystem()
{
// TODO
}
/******************
* Simulation fxns
******************/
/** Start the simulation */
void ParticleSystem::startSimulation(float t)
{
#ifdef _DEBUG
printf("start simulation %f\n", t);
#endif
// TODO
if (!simulate) {
bake_start_time = t;
clearBaked();
}
// These values are used by the UI ...
// -ve bake_end_time indicates that simulation
// is still progressing, and allows the
// indicator window above the time slider
// to correctly show the "baked" region
// in grey.
bake_end_time = -1;
simulate = true;
dirty = true;
}
/** Stop the simulation */
void ParticleSystem::stopSimulation(float t)
{
// TODO
bake_end_time = t;
// These values are used by the UI
simulate = false;
dirty = true;
}
/** Reset the simulation */
void ParticleSystem::resetSimulation(float t)
{
// TODO
// These values are used by the UI
simulate = false;
dirty = true;
}
/** Compute forces and update particles **/
void ParticleSystem::computeForcesAndUpdateParticles(float t)
{
m_gravity[1] = -VAL(PS_GRAVITY);
#ifdef _DEBUG
#if 0 //just curious
if (m_cache.find(t) == m_cache.end())
printf("NEW time %f\n", t);
else
printf("OLD time %f\n", t);
m_cache[t];
#endif
#endif
//only handle unbaken situation
if (m_cache.find(get_frame(t)) == m_cache.end()) { //no, not baken
if (simulate) {
//initialize
//the last tick
auto last_iter = m_cache.rbegin();
if (last_iter == m_cache.rend()) { //the ticks are empty
//this is the very first tick in history
//add new particles
for (auto particleSrc : m_particleSrcs) {
particleSrc->newParticles(m_cache[get_frame(t)]);
}
}
else { //there's a last tick
float delta_t = 1.0f / bake_fps;
int last_frame = last_iter->first;
for (int frame = last_frame + 1; frame <= get_frame(t); ++frame) {
#ifdef _DEBUG
printf("simulating frame %d\n", frame);
#endif
m_cache[frame] = m_cache[frame-1]; //copy the last tick state
//add new particles
for (auto particleSrc : m_particleSrcs) {
particleSrc->newParticles(m_cache[frame]);
}
//simulate the particles
auto iter = m_cache[frame].begin();
while (iter != m_cache[frame].end()) {
Particle& particle = *iter;
particle.life -= delta_t;
//kill the ones that are dead
if (particle.life < 0) {
iter = m_cache[frame].erase(iter);
continue;
}
else {
++iter;
}
//evolve
particle.position += particle.velocity * delta_t;
if (ModelerApplication::Instance()->rb() && particle.position[1] < m_groundY) {
particle.position[1] = 2 * m_groundY - particle.position[1];
particle.velocity[1] = -particle.velocity[1];
}
else if (ModelerApplication::Instance()->flock()) {
//steer towards the average of neighbors
double radius = 1.5;
Vec3d avgV;
int count = 0;
for (auto par : m_cache[frame]) {
if ((par.position - particle.position).length2() < radius * radius) {
++count;
avgV += par.velocity;
}
}
avgV /= count;
particle.velocity = 0.5 * avgV + 0.5 * particle.velocity;
} else {
particle.velocity += m_gravity * delta_t;
}
}
}
}
}
}
}
/** Render particles */
void ParticleSystem::drawParticles(float t)
{
auto iter = m_cache.find(get_frame(t));
if (iter == m_cache.end()) return; //do nothing if this frame is not yet baken
glPushMatrix();
glTranslated(-10, m_groundY, -10);
drawBox(20, 0.1, 20);
glPopMatrix();
Particles& particles = iter->second;
for (Particle & particle : particles) {
glPushMatrix();
glTranslated(particle.position[0], particle.position[1], particle.position[2]);
drawSphere(0.1);
glPopMatrix();
}
}
/** Adds the current configuration of particles to
* your data structure for storing baked particles **/
void ParticleSystem::bakeParticles(float t)
{
//UNUSED
// TODO
}
/** Clears out your data structure of baked particles */
void ParticleSystem::clearBaked()
{
m_cache.clear();
// TODO
}