-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticleSystem.cpp
273 lines (219 loc) · 8.35 KB
/
particleSystem.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#pragma warning(disable : 4786)
#include "particleSystem.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <limits.h>
/***************
* Constructors
***************/
ParticleSystem::ParticleSystem()
{
// TODO - done
//set random seed
srand(time(NULL));
// set baked fps
bake_fps = 10;
// set number of initial_state
n = 300;
particles = new Particle[n];
simulate = false;
// store the initial state, deep copy
initial_state = new Particle[n];
for (int i = 0; i < n; i++){
float newPosVectors[3] = { particles[i].getPositionVectors()[0], particles[i].getPositionVectors()[1], particles[i].getPositionVectors()[2] };
float newVelVectors[3] = { particles[i].getVelocityVectors()[0], particles[i].getVelocityVectors()[1], particles[i].getVelocityVectors()[2] };
float newForVectors[3] = { particles[i].getForceVectors()[0], particles[i].getForceVectors()[1], particles[i].getForceVectors()[2] };
initial_state[i].setPositionVectors(newPosVectors);
initial_state[i].setVelocityVectors(newVelVectors);
initial_state[i].setForceVectors(newForVectors);
}
}
/*************
* Destructor
*************/
ParticleSystem::~ParticleSystem()
{
// TODO - done
delete[] particles;
delete[] initial_state;
}
/******************
* Simulation fxns
******************/
/** Start the simulation */
void ParticleSystem::startSimulation(float t)
{
// TODO
bake_start_time = t;
// 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;
resetSimulation(t);
// These values are used by the UI
simulate = false;
dirty = true;
}
/** Reset the simulation */
void ParticleSystem::resetSimulation(float t)
{
// TODO
// reset the particle status to its initial state
for (int i = 0; i < n; i++){
float newPosVectors[3] = { initial_state[i].getPositionVectors()[0], initial_state[i].getPositionVectors()[1], initial_state[i].getPositionVectors()[2] };
float newVelVectors[3] = { initial_state[i].getVelocityVectors()[0], initial_state[i].getVelocityVectors()[1], initial_state[i].getVelocityVectors()[2] };
float newForVectors[3] = { initial_state[i].getForceVectors()[0], initial_state[i].getForceVectors()[1], initial_state[i].getForceVectors()[2] };
particles[i].setPositionVectors(newPosVectors);
particles[i].setVelocityVectors(newVelVectors);
particles[i].setForceVectors(newForVectors);
}
// These values are used by the UI
simulate = false;
dirty = true;
}
/** Compute forces and update initial_state **/
void ParticleSystem::computeForcesAndUpdateParticles(float t)
{
// TODO
if (simulate){
// no need to update if the particles are already baked
float roundedTime = round(t*bake_fps) / bake_fps;
if (bakedParticles.count(roundedTime)){
return;
}
// compute new force
for (int i = 0; i < n; i++){
// clear force accumulator
float newForVectors[3] = { 0.0, 0.0, 0.0 };
particles[i].setPositionVectors(newForVectors);
float* derivatives = new float[6 * n];
float totalForce[3] = { 0.0, 0.0, 0.0 };
// gravity force
float gravityForce[3] = { 0.0, particles[i].getMass() * (-1) * 9.8, 0.0 };
totalForce[0] += gravityForce[0];
totalForce[1] += gravityForce[1];
totalForce[2] += gravityForce[2];
// air drag
float airDragCoeff = 10;
float airDrag[3] = { airDragCoeff*particles[i].getVelocityVectors()[0], airDragCoeff*particles[i].getVelocityVectors()[1], airDragCoeff*particles[i].getVelocityVectors()[2] };
totalForce[0] += airDrag[0];
totalForce[1] += airDrag[1];
totalForce[2] += airDrag[2];
//update particles derivatives
derivatives[0] = particles[i].getVelocityVectors()[0];
derivatives[1] = particles[i].getVelocityVectors()[1];
derivatives[2] = particles[i].getVelocityVectors()[2];
derivatives[3] = totalForce[0] / particles[i].getMass();
derivatives[4] = totalForce[1] / particles[i].getMass();
derivatives[5] = totalForce[2] / particles[i].getMass();
float newPosVectors[3];
float newVelVectors[3];
newPosVectors[0] = particles[i].getPositionVectors()[0] + 1.0 / bake_fps*derivatives[0];
newPosVectors[1] = particles[i].getPositionVectors()[1] + 1.0 / bake_fps*derivatives[1];
newPosVectors[2] = particles[i].getPositionVectors()[2] + 1.0 / bake_fps*derivatives[2];
newVelVectors[0] = particles[i].getVelocityVectors()[0] + 1.0 / bake_fps*derivatives[3];
newVelVectors[1] = particles[i].getVelocityVectors()[1] + 1.0 / bake_fps*derivatives[4];
newVelVectors[2] = particles[i].getVelocityVectors()[2] + 1.0 / bake_fps*derivatives[5];
// reset the position of the particles if it exceeds the limit
double limit = 5.0;
if (newPosVectors[0] > limit || newPosVectors[1] > limit || newPosVectors[2] > limit){
newPosVectors[0] = initial_state[i].getPositionVectors()[0];
newPosVectors[1] = initial_state[i].getPositionVectors()[1];
newPosVectors[2] = initial_state[i].getPositionVectors()[2];
newVelVectors[0] = initial_state[i].getVelocityVectors()[0];
newVelVectors[1] = initial_state[i].getVelocityVectors()[1];
newVelVectors[2] = initial_state[i].getVelocityVectors()[2];
}
// update particles state
particles[i].setPositionVectors(newPosVectors);
particles[i].setVelocityVectors(newVelVectors);
particles[i].setForceVectors(totalForce);
delete[] derivatives;
}
bakeParticles(roundedTime);
}
}
/** Render initial_state */
void ParticleSystem::drawParticles(float t)
{
if (simulate){
float roundedTime = round(t*bake_fps) / bake_fps;
Particle* p = particles;
// check if the initial_state are already baked at this time
if (!bakedParticles.empty() && bakedParticles.count(roundedTime)){
p = bakedParticles.at(roundedTime);
}
// draw shape
double size = 0.05;
float grayColor = (rand() % 100) / 100.0;
setDiffuseColor(grayColor,grayColor,grayColor);
for (int i = 0; i < n; i++){
float* location = p[i].getPositionVectors();
glPushMatrix();
glTranslated(location[0], location[1], location[2]);
glTranslated(-size / 2, -size / 2, -size / 2);
drawSphere(size);
//drawBox(size, size, size);
glPopMatrix();
}
}
}
/** Adds the current configuration of initial_state to
* your data structure for storing baked initial_state **/
void ParticleSystem::bakeParticles(float t)
{
// insert configuration of current initial_state to the bakedParticles
float roundedTime = round(t*bake_fps) / bake_fps;
Particle* currentParticles = new Particle[n];
for (int i = 0; i < n; i++){
float newPosVectors[3] = { particles[i].getPositionVectors()[0], particles[i].getPositionVectors()[1], particles[i].getPositionVectors()[2] };
float newVelVectors[3] = { particles[i].getVelocityVectors()[0], particles[i].getVelocityVectors()[1], particles[i].getVelocityVectors()[2] };
float newForVectors[3] = { particles[i].getForceVectors()[0], particles[i].getForceVectors()[1], particles[i].getForceVectors()[2] };
currentParticles[i].setPositionVectors(newPosVectors);
currentParticles[i].setVelocityVectors(newVelVectors);
currentParticles[i].setForceVectors(newForVectors);
}
bakedParticles.insert(std::pair<float, Particle*>(roundedTime, currentParticles));
}
/** Clears out your data structure of baked initial_state */
void ParticleSystem::clearBaked()
{
// TODO
for (std::map<float, Particle*>::iterator it = bakedParticles.begin(); it != bakedParticles.end(); ++it){
delete[] it->second;
}
bakedParticles.clear();
}
// functions from the given pdf (Physically Based Modeling: Principles and Practice)
/* gather state from the initial_state into dst */
void ParticleSystem::getState(float *dst){
for (int i = 0; i < n; i++){
*(dst++) = particles[i].getPositionVectors()[0];
*(dst++) = particles[i].getPositionVectors()[1];
*(dst++) = particles[i].getPositionVectors()[2];
*(dst++) = particles[i].getVelocityVectors()[0];
*(dst++) = particles[i].getVelocityVectors()[1];
*(dst++) = particles[i].getVelocityVectors()[2];
}
}
/* scatter state from src into the initial_state */
void ParticleSystem::setState(float *src){
for (int i = 0; i < n; i++){
particles[i].setPositionVectors(src);
src += 3;
particles[i].setVelocityVectors(src);
}
}