-
Notifications
You must be signed in to change notification settings - Fork 0
/
fluid.go
127 lines (97 loc) · 2.25 KB
/
fluid.go
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
package main
import (
"fmt"
rl "github.com/gen2brain/raylib-go/raylib"
)
var (
N = 128
iter = 16
SCALE = 5
)
func constrain(val, min, max int) int {
if val < min {
return min
}
if val > max {
return max
}
return val
}
func IX(x, y int) int {
x = constrain(x, 0, N-1)
y = constrain(y, 0, N-1)
k := x + (y * N)
if k >= 16384 {
fmt.Println("FLUID: OUT OF BOUNDS BRO, REQUESTED: ", k)
return 1
} else {
return k
}
}
type Fluid struct {
size int
dt float32
diff float32
visc float32
s []float32
density []float32
Vx []float32
Vy []float32
Vx0 []float32
Vy0 []float32
}
func (f *Fluid) setup(dt, diffusion, viscosity float32) {
f.size = N
f.dt = dt
f.diff = diffusion
f.visc = viscosity
f.s = make([]float32, N*N)
f.density = make([]float32, N*N)
f.Vx = make([]float32, N*N)
f.Vy = make([]float32, N*N)
f.Vx0 = make([]float32, N*N)
f.Vy0 = make([]float32, N*N)
fmt.Println("FLUID: Setup done!")
}
func (f *Fluid) step() {
//El algoritmo
diffuse(1, f.Vx0, f.Vx, f.visc, f.dt)
diffuse(2, f.Vy0, f.Vy, f.visc, f.dt)
project(f.Vx0, f.Vy0, f.Vx, f.Vy)
advect(1, f.Vx, f.Vx0, f.Vx0, f.Vy0, f.dt)
advect(2, f.Vy, f.Vy0, f.Vx0, f.Vy0, f.dt)
project(f.Vx, f.Vy, f.Vx0, f.Vy0)
diffuse(0, f.s, f.density, f.diff, f.dt)
advect(0, f.density, f.s, f.Vx, f.Vy, f.dt)
}
func (f *Fluid) addDensity(x, y int, amount float32) {
index := IX(x, y)
f.density[index] += amount
}
func (f *Fluid) addVelocity(x, y int, amountX, amountY float32) {
index := IX(x, y)
// fmt.Println("\nFLUID: VELOCITY INDEX RETURNED: ", index)
f.Vx[index] += amountX
f.Vy[index] += amountY
}
func (f *Fluid) renderD() {
for i := 0; i < N; i++ {
for j := 0; j < N; j++ {
x := i * SCALE
y := j * SCALE
d := uint8(f.density[IX(i, j)])
// fmt.Println("DENSITY:", d)
// alpha := uint8(int((d + 50)) % 25)
// fmt.Println("ALPHA:", alpha)
// alpha := uint8(255)
// fmt.Println("X: ", x, "Y: ", y)
if d < 1 {
rl.DrawRectangle(int32(x), int32(y), int32(SCALE), int32(SCALE), rl.NewColor(0, 0, 0, 255))
} else {
slope := (255 - 0) / (180 - 0)
output := uint8(0 + uint8(slope)*(d-0))
rl.DrawRectangle(int32(x), int32(y), int32(SCALE), int32(SCALE), rl.NewColor(output, output, output-140, output))
}
}
}
}