-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fluidje.pde
263 lines (215 loc) · 8.11 KB
/
Fluidje.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
class FluidRenderer extends AudioRenderer {
/* OpenProcessing Tweak of *@*http://www.openprocessing.org/sketch/29833*@* */
/* !do not delete the line above, required for linking your tweak if you re-upload */
/* Tweaked by Lighting Organism */
/*
Circus Fluid
Made by Jared "BlueThen" C. on June 5th, 2011.
Updated June 7th, 2011 (Commenting, refactoring, coloring changes)
www.bluethen.com
www.twitter.com/BlueThen
www.openprocessing.org/portal/?userID=3044
www.hawkee.com/profile/37047/
Feel free to email me feedback, criticism, advice, job offers at:
bluethen (@) gmail.com
*/
// Variables for the timeStep
long previousTime;
long currentTime;
float timeScale = 1; // Play with this to slow down or speed up the fluid (the higher, the faster)
final int fixedDeltaTime = (int)(10 / timeScale);
float fixedDeltaTimeSeconds = (float)fixedDeltaTime / 1000;
float leftOverDeltaTime = 0;
// The grid for fluid solving
GridSolver grid;
int rotations;
FluidRenderer(AudioSource source) {
rotations = (int) source.sampleRate() / source.bufferSize();
}
void setup () {
//size(canvasW, canvasH, P3D);
colorMode(HSB, 255);
noStroke();
// grid = new GridSolver(integer cellWidth)
grid = new GridSolver(5);
//colorMode(RGB, 255);
setcolorMode = 0;
vFader3 = 100;
}
synchronized void draw () {
colorMode(HSB, 255);
/******** Physics ********/
// time related stuff
// Calculate amount of time since last frame (Delta means "change in")
currentTime = millis();
long deltaTimeMS = (long)((currentTime - previousTime));
previousTime = currentTime; // reset previousTime
// timeStepAmt will be how many of our fixedDeltaTimes we need to make up for the passed time since last frame.
int timeStepAmt = (int)(((float)deltaTimeMS + leftOverDeltaTime) / (float)(fixedDeltaTime));
// If we have any left over time left, add it to the leftOverDeltaTime.
leftOverDeltaTime += deltaTimeMS - (timeStepAmt * (float)fixedDeltaTime);
if (timeStepAmt > 15) {
timeStepAmt = 15; // too much accumulation can freeze the program!
println("Time step amount too high");
}
// Update physics
for (int iteration = 1; iteration <= timeStepAmt; iteration++) {
try {
grid.solve(fixedDeltaTimeSeconds * timeScale);
throw new NullPointerException();
}
catch (NullPointerException e) {
}
}
grid.draw();
//println(frameRate);
}
/* Interation stuff below this line */
public void mouseDragged () {
// The ripple size will be determined by mouse speed
float force = dist(mouseX, mouseY, pmouseX, pmouseY) * 255;
/* This is bresenham's line algorithm
http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
Instead of plotting points for a line, we create a ripple for each pixel between the
last cursor pos and the current cursor pos
*/
float dx = abs(mouseX - pmouseX);
float dy = abs(mouseY - pmouseY);
float sx;
float sy;
if (pmouseX < mouseX)
sx = 1;
else
sx = -1;
if (pmouseY < mouseY)
sy = 1;
else
sy = -1;
float err = dx - dy;
float x0 = pmouseX;
float x1 = mouseX;
float y0 = pmouseY;
float y1 = mouseY;
while ( (x0 != x1) || (y0 != y1)) {
// Make sure the coordinate is within the window
if (((int)(x0 / grid.cellSize) < grid.density.length) && ((int)(y0 / grid.cellSize) < grid.density[0].length) &&
((int)(x0 / grid.cellSize) > 0) && ((int)(y0 / grid.cellSize) > 0))
grid.velocity[(int)(x0 / grid.cellSize)][(int)(y0 / grid.cellSize)] += force;
float e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
x0 = x0 + sx;
}
if (e2 < dx) {
err = err + dx;
y0 = y0 + sy;
}
}
}
public void onClick(float mX, float mY) {
float cX = mX * canvasW;
float cY = mY * canvasH;
int oX = (int)cX;
int oY = (int)cY;
float force = 200000;
if (((int)(cX / grid.cellSize) < grid.density.length) && ((int)(cY / grid.cellSize) < grid.density[0].length) &&
((int)(cX / grid.cellSize) > 0) && ((int)(cY / grid.cellSize) > 0)) {
grid.velocity[(int)(cX / grid.cellSize)][(int)(cY / grid.cellSize)] += force;
}
}
// If the user clicks instead of drags the mouse, we create a ripple at one spot.
public void mouseClicked () {
float force = 200000;
if (((int)(mouseX / grid.cellSize) < grid.density.length) && ((int)(mouseY / grid.cellSize) < grid.density[0].length) &&
((int)(mouseX / grid.cellSize) > 0) && ((int)(mouseY / grid.cellSize) > 0)) {
grid.velocity[(int)(mouseX / grid.cellSize)][(int)(mouseY / grid.cellSize)] += force;
}
}
}
// Velocity: How fast each pixel is moving up or down
// Density: How much "fluid" is in each pixel.
// *note*
// Density isn't conserved as far as I know.
// Changing the velocity ends up changing the density too.
class GridSolver {
int cellSize;
// Use 2 dimensional arrays to store velocity and density for each pixel.
// To access, use this: grid[x/cellSize][y/cellSize]
float [][] velocity;
float [][] density;
float [][] oldVelocity;
float [][] oldDensity;
float friction = .581;
float speed = 21;
float setContrastModeF = .00003 ;
// float setcolorMode = 167;
/* Constructor */
GridSolver (int sizeOfCells) {
cellSize = sizeOfCells;
velocity = new float[int(width/cellSize)][int(height/cellSize)];
density = new float[int(width/cellSize)][int(height/cellSize)];
}
/* Drawing */
void draw () {
for (int x = 0; x < velocity.length; x++) {
for (int y = 0; y < velocity[x].length; y++) {
/* Sine probably isn't needed, but oh well. It's pretty and looks more organic. */
//color
float setContrastModeF = (float)map(vFader4, 0, 255, 0, .00004);
//fill(167 + 127 * sin(density[x][y]*0.00004), 127, 0 + 127 * sin(velocity[x][y]*0.0004));
//variable color
fill(setcolorMode + 20 * sin(density[x][y]*setContrastModeF), vFader2, vFader3 + 127 * sin(velocity[x][y]*0.0004));
//(setcolorMode-50)+40*sin(PI*x/width
rect(x*cellSize, y*cellSize, cellSize, cellSize);
}
}
}
/*
public void setcolorMode(float colorspectrum) {
setcolorMode = (int)map(colorspectrum, 0, 1, 0, 255);
}
*/
/* "Fluid" Solving
Based on http://www.cs.ubc.ca/~rbridson/fluidsimulation/GameFluids2007.pdf
To help understand this better, imagine each pixel as a spring.
Every spring pulls on springs adjacent to it as it moves up or down (The speed of the pull is the Velocity)
This pull flows throughout the window, and eventually deteriates due to friction
*/
void solve (float timeStep) {
// Reset oldDensity and oldVelocity
oldDensity = (float[][])density.clone();
oldVelocity = (float[][])velocity.clone();
for (int x = 0; x < velocity.length; x++) {
for (int y = 0; y < velocity[x].length; y++) {
/* Equation for each cell:
Velocity = oldVelocity + (sum_Of_Adjacent_Old_Densities - oldDensity_Of_Cell * 4) * timeStep * speed)
Density = oldDensity + Velocity
Scientists and engineers: Please don't use this to model tsunamis, I'm pretty sure it's not *that* accurate
*/
velocity[x][y] = friction * oldVelocity[x][y] + ((getAdjacentDensitySum(x,y) - density[x][y] * 4) * timeStep * speed);
density[x][y] = oldDensity[x][y] + velocity[x][y];
}
}
}
float getAdjacentDensitySum (int x, int y) {
// If the x or y is at the boundary, use the closest available cell
float sum = 0;
if (x-1 > 0)
sum += oldDensity[x-1][y];
else
sum += oldDensity[0][y];
if (x+1 <= oldDensity.length-1)
sum += (oldDensity[x+1][y]);
else
sum += (oldDensity[oldDensity.length-1][y]);
if (y-1 > 0)
sum += (oldDensity[x][y-1]);
else
sum += (oldDensity[x][0]);
if (y+1 <= oldDensity[x].length-1)
sum += (oldDensity[x][y+1]);
else
sum += (oldDensity[x][oldDensity[x].length-1]);
return sum;
}
}