-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
317 lines (272 loc) · 7.69 KB
/
main.js
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
const gW = 500;
const gH = 10;
const grid = [];
let cellSize;
const flowSpeed = 0.000;
let showStats = false;
const sizeFactor = 10;
const barrierLine = [];
function setup() {
frameRate(60);
createCanvas(gW*sizeFactor, gH*sizeFactor);
for (let i = 0; i < gW; i++) {
const col = [];
for (let j = 0; j < gH; j++) {
col.push(new Site());
}
grid.push(col);
}
cellSize = width/gW;
// Init fluid
for (let i = 0; i < gW; i++) {
for (let j = 0; j < gH; j++) {
// setEquil takes velocity vector and density
// Setting an x flow between 0 and 0.120 works the best
grid[i][j].setEquil(new p5.Vector(flowSpeed, 0), 1);
}
}
barrierLine.push(grid[20][3]);
barrierLine.push(grid[20][4]);
barrierLine.push(grid[20][5]);
barrierLine.push(grid[20][6]);
barrierLine.push(grid[20][7]);
}
let gridMouseX = 0;
let gridMouseY = 0;
function mouseMoved() {
gridMouseX = Math.floor(map(mouseX, 0, width, 0, gW));
gridMouseY = Math.floor(map(mouseY, 0, height, 0, gH));
showStats = true;
}
function mouseClicked() {
console.log(grid[gridMouseX][gridMouseY]);
}
function mouseDragged() {
gridMouseX = Math.floor(map(mouseX, 0, width, 0, gW));
gridMouseY = Math.floor(map(mouseY, 0, height, 0, gH));
//grid[gridMouseX][gridMouseY].isBarrier = true;
for (let b = 0; b < barrierLine.length; b++) {
barrierLine[b] = grid[gridMouseX][b + 3];
barrierLine[b].setEquil(new p5.Vector(0.120, 0), 2)
}
}
function drawArrow(base, vec, myColor) {
push();
stroke(myColor);
strokeWeight(2);
fill(myColor);
translate(base.x, base.y);
line(0, 0, vec.x, vec.y);
rotate(vec.heading());
let arrowSize = 4;
translate(vec.mag() - arrowSize, 0);
line(0, -arrowSize/2, arrowSize, 0);
line(0, arrowSize/2, arrowSize, 0);
pop();
}
function mapColor(val) {
//return map(val, 0, 0.120, 255, 100);
return map(val, 0.05, 1.5, 255, 0);
//return map(val, -0.05, 0.120, 255, 5);
}
function computeCurl() {
for (let y = 1; y < gH - 1; y++) {
for (let x = 1; x < gW - 1; x++) {
grid[x][y].curl =
grid[x+1][y].velocity.y -
grid[x-1][y].velocity.y -
grid[x][y+1].velocity.x +
grid[x][y-1].velocity.x;
}
}
}
function paintSpeed() {
for (let i = 0; i < gW; i++) {
for (let j = 0; j < gH; j++) {
const drawx = i * cellSize;
const drawy = j * cellSize;
noStroke()
//fill(mapColor(grid[i][j].velocity.magSq()))
fill(mapColor(grid[i][j].density))
square(drawx, drawy, cellSize);
}
}
}
function paintBarrier() {
for (let i = 0; i < gW; i++) {
for (let j = 0; j < gH; j++) {
const drawx = i * cellSize;
const drawy = j * cellSize;
if (grid[i][j].isBarrier) {
fill("black")
square(drawx, drawy, cellSize);
}
}
}
}
function paintCurl() {
for (let i = 0; i < gW; i++) {
for (let j = 0; j < gH; j++) {
const drawx = i * cellSize;
const drawy = j * cellSize;
noStroke();
fill(mapColor(grid[i][j].curl));
square(drawx, drawy, cellSize);
}
}
}
function paint() {
for (let i = 0; i < gW; i++) {
for (let j = 0; j < gH; j++) {
const drawx = i * cellSize;
const drawy = j * cellSize;
push();
if (grid[i][j].isBarrier) {
stroke("green")
strokeWeight(4)
square(drawx, drawy, cellSize);
} else {
//stroke("red");
}
pop();
drawArrow(createVector(drawx + cellSize/2, drawy + cellSize/2), grid[i][j].velocity, "#f00");
push();
translate(drawx, drawy);
noFill();
const boxSize = cellSize/3;
// NW
push();
fill(mapColor(grid[i][j].displacements.nw.density), 100);
square(0, 0, boxSize);
pop();
// N
push();
fill(mapColor(grid[i][j].displacements.n.density), 100);
square(1 * boxSize, 0, boxSize);
pop();
// NE
push();
fill(mapColor(grid[i][j].displacements.ne.density), 100);
square(2 * boxSize, 0, boxSize);
pop();
// W
push();
fill(mapColor(grid[i][j].displacements.w.density), 100);
square(0, 1 * boxSize, boxSize);
pop();
// C
push();
fill(mapColor(grid[i][j].displacements.c.density), 100);
square(1 * boxSize, 1 * boxSize, boxSize);
pop();
// E
push();
fill(mapColor(grid[i][j].displacements.e.density), 100);
square(2 * boxSize, 1 * boxSize, boxSize);
pop();
// SW
push();
fill(mapColor(grid[i][j].displacements.sw.density), 100);
square(0, 2 * boxSize, boxSize);
pop();
// S
push();
fill(mapColor(grid[i][j].displacements.s.density), 100);
square(1 * boxSize, 2 * boxSize, boxSize);
pop();
// SE
push();
fill(mapColor(grid[i][j].displacements.se.density), 100);
square(2 * boxSize, 2 * boxSize, boxSize);
pop();
pop();
}
}
}
function stream() {
for (let x = 1; x < gW - 1; x++) {
for (let y = 1; y < gH - 1; y++) {
grid[x][y].displacements.n.density = grid[x][(y+1)].displacements.n.density;
grid[x][y].displacements.nw.density = grid[(x+1)][(y+1)].displacements.nw.density;
}
}
for (let x = gW - 2; x > 0; x--) {
for (let y = 1; y < gH - 1; y++) {
grid[x][y].displacements.e.density = grid[(x-1)][y].displacements.e.density;
grid[x][y].displacements.ne.density = grid[(x-1)][(y+1)].displacements.ne.density;
}
}
for (let x = gW - 2; x > 0; x--) {
for (let y = gH - 2; y > 0; y--) {
grid[x][y].displacements.s.density = grid[x][(y-1)].displacements.s.density;
grid[x][y].displacements.se.density = grid[(x-1)][(y-1)].displacements.se.density;
}
}
for (let x = 1; x < gW - 1; x++) {
for (let y = gH - 2; y > 0; y--) {
grid[x][y].displacements.w.density = grid[(x+1)][y].displacements.w.density;
grid[x][y].displacements.sw.density = grid[(x+1)][(y-1)].displacements.sw.density;
}
}
}
function reflect() {
for (let y = 1; y < gH - 1; y++) {
for (let x = 1; x < gW - 1; x++) {
if (grid[x][y].isBarrier) {
grid[x-1][y].displacements.w.density = grid[x][y].displacements.e.density;
grid[x][y-1].displacements.n.density = grid[x][y].displacements.s.density;
grid[x][y+1].displacements.s.density = grid[x][y].displacements.n.density;
grid[x+1][y].displacements.e.density = grid[x][y].displacements.w.density;
grid[x+1][y-1].displacements.ne.density = grid[x][y].displacements.sw.density;
grid[x-1][y-1].displacements.nw.density = grid[x][y].displacements.se.density;
grid[x+1][y+1].displacements.se.density = grid[x][y].displacements.nw.density;
grid[x-1][y+1].displacements.sw.density = grid[x][y].displacements.ne.density;
}
}
}
}
function draw() {
//strokeWeight(0.5)
background(255);
computeCurl();
push();
paintSpeed();
//paintCurl();
pop();
push()
paintBarrier();
pop()
if (!keyIsDown(32)) {
// Set boundaries
for (let i = 0; i < gW; i++) {
grid[i][0].setEquil(createVector(flowSpeed, 0), 1);
grid[i][gH-1].setEquil(createVector(flowSpeed, 0), 1);
}
for (let j = 1; j < gH - 1; j++) {
grid[0][j].setEquil(createVector(flowSpeed, 0), 1);
grid[gW-1][j].setEquil(createVector(flowSpeed, 0), 1);
}
for (let x = 0; x < 10; x++) {
// THIS IS COLLIDING
for (let j = 0; j < gH; j++) {
for (let i = 0; i < gW; i++) {
grid[i][j].collide();
}
}
for (let k = 1; k < gH - 2; k++) {
grid[gW-1][k].displacements.w.density = grid[gW-2][k].displacements.w.density;
grid[gW-1][k].displacements.nw.density = grid[gW-2][k].displacements.nw.density;
grid[gW-1][k].displacements.sw.density = grid[gW-2][k].displacements.sw.density;
}
stream();
reflect();
}
}
fill(0);
textSize(16);
text(`[${gridMouseX}, ${gridMouseY}]:`, 10, 10)
text(`Omega: ${grid[gridMouseX][gridMouseY].OMEGA.toFixed(4)}`, 10 + 10, 10 + 20)
text(`Density: ${grid[gridMouseX][gridMouseY].density.toFixed(4)}`, 10 + 10, 10 + 40)
text(`Velocity (X): ${grid[gridMouseX][gridMouseY].velocity.x.toFixed(4)}`, 10 + 10, 10 + 60)
text(`Velocity (Y): ${grid[gridMouseX][gridMouseY].velocity.y.toFixed(4)}`, 10 + 10, 10 + 80)
}