@typeonce/ritmo-math 0.22.0 → 0.23.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/CHANGELOG.md CHANGED
@@ -1,5 +1,52 @@
1
1
  # @typeonce/ritmo-math
2
2
 
3
+ ## 0.23.0
4
+
5
+ ### Patch Changes
6
+
7
+ - 84caa60: `Vec2.moveToward` and `Vec2.clampLength` count a limit below 0 as 0, as `Scalar.moveToward` and `Angle.turnToward` already did. A speed that went negative (a slow effect minus friction) moved the point away from its target, and a negative cap flipped the vector; now `moveToward` returns `current` unchanged and `clampLength` the zero vector. A `NaN` limit counts as 0 too, as in `Scalar.moveToward`.
8
+
9
+ `FlowField.distance` gives a distance wherever `way` gives a direction. From a blocked cell (a body pressed against a wall or into a corner) it was `undefined` even where `way` led it out, so a chaser ranking targets by distance skipped it. It is now the distance out along the steps `way` takes: to the open cells it heads for, plus their distance. It is `undefined` exactly where `way` is, except where the body is already there. Every open cell's distance is unchanged.
10
+
11
+ `FlowField`'s docs say which wall sizes the field can step through: a cell is blocked by its center alone, so a wall `width` by `height` can fall between open cells exactly when `hypot(max(0, cellSize - width), max(0, cellSize - height)) > 2 * clearance`. No step crosses a wall at least `cellSize - clearance * √2` thick both ways, and none crosses any wall when `cellSize <= clearance * √2`; with thinner walls, use smaller cells or a larger clearance. Behaviour is unchanged.
12
+ - 30b6570: `Collide2.moveCircle` no longer passes through walls where they close in on a circle. Pushing off one wall by the skin could leave the circle a hair inside another, and a wall the circle started in was ignored for the rest of the move, so the slide went through it: a circle sliding between a crate and a wall left the room on the next move, and one pushed into an acute corner of segments came out the other side. Now a circle that starts outside every wall ends outside every wall, also in acute corners and gaps narrower than it, and stops there (`blocked`) when nothing is left to slide along. A wall it touches or is inside, while moving into it, is a contact at once; where the skin would push it into another wall, it stays touching instead.
13
+
14
+ `Collide2.pushOut` leaves the deepest wall first, leaves both walls of an acute corner at once (pushing out of one alone pushed into the other), and takes a center inside a box out through the nearest side with room, so a circle spawned in a corner or in a crate against a wall comes out clear of every wall. Only walls with no room between them (a gap narrower than the circle) can leave it overlapping.
15
+
16
+ `FlowField` finds the way to a goal backed into a corner. When the goal's cell and every cell it could step to are blocked (a player pushed into a room corner, or standing against a wall more than one blocked cell deep), the search starts from the nearest open cells the goal can see in a straight line, where every chaser used to get `undefined`. A body in such a blocked cell gets `way` out to the nearest open cell it can see. Every way the field gave before is unchanged.
17
+
18
+ `moveCircle` is also faster: it skips walls outside the bounds of the swept circle before testing them.
19
+ - Updated dependencies [5a16a40]
20
+ - Updated dependencies [148ec5f]
21
+ - Updated dependencies [4df0496]
22
+ - Updated dependencies [4df0496]
23
+ - Updated dependencies [0dd0a84]
24
+ - Updated dependencies [51a0b7f]
25
+ - Updated dependencies [dff932a]
26
+ - Updated dependencies [dfe3432]
27
+ - Updated dependencies [52d6a6f]
28
+ - Updated dependencies [02ca2a4]
29
+ - Updated dependencies [7e416f5]
30
+ - Updated dependencies [40f60e7]
31
+ - Updated dependencies [51efba5]
32
+ - Updated dependencies [5f9fe33]
33
+ - Updated dependencies [1b83e0c]
34
+ - Updated dependencies [ca0544d]
35
+ - Updated dependencies [4a32ad1]
36
+ - Updated dependencies [917001b]
37
+ - Updated dependencies [0f4a5fe]
38
+ - Updated dependencies [4df0496]
39
+ - Updated dependencies [02b8683]
40
+ - Updated dependencies [791517f]
41
+ - Updated dependencies [4fc0728]
42
+ - Updated dependencies [207e611]
43
+ - Updated dependencies [d2970e8]
44
+ - Updated dependencies [c99b968]
45
+ - Updated dependencies [08c0c91]
46
+ - Updated dependencies [78683e7]
47
+ - Updated dependencies [04d2a9d]
48
+ - @typeonce/ritmo@0.23.0
49
+
3
50
  ## 0.22.0
4
51
 
5
52
  ### Patch Changes
@@ -78,7 +78,8 @@ export interface MoveResult {
78
78
  }
79
79
  /**
80
80
  * The distance `pushOut` and `moveCircle` keep from walls after a contact
81
- * (in world units), so the next sweep starts outside the wall.
81
+ * (in world units), so the next sweep starts outside the wall. Where another
82
+ * wall leaves no room for it (a corner, a gap), `moveCircle` stays touching.
82
83
  *
83
84
  * @category Movement
84
85
  */
@@ -151,10 +152,13 @@ export declare const sweepCircleCircle: (origin: Vec2, delta: Vec2, radius: numb
151
152
  */
152
153
  export declare const overlapsCircleCircle: (center: Vec2, radius: number, other: Vec2, otherRadius: number) => boolean;
153
154
  /**
154
- * Pushes a circle out of every wall it overlaps, boxes first and then
155
- * segments, each in order, leaving `skin` between it and each wall. Returns
156
- * the corrected center. A center inside a box leaves through the nearest
157
- * side; a center exactly on a segment leaves along the segment's left normal.
155
+ * Pushes a circle out of every wall it overlaps, the deepest first, leaving
156
+ * `skin` between it and each wall, and returns the corrected center. The two
157
+ * walls of an acute corner are left at once, since leaving one alone pushes
158
+ * into the other. Only walls with no room between them (a gap narrower than
159
+ * the circle) can leave it overlapping. A center inside a box leaves through
160
+ * the nearest side with room; a center exactly on a segment leaves along the
161
+ * segment's left normal.
158
162
  *
159
163
  * @category Movement
160
164
  */
@@ -162,9 +166,12 @@ export declare const pushOut: (center: Vec2, radius: number, walls: Walls, skin?
162
166
  /**
163
167
  * Moves a circle by `delta`, stopping at walls and sliding along them (up to
164
168
  * four contacts per call). The whole path is swept, so no displacement can
165
- * tunnel through a wall. A circle that starts inside a wall is pushed out
166
- * first. A circle meeting a box's corner or a segment's end dead-on goes
167
- * around it rather than stopping against it.
169
+ * tunnel through a wall: a circle that starts outside every wall ends outside
170
+ * every wall, also where walls close in on it (an acute corner, a gap
171
+ * narrower than it), stopping there when nothing is left to slide along. A
172
+ * circle that starts inside a wall is pushed out first. A circle meeting a
173
+ * box's corner or a segment's end dead-on goes around it rather than stopping
174
+ * against it.
168
175
  *
169
176
  * `blocked` says the move touched a wall, not which one, nor whether the
170
177
  * circle stands on one now (a circle resting on a floor and not moving into
@@ -1 +1 @@
1
- {"version":3,"file":"Collide2.d.ts","sourceRoot":"","sources":["../src/Collide2.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;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AM,SAAS,KAAK,SAAQ,MAAM,KAAiB,IAIhG,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,eAAO,MAAM,UAAU,UAAW,IAAI,SAAS,IAAI,UAAU,MAAM,SAAS,KAAK,SAAQ,MAAM,KAAiB,UAuG/G,CAAA"}
package/dist/Collide2.js CHANGED
@@ -3,7 +3,8 @@ const noBoxes = [];
3
3
  const noSegments = [];
4
4
  /**
5
5
  * The distance `pushOut` and `moveCircle` keep from walls after a contact
6
- * (in world units), so the next sweep starts outside the wall.
6
+ * (in world units), so the next sweep starts outside the wall. Where another
7
+ * wall leaves no room for it (a corner, a gap), `moveCircle` stays touching.
7
8
  *
8
9
  * @category Movement
9
10
  */
@@ -240,83 +241,212 @@ export const overlapsCircleCircle = (center, radius, other, otherRadius) => {
240
241
  const dy = center.y - other.y;
241
242
  return dx * dx + dy * dy <= r * r;
242
243
  };
244
+ // How far into a wall still counts as touching it: rounding, not overlap.
245
+ const hair = 1e-9;
246
+ // The way out of a wall that `boxDepth` and `segmentDepth` found, read right
247
+ // after the call: module state, so testing every wall on every step
248
+ // allocates nothing.
249
+ const wayOut = { x: 0, y: 0 };
243
250
  /**
244
- * Pushes a circle out of every wall it overlaps, boxes first and then
245
- * segments, each in order, leaving `skin` between it and each wall. Returns
246
- * the corrected center. A center inside a box leaves through the nearest
247
- * side; a center exactly on a segment leaves along the segment's left normal.
248
- *
249
- * @category Movement
251
+ * How far a circle of `radius` at (`x`, `y`) is inside `box` (0 when it does
252
+ * not overlap it), with the unit way out in `wayOut`. A center inside the box
253
+ * leaves through the nearest side.
250
254
  */
251
- export const pushOut = (center, radius, walls, skin = defaultSkin) => {
252
- let x = center.x;
253
- let y = center.y;
254
- for (const box of walls.boxes ?? noBoxes) {
255
- const closestX = Math.max(box.minX, Math.min(x, box.maxX));
256
- const closestY = Math.max(box.minY, Math.min(y, box.maxY));
257
- const dx = x - closestX;
258
- const dy = y - closestY;
259
- const distanceSquared = dx * dx + dy * dy;
260
- if (distanceSquared >= radius * radius)
261
- continue;
262
- if (distanceSquared > 1e-12) {
263
- const distance = Math.sqrt(distanceSquared);
264
- x = closestX + (dx / distance) * (radius + skin);
265
- y = closestY + (dy / distance) * (radius + skin);
266
- }
267
- else {
268
- // Center inside the box: leave through the nearest side.
269
- const left = x - box.minX;
270
- const right = box.maxX - x;
271
- const top = y - box.minY;
272
- const bottom = box.maxY - y;
273
- const nearest = Math.min(left, right, top, bottom);
274
- if (nearest === left)
275
- x = box.minX - radius - skin;
276
- else if (nearest === right)
277
- x = box.maxX + radius + skin;
278
- else if (nearest === top)
279
- y = box.minY - radius - skin;
280
- else
281
- y = box.maxY + radius + skin;
255
+ const boxDepth = (x, y, radius, box) => {
256
+ const closestX = Math.max(box.minX, Math.min(x, box.maxX));
257
+ const closestY = Math.max(box.minY, Math.min(y, box.maxY));
258
+ const dx = x - closestX;
259
+ const dy = y - closestY;
260
+ const distanceSquared = dx * dx + dy * dy;
261
+ if (distanceSquared >= radius * radius)
262
+ return 0;
263
+ if (distanceSquared > 1e-12) {
264
+ const distance = Math.sqrt(distanceSquared);
265
+ wayOut.x = dx / distance;
266
+ wayOut.y = dy / distance;
267
+ return radius - distance;
268
+ }
269
+ const left = x - box.minX;
270
+ const right = box.maxX - x;
271
+ const top = y - box.minY;
272
+ const bottom = box.maxY - y;
273
+ const nearest = Math.min(left, right, top, bottom);
274
+ wayOut.x = nearest === left ? -1 : nearest === right ? 1 : 0;
275
+ wayOut.y = wayOut.x !== 0 ? 0 : nearest === top ? -1 : 1;
276
+ return radius + nearest;
277
+ };
278
+ /**
279
+ * How far a circle of `radius` at (`x`, `y`) is inside `segment` (0 when it
280
+ * does not overlap it), with the unit way out in `wayOut`. A center on the
281
+ * segment leaves along its left normal, one on a round post along +x.
282
+ */
283
+ const segmentDepth = (x, y, radius, segment) => {
284
+ const ex = segment.bx - segment.ax;
285
+ const ey = segment.by - segment.ay;
286
+ const lengthSquared = ex * ex + ey * ey;
287
+ const along = lengthSquared > 1e-12 ? Math.max(0, Math.min(1, ((x - segment.ax) * ex + (y - segment.ay) * ey) / lengthSquared)) : 0;
288
+ const dx = x - (segment.ax + ex * along);
289
+ const dy = y - (segment.ay + ey * along);
290
+ const distanceSquared = dx * dx + dy * dy;
291
+ if (distanceSquared >= radius * radius)
292
+ return 0;
293
+ if (distanceSquared > 1e-12) {
294
+ const distance = Math.sqrt(distanceSquared);
295
+ wayOut.x = dx / distance;
296
+ wayOut.y = dy / distance;
297
+ return radius - distance;
298
+ }
299
+ if (lengthSquared > 1e-12) {
300
+ const length = Math.sqrt(lengthSquared);
301
+ wayOut.x = -ey / length;
302
+ wayOut.y = ex / length;
303
+ }
304
+ else {
305
+ wayOut.x = 1;
306
+ wayOut.y = 0;
307
+ }
308
+ return radius;
309
+ };
310
+ /** Whether a circle of `radius` at (`x`, `y`) overlaps none of the walls (touching is clear). */
311
+ const clear = (x, y, radius, boxes, segments) => {
312
+ for (const box of boxes)
313
+ if (boxDepth(x, y, radius, box) > hair)
314
+ return false;
315
+ for (const segment of segments)
316
+ if (segmentDepth(x, y, radius, segment) > hair)
317
+ return false;
318
+ return true;
319
+ };
320
+ /**
321
+ * How far a circle of `radius` whose center is inside `box` must go to leave
322
+ * it, through the nearest side where it then overlaps none of the walls (the
323
+ * nearest side when none has room), with the way out in `wayOut`.
324
+ */
325
+ const exitBox = (x, y, radius, box, boxes, segments, skin) => {
326
+ let nearest = Infinity;
327
+ let nx = 0;
328
+ let ny = 0;
329
+ let room = false;
330
+ for (let side = 0; side < 4; side++) {
331
+ const distance = side === 0 ? x - box.minX : side === 1 ? box.maxX - x : side === 2 ? y - box.minY : box.maxY - y;
332
+ const sx = side === 0 ? -1 : side === 1 ? 1 : 0;
333
+ const sy = side === 2 ? -1 : side === 3 ? 1 : 0;
334
+ const open = clear(x + sx * (distance + radius + skin), y + sy * (distance + radius + skin), radius, boxes, segments);
335
+ if ((open && !room) || (open === room && distance < nearest)) {
336
+ nearest = distance;
337
+ nx = sx;
338
+ ny = sy;
339
+ room = open;
282
340
  }
283
341
  }
284
- for (const segment of walls.segments ?? noSegments) {
285
- const ex = segment.bx - segment.ax;
286
- const ey = segment.by - segment.ay;
287
- const lengthSquared = ex * ex + ey * ey;
288
- const along = lengthSquared > 1e-12 ? Math.max(0, Math.min(1, ((x - segment.ax) * ex + (y - segment.ay) * ey) / lengthSquared)) : 0;
289
- const closestX = segment.ax + ex * along;
290
- const closestY = segment.ay + ey * along;
291
- const dx = x - closestX;
292
- const dy = y - closestY;
293
- const distanceSquared = dx * dx + dy * dy;
294
- if (distanceSquared >= radius * radius)
295
- continue;
296
- if (distanceSquared > 1e-12) {
297
- const distance = Math.sqrt(distanceSquared);
298
- x = closestX + (dx / distance) * (radius + skin);
299
- y = closestY + (dy / distance) * (radius + skin);
342
+ wayOut.x = nx;
343
+ wayOut.y = ny;
344
+ return radius + nearest;
345
+ };
346
+ /** Pushes the circle at `at` out of the walls in place: {@link pushOut} without allocating. */
347
+ const separate = (at, radius, boxes, segments, skin) => {
348
+ for (let pass = 0; pass < 8; pass++) {
349
+ // The deepest wall the circle is in: the one to leave first.
350
+ let depth = hair;
351
+ let nx = 0;
352
+ let ny = 0;
353
+ let deepest;
354
+ let inside;
355
+ for (const box of boxes) {
356
+ const found = boxDepth(at.x, at.y, radius, box);
357
+ if (found <= depth)
358
+ continue;
359
+ depth = found;
360
+ nx = wayOut.x;
361
+ ny = wayOut.y;
362
+ deepest = box;
363
+ // Only a center inside the box is a radius or more deep.
364
+ inside = found >= radius ? box : undefined;
365
+ }
366
+ for (const segment of segments) {
367
+ const found = segmentDepth(at.x, at.y, radius, segment);
368
+ if (found <= depth)
369
+ continue;
370
+ depth = found;
371
+ nx = wayOut.x;
372
+ ny = wayOut.y;
373
+ deepest = segment;
374
+ inside = undefined;
375
+ }
376
+ if (deepest === undefined)
377
+ return;
378
+ if (inside !== undefined) {
379
+ depth = exitBox(at.x, at.y, radius, inside, boxes, segments, skin);
380
+ nx = wayOut.x;
381
+ ny = wayOut.y;
382
+ }
383
+ // The wall that way out pushes deepest into, facing back across an
384
+ // acute corner: leaving one of the two alone pushes into the other, so
385
+ // leave both at once. Each wall is measured from the start, so the side
386
+ // the circle is on stays the side it leaves to.
387
+ const out = depth + skin;
388
+ let landing = hair;
389
+ let across = 0;
390
+ let fx = 0;
391
+ let fy = 0;
392
+ for (const box of boxes) {
393
+ // How deep it is now (below 0: that far clear), and once pushed.
394
+ const now = boxDepth(at.x, at.y, radius + out, box) - out;
395
+ const facing = wayOut.x * nx + wayOut.y * ny;
396
+ if (box === deepest || now <= -out || facing >= 0 || now - out * facing <= landing)
397
+ continue;
398
+ landing = now - out * facing;
399
+ across = now + skin;
400
+ fx = wayOut.x;
401
+ fy = wayOut.y;
402
+ }
403
+ for (const segment of segments) {
404
+ const now = segmentDepth(at.x, at.y, radius + out, segment) - out;
405
+ const facing = wayOut.x * nx + wayOut.y * ny;
406
+ if (segment === deepest || now <= -out || facing >= 0 || now - out * facing <= landing)
407
+ continue;
408
+ landing = now - out * facing;
409
+ across = now + skin;
410
+ fx = wayOut.x;
411
+ fy = wayOut.y;
300
412
  }
301
- else if (lengthSquared > 1e-12) {
302
- // Center on the segment: leave along its left normal.
303
- const length = Math.sqrt(lengthSquared);
304
- x = closestX - (ey / length) * (radius + skin);
305
- y = closestY + (ex / length) * (radius + skin);
413
+ // Walls face to face (a gap narrower than the circle) have no corner to leave by.
414
+ const det = nx * fy - ny * fx;
415
+ if (landing > hair && Math.abs(det) > 1e-6) {
416
+ at.x += (out * fy - across * ny) / det;
417
+ at.y += (across * nx - out * fx) / det;
306
418
  }
307
419
  else {
308
- // Center on a round post: leave along +x.
309
- x = closestX + radius + skin;
420
+ at.x += nx * out;
421
+ at.y += ny * out;
310
422
  }
311
423
  }
312
- return { x, y };
424
+ };
425
+ /**
426
+ * Pushes a circle out of every wall it overlaps, the deepest first, leaving
427
+ * `skin` between it and each wall, and returns the corrected center. The two
428
+ * walls of an acute corner are left at once, since leaving one alone pushes
429
+ * into the other. Only walls with no room between them (a gap narrower than
430
+ * the circle) can leave it overlapping. A center inside a box leaves through
431
+ * the nearest side with room; a center exactly on a segment leaves along the
432
+ * segment's left normal.
433
+ *
434
+ * @category Movement
435
+ */
436
+ export const pushOut = (center, radius, walls, skin = defaultSkin) => {
437
+ const corrected = { x: center.x, y: center.y };
438
+ separate(corrected, radius, walls.boxes ?? noBoxes, walls.segments ?? noSegments, skin);
439
+ return corrected;
313
440
  };
314
441
  /**
315
442
  * Moves a circle by `delta`, stopping at walls and sliding along them (up to
316
443
  * four contacts per call). The whole path is swept, so no displacement can
317
- * tunnel through a wall. A circle that starts inside a wall is pushed out
318
- * first. A circle meeting a box's corner or a segment's end dead-on goes
319
- * around it rather than stopping against it.
444
+ * tunnel through a wall: a circle that starts outside every wall ends outside
445
+ * every wall, also where walls close in on it (an acute corner, a gap
446
+ * narrower than it), stopping there when nothing is left to slide along. A
447
+ * circle that starts inside a wall is pushed out first. A circle meeting a
448
+ * box's corner or a segment's end dead-on goes around it rather than stopping
449
+ * against it.
320
450
  *
321
451
  * `blocked` says the move touched a wall, not which one, nor whether the
322
452
  * circle stands on one now (a circle resting on a floor and not moving into
@@ -349,63 +479,113 @@ export const pushOut = (center, radius, walls, skin = defaultSkin) => {
349
479
  * @category Movement
350
480
  */
351
481
  export const moveCircle = (start, delta, radius, walls, skin = defaultSkin) => {
352
- let position = pushOut(start, radius, walls, skin);
353
- let rx = delta.x;
354
- let ry = delta.y;
482
+ const boxes = walls.boxes ?? noBoxes;
483
+ const segments = walls.segments ?? noSegments;
484
+ // The circle's place and the movement left, changed in place as it slides
485
+ // (the sweeps read them), so a move allocates only its result and hits.
486
+ const position = { x: start.x, y: start.y };
487
+ separate(position, radius, boxes, segments, skin);
488
+ const step = { x: delta.x, y: delta.y };
489
+ // Within a hair of a wall counts as touching it, so a circle left
490
+ // touching cannot slip in where the sweeps round the other way.
491
+ const touching = radius + hair;
355
492
  let blocked = false;
356
493
  for (let iteration = 0; iteration < 4; iteration++) {
357
- if (rx * rx + ry * ry < 1e-12)
494
+ if (step.x * step.x + step.y * step.y < 1e-12)
358
495
  break;
359
- const step = { x: rx, y: ry };
360
- // The first contact, and whether it is round (a box's corner or a
361
- // segment's end) rather than a flat side.
362
- let hit;
496
+ // The first contact: when, its normal, and whether it is round (a box's
497
+ // corner or a segment's end) rather than a flat side.
498
+ let time = Infinity;
499
+ let nx = 0;
500
+ let ny = 0;
363
501
  let round = false;
364
- for (const box of walls.boxes ?? noBoxes) {
365
- const found = sweepCircleBox(position, step, radius, box);
366
- if (found === undefined || (hit !== undefined && found.time >= hit.time))
502
+ // The bounds of the swept circle: a wall outside them is not in the way.
503
+ const minX = Math.min(position.x, position.x + step.x) - touching;
504
+ const minY = Math.min(position.y, position.y + step.y) - touching;
505
+ const maxX = Math.max(position.x, position.x + step.x) + touching;
506
+ const maxY = Math.max(position.y, position.y + step.y) + touching;
507
+ // A wall the circle touches or is already inside (by a rounding error,
508
+ // or a push out with no room) may not be reported by the sweeps, so test
509
+ // it first: moving into it is a contact now, moving out of it is free.
510
+ for (const box of boxes) {
511
+ if (box.minX > maxX || box.maxX < minX || box.minY > maxY || box.maxY < minY)
367
512
  continue;
368
- hit = found;
369
- round = found.nx !== 0 && found.ny !== 0;
513
+ if (boxDepth(position.x, position.y, touching, box) > 0) {
514
+ if (time === 0 || step.x * wayOut.x + step.y * wayOut.y >= 0)
515
+ continue;
516
+ time = 0;
517
+ nx = wayOut.x;
518
+ ny = wayOut.y;
519
+ }
520
+ else {
521
+ const hit = sweepCircleBox(position, step, radius, box);
522
+ if (hit === undefined || hit.time >= time)
523
+ continue;
524
+ time = hit.time;
525
+ nx = hit.nx;
526
+ ny = hit.ny;
527
+ }
528
+ round = nx !== 0 && ny !== 0;
370
529
  }
371
- for (const segment of walls.segments ?? noSegments) {
372
- const found = sweepCircleSegment(position, step, radius, segment);
373
- if (found === undefined || (hit !== undefined && found.time >= hit.time))
530
+ for (const segment of segments) {
531
+ if (Math.min(segment.ax, segment.bx) > maxX || Math.max(segment.ax, segment.bx) < minX ||
532
+ Math.min(segment.ay, segment.by) > maxY || Math.max(segment.ay, segment.by) < minY)
374
533
  continue;
375
- hit = found;
534
+ if (segmentDepth(position.x, position.y, touching, segment) > 0) {
535
+ if (time === 0 || step.x * wayOut.x + step.y * wayOut.y >= 0)
536
+ continue;
537
+ time = 0;
538
+ nx = wayOut.x;
539
+ ny = wayOut.y;
540
+ }
541
+ else {
542
+ const hit = sweepCircleSegment(position, step, radius, segment);
543
+ if (hit === undefined || hit.time >= time)
544
+ continue;
545
+ time = hit.time;
546
+ nx = hit.nx;
547
+ ny = hit.ny;
548
+ }
376
549
  const ex = segment.bx - segment.ax;
377
550
  const ey = segment.by - segment.ay;
378
551
  const lengthSquared = ex * ex + ey * ey;
379
- const along = found.nx * ex + found.ny * ey;
552
+ const along = nx * ex + ny * ey;
380
553
  // A flat side's normal is perpendicular to the segment.
381
554
  round = lengthSquared < 1e-12 || along * along > 1e-18 * lengthSquared;
382
555
  }
383
- if (hit === undefined) {
384
- position = { x: position.x + rx, y: position.y + ry };
556
+ if (time === Infinity) {
557
+ position.x += step.x;
558
+ position.y += step.y;
385
559
  break;
386
560
  }
387
561
  blocked = true;
388
- position = {
389
- x: position.x + rx * hit.time + hit.nx * skin,
390
- y: position.y + ry * hit.time + hit.ny * skin
391
- };
562
+ position.x += step.x * time;
563
+ position.y += step.y * time;
564
+ // Keep `skin` off the wall, unless that would put the circle inside
565
+ // another one (in a corner or a gap narrower than it): then stay touching.
566
+ if (clear(position.x + nx * skin, position.y + ny * skin, radius, boxes, segments)) {
567
+ position.x += nx * skin;
568
+ position.y += ny * skin;
569
+ }
392
570
  // Slide: keep the remaining movement tangent to the contact.
393
- const remaining = 1 - hit.time;
394
- rx *= remaining;
395
- ry *= remaining;
396
- const into = rx * hit.nx + ry * hit.ny;
571
+ const remaining = 1 - time;
572
+ let rx = step.x * remaining;
573
+ let ry = step.y * remaining;
574
+ const into = rx * nx + ry * ny;
397
575
  if (into < 0) {
398
- rx -= into * hit.nx;
399
- ry -= into * hit.ny;
576
+ rx -= into * nx;
577
+ ry -= into * ny;
400
578
  // Dead-on into a rounded corner or end (as an exact diagonal meets an
401
579
  // axis-aligned box) nothing is left to slide along: go around it
402
580
  // instead of stopping against it.
403
581
  if (round && rx * rx + ry * ry < 1e-12 * into * into) {
404
- const side = rx * -hit.ny + ry * hit.nx < 0 ? -1 : 1;
405
- rx = -hit.ny * -into * side;
406
- ry = hit.nx * -into * side;
582
+ const side = rx * -ny + ry * nx < 0 ? -1 : 1;
583
+ rx = -ny * -into * side;
584
+ ry = nx * -into * side;
407
585
  }
408
586
  }
587
+ step.x = rx;
588
+ step.y = ry;
409
589
  }
410
590
  return { position, blocked };
411
591
  };