Skip to content

Commit 6c9bcd6

Browse files
committedJul 31, 2020
feat: add velocity buffers
1 parent 9d99aae commit 6c9bcd6

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.physics2d.states;
2+
3+
public class Buffer {
4+
public float[][] velXxDens,velYxDens,density;
5+
}

‎core/src/com/physics2d/states/PlayState.java

+34-14
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected void update(float dt) {
3333

3434
float[][] densityAddBuffer = new float[X_SIZE][Y_SIZE];
3535
float[][] densityRemoveBuffer = new float[X_SIZE][Y_SIZE];
36-
advection(densityAddBuffer, densityRemoveBuffer);
36+
advection(densityAddBuffer);
3737
}
3838

3939
@Override
@@ -48,37 +48,57 @@ public void dispose() {
4848
background.dispose();
4949
}
5050

51-
private void advection(float[][] densityAddBuffer, float[][] densityRemoveBuffer) {
51+
private void advection(float[][] densityAddBuffer) {
5252

5353

5454
for (int x = 0; x < X_SIZE; x++) {
5555
for (int y = 0; y < X_SIZE; y++) {
56-
pushDensity(x, y, densityAddBuffer, densityRemoveBuffer);
56+
pushDensity(x, y, densityAddBuffer);
5757
}
5858
}
5959
}
6060

61-
private void pushDensity(int x, int y, float[][] densityAddBuffer, float[][] densityRemoveBuffer) {
61+
private Buffer pushDensity(int x, int y, Buffer buffer) {
6262
float newX = x + velX[x][y];
6363
float newY = y + velY[x][y];
6464

6565
float right = (newX % 1);
6666
float up = newY % 1;
6767

68-
float value = (1-right)*(1-up)*density[x][y];
69-
densityAddBuffer[(int)Math.floor(newX)][(int)Math.floor(newY)] += value;
70-
densityRemoveBuffer[x][y] -= value;
71-
68+
//TODO: loop this shit
69+
int destX, destY;
70+
float value;
71+
72+
//down left
73+
destX = (int)Math.floor(newX);
74+
destY = (int)Math.floor(newY);
75+
value = (1-right)*(1-up)*density[x][y];
76+
buffer.density[destX][destY] += value;
77+
buffer.velXxDens[destX][destY] += value*velX[x][y];
78+
buffer.velYxDens[destX][destY] += value*velY[x][y];
79+
80+
//down right
81+
destX = (int)Math.floor(newX+1);
82+
destY = (int)Math.floor(newY);
7283
value = (right)*(1-up)*density[x][y];
73-
densityAddBuffer[(int)Math.floor(newX+1)][(int)Math.floor(newY)] += value;
74-
densityRemoveBuffer[x][y] -= value;
84+
buffer.density[destX][destY] += value;
85+
buffer.velXxDens[destX][destY] += value*velX[x][y];
86+
buffer.velYxDens[destX][destY] += value*velY[x][y];
7587

88+
//up left
89+
destX = (int)Math.floor(newX);
90+
destY = (int)Math.floor(newY+1);
7691
value = (1-right)*(up)*density[x][y];
77-
densityAddBuffer[(int)Math.floor(newX)][(int)Math.floor(newY+1)] += value;
78-
densityRemoveBuffer[x][y] -= value;
92+
buffer.density[destX][destY] += value;
93+
buffer.velXxDens[destX][destY] += value*velX[x][y];
94+
buffer.velYxDens[destX][destY] += value*velY[x][y];
7995

96+
//up right
97+
destX = (int)Math.floor(newX+1);
98+
destY = (int)Math.floor(newY+1);
8099
value = (right)*(up)*density[x][y];
81-
densityAddBuffer[(int)Math.floor(newX+1)][(int)Math.floor(newY+1)] += value;
82-
densityRemoveBuffer[x][y] -= value;
100+
buffer.density[destX][destY] += value;
101+
buffer.velXxDens[destX][destY] += value*velX[x][y];
102+
buffer.velYxDens[destX][destY] += value*velY[x][y];
83103
}
84104
}

0 commit comments

Comments
 (0)
Please sign in to comment.