-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwave.js
84 lines (65 loc) · 2.46 KB
/
wave.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
function updateSurface(surface) {
for (let i = 1; i < SURFACE_RESOL - 1; i++) {
surface[i] += sourceField[i];
surface[i] *= obstacleField[i];
}
//propagate
for (let i = 1; i < SURFACE_RESOL - 1; i++) {
velocityField[i] += (surface[(i - 1)] + surface[(i + 1)]) / 2.0 - surface[i];
velocityField[i] *= damping;
}
//update vertex heights
for (let i = 1; i < SURFACE_RESOL - 1; i++) {
surface[i] += velocityField[i];
}
}
function updateSurface1(surface, dt, offset = 1) {
// a bit hard code
dt /= 9
const c = 1, c2 = c * c
const segmentSize = abs(surface.range[1] - surface.range[0]) / surface.resol;
const segmentSize2 = pow(segmentSize, 2)
for (let i = offset; i < surface.resol - offset; i++) {
surface.surface[i] += surface.sourceField[i];
surface.surface[i] *= surface.obstacleField[i];
}
//propagate
//const obsLevel = boxObj.pos.valss[1] + boxObj.h
for (let i = offset; i < surface.resol - offset; i++) {
//const itSelf = (col[i]?obsLevel:surface[i])
const itSelf = surface.surface[i]
let lhs = surface.surface[i - 1]
if (i === 0)
lhs = surface.extLeft ? surface.extLeft : itSelf
let rhs = surface.surface[i + 1]
if (i === surface.resol - 1)
rhs = surface.extRight ? surface.extRight : itSelf
f = c2 * (lhs + rhs - 2 * itSelf) / segmentSize2 + calExtForce(i)
surface.velocityField[i] += f * dt;
if (offset === 0) {
for (let idx = 0; idx < surface.resol; idx++) {
const heigth = surface.surface[idx]
const dir = heigth > surface.normalLevel ? -1 : 1
surface.velocityField[idx] += dir * abs(heigth - surface.normalLevel) * 2e-7 * dt
}
}
surface.velocityField[i] *= damping;
}
//update vertex heights
for (let i = offset; i < surface.resol - offset; i++) {
surface.surface[i] += surface.velocityField[i] * dt;
if (surface.box)
surface.surface[i] = max(surface.surface[i],
surface.box.pos.val[1] + surface.box.h)
}
}
function calExtForce(i) {
return 0;
if (col[i]) {
//console.log((surface[i]-prevSurface[i]) * waterDense * G)
// console.log(surface[i],prevSurface[i])
// return (surface[i]-prevSurface[i])
return -(surface[i] - prevSurface[i]) * waterDense * G
}
return 0
}