-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bubble.pde
329 lines (297 loc) · 7.38 KB
/
Bubble.pde
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//
//
//
// bubble fields
int scale = 7;
int minScale = 2, maxScale = 16;
// single object bubble
class Bubble
{
PVector coord;
PVector vel;
PVector acc;
float radius;
float mass;
PImage img;
PGraphics canvasSplash;
ArrayList<Bubble> parent;
int TTL, initTTL;
Bubble(int x, int y, int scale, PImage img, PGraphics canvasSplash, int TTL)
{
this.vel = new PVector();
this.acc = new PVector();
this.radius = random(scale, scale * 4);
this.mass = radius * radius;
this.img = img;
this.canvasSplash = canvasSplash;
this.parent = null;
setCoord(x, y);
this.TTL = this.initTTL = TTL;
}
// draw and sub-functions
void onDraw(PGraphics canvas)
{
calcAcc();
calcVel();
calcPos();
if (canvas != g) canvas.beginDraw();
int pixel = img.pixels[(int)coord.x + (int)coord.y*img.width];
// translucent or grayscaled
if (translucent)
{
int fade = (int)map(TTL, initTTL, 0, 0x0, 0xff);
canvas.fill(pixel & ((fade << 24) + 0xffffff));
}
else
{
int grayValue =
(int)((pixel >> 16 & 0xff) * 0.299) +
(int)((pixel >> 8 & 0xff) * 0.587) +
(int)((pixel & 0xff) * 0.114);
// initTTL+2 to ensure when TTL==-1(infinity)
// the bubble is colored
float grayScale = map(TTL, initTTL+2, 0, 0, 1);
canvas.fill(lerpColor(color(grayValue), pixel, grayScale));
}
// stroke
if (drawStroke)
canvas.stroke(0xff363532);
else
canvas.noStroke();
// draw bubble
canvas.ellipse(bleedingX+coord.x, bleedingY+coord.y, radius, radius);
if (canvas != g) canvas.endDraw();
// highlight
if (highlight) drawHighlight(canvas);
}
void drawHighlight(PGraphics canvas)
{
if (canvas != g) canvas.beginDraw();
canvas.noStroke();
canvas.fill(0xd0ffffff);
canvas.ellipse(bleedingX+coord.x-radius/3, bleedingY+coord.y-radius/3, radius/3, radius/3);
if (canvas != g) canvas.endDraw();
}
// update and sub-functions
void updateData()
{
updateParentChunk();
updateTTL();
}
void updateTTL()
{
if (TTL == 0) poke();
if (TTL >= 0) TTL--;
}
void updateParentChunk()
{
if (parent != getChunkByPixel((int)coord.x, (int)coord.y))
{
parent.remove(this);
parent = getChunkByPixel((int)coord.x, (int)coord.y);
parent.add(this);
}
}
// poke operations
boolean tryPokeFrom(int x, int y)
{
PVector fromVector = new PVector(x, y);
if (PVector.dist(coord, fromVector) < radius)
{
poke();
return true;
}
else
return false;
}
void poke()
{
if (rawSplash)
{
canvasSplash.beginDraw();
canvasSplash.stroke(img.pixels[(int)coord.x + (int)coord.y*img.width] & 0x80ffffff);
canvasSplash.strokeWeight(2*radius);
canvasSplash.point(bleedingX+coord.x, bleedingY+coord.y);
canvasSplash.endDraw();
}
else
{
Splash corpse = new Splash(
(int)coord.x, (int)coord.y, radius*1.2, vel,
img.pixels[(int)coord.x + (int)coord.y*img.width], canvasSplash);
corpse.onDraw();
}
// remove this and free storage
if (parent != null) parent.remove(this);
}
// physical calculation
void collisionTest()
{
// collision force
ArrayList<ArrayList<Bubble>> adjacentChunks =
get3x3ChunksByPixel((int)coord.x, (int)coord.y);
for (int i = 0; i < adjacentChunks.size(); i++)
{
for (int j = 0; j < adjacentChunks.get(i).size(); j++)
{
Bubble target = adjacentChunks.get(i).get(j);
if (target == this) continue;
float dist = PVector.dist(coord, target.coord);
float collideLength = (radius + target.radius) - dist;
if (collideLength > 0)
{
addForce(
PVector.mult(
PVector.sub(coord, target.coord).normalize(),
target.mass
)
);
}
}
}
// border
if (coord.x+radius >= img.width)
addForce(PVector.mult(PVector.sub
(new PVector(img.width-radius, coord.y), coord).normalize(), mass));
if (coord.x-radius < 0)
addForce(PVector.mult(PVector.sub
(new PVector(radius, coord.y), coord).normalize(), mass));
if (coord.y+radius >= img.height)
addForce(PVector.mult(PVector.sub
(new PVector(coord.x, img.height-radius), coord).normalize(), mass));
if (coord.y-radius < 0)
addForce(PVector.mult(PVector.sub
(new PVector(coord.x, radius), coord).normalize(), mass));
}
void blowFrom(int x, int y)
{
PVector fromVector = new PVector(x, y);
PVector direction = PVector.sub(coord, fromVector).normalize();
addForce(direction.mult(8 * (img.height - PVector.dist(coord, fromVector))));
calcVel();
}
// Newton's F-a-v-d system
void addForce(float x, float y)
{
addForce(new PVector(x, y));
}
void addForce(PVector force)
{
acc.add(PVector.div(force, mass));
}
void calcAcc()
{
acc = new PVector();
// gravity
addForce(0, gravity * mass);
// fraction
addForce(PVector.mult(vel, mass * -fraction));
collisionTest();
}
void calcVel()
{
vel.add(acc);
}
void calcPos()
{
setCoord(PVector.add(coord, vel));
}
// coordination operations
void setCoord(int x, int y)
{
setCoord(new PVector(x, y));
}
void setCoord(PVector v)
{
coord = v;
coord.x = constrain(coord.x, 0, img.width-1);
coord.y = constrain(coord.y, 0, img.height-1);
}
}
// bubble functions
void updateBubbles()
{
for (int i = 0; i < chunks.size(); i++)
{
for (int j = 0; j < chunks.get(i).size(); j++)
{
chunks.get(i).get(j).updateData();
}
}
}
void drawBubbles(PGraphics canvas)
{
for (int i = 0; i < chunks.size(); i++)
{
for (int j = 0; j < chunks.get(i).size(); j++)
{
chunks.get(i).get(j).onDraw(canvas);
}
}
}
// instantiate one bubble
Bubble setBubble(int x, int y)
{
// border limits
if (x < bleedingX || x > bleedingX+img.width) return null;
if (y < bleedingY || y > bleedingY+img.height) return null;
// canvas coord to image coord
x -= bleedingX;
y -= bleedingY;
Bubble bubble = new Bubble(
x + round(random(-1, 1)),
y + round(random(-1, 1)),
scale, img, splashed, TTL);
getChunkByPixel(x, y).add(bubble);
bubble.parent = getChunkByPixel(x, y);
return bubble;
}
// set a cluster of bubbles
void setCluster(int x, int y)
{
for (int i = 0; i < 64; i++)
setBubble(x, y);
}
// blow away bubbles
void blowFrom(int x, int y)
{
// canvas coord to image coord
x -= bleedingX;
y -= bleedingY;
for (int i = 0; i < chunks.size(); i++)
{
for (int j = 0; j < chunks.get(i).size(); j++)
{
chunks.get(i).get(j).blowFrom(x, y);
}
}
}
// poke the bubble under cursor
boolean tryPokeFrom(int x, int y)
{
// canvas coord to image coord
x -= bleedingX;
y -= bleedingY;
ArrayList<ArrayList<Bubble>> adjacentChunks =
get3x3ChunksByPixel(x, y);
for (int i = 0; i < adjacentChunks.size(); i++)
{
for (int j = 0; j < adjacentChunks.get(i).size(); j++)
{
adjacentChunks.get(i).get(j).tryPokeFrom(x, y);
}
}
return false;
}
// poke all the bubbles
void pokeAll()
{
for (int i = 0; i < chunks.size(); i++)
{
// poke() lets the size of the ArrayList minus 1
while (chunks.get(i).size() > 0)
{
chunks.get(i).get(0).poke();
}
}
}