Repository navigation
Expand file tree
/
Copy pathparticleSystem.cpp
More file actions
217 lines (158 loc) · 4.5 KB
/
Copy pathparticleSystem.cpp
File metadata and controls
217 lines (158 loc) · 4.5 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
#pragma warning(disable : 4786)
#include "particleSystem.h"
#include "modelerui.h"
#include "force.h"
#include "particle.h"
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cmath>
//#include <limits>
using namespace std;
static float prevT;
static Force Forces;
/***************
* Constructors
***************/
ParticleSystem::ParticleSystem() : restitution("Restitution", 0.0f, 2.0f, 1.0f, 0.1f)
{
//Gravity G = new Gravity();
bake_fps = 0;
bake_start_time = 0;
bake_end_time = 0;
// Leave these here; the UI needs them to work correctly.
dirty = false;
simulate = false;
spawnPoint = Vec3f(30.0);
}
/*************
* Destructor
*************/
ParticleSystem::~ParticleSystem()
{
}
/******************
* Simulation fxns
******************/
/** Start the simulation */
void ParticleSystem::startSimulation(float t)
{
prevT = t;
bake_start_time = 0;
// These values are used by the UI ...
// negative 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)
{
// These values are used by the UI
simulate = false;
dirty = true;
}
/** Reset the simulation */
void ParticleSystem::resetSimulation(float t)
{
bake_start_time = 0;
// These values are used by the UI
simulate = false;
dirty = true;
}
/** Compute forces and update particles **/
void ParticleSystem::computeForcesAndUpdateParticles(float t)
{
if (simulate){
const float deltaT = t - prevT;
prevT = t;
float k = 0.001;
for (auto &particle : _Particles) {
Vec3f force = Vec3f(0.0f, 0.0f, 0.0f);
Vec3f position = particle->getPosition();
Vec3f velocity = particle->getVelocity();
float mass = particle->getMass();
force += mass * Force::G();
force -= Force::K() * velocity;
Force::randomizeK();
particle->derivEval(deltaT, force);
// Plane Collision detection
Vec3f N = Vec3f(0, 1, 0);
if(position[1] < 0 && velocity * N < 0) {
Vec3f vn;
Vec3f vt;
vn = (N * velocity) * N;
vt = velocity-vn;
velocity = vt - k * vn;
particle->setVelocity(Vec3f(velocity[0], -velocity[1], velocity[2]));
}
/*Vec3f N = Vec3f(0, 1, 1);
if(i)*/
// Vec3f new_position = velocity;
// Vec3f new_velocity = force / mass;
// new_position *= deltaT;
// new_velocity *= deltaT;
// new_position +=position;
// new_velocity +=velocity;
// particle->setPosition(new_position);
// particle->setVelocity(new_velocity);
// cout << "generating particle" << endl;
// for(auto &f : _Forces){
// force += f->getForce();
// }
// particle->addForce(particle->getMass() * Force::G());
// particle->addForce(-(Force::K() * particle->getVelocity()));
// Vec3f* oldState = particle->getState();
// cout << "state is " << oldState[0][0] << ", " << oldState[0][1] << ", " << oldState[0][2] << endl;
// Vec3f* newState = particle->derivEval();
// newState[0] *=deltaT;
// newState[1] *=deltaT;
// newState[0] += oldState[0];
// newState[1] += oldState[1];
// particle->setState(newState[0], newState[1]);
// cout << particle->getState()[0] << endl;
// velocity += force / particle->getMass() * deltaT;
// position += velocity * deltaT;
// particle->setVelocity(velocity);
// particle->setPosition(position);
}
}
// Debugging info
/*if( t - prevT > .08 )
printf("(!!) Dropped Frame %lf (!!)\n", t-prevT);*/
//prevT = t;
}
/** Render particles */
void ParticleSystem::drawParticles(float t)
{
// YOUR CODE HERE
if (simulate) {
// for (vector<Particle*>::iterator it = particles[t].begin(); it != particles[t].end(); it++) {
// (*it)->Draw();
// }
for (auto &particle : _Particles) {
particle->Draw();
// cout << "drawing" << endl;
}
}
}
/** Adds the current configuration of particles to
* your data structure for storing baked particles **/
void ParticleSystem::bakeParticles(float t)
{
// TODO (baking is extra credit)
}
/** Clears out your data structure of baked particles */
void ParticleSystem::clearBaked()
{
// TODO (baking is extra credit)
}
void ParticleSystem::addParticleStartingAt(Vec4f WorldPoint){
// cout << "spawn point is: "<< WorldPoint[0] << ", " << WorldPoint[1] << ", " << WorldPoint[2] << endl;
spawnPoint = Vec3f(WorldPoint);
// cout << "spawning at "<< spawnPoint[0] << ", " << spawnPoint[1] << ", " << spawnPoint[2] << endl;
}