-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleSystem.pde
More file actions
218 lines (188 loc) · 5.32 KB
/
ParticleSystem.pde
File metadata and controls
218 lines (188 loc) · 5.32 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
/*
Skyline:
Input image: > skylineThreshold means particle freezen when location hit
Input image is transformed slightly:
- bottom pixel in each row in skyline is set to 254
- when a particle reaches bottom pixel, bottom pixel is set to 255 and particle is deleted
(to avoid growing amount of frozen particles)
*/
class ParticleSystem
{
ArrayList<Particle> particles;
PImage sprite;
PVector frameMin = null;
PVector frameMax = null;
PImage skyline;
PVector skylinePosition;
PGraphics layer;
int skylineThreshold = 50;
int stormyness = 0; // [0..255] 0 = off, 255 = max
private float lastTime = 0;
private int[] lowestPointOnSkyline;
private float accumulatedTime = 0;
boolean invisibleSnow = false;
ParticleSystem(PGraphics layer, PImage sprite)
{
this.layer = layer;
this.sprite = sprite;
particles = new ArrayList<Particle>();
lastTime = 0;
//populate();
}
void populate()
{
for (int i=0; i<100; i++)
{
Particle p = new Particle(layer, sprite);
particles.add(p);
p.position = new PVector(i*2, 20);
p.velocity = new PVector(random(0.1, 1), random(0.5, 5));
p.acceleration = new PVector(0, random(0, 0.5));
}
}
// position is upper left corner of image
void setSkyline(PImage skyline, PVector position)
{
this.skyline = skyline;
this.skylinePosition = position;
updateSkyline();
}
void updateSkyline()
{
this.skyline.loadPixels();
lowestPointOnSkyline = new int[this.skyline.width];
Arrays.fill(lowestPointOnSkyline, this.skyline.height);
for (int x = 0; x < this.skyline.width; x++)
{
boolean first = true;
for (int y = this.skyline.height - 1; y >= 0; y--)
{
int loc = y * this.skyline.width + x;
if (red(this.skyline.pixels[loc]) > skylineThreshold)
{
if (first)
{
lowestPointOnSkyline[x] = y;
first = false;
}
}
}
}
}
void addParticle(Particle p)
{
particles.add(p);
}
void update()
{
float time = (float) millis() / 1000.0;
float dt = time - lastTime;
if (lastTime == 0)
dt = 0;
lastTime = time;
update(dt);
}
// dt is difference in time since last time the function was called, given in seconds
void update(float dt)
{
if (dt <= 0)
return;
accumulatedTime += dt;
Iterator<Particle> it = particles.iterator();
while (it.hasNext())
{
Particle particle = it.next();
applyForce(particle);
particle.update(dt);
checkInsideFrame(particle);
checkSkyline(particle);
if (particle.isDead())
{
it.remove();
}
}
}
void draw()
{
imageMode(CENTER);
for (Particle particle: particles)
{
if ( (!invisibleSnow) || (invisibleSnow && particle.frozen))
particle.draw();
}
}
int numParticles()
{
return particles.size();
}
void unfreezeAll()
{
for (Particle particle: particles)
{
particle.frozen = false;
}
}
private void applyForce(Particle particle)
{
//if (stormyness <= 4)
// return;
float scale = (float)stormyness / 255.0;
float sign = particle.position.z < 0 ? -1.0 : 1.0;
float phase = 400.0 * sin (accumulatedTime / 5.0 * 2 * PI) + random(-30, 30);
float amp = scale * 100.0;
float wavelength = 600 + 400 * sin(accumulatedTime / 10.0 * 2 * PI);
PVector f = new PVector(
amp * sign * sin((particle.position.x + phase) / wavelength * 2 * PI),
amp * 0.5 * ( 0.2 + cos((particle.position.x + phase) / wavelength * 2 * PI)));
particle.setForce(f);
particle.velocityBlend = 1.0 - scale;
}
private void checkInsideFrame(Particle particle)
{
if ( (frameMin != null) && (frameMax != null) &&
((particle.position.x < frameMin.x) || (particle.position.y < frameMin.y) ||
(particle.position.x > frameMax.x) || (particle.position.y > frameMax.y)) )
{
particle.dead = true;
//print("-");
}
}
private void checkSkyline(Particle particle)
{
if (this.skyline == null)
return;
float fsx = particle.position.x - skylinePosition.x;
fsx = fsx < 0 ? 0 : fsx > this.skyline.width - 1 ? this.skyline.width - 1 : fsx;
float fsy = particle.position.y - skylinePosition.y;
fsy = fsy < 0 ? 0 : fsy > this.skyline.height - 1 ? this.skyline.height - 1 : fsy;
int sx = round(fsx);
int sy = round(fsy);
int loc = sy * this.skyline.width + sx;
//println("x: " + sx + ", y: " + sy + " - " + loc + " - " + particle.position);
if (red(skyline.pixels[loc]) > skylineThreshold)
{
particle.frozen = true;
for (int y=-1; y<=1; y++)
{
for (int x=-1; x<=1; x++)
{
loc = (sy + y) * this.skyline.width + (sx + x);
if (loc < skyline.pixels.length)
{
skyline.pixels[loc] = 0; // remove pixel in skyline
}
}
}
}
if ( (sx < lowestPointOnSkyline.length) && (sy > lowestPointOnSkyline[sx]))
particle.dead = true; // don't snow past last pixel in row
}
boolean lessThan(PVector v1, PVector v2)
{
return (v1.x < v2.x) && (v1.y < v2.y);
}
boolean greaterThan(PVector v1, PVector v2)
{
return (v1.x > v2.x) && (v1.y > v2.y);
}
}