-
Notifications
You must be signed in to change notification settings - Fork 7
/
SketchFluidDynamics2.java
executable file
·104 lines (87 loc) · 2.99 KB
/
SketchFluidDynamics2.java
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
package de.hfkbremen.algorithmiccliches.examples;
import de.hfkbremen.algorithmiccliches.fluiddynamics.FluidDynamics;
import processing.core.PApplet;
public class SketchFluidDynamics2 extends PApplet {
private boolean showDensity = true;
private boolean showVelocity = true;
private FluidDynamics mFluid;
public void settings() {
size(1024, 768, P3D);
}
public void setup() {
noStroke();
textFont(createFont("Courier", 11));
mFluid = new FluidDynamics(96, 72);
mFluid.diffusion(0.0003f);
mFluid.drag(0.995f);
strokeWeight(0.1f);
}
public void draw() {
background(255);
float mDeltaTime = 1.0f / frameRate;
mFluid.update(mDeltaTime);
if (mousePressed) {
int x = (mouseX * mFluid.width()) / width + 1;
int y = (mouseY * mFluid.height()) / height + 1;
if (mouseButton == LEFT) {
final float vX = (mouseX - pmouseX) * 0.01f;
final float vY = (mouseY - pmouseY) * 0.01f;
mFluid.setVelocityArea(x, y, vX, vY, 3);
} else {
float m = (mouseX - pmouseX) + (mouseY - pmouseY);
mFluid.setDensityArea(x, y, range(abs(m), 0, 1), 5);
}
}
if (showVelocity) {
noFill();
stroke(0, 127, 255, 63);
pushMatrix();
scale((float) width / mFluid.width(), (float) height / mFluid.height());
mFluid.drawVelocity(g);
popMatrix();
}
if (showDensity) {
noStroke();
pushMatrix();
scale((float) width / mFluid.width(), (float) height / mFluid.height());
mFluid.drawDensity(g, color(255, 127, 0, 225));
popMatrix();
}
noStroke();
fill(127);
text("VISCOSITY : " + mFluid.viscosity() * 100, 10, 12);
text("DIFFUSION : " + mFluid.diffusion() * 100, 10, 24);
text("FPS : " + (int) frameRate, 10, 36);
}
public void keyPressed() {
if (key == 'v') {
showVelocity = !showVelocity;
}
if (key == 'd') {
showDensity = !showDensity;
}
if (key == 'r') {
mFluid.reset();
}
if (key == '+') {
mFluid.viscosity(mFluid.viscosity() + 0.00001f);
}
if (key == '-') {
mFluid.viscosity(mFluid.viscosity() - 0.00001f);
}
if (key == '*') {
mFluid.diffusion(mFluid.diffusion() + 0.00001f);
}
if (key == '_') {
mFluid.diffusion(mFluid.diffusion() - 0.00001f);
}
mFluid.viscosity(max(mFluid.viscosity(), 0.0f));
mFluid.diffusion(max(mFluid.diffusion(), 0.0f));
}
private float range(float f, float minf, float maxf) {
return Math.max(Math.min(f, maxf), minf);
}
public static void main(String[] args) {
PApplet.main(new String[]{SketchFluidDynamics2.class.getName()});
}
}