-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation_worker.js
92 lines (76 loc) · 2.5 KB
/
simulation_worker.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
85
86
87
88
89
90
91
92
self.addEventListener("message", messageHandler);
function messageHandler(message) {
if (message.data.mode == "precompute") {
tickFrames(message);
}
}
function tickFrames(message) {
console.log(message.data);
let dt = message.data.dt;
let saveEach = message.data.saveEach;
let bodies = message.data.bodies;
let gee = message.data.gee;
let frameCount = message.data.frameCount;
let trajectory = [];
for (let frame = 0; frame < frameCount; frame++) {
bodies = tickFrame(saveEach, dt, gee, bodies);
self.postMessage({
result: "frameTick",
frameNumber: frame,
percent: `${Math.round(100* (frame + 1)/frameCount)}%`
});
trajectory.push(JSON.parse(JSON.stringify(bodies)));
}
self.postMessage({
result: "complete",
trajectory: trajectory
});
}
function tickFrame(saveEach, dt, gee, bodies) {
for (let step = 0; step < saveEach; step++) {
// Increment positions due to velocities
bodies.forEach((b) => {
if (b.fix) return;
b.x = b.x + b.vx * dt;
b.y = b.y + b.vy * dt;
// s.x = b.x;
// s.y = b.y;
//console.log(`${b.vx} ${b.vy}`);
});
//Compute accelerations. Indices are i and j to be consistent with notes
bodies.forEach((i) => {
if (i.fix) return;
var ax = 0.0,
ay = 0.0;
bodies.forEach((j) => {
if (i == j) return;
let dx = i.x - j.x;
let dy = i.y - j.y;
let dxy = Math.abs(dx * dx * dx) +
Math
.abs(dy * dy * dy);
let radsum = i.r + j.r;
if ((dx * dx + dy * dy) <=
radsum *
radsum) {
// let tan = Math.atan2(dx, dy)
// let dm = i.m - j.m;
// let am = (i.m + j.ml) * this.dt;
// ax += (dm * i.vx + 2 * j.m * j.vx) /
// am;
// ay += (dm * i.vy + 2 * j.m * j.vy) /
// am;
} else {
ax += j.m * dx / dxy;
ay += j.m * dy / dxy;
}
});
ax *= -gee;
ay *= -gee;
//console.log(`${ax} ${ay}`);
if (ax) i.vx += ax * dt;
if (ay) i.vy += ay * dt;
});
}
return bodies;
}