-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation.js
204 lines (197 loc) · 5.72 KB
/
simulation.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
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
window.createSimulation = function (initHeight, initWidth) {
var maxV = 0.5;
var height = initHeight;
var width = initWidth;
var particles = {};
var nextParticleId = 0;
var events = createEventQueue();
function moveParticles(timestamp) {
for (var id in particles) {
if (particles.hasOwnProperty(id)) {
particles[id].move(timestamp);
}
}
}
function predictNextCollision(particle, timestamp) {
particle.startEvt = {
x: particle.x,
y: particle.y,
timestamp: timestamp
};
function timeToHitWall(coord, speed, size) {
if (speed == 0)
return Infinity;
var dist = speed > 0 ? size - coord - particle.r : coord - particle.r;
if (dist < 0)
return 0;
else
return dist / Math.abs(speed);
}
function timeToHitParticle(another) {
if (particle === another) return Infinity;
var dx = another.x - particle.x, dy = another.y - particle.y;
var dvx = another.vx - particle.vx, dvy = another.vy - particle.vy;
var dvdr = dx*dvx + dy*dvy;
if (dvdr > 0) return Infinity;
var dvdv = dvx*dvx + dvy*dvy;
var drdr = dx*dx + dy*dy;
var sigma = particle.r + another.r;
var d = (dvdr*dvdr) - dvdv * (drdr - sigma*sigma);
if (d < 0) return Infinity;
return -(dvdr + Math.sqrt(d)) / dvdv;
}
var hrz = timeToHitWall(particle.y, particle.vy, height)
var vrt = timeToHitWall(particle.x, particle.vx, width);
var prt = Infinity;
var another;
for (var id in particles) {
if (particles.hasOwnProperty(id)) {
var currPrt = timeToHitParticle(particles[id]);
if (currPrt < prt) {
prt = currPrt;
another = particles[id];
}
}
}
function bounceOffHorizontalWall() {
particle.vy = -particle.vy;
}
function bounceOffVerticalWall() {
particle.vx = -particle.vx;
}
function bounceOffParticle() {
var dx = another.x - particle.x, dy = another.y - particle.y;
var dvx = another.vx - particle.vx, dvy = another.vy - particle.vy;
var dvdr = dx*dvx + dy*dvy;
var dist = particle.r + another.r;
var J = 2*particle.m*another.m*dvdr/((particle.m+another.m)*dist);
var Jx = J*dx/dist;
var Jy = J*dy/dist;
particle.vx += Jx / particle.m;
particle.vy += Jy / particle.m;
another.vx -= Jx / another.m;
another.vy -= Jy / another.m;
var v = Math.max(Math.abs(particle.vx), Math.abs(particle.vy),
Math.abs(another.vx), Math.abs(another.vy));
if (v > maxV) {
var coeff = maxV/v;
particle.vx *= coeff;
particle.vy *= coeff;
another.vx *= coeff;
another.vy *= coeff;
}
}
var min = Math.min(hrz, vrt, prt)
if (min === hrz) {
particle.addEndEvt(hrz);
var collision = {
active: true,
timestamp: particle.startEvt.timestamp + hrz,
effect: bounceOffHorizontalWall,
particles: [particle]
};
particle.collision = collision;
return collision;
} else if (min === vrt) {
particle.addEndEvt(vrt);
var collision = {
active: true,
timestamp: particle.startEvt.timestamp + vrt,
effect: bounceOffVerticalWall,
particles: [particle]
};
particle.collision = collision;
return collision;
} else {
particle.addEndEvt(prt);
var collision = {
active: true,
timestamp: particle.startEvt.timestamp + prt,
effect: bounceOffParticle,
particles: [particle, another]
};
particle.collision = collision;
if (collision.timestamp < another.collision.timestamp) {
another.addEndEvt(prt);
another.collision.active = false;
another.collision = collision
}
return collision;
}
}
function createParticle(that) {
that.move = function (timestamp) {
if (Math.abs(that.startEvt.timestamp - that.endEvt.timestamp) < 10) {
that.x = that.endEvt.x;
that.y = that.endEvt.y;
} else {
var progress =
(timestamp - that.startEvt.timestamp) /
(that.endEvt.timestamp - that.startEvt.timestamp);
that.x = that.startEvt.x + (that.endEvt.x - that.startEvt.x) * progress;
that.y = that.startEvt.y + (that.endEvt.y - that.startEvt.y) * progress;
}
}
that.addEndEvt = function (dt) {
var start = that.startEvt;
that.endEvt = {
x: start.x + that.vx*dt,
y: start.y + that.vy*dt,
timestamp: start.timestamp + dt
}
}
that.m = Math.PI * that.r * that.r;
return that;
}
return {
createParticle: function (state, timestamp) {
var particle = createParticle(state);
events.insert(predictNextCollision(particle, timestamp));
var id = nextParticleId++;
particles[id] = particle;
return id;
},
deleteParticle: function (id, timestamp) {
var particle = particles[id];
delete particles[id];
particle.collision.active = false;
for (var i = 0; i < particle.collision.particles.length; i++) {
var another = particle.collision.particles[i];
if (another !== particle)
events.insert(predictNextCollision(another, timestamp));
}
},
update: function (timestamp) {
var event = events.popIfHappened(timestamp);
while (event) {
if (event.active) {
moveParticles(event.timestamp);
event.effect();
for (var i = 0; i < event.particles.length; i++) {
var particle = event.particles[i];
events.insert(predictNextCollision(particle, timestamp));
}
}
event = events.popIfHappened(timestamp);
}
moveParticles(timestamp);
},
resize: function (newHeight, newWidth, timestamp) {
height = newHeight;
width = newWidth;
for (var id in particles) {
if (particles.hasOwnProperty(id)) {
particles[id].collision.active = false;
events.insert(predictNextCollision(particles[id], timestamp));
}
}
},
eachParticle: function (func) {
for (var id in particles) {
if (particles.hasOwnProperty(id)) {
func(id, particles[id]);
}
}
}
};
}