@journey-to-code/physics 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +157 -2
- package/dist/examples.d.ts +26 -0
- package/dist/examples.js +112 -0
- package/dist/examples.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/sim.d.ts +34 -0
- package/dist/sim.js +741 -0
- package/dist/sim.js.map +1 -0
- package/dist/trig.d.ts +8 -0
- package/dist/trig.js +52 -0
- package/dist/trig.js.map +1 -0
- package/dist/types.d.ts +219 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/dist/validate.d.ts +12 -0
- package/dist/validate.js +226 -0
- package/dist/validate.js.map +1 -0
- package/package.json +37 -4
package/dist/sim.js
ADDED
|
@@ -0,0 +1,741 @@
|
|
|
1
|
+
// The simulation. One shot is run step by step until everything is at rest, and the same run has
|
|
2
|
+
// to come out bit for bit the same on the server and in every browser, because the server stores
|
|
3
|
+
// only the resting table and each browser animates the shot for itself.
|
|
4
|
+
//
|
|
5
|
+
// What makes that hold:
|
|
6
|
+
// - Numbers are ordinary doubles, but the only operations on them are + - * /, comparisons and
|
|
7
|
+
// Math.sqrt, which IEEE-754 and the language pin down exactly. No sin, cos, atan2, pow, exp or
|
|
8
|
+
// hypot anywhere (see trig.ts for how an angle becomes a direction), no `**`.
|
|
9
|
+
// - The step is fixed and belongs to the world (`tickRate`), never to a clock or a frame rate.
|
|
10
|
+
// - Everything is visited in the order the world lists it, and ties go to the first one found.
|
|
11
|
+
// - At rest, positions are rounded to whole numbers, so the next shot starts from the same
|
|
12
|
+
// integers everywhere and the stored table is small and exact.
|
|
13
|
+
//
|
|
14
|
+
// How a step works. First the ground acts on each ball: slopes push, friction slows. Then the
|
|
15
|
+
// balls move for one step in straight lines, and rather than moving them and looking for overlaps
|
|
16
|
+
// afterwards, the step finds the earliest moment anything touches anything (a quadratic at most),
|
|
17
|
+
// moves everything to that moment, bounces the pair, and repeats until the step is used up.
|
|
18
|
+
// Nothing ever moves further than its next contact, so nothing passes through a wall or another
|
|
19
|
+
// ball however fast it goes.
|
|
20
|
+
import { direction } from "./trig.js";
|
|
21
|
+
import { MAX_POWER, PhysicsError, validateShot, validateWorld } from "./validate.js";
|
|
22
|
+
/**
|
|
23
|
+
* Bump this whenever a change could alter where anything comes to rest. A room restored by
|
|
24
|
+
* replaying its shots only lands on the same table if the physics is the same physics.
|
|
25
|
+
*/
|
|
26
|
+
export const PHYSICS_VERSION = 2;
|
|
27
|
+
// Two things closing on each other slower than this (units per step) are treated as not closing.
|
|
28
|
+
// A bounce leaves rounding dust in the velocity, and without the margin the same contact would be
|
|
29
|
+
// found again and again at the same instant.
|
|
30
|
+
const EPS = 1e-9;
|
|
31
|
+
// How many steps in a row everything has to be still before the shot is over. More than one, so
|
|
32
|
+
// a ball caught at the top of its roll up a slope isn't taken for one that has stopped.
|
|
33
|
+
const REST_TICKS = 4;
|
|
34
|
+
function inShape(s, x, y) {
|
|
35
|
+
if ("points" in s) {
|
|
36
|
+
// Even-odd rule: count the edges a ray to the right crosses.
|
|
37
|
+
let inside = false;
|
|
38
|
+
const pts = s.points;
|
|
39
|
+
for (let i = 0, j = pts.length - 1; i < pts.length; j = i++) {
|
|
40
|
+
const [xi, yi] = pts[i];
|
|
41
|
+
const [xj, yj] = pts[j];
|
|
42
|
+
if (yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi)
|
|
43
|
+
inside = !inside;
|
|
44
|
+
}
|
|
45
|
+
return inside;
|
|
46
|
+
}
|
|
47
|
+
const dx = x - s.x;
|
|
48
|
+
const dy = y - s.y;
|
|
49
|
+
return dx * dx + dy * dy < s.r * s.r;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Sets a shot going. The world is checked first and never changed. Without a shot, the bodies
|
|
53
|
+
* move with whatever `vx`, `vy` the world gives them.
|
|
54
|
+
*/
|
|
55
|
+
export function start(world, shot) {
|
|
56
|
+
const bad = validateWorld(world);
|
|
57
|
+
if (shot !== undefined && bad.length === 0)
|
|
58
|
+
bad.push(...validateShot(world, shot));
|
|
59
|
+
if (bad.length)
|
|
60
|
+
throw new PhysicsError(bad.join(". "));
|
|
61
|
+
const rate = world.tickRate;
|
|
62
|
+
const perTick = (speed) => speed / rate;
|
|
63
|
+
const perTick2 = (accel) => accel / (rate * rate);
|
|
64
|
+
const discs = world.bodies.map((b) => ({
|
|
65
|
+
id: b.id,
|
|
66
|
+
x: b.x,
|
|
67
|
+
y: b.y,
|
|
68
|
+
vx: b.fixed ? 0 : perTick(b.vx ?? 0),
|
|
69
|
+
vy: b.fixed ? 0 : perTick(b.vy ?? 0),
|
|
70
|
+
r: b.r,
|
|
71
|
+
im: b.fixed ? 0 : 1 / (b.mass ?? 1),
|
|
72
|
+
fixed: b.fixed === true,
|
|
73
|
+
rest: b.restitution ?? world.restitution,
|
|
74
|
+
kick: perTick(b.kick ?? 0),
|
|
75
|
+
live: b.pocketed === undefined && b.out === undefined,
|
|
76
|
+
via: -1,
|
|
77
|
+
away: 0,
|
|
78
|
+
pocketed: b.pocketed,
|
|
79
|
+
out: b.out,
|
|
80
|
+
drive: 0,
|
|
81
|
+
held: true,
|
|
82
|
+
contact: false,
|
|
83
|
+
}));
|
|
84
|
+
const segs = [];
|
|
85
|
+
const corners = [];
|
|
86
|
+
world.walls.forEach((wall, wi) => {
|
|
87
|
+
const rest = wall.restitution ?? world.wallRestitution;
|
|
88
|
+
const pts = wall.points;
|
|
89
|
+
const oneWay = wall.oneWay === true;
|
|
90
|
+
const n = wall.closed ? pts.length : pts.length - 1;
|
|
91
|
+
const base = segs.length;
|
|
92
|
+
pts.forEach(([x, y], k) => {
|
|
93
|
+
const before = k > 0 ? base + k - 1 : wall.closed ? base + n - 1 : -1;
|
|
94
|
+
corners.push({ wall: wi, x, y, rest, oneWay, before, after: k < n ? base + k : -1 });
|
|
95
|
+
});
|
|
96
|
+
for (let k = 0; k < n; k++) {
|
|
97
|
+
const [ax, ay] = pts[k];
|
|
98
|
+
const [bx, by] = pts[(k + 1) % pts.length];
|
|
99
|
+
const len = Math.sqrt((bx - ax) * (bx - ax) + (by - ay) * (by - ay));
|
|
100
|
+
const ex = (bx - ax) / len;
|
|
101
|
+
const ey = (by - ay) / len;
|
|
102
|
+
segs.push({ wall: wi, ax, ay, ex, ey, len, nx: 0 - ey, ny: ex, rest, oneWay });
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
const holes = world.pockets.map((p) => ({
|
|
106
|
+
id: p.id,
|
|
107
|
+
x: p.x,
|
|
108
|
+
y: p.y,
|
|
109
|
+
r2: p.r * p.r,
|
|
110
|
+
max2: p.maxSpeed === undefined ? Infinity : perTick(p.maxSpeed) * perTick(p.maxSpeed),
|
|
111
|
+
}));
|
|
112
|
+
const mouths = (world.tunnels ?? []).map((t) => {
|
|
113
|
+
const [dx, dy] = direction(t.angle);
|
|
114
|
+
return {
|
|
115
|
+
id: t.id,
|
|
116
|
+
x: t.x,
|
|
117
|
+
y: t.y,
|
|
118
|
+
r2: t.r * t.r,
|
|
119
|
+
max2: t.maxSpeed === undefined ? Infinity : perTick(t.maxSpeed) * perTick(t.maxSpeed),
|
|
120
|
+
tx: t.to[0],
|
|
121
|
+
ty: t.to[1],
|
|
122
|
+
vx: dx * perTick(t.speed),
|
|
123
|
+
vy: dy * perTick(t.speed),
|
|
124
|
+
ticks: t.ticks ?? 0,
|
|
125
|
+
};
|
|
126
|
+
});
|
|
127
|
+
const grounds = world.regions.map((g) => ({
|
|
128
|
+
id: g.id,
|
|
129
|
+
shape: g.shape,
|
|
130
|
+
friction: g.friction === undefined ? -1 : perTick2(g.friction),
|
|
131
|
+
ax: g.slope ? perTick2(g.slope[0]) : 0,
|
|
132
|
+
ay: g.slope ? perTick2(g.slope[1]) : 0,
|
|
133
|
+
acts: g.friction !== undefined || g.slope !== undefined,
|
|
134
|
+
out: g.out === true,
|
|
135
|
+
}));
|
|
136
|
+
let shooter = -1;
|
|
137
|
+
if (shot) {
|
|
138
|
+
shooter = discs.findIndex((d) => d.id === shot.body);
|
|
139
|
+
const d = discs[shooter];
|
|
140
|
+
const [dx, dy] = direction(shot.angle);
|
|
141
|
+
const speed = perTick((shot.power * world.maxShotSpeed) / MAX_POWER);
|
|
142
|
+
d.vx = dx * speed;
|
|
143
|
+
d.vy = dy * speed;
|
|
144
|
+
}
|
|
145
|
+
const n = discs.length;
|
|
146
|
+
const restSpeed = perTick(world.restSpeed);
|
|
147
|
+
const state = {
|
|
148
|
+
world,
|
|
149
|
+
tick: 0,
|
|
150
|
+
done: false,
|
|
151
|
+
capped: false,
|
|
152
|
+
still: 0,
|
|
153
|
+
discs,
|
|
154
|
+
segs,
|
|
155
|
+
corners,
|
|
156
|
+
holes,
|
|
157
|
+
mouths,
|
|
158
|
+
grounds,
|
|
159
|
+
wallIds: world.walls.map((w) => w.id),
|
|
160
|
+
friction: perTick2(world.friction),
|
|
161
|
+
rest2: restSpeed * restSpeed,
|
|
162
|
+
restSpeed,
|
|
163
|
+
restitution: world.restitution,
|
|
164
|
+
lastHit: new Int32Array(n * n).fill(-9),
|
|
165
|
+
lastWall: new Int32Array(n * world.walls.length).fill(-9),
|
|
166
|
+
lastSkip: new Int32Array(n * holes.length).fill(-9),
|
|
167
|
+
over: new Uint8Array(n * holes.length),
|
|
168
|
+
overMouth: new Uint8Array(n * mouths.length),
|
|
169
|
+
shooter,
|
|
170
|
+
firstContact: null,
|
|
171
|
+
events: [],
|
|
172
|
+
};
|
|
173
|
+
return state;
|
|
174
|
+
}
|
|
175
|
+
// Marks a pair as touching this step, and says whether that is news: true unless they were
|
|
176
|
+
// already touching in this step or the one before.
|
|
177
|
+
function fresh(last, at, tick) {
|
|
178
|
+
const before = last[at];
|
|
179
|
+
last[at] = tick;
|
|
180
|
+
return before < tick - 1;
|
|
181
|
+
}
|
|
182
|
+
function drop(s, d, h, ev) {
|
|
183
|
+
d.live = false;
|
|
184
|
+
d.pocketed = h.id;
|
|
185
|
+
d.x = h.x;
|
|
186
|
+
d.y = h.y;
|
|
187
|
+
d.vx = 0;
|
|
188
|
+
d.vy = 0;
|
|
189
|
+
ev.push({ type: "pocket", tick: s.tick, body: d.id, pocket: h.id });
|
|
190
|
+
}
|
|
191
|
+
// Into a tunnel: the ball is off the table, waiting at the mouth, until its steps inside are up.
|
|
192
|
+
function enter(s, d, mouth, ev) {
|
|
193
|
+
const m = s.mouths[mouth];
|
|
194
|
+
d.live = false;
|
|
195
|
+
d.via = mouth;
|
|
196
|
+
d.away = s.tick + m.ticks;
|
|
197
|
+
d.x = m.x;
|
|
198
|
+
d.y = m.y;
|
|
199
|
+
d.vx = 0;
|
|
200
|
+
d.vy = 0;
|
|
201
|
+
ev.push({ type: "tunnel", tick: s.tick, body: d.id, tunnel: m.id });
|
|
202
|
+
}
|
|
203
|
+
// Out of the other end, at the tunnel's own speed and heading. Anything standing in the way is
|
|
204
|
+
// met like any other contact, on the step that follows.
|
|
205
|
+
function emerge(s, d, ev) {
|
|
206
|
+
const m = s.mouths[d.via];
|
|
207
|
+
d.live = true;
|
|
208
|
+
d.via = -1;
|
|
209
|
+
d.x = m.tx;
|
|
210
|
+
d.y = m.ty;
|
|
211
|
+
d.vx = m.vx;
|
|
212
|
+
d.vy = m.vy;
|
|
213
|
+
ev.push({ type: "emerge", tick: s.tick, body: d.id, tunnel: m.id });
|
|
214
|
+
}
|
|
215
|
+
// A ball is out when its centre is past the edge of the world or on ground marked out. It stays
|
|
216
|
+
// where it went out, on a whole number, for the game to do what its rules say.
|
|
217
|
+
function checkOut(s, d, ev) {
|
|
218
|
+
const b = s.world.bounds;
|
|
219
|
+
let region;
|
|
220
|
+
if (d.x < b.x || d.x > b.x + b.w || d.y < b.y || d.y > b.y + b.h)
|
|
221
|
+
region = "bounds";
|
|
222
|
+
else
|
|
223
|
+
for (const g of s.grounds)
|
|
224
|
+
if (g.out && inShape(g.shape, d.x, d.y)) {
|
|
225
|
+
region = g.id;
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
if (region === undefined)
|
|
229
|
+
return;
|
|
230
|
+
d.live = false;
|
|
231
|
+
d.out = region;
|
|
232
|
+
d.x = Math.round(d.x);
|
|
233
|
+
d.y = Math.round(d.y);
|
|
234
|
+
d.vx = 0;
|
|
235
|
+
d.vy = 0;
|
|
236
|
+
ev.push({ type: "out", tick: s.tick, body: d.id, region });
|
|
237
|
+
}
|
|
238
|
+
function advance(s) {
|
|
239
|
+
const ev = [];
|
|
240
|
+
const { discs, segs, corners, holes, mouths } = s;
|
|
241
|
+
const n = discs.length;
|
|
242
|
+
const nh = holes.length;
|
|
243
|
+
const nm = mouths.length;
|
|
244
|
+
const rate = s.world.tickRate;
|
|
245
|
+
s.tick++;
|
|
246
|
+
// Anything whose time in a tunnel is up comes out before the ground acts, so its first step
|
|
247
|
+
// on the table is a step like any other.
|
|
248
|
+
if (nm)
|
|
249
|
+
for (const d of discs)
|
|
250
|
+
if (d.via >= 0 && s.tick > d.away)
|
|
251
|
+
emerge(s, d, ev);
|
|
252
|
+
// The ground. A slow ball on ground that can hold it (friction at least as strong as the
|
|
253
|
+
// slope's pull) simply stops: that is the rest threshold. Anything else is pushed by the slope
|
|
254
|
+
// and then slowed by friction, which takes a fixed amount off the speed each step and so
|
|
255
|
+
// brings a ball to exactly zero rather than creeping towards it for ever.
|
|
256
|
+
for (const d of discs) {
|
|
257
|
+
if (!d.live || d.fixed)
|
|
258
|
+
continue;
|
|
259
|
+
d.contact = false;
|
|
260
|
+
let f = s.friction;
|
|
261
|
+
let ax = 0;
|
|
262
|
+
let ay = 0;
|
|
263
|
+
for (const g of s.grounds) {
|
|
264
|
+
if (!g.acts || !inShape(g.shape, d.x, d.y))
|
|
265
|
+
continue;
|
|
266
|
+
if (g.friction >= 0)
|
|
267
|
+
f = g.friction;
|
|
268
|
+
ax += g.ax;
|
|
269
|
+
ay += g.ay;
|
|
270
|
+
}
|
|
271
|
+
const pull = ax === 0 && ay === 0 ? 0 : Math.sqrt(ax * ax + ay * ay);
|
|
272
|
+
d.drive = pull;
|
|
273
|
+
d.held = pull <= f;
|
|
274
|
+
if (d.held && d.vx * d.vx + d.vy * d.vy < s.rest2) {
|
|
275
|
+
d.vx = 0;
|
|
276
|
+
d.vy = 0;
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
d.vx += ax;
|
|
280
|
+
d.vy += ay;
|
|
281
|
+
const speed = Math.sqrt(d.vx * d.vx + d.vy * d.vy);
|
|
282
|
+
if (speed <= f) {
|
|
283
|
+
d.vx = 0;
|
|
284
|
+
d.vy = 0;
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
const k = (speed - f) / speed;
|
|
288
|
+
d.vx *= k;
|
|
289
|
+
d.vy *= k;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
// A ball already over a hole drops as soon as it is slow enough.
|
|
294
|
+
s.over.fill(0);
|
|
295
|
+
if (nm)
|
|
296
|
+
s.overMouth.fill(0);
|
|
297
|
+
for (let i = 0; i < n; i++) {
|
|
298
|
+
const d = discs[i];
|
|
299
|
+
if (!d.live || d.fixed)
|
|
300
|
+
continue;
|
|
301
|
+
for (let p = 0; p < nh; p++) {
|
|
302
|
+
const h = holes[p];
|
|
303
|
+
const dx = d.x - h.x;
|
|
304
|
+
const dy = d.y - h.y;
|
|
305
|
+
if (dx * dx + dy * dy >= h.r2)
|
|
306
|
+
continue;
|
|
307
|
+
if (d.vx * d.vx + d.vy * d.vy <= h.max2) {
|
|
308
|
+
drop(s, d, h, ev);
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
s.over[i * nh + p] = 1;
|
|
312
|
+
}
|
|
313
|
+
// The same for a tunnel's mouth.
|
|
314
|
+
for (let p = 0; p < nm && d.live; p++) {
|
|
315
|
+
const m = mouths[p];
|
|
316
|
+
const dx = d.x - m.x;
|
|
317
|
+
const dy = d.y - m.y;
|
|
318
|
+
if (dx * dx + dy * dy >= m.r2)
|
|
319
|
+
continue;
|
|
320
|
+
if (d.vx * d.vx + d.vy * d.vy <= m.max2)
|
|
321
|
+
enter(s, d, p, ev);
|
|
322
|
+
else
|
|
323
|
+
s.overMouth[i * nm + p] = 1;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
// The move. Kinds of contact: 1 two discs, 2 a wall piece, 3 a wall corner, 4 the rim of a
|
|
327
|
+
// hole, 5 the rim of a tunnel's mouth.
|
|
328
|
+
let remaining = 1;
|
|
329
|
+
// If one step needs more contacts than this (balls wedged in a corner, trading the same push
|
|
330
|
+
// back and forth), everything waits where it is for the rest of the step. Nothing is lost but a
|
|
331
|
+
// sliver of time, and friction ends the argument within a few steps.
|
|
332
|
+
const cap = 16 + 8 * n;
|
|
333
|
+
for (let count = 0;;) {
|
|
334
|
+
let bt = remaining;
|
|
335
|
+
let kind = 0;
|
|
336
|
+
let bi = -1;
|
|
337
|
+
let bj = -1;
|
|
338
|
+
let side = 1;
|
|
339
|
+
for (let i = 0; i < n; i++) {
|
|
340
|
+
const a = discs[i];
|
|
341
|
+
if (!a.live)
|
|
342
|
+
continue;
|
|
343
|
+
const moving = a.vx !== 0 || a.vy !== 0;
|
|
344
|
+
for (let j = i + 1; j < n; j++) {
|
|
345
|
+
const b = discs[j];
|
|
346
|
+
if (!b.live || (!moving && b.vx === 0 && b.vy === 0))
|
|
347
|
+
continue;
|
|
348
|
+
const dx = b.x - a.x;
|
|
349
|
+
const dy = b.y - a.y;
|
|
350
|
+
const wx = b.vx - a.vx;
|
|
351
|
+
const wy = b.vy - a.vy;
|
|
352
|
+
const reach = a.r + b.r;
|
|
353
|
+
// Too far apart to meet within one step, whichever way they are heading: skip the sums.
|
|
354
|
+
if (Math.abs(dx) > reach + Math.abs(wx) || Math.abs(dy) > reach + Math.abs(wy))
|
|
355
|
+
continue;
|
|
356
|
+
const closing = dx * wx + dy * wy;
|
|
357
|
+
if (closing > -EPS * reach)
|
|
358
|
+
continue;
|
|
359
|
+
const gap = dx * dx + dy * dy - reach * reach;
|
|
360
|
+
let t = 0;
|
|
361
|
+
if (gap > 0) {
|
|
362
|
+
const ww = wx * wx + wy * wy;
|
|
363
|
+
const disc = closing * closing - ww * gap;
|
|
364
|
+
if (disc < 0)
|
|
365
|
+
continue;
|
|
366
|
+
t = (-closing - Math.sqrt(disc)) / ww;
|
|
367
|
+
if (t < 0)
|
|
368
|
+
t = 0;
|
|
369
|
+
}
|
|
370
|
+
if (t < bt) {
|
|
371
|
+
bt = t;
|
|
372
|
+
kind = 1;
|
|
373
|
+
bi = i;
|
|
374
|
+
bj = j;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
if (!moving)
|
|
378
|
+
continue;
|
|
379
|
+
const avx = Math.abs(a.vx);
|
|
380
|
+
const avy = Math.abs(a.vy);
|
|
381
|
+
for (let k = 0; k < segs.length; k++) {
|
|
382
|
+
const g = segs[k];
|
|
383
|
+
const px = a.x - g.ax;
|
|
384
|
+
const py = a.y - g.ay;
|
|
385
|
+
let dist = px * g.nx + py * g.ny;
|
|
386
|
+
let vn = a.vx * g.nx + a.vy * g.ny;
|
|
387
|
+
let sgn = 1;
|
|
388
|
+
// Work on whichever side of the wall the ball is. Dead on the line, call it the side it
|
|
389
|
+
// is heading away from, so that it is turned back the way it came.
|
|
390
|
+
if (dist < 0 || (dist === 0 && vn > 0)) {
|
|
391
|
+
dist = -dist;
|
|
392
|
+
vn = -vn;
|
|
393
|
+
sgn = -1;
|
|
394
|
+
}
|
|
395
|
+
// A ledge lets through whatever comes at it from its open side.
|
|
396
|
+
if (g.oneWay && sgn < 0)
|
|
397
|
+
continue;
|
|
398
|
+
if (vn > -EPS)
|
|
399
|
+
continue;
|
|
400
|
+
let t = 0;
|
|
401
|
+
if (dist > a.r) {
|
|
402
|
+
t = (dist - a.r) / -vn;
|
|
403
|
+
if (t >= bt)
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
// Only the straight part counts here; past either end it is the corner's business.
|
|
407
|
+
const along = (px + a.vx * t) * g.ex + (py + a.vy * t) * g.ey;
|
|
408
|
+
if (along < 0 || along > g.len)
|
|
409
|
+
continue;
|
|
410
|
+
if (t < bt) {
|
|
411
|
+
bt = t;
|
|
412
|
+
kind = 2;
|
|
413
|
+
bi = i;
|
|
414
|
+
bj = k;
|
|
415
|
+
side = sgn;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
for (let k = 0; k < corners.length; k++) {
|
|
419
|
+
const c = corners[k];
|
|
420
|
+
const dx = a.x - c.x;
|
|
421
|
+
const dy = a.y - c.y;
|
|
422
|
+
if (Math.abs(dx) > a.r + avx || Math.abs(dy) > a.r + avy)
|
|
423
|
+
continue;
|
|
424
|
+
const closing = dx * a.vx + dy * a.vy;
|
|
425
|
+
if (closing > -EPS * a.r)
|
|
426
|
+
continue;
|
|
427
|
+
// A point along a ledge is no more solid than the ledge: a ball on the open side of every
|
|
428
|
+
// piece that meets there rolls over it.
|
|
429
|
+
if (c.oneWay) {
|
|
430
|
+
const g1 = c.before >= 0 ? segs[c.before] : null;
|
|
431
|
+
const g2 = c.after >= 0 ? segs[c.after] : null;
|
|
432
|
+
if ((!g1 || (a.x - g1.ax) * g1.nx + (a.y - g1.ay) * g1.ny < 0) && (!g2 || (a.x - g2.ax) * g2.nx + (a.y - g2.ay) * g2.ny < 0))
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
const gap = dx * dx + dy * dy - a.r * a.r;
|
|
436
|
+
let t = 0;
|
|
437
|
+
if (gap > 0) {
|
|
438
|
+
const vv = a.vx * a.vx + a.vy * a.vy;
|
|
439
|
+
const disc = closing * closing - vv * gap;
|
|
440
|
+
if (disc < 0)
|
|
441
|
+
continue;
|
|
442
|
+
t = (-closing - Math.sqrt(disc)) / vv;
|
|
443
|
+
if (t < 0)
|
|
444
|
+
t = 0;
|
|
445
|
+
}
|
|
446
|
+
if (t < bt) {
|
|
447
|
+
bt = t;
|
|
448
|
+
kind = 3;
|
|
449
|
+
bi = i;
|
|
450
|
+
bj = k;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
for (let p = 0; p < nh; p++) {
|
|
454
|
+
if (s.over[i * nh + p])
|
|
455
|
+
continue;
|
|
456
|
+
const h = holes[p];
|
|
457
|
+
const dx = a.x - h.x;
|
|
458
|
+
const dy = a.y - h.y;
|
|
459
|
+
const closing = dx * a.vx + dy * a.vy;
|
|
460
|
+
if (closing >= 0)
|
|
461
|
+
continue;
|
|
462
|
+
const gap = dx * dx + dy * dy - h.r2;
|
|
463
|
+
let t = 0;
|
|
464
|
+
if (gap > 0) {
|
|
465
|
+
const vv = a.vx * a.vx + a.vy * a.vy;
|
|
466
|
+
const disc = closing * closing - vv * gap;
|
|
467
|
+
if (disc < 0)
|
|
468
|
+
continue;
|
|
469
|
+
t = (-closing - Math.sqrt(disc)) / vv;
|
|
470
|
+
if (t < 0)
|
|
471
|
+
t = 0;
|
|
472
|
+
}
|
|
473
|
+
if (t < bt) {
|
|
474
|
+
bt = t;
|
|
475
|
+
kind = 4;
|
|
476
|
+
bi = i;
|
|
477
|
+
bj = p;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
for (let p = 0; p < nm; p++) {
|
|
481
|
+
if (s.overMouth[i * nm + p])
|
|
482
|
+
continue;
|
|
483
|
+
const m = mouths[p];
|
|
484
|
+
const dx = a.x - m.x;
|
|
485
|
+
const dy = a.y - m.y;
|
|
486
|
+
const closing = dx * a.vx + dy * a.vy;
|
|
487
|
+
if (closing >= 0)
|
|
488
|
+
continue;
|
|
489
|
+
const gap = dx * dx + dy * dy - m.r2;
|
|
490
|
+
let t = 0;
|
|
491
|
+
if (gap > 0) {
|
|
492
|
+
const vv = a.vx * a.vx + a.vy * a.vy;
|
|
493
|
+
const disc = closing * closing - vv * gap;
|
|
494
|
+
if (disc < 0)
|
|
495
|
+
continue;
|
|
496
|
+
t = (-closing - Math.sqrt(disc)) / vv;
|
|
497
|
+
if (t < 0)
|
|
498
|
+
t = 0;
|
|
499
|
+
}
|
|
500
|
+
if (t < bt) {
|
|
501
|
+
bt = t;
|
|
502
|
+
kind = 5;
|
|
503
|
+
bi = i;
|
|
504
|
+
bj = p;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
const dt = kind === 0 ? remaining : bt;
|
|
509
|
+
if (dt > 0) {
|
|
510
|
+
for (const d of discs) {
|
|
511
|
+
if (!d.live)
|
|
512
|
+
continue;
|
|
513
|
+
d.x += d.vx * dt;
|
|
514
|
+
d.y += d.vy * dt;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
if (kind === 0)
|
|
518
|
+
break;
|
|
519
|
+
remaining -= bt;
|
|
520
|
+
const a = discs[bi];
|
|
521
|
+
if (kind === 1) {
|
|
522
|
+
const b = discs[bj];
|
|
523
|
+
let nx = b.x - a.x;
|
|
524
|
+
let ny = b.y - a.y;
|
|
525
|
+
const dist = Math.sqrt(nx * nx + ny * ny);
|
|
526
|
+
if (dist > 0) {
|
|
527
|
+
nx /= dist;
|
|
528
|
+
ny /= dist;
|
|
529
|
+
}
|
|
530
|
+
else {
|
|
531
|
+
// Two centres on the same spot have no line between them; any fixed one will do.
|
|
532
|
+
nx = 1;
|
|
533
|
+
ny = 0;
|
|
534
|
+
}
|
|
535
|
+
const vn = (b.vx - a.vx) * nx + (b.vy - a.vy) * ny;
|
|
536
|
+
if (vn < 0) {
|
|
537
|
+
// A touch too gentle to bounce is a lean, not a hit. The margin grows with the slope
|
|
538
|
+
// pressing the two together, or a ball lying against another downhill would chatter.
|
|
539
|
+
const e = -vn < s.restSpeed + 2 * (a.drive + b.drive) ? 0 : a.rest < b.rest ? a.rest : b.rest;
|
|
540
|
+
const push = (-(1 + e) * vn) / (a.im + b.im);
|
|
541
|
+
a.vx -= push * a.im * nx;
|
|
542
|
+
a.vy -= push * a.im * ny;
|
|
543
|
+
b.vx += push * b.im * nx;
|
|
544
|
+
b.vy += push * b.im * ny;
|
|
545
|
+
// A bumper sends things away at its own speed if they would leave any slower.
|
|
546
|
+
const kick = a.kick > b.kick ? a.kick : b.kick;
|
|
547
|
+
if (kick > 0 && a.im + b.im > 0) {
|
|
548
|
+
const parting = (b.vx - a.vx) * nx + (b.vy - a.vy) * ny;
|
|
549
|
+
if (parting < kick) {
|
|
550
|
+
const more = (kick - parting) / (a.im + b.im);
|
|
551
|
+
a.vx -= more * a.im * nx;
|
|
552
|
+
a.vy -= more * a.im * ny;
|
|
553
|
+
b.vx += more * b.im * nx;
|
|
554
|
+
b.vy += more * b.im * ny;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
a.contact = true;
|
|
559
|
+
b.contact = true;
|
|
560
|
+
// A pass so close that the two only brush, with nothing pressing them together, is not a hit.
|
|
561
|
+
if (vn < -EPS) {
|
|
562
|
+
if (s.firstContact === null && (bi === s.shooter || bj === s.shooter))
|
|
563
|
+
s.firstContact = bi === s.shooter ? b.id : a.id;
|
|
564
|
+
if (fresh(s.lastHit, bi * n + bj, s.tick))
|
|
565
|
+
ev.push({ type: "hit", tick: s.tick, a: a.id, b: b.id, speed: -vn * rate });
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
else if (kind === 2 || kind === 3) {
|
|
569
|
+
let nx;
|
|
570
|
+
let ny;
|
|
571
|
+
let rest;
|
|
572
|
+
let wall;
|
|
573
|
+
if (kind === 2) {
|
|
574
|
+
const g = segs[bj];
|
|
575
|
+
nx = g.nx * side;
|
|
576
|
+
ny = g.ny * side;
|
|
577
|
+
rest = g.rest;
|
|
578
|
+
wall = g.wall;
|
|
579
|
+
}
|
|
580
|
+
else {
|
|
581
|
+
const c = corners[bj];
|
|
582
|
+
nx = a.x - c.x;
|
|
583
|
+
ny = a.y - c.y;
|
|
584
|
+
const dist = Math.sqrt(nx * nx + ny * ny);
|
|
585
|
+
const speed = Math.sqrt(a.vx * a.vx + a.vy * a.vy);
|
|
586
|
+
if (dist > 0) {
|
|
587
|
+
nx /= dist;
|
|
588
|
+
ny /= dist;
|
|
589
|
+
}
|
|
590
|
+
else {
|
|
591
|
+
nx = -a.vx / speed;
|
|
592
|
+
ny = -a.vy / speed;
|
|
593
|
+
}
|
|
594
|
+
rest = c.rest;
|
|
595
|
+
wall = c.wall;
|
|
596
|
+
}
|
|
597
|
+
const vn = a.vx * nx + a.vy * ny;
|
|
598
|
+
if (vn < 0) {
|
|
599
|
+
const e = -vn < s.restSpeed + 2 * a.drive ? 0 : rest;
|
|
600
|
+
a.vx -= (1 + e) * vn * nx;
|
|
601
|
+
a.vy -= (1 + e) * vn * ny;
|
|
602
|
+
}
|
|
603
|
+
a.contact = true;
|
|
604
|
+
if (vn < -EPS && fresh(s.lastWall, bi * s.wallIds.length + wall, s.tick))
|
|
605
|
+
ev.push({ type: "wall", tick: s.tick, body: a.id, wall: s.wallIds[wall], speed: -vn * rate });
|
|
606
|
+
}
|
|
607
|
+
else if (kind === 5) {
|
|
608
|
+
if (a.vx * a.vx + a.vy * a.vy <= mouths[bj].max2)
|
|
609
|
+
enter(s, a, bj, ev);
|
|
610
|
+
else
|
|
611
|
+
s.overMouth[bi * nm + bj] = 1;
|
|
612
|
+
}
|
|
613
|
+
else {
|
|
614
|
+
const h = holes[bj];
|
|
615
|
+
if (a.vx * a.vx + a.vy * a.vy <= h.max2)
|
|
616
|
+
drop(s, a, h, ev);
|
|
617
|
+
else {
|
|
618
|
+
s.over[bi * nh + bj] = 1;
|
|
619
|
+
if (fresh(s.lastSkip, bi * nh + bj, s.tick))
|
|
620
|
+
ev.push({ type: "skip", tick: s.tick, body: a.id, pocket: h.id });
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
if (++count >= cap)
|
|
624
|
+
break;
|
|
625
|
+
}
|
|
626
|
+
for (const d of discs)
|
|
627
|
+
if (d.live && !d.fixed)
|
|
628
|
+
checkOut(s, d, ev);
|
|
629
|
+
// Rest. A ball counts as still when it has no speed at all, or is slow and either on ground
|
|
630
|
+
// that holds it or leaning on something.
|
|
631
|
+
let still = true;
|
|
632
|
+
for (const d of discs) {
|
|
633
|
+
// Nothing is over while a ball is still on its way through a tunnel.
|
|
634
|
+
if (d.via >= 0) {
|
|
635
|
+
still = false;
|
|
636
|
+
break;
|
|
637
|
+
}
|
|
638
|
+
if (!d.live || d.fixed)
|
|
639
|
+
continue;
|
|
640
|
+
if (d.vx === 0 && d.vy === 0)
|
|
641
|
+
continue;
|
|
642
|
+
if (d.vx * d.vx + d.vy * d.vy < s.rest2 && (d.held || d.contact))
|
|
643
|
+
continue;
|
|
644
|
+
still = false;
|
|
645
|
+
break;
|
|
646
|
+
}
|
|
647
|
+
s.still = still ? s.still + 1 : 0;
|
|
648
|
+
if (s.still >= REST_TICKS)
|
|
649
|
+
settle(s, ev);
|
|
650
|
+
else if (s.tick >= s.world.maxTicks) {
|
|
651
|
+
s.capped = true;
|
|
652
|
+
ev.push({ type: "capped", tick: s.tick });
|
|
653
|
+
settle(s, ev);
|
|
654
|
+
}
|
|
655
|
+
for (const e of ev)
|
|
656
|
+
s.events.push(e);
|
|
657
|
+
return ev;
|
|
658
|
+
}
|
|
659
|
+
// The end of a shot: everything stops and moves onto whole numbers. Rounding can nudge a ball
|
|
660
|
+
// that stopped on the lip of a hole or the edge of the water over it, so those are looked at once
|
|
661
|
+
// more; otherwise the stored table would hold a ball that drops by itself at the start of the
|
|
662
|
+
// next shot.
|
|
663
|
+
function settle(s, ev) {
|
|
664
|
+
for (const d of s.discs) {
|
|
665
|
+
// Caught inside a tunnel when the steps ran out: it is set down where it would have come out.
|
|
666
|
+
if (d.via >= 0)
|
|
667
|
+
emerge(s, d, ev);
|
|
668
|
+
if (!d.live || d.fixed)
|
|
669
|
+
continue;
|
|
670
|
+
d.vx = 0;
|
|
671
|
+
d.vy = 0;
|
|
672
|
+
d.x = Math.round(d.x);
|
|
673
|
+
d.y = Math.round(d.y);
|
|
674
|
+
for (const h of s.holes) {
|
|
675
|
+
const dx = d.x - h.x;
|
|
676
|
+
const dy = d.y - h.y;
|
|
677
|
+
if (dx * dx + dy * dy < h.r2) {
|
|
678
|
+
drop(s, d, h, ev);
|
|
679
|
+
break;
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
if (d.live)
|
|
683
|
+
checkOut(s, d, ev);
|
|
684
|
+
}
|
|
685
|
+
s.done = true;
|
|
686
|
+
}
|
|
687
|
+
function frame(s, events) {
|
|
688
|
+
const rate = s.world.tickRate;
|
|
689
|
+
return {
|
|
690
|
+
tick: s.tick,
|
|
691
|
+
done: s.done,
|
|
692
|
+
events,
|
|
693
|
+
bodies: s.discs.map((d) => (d.via >= 0 ? { id: d.id, x: d.x, y: d.y, vx: 0, vy: 0, live: false, in: s.mouths[d.via].id } : { id: d.id, x: d.x, y: d.y, vx: d.vx * rate, vy: d.vy * rate, live: d.live })),
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
/** Moves a run on by one step and says where everything is and what happened in that step. */
|
|
697
|
+
export function step(run) {
|
|
698
|
+
const s = run;
|
|
699
|
+
if (s.done)
|
|
700
|
+
throw new PhysicsError("This shot has already come to rest");
|
|
701
|
+
return frame(s, advance(s));
|
|
702
|
+
}
|
|
703
|
+
/** The outcome of a run that has come to rest. */
|
|
704
|
+
export function result(run) {
|
|
705
|
+
const s = run;
|
|
706
|
+
if (!s.done)
|
|
707
|
+
throw new PhysicsError("This shot is still moving");
|
|
708
|
+
const bodies = s.world.bodies.map((b, i) => {
|
|
709
|
+
const d = s.discs[i];
|
|
710
|
+
const { vx: _vx, vy: _vy, ...rest } = b;
|
|
711
|
+
const at = { ...rest, x: d.x, y: d.y };
|
|
712
|
+
if (d.pocketed !== undefined)
|
|
713
|
+
at.pocketed = d.pocketed;
|
|
714
|
+
if (d.out !== undefined)
|
|
715
|
+
at.out = d.out;
|
|
716
|
+
return at;
|
|
717
|
+
});
|
|
718
|
+
return { world: { ...s.world, bodies }, events: s.events.slice(), ticks: s.tick, capped: s.capped, firstContact: s.firstContact };
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* Plays a shot to rest in one call: what a game engine uses to apply a move. Pure: the same world
|
|
722
|
+
* and shot always give the same result, and the world passed in is not changed.
|
|
723
|
+
*/
|
|
724
|
+
export function simulate(world, shot) {
|
|
725
|
+
const run = start(world, shot);
|
|
726
|
+
const s = run;
|
|
727
|
+
while (!s.done)
|
|
728
|
+
advance(s);
|
|
729
|
+
return result(run);
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* The same shot one step at a time: what a canvas uses to draw it. Yields a frame per step and
|
|
733
|
+
* returns the same result `simulate` gives, because it is the same code taking the same steps.
|
|
734
|
+
*/
|
|
735
|
+
export function* frames(world, shot) {
|
|
736
|
+
const run = start(world, shot);
|
|
737
|
+
while (!run.done)
|
|
738
|
+
yield step(run);
|
|
739
|
+
return result(run);
|
|
740
|
+
}
|
|
741
|
+
//# sourceMappingURL=sim.js.map
|