-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleSystems.html
More file actions
286 lines (274 loc) · 13 KB
/
Copy pathParticleSystems.html
File metadata and controls
286 lines (274 loc) · 13 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<div class="container">
<header>
<div class="page-header">
<h1><strong>Understanding and Building Particle Systems</strong></h1>
<h3> Kartikeya Shukla
<small>CS5310 - Computer Graphics</small>
</h3>
</div>
</header>
<div>
<ol>
<div>
<li><strong>Introduction:</strong>
<p>In 1982, William T. Reeves, a researcher at Lucasfilm Ltd., was working on the film <em>Star
Trek II: The Wrath of
Khan</em>. Much of the movie revolves
around the Genesis Device, a torpedo that when shot at a barren, lifeless planet has the ability
to reorganize
matter and create a habitable world for
colonization. During the sequence, a wall of fire ripples over the planet while it is being
'terraformed'. The term
<strong><em>particle system</em></strong>, an incredibly common and useful technique in computer
graphics, was
coined in the creation of this particular
effect.
</p>
<p>
<em>
"A particle system is a collection of many many minute particles that together represent a
fuzzy object. Over a
period of time, particles are generated
into a system, move and change from within the system, and die from the system."
</em>
</p>
<p>
This type of effect is commonly replicated and re-produced using particle systems in games,
graphics, movies etc.
Examples of such effects include fire,
rain, snow, sand, waterfalls, stars, galaxies, etc.
</p>
<p>
Particle system can also be used to depict hair or fur, by rendering the entire lifetime of a
single particle at
once, which can be manipulated as per the
interaction with the environment.
</p>
</li>
</div>
<div>
<li><strong>Building a simple Particle System:</strong></li>
<p>
From the definition mentioned above, it is easy to tell that a particle system is just a
container of individual
independent elements, which have their own
properties/attributes and each interact with their environment, and as a whole produce a desired
effect. This
behavior can be reproduced quite easily via
code using Object Oriented Programming principles.
</p>
<p>
We intend to demonstrate the creation of a simple particle system using Processing.
</p>
<p>
Our goal is to make a particle system container, a class, which when called would display a
screen full of
particles, without the need of directly
referencing/initializing any individual particles.
</p>
<ul>
<li><em>A single particle</em></li>
<p>
First, we will try and describe a single particle. What would it look like? What are its basic
attributes? And then
wrap them into a single component, a class.
</p>
<pre>
class Particle {
PVector location; // Vector storing the location of the particle
PVector velocity; // Vector stores velocity
PVector acceleration; // Vector stores acceleration
Float lifespan; // The duration that the particle ‘lives’
Particle(PVector l) {
location = l.get(); //Stores x and y co-ordinates when particle is initialized
velocity = new PVector(random(-1,1), random(-2, 0)); // Particle will either go left or right and upward direction.
acceleration = new PVector(0, 0.02); // Will have a downward acceleration (gravity)
lifespan = 255; // Can be chosen as per required effect
}
void update() {
velocity.add(acceleration);
location.add(velocity); // Here the velocity and acceleration change the location of // this particle
lifespan -= 2.0; // each time the particle is updated it ‘grows older’
}
void display() {
stroke(0);
fill(175);
ellipse(location.x,location.y,8,8); // For now we consider our particle as a circle
}
}</pre>
<li><em>ArrayList and Iterator Class</em></li>
<pre>
ArrayList<Particle> particles;
void setup() {
size(640,360);
particles = new ArrayList<Particle>();
}
void draw() {
background(255);
particles.add(new Particle(new PVector(width/2,50)));
Iterator<Particle> it = particles.iterator();
//[full] Using an Iterator object instead of counting with int i
while (it.hasNext()) {
Particle p = it.next();
p.run();
if (p.isDead()) {
it.remove();
}
}
}
</pre>
<video width="400" height="300" controls align="center">
<source src="assets/multiple_particles.mp4" type="video/mp4"/>
Your browser does not support the video tag.
</video>
<p>
That was a single particle, but we need our code to organize the data to accommodate many many
such particles. One
way of doing this is using the ArrayList
- we can simply add a particle with every call to draw().
</p>
<p>
Another thing to note is the isDead() function - if we were to keep adding particles to the
system, after a while we
would see the frame rate slow down and
eventually seemingly halts because of the sheer number of particles. Here, the remove() function
of the ArrayList is
extremely useful i.e. remove the
particle whose lifeSpan has elapsed.
</p>
<p>
We can also look at using the Iterator class to loop through the particles making sure each
particle is updated
correctly.
</p>
<li><em>Encapsulate it all into a Class</em></li>
<pre>
class ParticleSystem {
ArrayList particles;
// This particular ParticleSystem implementation
// includes an origin point where each Particle begins.
PVector origin;
ParticleSystem(PVector location) {
origin = location.get();
particles = new ArrayList();
}
void addParticle() {
// The origin is passed to each Particle when it is added.
particles.add(new Particle(origin));
}</pre>
<li><em>Applying a Force</em></li>
<p>
Now that we have a simple Particle system built, we would also like it to react to external
forces. We do this by
looping through the list of particles
applying the force on each one.
</p>
<pre>
Void applyForce( Pvector force) {
For(Particle p : particles){
p.applyForce(f);
}
}</pre>
<p>
And within the particle , in accordance to Newton's law of motion, update the acceleration using
the mass (For now
consider, mass = 1, randomize this to
achieve different results).
</p>
<pre>
Void applyForce(PVector force){
PVector f = new force.get();
f.div(mass); // consider the mass = 1 for a particle, can be changed for
// different results
acceleration.add(f);
}
</pre>
<li><em>Adding Texture </em></li>
<p>
Adding a simple texture such as the one below, with appropriate color (tint), our system looks
like smoke or fire.
</p>
<p align="center">
<img width="100" height="102" src="assets/texture.png">
</p>
<p>
Add the following statement for additive blending, which combines a certain percentage pixels
from the background to
the foreground depending on the alpha
values.
</p>
</ul>
</div>
<div>
<li><strong>Discussions:</strong></li>
<p>
In the previous section, we saw a working Particle System capable of reproducing recognizable
special effects;
albeit quite simple looking effects but
special effects nonetheless.
</p>
<p>
This is one of the main impetus of this paper that the myriads of complex systems that we are used
to seeing in
various 3d softwares are inherently built
on something very basic and easy to understand. If one were to sit down and expand the workings of
our particle
system by including things such as
interactions between particles, evolution of particles (with time or environment), dynamic stimuli
etc. it would
more realistically depict the systems that
are used in the industry.
</p>
<p>
We also highly recommend the use of artist friendly 3d softwares like Maya and Houdini, to
experience the extent
of
sophistication to which one can take a
particle system.
</p>
<p>
Particle systems can be created and modified natively in many 3D modeling and rendering packages
including
Cinema
4D, Lightwave, Houdini, Maya, XSI, 3D
Studio Max and Blender.
</p>
<ul>
<li>Using the simple particle system an example of camp-fire</li>
<video width="600" height="400" controls>
<source src="assets/fire_system.mp4" type="video/mp4"/>
Your browser does not support the video tag.
</video>
<li>Following is a video showing how we can re-create simple particle systems by enhacing the system
we
created
<ul>
<li> Snow - 1:55</li>
<li> Fireball - 2:44</li>
<li> Sun (with Corona) - 3:08</li>
<li> Particle behaving like rigid bodies - 3:35</li>
</ul>
</li>
<video width="600" height="400" controls>
<source src="assets/ParticleSystem.mp4" type="video/mp4"/>
Your browser does not support the video tag.
</video>
</ul>
<li> Code : On <a target="_blank" href="https://github.com/Qartks89/ParticleSystems">Github</a></li>
<li><strong>Reference:</strong></li>
<ol>
<li>Nature Of Code – Daniel Shiffman <a href="http://natureofcode.com/">http://natureofcode.com/</a>
</li>
<li>Build New Games - <a href="http://buildnewgames.com/particle-systems/">http://buildnewgames.com/particle-systems/</a>
</li>
<li>Particle Systems - <a href="http://en.wikipedia.org/wiki/Particle_system">http://en.wikipedia.org/wiki/Particle_system</a>
</li>
<li>William Reeves, "Particle Systems - A Technique for Modeling a Class of Fuzzy Objects," ACM
Transactions on
Graphics<em> 2:2 (April 1983), 92.</em></li>
<li>Processing - <a href="http://processing.org">http://processing.org</a>/</li>
</ol>
</div>
</ol>
</div>
</div>