@johnmorrisdotca/wakusei-planets 0.54.0 → 0.55.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.
@@ -50,7 +50,7 @@ const unit = (a) => {
50
50
  return n === 0 ? [0, 0, 0] : [a[0] / n, a[1] / n, a[2] / n];
51
51
  };
52
52
  /**
53
- * How near a move may come to a body, in that body's radii above its surface: 1, so a body passed is never more than 60 degrees across. A move that starts or ends nearer than one and a half times that keeps two thirds of the nearer distance.
53
+ * How near a move may come to a body, in that body's radii above its surface: a third of a radius (0.35), a margin so the camera never flies through a body. Bodies near the way are passed, not routed round: a body passed close is one more thing to see on the way. A move that starts or ends nearer than one and a half times that keeps two thirds of the nearer distance.
54
54
  *
55
55
  * @example
56
56
  * ```ts
@@ -59,7 +59,7 @@ const unit = (a) => {
59
59
  * console.log(TRAVEL_CLEARANCE);
60
60
  * ```
61
61
  */
62
- export const TRAVEL_CLEARANCE = 1;
62
+ export const TRAVEL_CLEARANCE = 0.35;
63
63
  /**
64
64
  * The time in milliseconds, counted from the start of the plan, that the space view lets the planner spend on the other ways of a move within a planet's family (`TravelInput.budget`); the first way, with its aim
65
65
  * turn, is always worked out. Measured on the author's Mac mini over the 360 surveyed plain-view moves of six pairs of moons (most need no other way): the first way alone takes a median of 4 ms, 90 percent under
@@ -307,7 +307,19 @@ function clearOf(route, position, t) {
307
307
  }
308
308
  function along(route, t) {
309
309
  const inTurn = route.turn !== null && t < route.share;
310
- const at = inTurn ? turnAt(route.turn, ease(t / route.share), t, clamp01(t / route.share)) : legAt(route.leg, ease(route.share >= 1 ? 1 : (t - route.share) / (1 - route.share)), t);
310
+ let at;
311
+ if (inTurn) {
312
+ at = turnAt(route.turn, ease(t / route.share), t, clamp01(t / route.share));
313
+ if (t > route.legStart) {
314
+ // the way on has started while the turn is still going: the camera goes where the turn takes it and, as well, as far along the way on as that has got, so the two motions add and there is no stop and no corner where one gives way to the other
315
+ const q = ease((t - route.legStart) / (1 - route.legStart));
316
+ const now = legAt(route.leg, q, t);
317
+ const start = legAt(route.leg, 0, t);
318
+ at = { ...at, position: add(at.position, sub(now.position, start.position)) };
319
+ }
320
+ }
321
+ else
322
+ at = legAt(route.leg, ease(route.legStart >= 1 ? 1 : (t - route.legStart) / (1 - route.legStart)), t);
311
323
  return { position: clearOf(route, at.position, t), o: at.o, fov: at.fov, stage: inTurn ? "turn" : t < route.arrive ? "travel" : "arrive" };
312
324
  }
313
325
  function sightOf(body, t, position, o, frame) {
@@ -319,7 +331,12 @@ function sightOf(body, t, position, o, frame) {
319
331
  return { inside: v[2] > 0 && Math.abs(x) <= 1 && Math.abs(y) <= 1, x, y, radius: d <= body.radius ? 90 : Math.asin(body.radius / d) / RAD };
320
332
  }
321
333
  /** The pivot's stand at the end of the turn, for a moment `tm` of the move. */
322
- function standAt(input, tm, family, variant = 0) {
334
+ function standAt(input, tm, family, variant = 0, framed = false) {
335
+ if (framed && !family) {
336
+ const picture = frameStand(input, tm);
337
+ if (picture !== null)
338
+ return picture;
339
+ }
323
340
  const o0 = facingOf(input.start);
324
341
  const o1 = facingOf(input.end);
325
342
  const clearance = input.clearance ?? TRAVEL_CLEARANCE;
@@ -459,6 +476,135 @@ function standAt(input, tm, family, variant = 0) {
459
476
  },
460
477
  };
461
478
  }
479
+ /** The share of the picture's height the body left keeps at the stand of a framed move (`frameStand`), and the least and most it may keep. */
480
+ const FRAME_HEIGHT = 0.5;
481
+ const FRAME_LEAST = 0.4;
482
+ const FRAME_MOST = 0.6;
483
+ /** The most the line between the two bodies may be tilted from the picture's long axis at the stand, in radians. */
484
+ const FRAME_TILT = 0.6;
485
+ /** Where the stand frames the pair along their line in the picture: the body left this share of the way from the middle to the far edge, so the other body is on the other side. */
486
+ const FRAME_SPLIT = 0.45;
487
+ /** The distance at which a body of radius `r` keeps `share` of the picture's height. */
488
+ const distanceFor = (r, share, tv) => {
489
+ const t = Math.max(1e-6, share * tv);
490
+ return r / (t / Math.sqrt(1 + t * t));
491
+ };
492
+ const FRAME_SPHERE = (() => {
493
+ const points = [];
494
+ const n = 240;
495
+ for (let i = 0; i < n; i += 1) {
496
+ const z = 1 - (2 * (i + 0.5)) / n;
497
+ const r = Math.sqrt(1 - z * z);
498
+ const a = i * 2.399963229728653;
499
+ points.push([r * Math.cos(a), r * Math.sin(a), z]);
500
+ }
501
+ return points;
502
+ })();
503
+ /**
504
+ * The stand of a framed move (B1): the camera backs up a little from the body left, until it keeps about half the picture's height, and goes round it only as far as it takes for the other body to be in the picture
505
+ * too, on the other side of the middle: the body left large on one side, the destination small on the other, whichever side needs less movement. The look direction is the one that gives that picture, so the whole turn is
506
+ * made before the way on and the way on starts from a picture that holds both. Null where there is no such picture (the destination too near the body left to leave its disc, or the picture too narrow).
507
+ */
508
+ function frameStand(input, tm) {
509
+ const pivot = input.leaving;
510
+ if (pivot.reach !== undefined || input.destination.reach !== undefined)
511
+ return null;
512
+ const o0 = facingOf(input.start);
513
+ const o1 = facingOf(input.end);
514
+ const frame = frameOf(input.end.fov, input.aspect);
515
+ const wide = frame.th >= frame.tv;
516
+ const clearance = input.clearance ?? TRAVEL_CLEARANCE;
517
+ const centre = placeOf(pivot, tm);
518
+ const b = placeOf(input.destination, tm);
519
+ const e1 = sub(input.end.position, placeOf(input.destination, 1));
520
+ const last = add(b, e1);
521
+ const fromPivot = sub(pivot.from, input.start.position);
522
+ const d0 = Math.max(1e-12, length(fromPivot));
523
+ const near = distanceFor(pivot.radius, FRAME_MOST, frame.tv);
524
+ const far = distanceFor(pivot.radius, FRAME_LEAST, frame.tv);
525
+ const dm = d0 < near ? distanceFor(pivot.radius, FRAME_HEIGHT, frame.tv) : d0;
526
+ const share = pivot.radius / dm;
527
+ const tanL = share / Math.sqrt(Math.max(1e-12, 1 - share * share));
528
+ const n0 = unit(scale(fromPivot, -1));
529
+ // the camera at `n` round the body left, looking so that the two bodies are either side of the middle: its picture, or null if the picture does not do
530
+ const standAtN = (n) => {
531
+ const stand = add(centre, scale(n, dm));
532
+ const toB = sub(b, stand);
533
+ if (length(toB) < 1e-9)
534
+ return null;
535
+ const dirL = scale(n, -1);
536
+ const dirB = unit(toB);
537
+ const apart = angleOf(dirL, dirB);
538
+ if (apart < 1e-6)
539
+ return null;
540
+ const f = arc(dirL, dirB, FRAME_SPLIT);
541
+ const pick = o0.u;
542
+ const across = sub(pick, scale(f, dot(pick, f)));
543
+ const o = { f, u: length(across) < 1e-6 ? squareTo(f) : unit(across) };
544
+ const sl = toCamera(o, sub(centre, stand));
545
+ const sb = toCamera(o, toB);
546
+ if (!(sl[2] > 0) || !(sb[2] > 0))
547
+ return null;
548
+ const xl = sl[0] / (sl[2] * frame.th);
549
+ const yl = sl[1] / (sl[2] * frame.tv);
550
+ const xb = sb[0] / (sb[2] * frame.th);
551
+ const yb = sb[1] / (sb[2] * frame.tv);
552
+ // the disc of the body left inside the picture, the destination's place inside it, and the destination clear of the disc
553
+ if (Math.abs(xl) + tanL / frame.th > 1 || Math.abs(yl) + tanL / frame.tv > 1)
554
+ return null;
555
+ if (Math.abs(xb) > 0.85 || Math.abs(yb) > 0.85)
556
+ return null;
557
+ const dx = (xb - xl) * frame.th;
558
+ const dy = (yb - yl) * frame.tv;
559
+ const gap = Math.hypot(dx, dy);
560
+ if (gap < 1.2 * tanL)
561
+ return null;
562
+ const tilt = wide ? Math.atan2(Math.abs(dy), Math.abs(dx)) : Math.atan2(Math.abs(dx), Math.abs(dy));
563
+ if (tilt > FRAME_TILT)
564
+ return null;
565
+ if (missOf(stand, last, centre) < Math.min(1.05 * (1 + clearance) * pivot.radius, 0.9 * dm))
566
+ return null;
567
+ return { stand, o, cost: angleOf(n0, n) + 0.8 * tilt };
568
+ };
569
+ let best = null;
570
+ const here = standAtN(n0);
571
+ if (here !== null)
572
+ best = { n: n0, cost: here.cost };
573
+ else {
574
+ for (const n of FRAME_SPHERE) {
575
+ const found = standAtN(n);
576
+ if (found !== null && (best === null || found.cost < best.cost))
577
+ best = { n, cost: found.cost };
578
+ }
579
+ if (best === null)
580
+ return null;
581
+ // the least turn about the body left that is still a good picture: along the way from where the camera is to the best found
582
+ let lo = 0;
583
+ let hi = 1;
584
+ const goal = best.n;
585
+ for (let i = 0; i < 14; i += 1) {
586
+ const mid = (lo + hi) / 2;
587
+ if (standAtN(arc(n0, goal, mid)) !== null)
588
+ hi = mid;
589
+ else
590
+ lo = mid;
591
+ }
592
+ best = { n: arc(n0, goal, hi), cost: 0 };
593
+ }
594
+ const at = standAtN(best.n);
595
+ if (at === null)
596
+ return null;
597
+ const { stand, o } = at;
598
+ const from = sub(stand, b);
599
+ const lengthOn = length(sub(e1, from));
600
+ const leaving = placeOf(input.leaving, tm);
601
+ const d1 = Math.max(1e-12, length(e1));
602
+ void far;
603
+ return {
604
+ turn: { pivot, swing: swingOf(o0, o), u0: unit(toCamera(o0, fromPivot)), u1: unit(toCamera(o, sub(centre, stand))), d0, d1: dm, lift: 0, fov0: input.start.fov, fov1: input.end.fov, framed: true },
605
+ leg: { about: input.destination, type: "line", swing: swingOf(o, o1), fov0: input.end.fov, fov1: input.end.fov, from, to: e1, d0: length(from), d1, dn: d1, u0: unit(toCamera(o0, fromPivot)), u1: unit(toCamera(o0, fromPivot)), l: lengthOn, ea: Math.max(1e-9, Math.min(dm, length(sub(leaving, stand)))), eb: d1 },
606
+ };
607
+ }
462
608
  function lineLeg(input, about, watch) {
463
609
  const from = sub(input.start.position, about.from);
464
610
  const to = sub(input.end.position, placeOf(about, 1));
@@ -637,7 +783,7 @@ function backOffKit(input, turn, share) {
637
783
  return missed;
638
784
  };
639
785
  }
640
- function routeBase(input, variant = 0) {
786
+ function routeBase(input, variant = 0, framed = false) {
641
787
  const wanted = input.reduced ? "cut" : (input.kind ?? "turn-travel");
642
788
  const kind = wanted === "auto" ? "turn-travel" : wanted;
643
789
  const clearance = input.clearance ?? TRAVEL_CLEARANCE;
@@ -649,7 +795,7 @@ function routeBase(input, variant = 0) {
649
795
  const easing = { turn: "smooth-step", distance: "geometric" };
650
796
  if (kind === "cut") {
651
797
  const leg = lineLeg(input, input.destination, undefined);
652
- return { route: { ...base, cut: true, turn: null, share: 0, arrive: 0, leg }, plan: { kind, path: "cut", stages: { turn: 0, travel: 0, arrive: 1 }, duration: 0, turn: least, settle: 0, easing } };
798
+ return { route: { ...base, cut: true, turn: null, share: 0, legStart: 0, arrive: 0, leg }, plan: { kind, path: "cut", stages: { turn: 0, travel: 0, arrive: 1 }, duration: 0, turn: least, settle: 0, blend: 0, easing } };
653
799
  }
654
800
  let turn = null;
655
801
  let leg;
@@ -667,7 +813,7 @@ function routeBase(input, variant = 0) {
667
813
  const d0 = length(sub(input.leaving.from, input.start.position));
668
814
  const facing = swingDegrees(swingOf(o0, facingOf(input.end))) < 1 && within(toCamera(o0, sub(input.destination.from, input.start.position)), frameOf(input.start.fov, input.aspect), 0.93);
669
815
  const open = missOf(input.start.position, input.end.position, input.leaving.from) >= Math.min(1.52 * (1 + clearance) * input.leaving.radius, 0.9 * d0);
670
- let stand = facing && open ? { turn: { pivot: input.leaving, swing: swingOf(o0, o0), u0: [0, 0, 1], u1: [0, 0, 1], d0: 1, d1: 1, lift: 0, fov0: input.start.fov, fov1: input.start.fov }, leg: lineLeg(input, input.destination, undefined) } : standAt(input, 0.25, family, variant);
816
+ let stand = facing && open ? { turn: { pivot: input.leaving, swing: swingOf(o0, o0), u0: [0, 0, 1], u1: [0, 0, 1], d0: 1, d1: 1, lift: 0, fov0: input.start.fov, fov1: input.start.fov }, leg: lineLeg(input, input.destination, undefined) } : standAt(input, 0.25, family, variant, framed);
671
817
  // the stand is worked out for the moment the turn ends, which is the share of the move its own length gives: found by trying, until the two agree (the bodies move, so a stand for another moment would not meet the way on)
672
818
  let tm = 0.25;
673
819
  for (let pass = 0; pass < 8; pass += 1) {
@@ -676,7 +822,7 @@ function routeBase(input, variant = 0) {
676
822
  if ((facing && open) || share === 0 || Math.abs(share - tm) < 0.002)
677
823
  break;
678
824
  tm = pass === 0 ? share : (tm + share) / 2;
679
- stand = standAt(input, tm, family, variant);
825
+ stand = standAt(input, tm, family, variant, framed);
680
826
  }
681
827
  if (share > 0 && !(facing && open))
682
828
  share = tm;
@@ -746,8 +892,8 @@ function routeBase(input, variant = 0) {
746
892
  hi = mid;
747
893
  }
748
894
  const arrive = share + (1 - share) * Math.min(0.9, Math.max(0.6, hi));
749
- const route = { ...base, cut: false, turn, share, arrive, leg };
750
- return { route, plan: { kind, path, stages: { turn: share, travel: arrive - share, arrive: 1 - arrive }, duration: Math.round(Math.min(9000, Math.max(1200, total)) * Math.max(0.05, input.pace ?? 1)), turn: least, settle: leg.type === "swing" ? swingDegrees(leg.swing) : 0, easing } };
895
+ const route = { ...base, cut: false, turn, share, legStart: share, arrive, leg };
896
+ return { route, plan: { kind, path, stages: { turn: share, travel: arrive - share, arrive: 1 - arrive }, duration: Math.round(Math.min(9000, Math.max(1200, total)) * Math.max(0.05, input.pace ?? 1)), turn: least, settle: leg.type === "swing" ? swingDegrees(leg.swing) : 0, blend: 0, easing } };
751
897
  }
752
898
  const FAULT_DRIFT = 0.02;
753
899
  const FAULT_STEP = 0.3;
@@ -756,7 +902,7 @@ function faultsOf(route) {
756
902
  const plan = { route };
757
903
  const at = (t) => travelPose(input, t, plan);
758
904
  const last = at(1);
759
- const faults = { empty: 0, lost: 0, drift: 0, driftWorst: 0, step: 0, pace: 0, planet: 0 };
905
+ const faults = { empty: 0, lost: 0, drift: 0, driftWorst: 0, step: 0, pace: 0, planet: 0, held: 0 };
760
906
  const lastF = unit(input.end.forward);
761
907
  let turned = 0;
762
908
  let way = 0;
@@ -769,6 +915,8 @@ function faultsOf(route) {
769
915
  faults.empty += 1;
770
916
  if (s.around !== undefined && !s.around.inside)
771
917
  faults.planet += 1;
918
+ if (i / 240 < route.share && !s.leaving.inside && !(s.around?.inside === true))
919
+ faults.held += 1;
772
920
  if (i >= afterTurn && !s.destination.inside)
773
921
  faults.lost += 1;
774
922
  if (i > afterTurn) {
@@ -866,6 +1014,77 @@ function withAim(input, first, stop) {
866
1014
  const total = Math.max(t0 / share, first.plan.duration / Math.max(0.05, input.pace ?? 1));
867
1015
  return { route: aimed, plan: { ...first.plan, duration: Math.round(Math.min(9000, Math.max(1200, total)) * Math.max(0.05, input.pace ?? 1)) } };
868
1016
  }
1017
+ /** How much of the turn the way on may overlap, tried from the most, and how much of the shorter stage's time the overlap saves. */
1018
+ const BLENDS = [1, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1];
1019
+ const BLEND_SAVES = 0.6;
1020
+ /** The moment the arrival starts for a way on that starts at `legStart`. */
1021
+ const arriveFor = (leg, legStart) => {
1022
+ const near = leg.type === "line" ? 2 * leg.eb : leg.dn - leg.d1;
1023
+ let lo = 0;
1024
+ let hi = 1;
1025
+ for (let i = 0; i < 30; i += 1) {
1026
+ const mid = (lo + hi) / 2;
1027
+ if (leftOf(ease(mid), leg.l, leg.ea, leg.eb) > near)
1028
+ lo = mid;
1029
+ else
1030
+ hi = mid;
1031
+ }
1032
+ return legStart + (1 - legStart) * Math.min(0.9, Math.max(0.6, hi));
1033
+ };
1034
+ /** The most the look direction or the picture's up turns between two moments of a walk of the move (a 240th apart), in degrees. */
1035
+ function steepest(route) {
1036
+ const plan = { route };
1037
+ let before = travelPose(route.input, 0, plan);
1038
+ let worst = 0;
1039
+ for (let i = 1; i <= 240; i += 1) {
1040
+ const s = travelPose(route.input, i / 240, plan);
1041
+ worst = Math.max(worst, angleOf(before.forward, s.forward) / RAD, angleOf(before.up, s.up) / RAD);
1042
+ before = s;
1043
+ }
1044
+ return worst;
1045
+ }
1046
+ /** The furthest from the middle of the picture the nearer of the two bodies gets in the turn (1 is the edge of the picture, infinity is neither in it at some moment). */
1047
+ function turnEdge(route) {
1048
+ const plan = { route };
1049
+ const off = (sight) => (sight.inside ? Math.max(Math.abs(sight.x), Math.abs(sight.y)) : Number.POSITIVE_INFINITY);
1050
+ let edge = 0;
1051
+ for (let i = 0; i <= 240; i += 1) {
1052
+ if (i / 240 >= route.share)
1053
+ break;
1054
+ const s = travelPose(route.input, i / 240, plan);
1055
+ edge = Math.max(edge, Math.min(off(s.leaving), off(s.destination)));
1056
+ }
1057
+ return edge;
1058
+ }
1059
+ /** `now` has none of any kind of fault more than `was` (the planet out of the picture only where it was in all through, and the drift's worst no worse). */
1060
+ const noWorse = (now, was) => now.empty <= was.empty && now.lost <= was.lost && now.drift <= was.drift && now.step <= was.step && now.pace <= was.pace && now.held <= was.held && now.driftWorst <= was.driftWorst + 1e-12 && (was.planet > 0 || now.planet === 0) && total(now) <= total(was);
1061
+ /**
1062
+ * The way on starts while the turn is still going, so the camera never stops between the two and never turns a corner: its place is where the turn takes it plus as far along the way on as that has got, and both
1063
+ * stages ease in and out, so the speed does not drop to nothing in between and the direction of travel bends over the overlap instead of changing at once. The turn ends where it did, so the look direction, the
1064
+ * stand and every picture from the end of the turn on are the same shape; the overlap is the largest of `BLENDS` that leaves no kind of fault more than the move had, and none if none does. The move is shorter by the
1065
+ * time the two stages now share.
1066
+ */
1067
+ function blended(first) {
1068
+ const route = first.route;
1069
+ // a way on that turns round the destination as it goes keeps its own timing: its look direction follows the distance, which an earlier start would make jump
1070
+ if (route.cut || route.turn === null || route.leg.type !== "line" || route.share <= 0 || route.share >= 1)
1071
+ return first;
1072
+ const was = faultsOf(route);
1073
+ const rate = Math.max(1.25 * steepest(route), 3);
1074
+ for (const share of BLENDS) {
1075
+ const legStart = route.share * (1 - share);
1076
+ const candidate = { ...route, legStart, arrive: arriveFor(route.leg, legStart) };
1077
+ if (!noWorse(faultsOf(candidate), was) || steepest(candidate) > rate)
1078
+ continue;
1079
+ // a turn made by an aim point keeps the nearer body inside `AIM_EDGE` of the way to the edge, and the overlap must not take it out
1080
+ if (route.turn.aim !== undefined && turnEdge(candidate) > AIM_EDGE)
1081
+ continue;
1082
+ const pace = Math.max(0.05, route.input.pace ?? 1);
1083
+ const duration = Math.round(Math.max(1200, Math.round(first.plan.duration / pace) * (1 - BLEND_SAVES * share * Math.min(route.share, 1 - route.share))) * pace);
1084
+ return { route: candidate, plan: { ...first.plan, duration, blend: share, stages: { turn: route.share, travel: candidate.arrive - route.share, arrive: 1 - candidate.arrive } } };
1085
+ }
1086
+ return first;
1087
+ }
869
1088
  /**
870
1089
  * The route of a move. A move within one planet's family (the path `circle`) is first made as it always was. If a walk of it has a picture with neither the body left nor the destination in it, its turn is made again by an
871
1090
  * aim point (see `Aim`): the turn takes the same share of the move and ends in the same place, so the way on and the arrival are the same, sample for sample, and only the turn is new. The new turn is taken when
@@ -880,7 +1099,7 @@ function routeOf(input) {
880
1099
  return kept;
881
1100
  // a plan is kept for the input it was made for, so the input is frozen (its top level: a field set after planning would be a plan of something else; the views and bodies are not to be changed either)
882
1101
  Object.freeze(input);
883
- const made = routeMade(input);
1102
+ const made = blended(routeMade(input));
884
1103
  routes.set(input, made);
885
1104
  return made;
886
1105
  }
@@ -889,7 +1108,13 @@ function routeMade(input) {
889
1108
  // before the other ways were tried and what they are measured against
890
1109
  const started = input.budget?.now();
891
1110
  const stop = input.budget === undefined || started === undefined ? undefined : () => input.budget.now() - started >= input.budget.ms;
892
- const first = withAim(input, routeBase(input));
1111
+ let first = withAim(input, routeBase(input));
1112
+ if (first.plan.path === "pivot" && first.route.turn !== null && first.route.share > 0) {
1113
+ // a move that turns beside the body left is framed (B1) where there is a picture that holds both bodies, its look direction and stand being those of the picture; it is taken if it has no fault more than the other
1114
+ const framed = routeBase(input, 0, true);
1115
+ if (framed.route.turn?.framed === true && framed.route.share > 0 && noWorse(faultsOf(framed.route), faultsOf(first.route)) && steepest(framed.route) <= Math.max(1.25 * steepest(first.route), 3))
1116
+ first = framed;
1117
+ }
893
1118
  if (first.route.turn === null || first.plan.path !== "circle" || first.route.cut || first.route.share <= 0)
894
1119
  return first;
895
1120
  const was = faultsOf(first.route);
@@ -1008,14 +1233,20 @@ export function travelPose(input, t, plan) {
1008
1233
  sample.around = sightOf(input.around, time, view.position, o, frame);
1009
1234
  return sample;
1010
1235
  }
1236
+ /** The least share of the body gone to that must be lit for the body left to be kept behind it in the last view (`arrivalSide`'s `behind`): half, the same as the rule for every other arrival. */
1237
+ const BEHIND_LIT = 0.5;
1011
1238
  /**
1012
1239
  * The side of a body to stand on so a companion is in the picture, and the body stays at least half lit.
1013
1240
  * Companions are tried in rank order (1, then 2, then 3). The first rank that fits wins; within a rank the side nearest the approach is kept.
1014
1241
  * A side more than 90 degrees from the Sun is refused, so a lit globe is never traded for a black one. With no companion that fits, the side is the
1015
1242
  * approach, swung toward the Sun when it would show less than about three fifths of the body lit (never more than 75 degrees off the Sun).
1016
1243
  *
1017
- * @param input - The body, where the move comes from, the Sun, and the companions in rank order.
1018
- * @returns A unit side from the body toward the camera, how much of the disc is lit, and the companion's id (null when none was framed).
1244
+ * `behind` is the body the move comes from: a side that puts it in the picture at the back left or right of the body, clear of the disc, is taken before any companion where the body is still at least half lit
1245
+ * (`BEHIND_LIT`); a side that also frames a moon of the body is preferred, and that moon is the companion returned. Where the body would show its night side, or no side frames the body left (a picture too narrow),
1246
+ * the rules above stand: the lit side wins, with a moon where one fits, and the body left is not in the last view.
1247
+ *
1248
+ * @param input - The body, where the move comes from, the Sun, the companions in rank order, and optionally `behind`.
1249
+ * @returns A unit side from the body toward the camera, how much of the disc is lit, the companion's id (null when none was framed), and whether the side is the one `behind` asked for.
1019
1250
  *
1020
1251
  * @example
1021
1252
  * ```ts
@@ -1058,11 +1289,43 @@ export function arrivalSide(input) {
1058
1289
  const limb = Math.atan(input.radius / distance);
1059
1290
  return angleOf(forward, unit(v)) > limb * 1.15;
1060
1291
  };
1292
+ // the body left at the back left or right (above or below in a tall picture): not far from the line across the picture's middle
1293
+ const level = (side, at) => {
1294
+ const forward = scale(side, -1);
1295
+ const north = sub([0, 1, 0], scale(forward, forward[1]));
1296
+ const up = length(north) < 1e-6 ? squareTo(forward) : unit(north);
1297
+ const v = sub(at, add(input.body, scale(side, distance)));
1298
+ const z = dot(v, forward);
1299
+ const x = dot(v, cross(forward, up)) / (tv * aspect * z);
1300
+ const y = dot(v, up) / (tv * z);
1301
+ // a near-square picture has no room beside the disc: any side of it will do
1302
+ return aspect > 0.77 && aspect < 1.3 ? true : aspect >= 1 ? Math.abs(y) <= 0.45 : Math.abs(x) <= 0.45;
1303
+ };
1061
1304
  const found = [];
1062
1305
  const half = Math.PI / 2 + 1e-4;
1063
1306
  const companions = input.companions ?? [];
1307
+ const behind = input.behind;
1064
1308
  const consider = (side) => {
1065
1309
  const u = length(side) < 1e-9 ? approach : unit(side);
1310
+ // the body left, behind the body gone to at its back left or right, comes first and is kept even where that gives a dark side: it is what keeps the body left in the picture
1311
+ // ...but only where the body gone to is still at least half lit: the lit side wins over the body left (an outward move to a planet shows its sunlit side, with a moon, and the body left is not in the last view)
1312
+ if (behind !== undefined && (sunDir === null || litOf(u) >= BEHIND_LIT) && frames(u, behind) && level(u, behind)) {
1313
+ // a side that also frames a moon of the body (or its planet) is taken before one that does not: the arrival keeps both, and names the moon
1314
+ let moon = null;
1315
+ let framed = false;
1316
+ for (const companion of companions) {
1317
+ if (companion.rank === 1 && frames(u, companion.at)) {
1318
+ framed = true;
1319
+ moon = companion.id ?? null;
1320
+ break;
1321
+ }
1322
+ }
1323
+ const near = angleOf(u, approach);
1324
+ const top = found[0];
1325
+ if (top === undefined || top.rank > 0 || (framed && !top.both) || (framed === (top.both === true) && near < top.near - 1e-6))
1326
+ found[0] = { side: u, rank: 0, near, id: moon, both: framed };
1327
+ return;
1328
+ }
1066
1329
  if (sunDir && angleOf(u, sunDir) > half)
1067
1330
  return;
1068
1331
  let rank = 99;
@@ -1081,7 +1344,7 @@ export function arrivalSide(input) {
1081
1344
  found[0] = { side: u, rank, near, id };
1082
1345
  };
1083
1346
  consider(approach);
1084
- for (const companion of companions) {
1347
+ for (const companion of behind === undefined ? companions : [{ at: behind, rank: 2 }, ...companions]) {
1085
1348
  const away = sub(input.body, companion.at);
1086
1349
  if (length(away) < 1e-6)
1087
1350
  continue;
@@ -1104,7 +1367,7 @@ export function arrivalSide(input) {
1104
1367
  }
1105
1368
  const picked = found[0];
1106
1369
  const side = picked?.side ?? swing(approach);
1107
- return { side, lit: litOf(side), companion: picked?.id ?? null };
1370
+ return { side, lit: litOf(side), companion: picked?.id ?? null, behind: picked?.rank === 0 };
1108
1371
  }
1109
1372
  /**
1110
1373
  * A framed view of a whole body to arrive at, from the side the move comes from, so the approach is straight and nothing has to be turned back: the camera stands on the line towards `from`, moved round
@@ -1129,5 +1392,5 @@ export function travelArrival(input) {
1129
1392
  const forward = scale(side, -1);
1130
1393
  const north = input.north ?? [0, 1, 0];
1131
1394
  const across = sub(north, scale(forward, dot(north, forward)));
1132
- return { position: add(input.body, scale(side, distance)), forward, up: length(across) < 1e-6 ? squareTo(forward) : unit(across), fov: input.fov ?? 35, lit: picked.lit, companion: picked.companion };
1395
+ return { position: add(input.body, scale(side, distance)), forward, up: length(across) < 1e-6 ? squareTo(forward) : unit(across), fov: input.fov ?? 35, lit: picked.lit, companion: picked.companion, behind: picked.behind };
1133
1396
  }
@@ -157,25 +157,43 @@ export interface DriveView {
157
157
  azimuth: number;
158
158
  pitch: number;
159
159
  }
160
+ /**
161
+ * What a drag does in the chase view of the rover: `head` (the default of the element, its `drive-drag`) turns the view where the camera is, like turning your head, so the camera keeps its place 6.5 m behind the rover
162
+ * and the scene turns round it; `orbit` takes the camera round the rover as it looks about, so the rover stays in the same place in the picture and is seen from every side, as the chase view was up to 0.55.
163
+ *
164
+ * @example
165
+ * ```ts
166
+ * import type { DriveDrag } from "@johnmorrisdotca/wakusei-planets/surface";
167
+ *
168
+ * const drags: DriveDrag[] = ["head", "orbit"];
169
+ * console.log(drags.join(" or "));
170
+ * ```
171
+ */
172
+ export type DriveDrag = "head" | "orbit";
160
173
  /**
161
174
  * The eyes for a view of the rover. The chase view stands 6.5 m behind it and 2.8 m up (never under the ground), looking at it; the seat view is the commander's eyes in the left seat, looking along the rover's heading
162
- * and tilting with it. `offset` is the driver's own turn of the view from a drag (degrees), added to the direction either looks in.
175
+ * and tilting with it. `offset` is the driver's own turn of the view from a drag (degrees), added to the direction either looks in. In the chase view `drag` says what that turn does: with `head` the eyes stay where the rover
176
+ * puts them with no drag and only the way they look turns, so the rover leaves the picture as the view turns away; with `orbit` (what a call without it does, as before) the eyes go round the rover as they look about.
177
+ * The seat view turns in place either way.
163
178
  *
164
179
  * @param state - The rover.
165
180
  * @param view - `chase` or `seat`.
166
181
  * @param offset - The driver's turn of the view, degrees right and up.
167
182
  * @param ground - The ground's height over the site's ground at a place.
183
+ * @param drag - What a drag does in the chase view: `head` or `orbit` (the default).
168
184
  * @returns The eyes.
169
185
  *
170
186
  * @example
171
187
  * ```ts
172
188
  * import { driveView, startDrive } from "@johnmorrisdotca/wakusei-planets/surface";
173
189
  *
174
- * const view = driveView(startDrive(0, 0, 0, 0), "chase", { azimuth: 0, pitch: 0 }, () => 0);
175
- * console.log(view.eye.north.toFixed(1), view.eye.up.toFixed(1));
190
+ * const rover = startDrive(0, 0, 0, 0);
191
+ * const view = driveView(rover, "chase", { azimuth: 0, pitch: 0 }, () => 0);
192
+ * const turned = driveView(rover, "chase", { azimuth: 90, pitch: 0 }, () => 0, "head");
193
+ * console.log(view.eye.north.toFixed(1), turned.eye.north.toFixed(1), turned.azimuth);
176
194
  * ```
177
195
  */
178
196
  export declare function driveView(state: DriveState, view: "chase" | "seat", offset: {
179
197
  azimuth: number;
180
198
  pitch: number;
181
- }, ground: (east: number, north: number) => number): DriveView;
199
+ }, ground: (east: number, north: number) => number, drag?: DriveDrag): DriveView;
@@ -218,28 +218,34 @@ export function driveParts(state) {
218
218
  }
219
219
  /**
220
220
  * The eyes for a view of the rover. The chase view stands 6.5 m behind it and 2.8 m up (never under the ground), looking at it; the seat view is the commander's eyes in the left seat, looking along the rover's heading
221
- * and tilting with it. `offset` is the driver's own turn of the view from a drag (degrees), added to the direction either looks in.
221
+ * and tilting with it. `offset` is the driver's own turn of the view from a drag (degrees), added to the direction either looks in. In the chase view `drag` says what that turn does: with `head` the eyes stay where the rover
222
+ * puts them with no drag and only the way they look turns, so the rover leaves the picture as the view turns away; with `orbit` (what a call without it does, as before) the eyes go round the rover as they look about.
223
+ * The seat view turns in place either way.
222
224
  *
223
225
  * @param state - The rover.
224
226
  * @param view - `chase` or `seat`.
225
227
  * @param offset - The driver's turn of the view, degrees right and up.
226
228
  * @param ground - The ground's height over the site's ground at a place.
229
+ * @param drag - What a drag does in the chase view: `head` or `orbit` (the default).
227
230
  * @returns The eyes.
228
231
  *
229
232
  * @example
230
233
  * ```ts
231
234
  * import { driveView, startDrive } from "@johnmorrisdotca/wakusei-planets/surface";
232
235
  *
233
- * const view = driveView(startDrive(0, 0, 0, 0), "chase", { azimuth: 0, pitch: 0 }, () => 0);
234
- * console.log(view.eye.north.toFixed(1), view.eye.up.toFixed(1));
236
+ * const rover = startDrive(0, 0, 0, 0);
237
+ * const view = driveView(rover, "chase", { azimuth: 0, pitch: 0 }, () => 0);
238
+ * const turned = driveView(rover, "chase", { azimuth: 90, pitch: 0 }, () => 0, "head");
239
+ * console.log(view.eye.north.toFixed(1), turned.eye.north.toFixed(1), turned.azimuth);
235
240
  * ```
236
241
  */
237
- export function driveView(state, view, offset, ground) {
242
+ export function driveView(state, view, offset, ground, drag = "orbit") {
238
243
  if (view === "seat") {
239
244
  const eye = toWorld(state, ROVER.seat);
240
245
  return { eye: { east: eye[0], north: eye[2], up: eye[1] }, azimuth: (((state.heading + offset.azimuth) % 360) + 360) % 360, pitch: state.pitch / RAD + offset.pitch };
241
246
  }
242
- const a = (state.heading + offset.azimuth) * RAD;
247
+ // the eyes stand behind the rover along the heading turned by the drag (`orbit`: they go round it) or along the heading alone (`head`: they keep their place and only the look turns)
248
+ const a = (state.heading + (drag === "head" ? 0 : offset.azimuth)) * RAD;
243
249
  const [east, north] = [state.east - Math.sin(a) * 6.5, state.north - Math.cos(a) * 6.5];
244
250
  const up = Math.max(state.y + 2.8, ground(east, north) + 1.4);
245
251
  const drop = (up - (state.y + 1.0)) / 6.5;
@@ -1,7 +1,7 @@
1
1
  import type { SharedRenderer } from "@johnmorrisdotca/kazoku-ui/renderer";
2
2
  import type { ThreeParts } from "../scene/body.ts";
3
3
  import { setThreeLoader } from "../scene/shared.ts";
4
- import { type DriveState } from "./drive.ts";
4
+ import { type DriveDrag, type DriveState } from "./drive.ts";
5
5
  import { type HeightLevel, type TerrainManifest } from "./height.ts";
6
6
  import { type TerrainProgress } from "./load.ts";
7
7
  import { type SurfaceScene } from "./scene.ts";
@@ -102,6 +102,20 @@ export declare class WakuseiSurface extends HTMLElement {
102
102
  /** Which view of the rover: `chase` (from behind and above, the default) or `seat` (the commander's). The key C turns from one to the other. */
103
103
  get driveCamera(): "chase" | "seat";
104
104
  set driveCamera(value: "chase" | "seat");
105
+ /**
106
+ * What a drag does in the chase view: `head` (the default; the attribute `drive-drag`) turns the view where the camera is, like turning your head, so the camera keeps its place 6.5 m behind the rover and the rover leaves
107
+ * the picture as you look away and comes back when you look back; `orbit` takes the camera round the rover as it looks about, as the chase view did up to 0.55. The seat view turns in place either way. Set while driving it
108
+ * moves the camera at once and keeps the drive.
109
+ *
110
+ * @example
111
+ * ```ts
112
+ * const surface = document.querySelector("wakusei-surface") as HTMLElement & { driveDrag: "head" | "orbit" };
113
+ * surface.driveDrag = "orbit";
114
+ * console.log(surface.driveDrag);
115
+ * ```
116
+ */
117
+ get driveDrag(): DriveDrag;
118
+ set driveDrag(value: DriveDrag);
105
119
  /** The rover being driven, or `undefined` when it is not. */
106
120
  get vehicle(): DriveState | undefined;
107
121
  /**
@@ -66,7 +66,7 @@ const num = (value, fallback) => {
66
66
  * ```
67
67
  */
68
68
  export class WakuseiSurface extends HTMLElement {
69
- static observedAttributes = ["quality", "shadows", "detail", "exposure", "site", "time", "heading", "pitch", "fov", "tiles", "lang", "body", "standby", "scale", "stick", "objects", "drive", "drive-camera", "watch", "watch-from", "watch-camera"];
69
+ static observedAttributes = ["quality", "shadows", "detail", "exposure", "site", "time", "heading", "pitch", "fov", "tiles", "lang", "body", "standby", "scale", "stick", "objects", "drive", "drive-camera", "drive-drag", "watch", "watch-from", "watch-camera"];
70
70
  #root;
71
71
  #stage;
72
72
  #noteEl;
@@ -259,6 +259,13 @@ export class WakuseiSurface extends HTMLElement {
259
259
  this.driveCamera = this.getAttribute("drive-camera") === "seat" ? "seat" : "chase";
260
260
  return;
261
261
  }
262
+ if (name === "drive-drag") {
263
+ if (this.#vehicle !== undefined) {
264
+ this.#syncDriveCamera();
265
+ this.#changed();
266
+ }
267
+ return;
268
+ }
262
269
  if (name === "stick") {
263
270
  this.#showStick();
264
271
  return;
@@ -507,6 +514,24 @@ export class WakuseiSurface extends HTMLElement {
507
514
  this.#syncDriveCamera();
508
515
  this.#changed();
509
516
  }
517
+ /**
518
+ * What a drag does in the chase view: `head` (the default; the attribute `drive-drag`) turns the view where the camera is, like turning your head, so the camera keeps its place 6.5 m behind the rover and the rover leaves
519
+ * the picture as you look away and comes back when you look back; `orbit` takes the camera round the rover as it looks about, as the chase view did up to 0.55. The seat view turns in place either way. Set while driving it
520
+ * moves the camera at once and keeps the drive.
521
+ *
522
+ * @example
523
+ * ```ts
524
+ * const surface = document.querySelector("wakusei-surface") as HTMLElement & { driveDrag: "head" | "orbit" };
525
+ * surface.driveDrag = "orbit";
526
+ * console.log(surface.driveDrag);
527
+ * ```
528
+ */
529
+ get driveDrag() {
530
+ return this.getAttribute("drive-drag") === "orbit" ? "orbit" : "head";
531
+ }
532
+ set driveDrag(value) {
533
+ this.setAttribute("drive-drag", value === "orbit" ? "orbit" : "head");
534
+ }
510
535
  /** The rover being driven, or `undefined` when it is not. */
511
536
  get vehicle() {
512
537
  return this.#vehicle === undefined ? undefined : { ...this.#vehicle, spin: [...this.#vehicle.spin] };
@@ -545,7 +570,7 @@ export class WakuseiSurface extends HTMLElement {
545
570
  const v = this.#vehicle;
546
571
  if (v === undefined)
547
572
  return;
548
- const view = driveView(v, this.#driveCamera, this.#camOffset, (e, n) => this.#groundAt(e, n));
573
+ const view = driveView(v, this.#driveCamera, this.#camOffset, (e, n) => this.#groundAt(e, n), this.driveDrag);
549
574
  this.#at = { east: view.eye.east, north: view.eye.north };
550
575
  this.#driveEyeUp = view.eye.up;
551
576
  this.#look = clampLook({ azimuth: view.azimuth, pitch: view.pitch, fov: this.#look.fov });
@@ -7,7 +7,7 @@ export type { SurfaceState } from "./element.ts";
7
7
  export { ascentParts, landerParts, roverParts } from "./shapes.ts";
8
8
  export { calloutsBy, CALLOUTS, clockText, LANDING, landingAt, type Callout, type LandingPose } from "./landing.ts";
9
9
  export { DustField } from "./dust.ts";
10
- export { driveParts, driveStep, driveView, startDrive, ROVER, type DriveInput, type DriveState, type DriveView } from "./drive.ts";
10
+ export { driveParts, driveStep, driveView, startDrive, ROVER, type DriveDrag, type DriveInput, type DriveState, type DriveView } from "./drive.ts";
11
11
  export { keepAway, objectParts, objectsOf, partShadows, traverseOf, KEEP_OUT, type Part, type SiteObject } from "./objects.ts";
12
12
  export { craterIn, craterProfile, madeGround, setKeepOut, rimBoost, rocksAround, standingPlace, type Crater, type Rock } from "./detail.ts";
13
13
  export { rockMesh, type RockMesh } from "./rocks.ts";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/wakusei-planets",
3
- "version": "0.54.0",
3
+ "version": "0.55.0",
4
4
  "description": "Wakusei Planets 惑星 — The Sun, planets and moons for the web: each as a clean line drawing, a shaded realistic drawing or a painted surface map, with real sizes, axial tilt and rotation as data, and positions of the moons for any date and time. SVG for the lines, three.js (optional) for the rest.",
5
5
  "keywords": [
6
6
  "planets",