bpHigh commited on
Commit
33fc83e
·
verified ·
1 Parent(s): a003fc6

Keep trains straight at the terminals

Browse files
openenv/rail_openenv/web/twin/geo.js CHANGED
@@ -11,6 +11,7 @@
11
  export const CANONICAL = { E: "E-8-5a", W: "W-5a-8" };
12
  const RAMP_SWITCH = 0.03; // km: zones bounded by a switch, where the two tracks meet
13
  const RAMP_CROSS = 0.25; // km: zones that end on double track
 
14
 
15
  export function smoothstep(u) {
16
  const x = Math.min(1, Math.max(0, u));
@@ -65,12 +66,21 @@ export class Route {
65
  return kl + (s - sl) / 1000 * (this.dir === "E" ? 1 : -1);
66
  }
67
 
68
- // [x, y] at distance s (clamped to the route)
 
 
69
  pointAtS(s) {
70
  const S = this.s;
71
  const n = S.length;
72
- if (s <= 0) return [this.x[0], this.y[0]];
73
- if (s >= this.length) return [this.x[n - 1], this.y[n - 1]];
 
 
 
 
 
 
 
74
  let lo = 0, hi = n - 1;
75
  while (hi - lo > 1) {
76
  const mid = (lo + hi) >> 1;
 
11
  export const CANONICAL = { E: "E-8-5a", W: "W-5a-8" };
12
  const RAMP_SWITCH = 0.03; // km: zones bounded by a switch, where the two tracks meet
13
  const RAMP_CROSS = 0.25; // km: zones that end on double track
14
+ const END_RUN = 20; // m: the stretch whose direction a route keeps past its ends
15
 
16
  export function smoothstep(u) {
17
  const x = Math.min(1, Math.max(0, u));
 
66
  return kl + (s - sl) / 1000 * (this.dir === "E" ? 1 : -1);
67
  }
68
 
69
+ // [x, y] at distance s. Routes end at the terminals' stop points, so a train standing there reaches past an end by
70
+ // half its length: past either end the track goes on straight, along its last END_RUN metres. (Clamping folded
71
+ // those cars back onto the end point.)
72
  pointAtS(s) {
73
  const S = this.s;
74
  const n = S.length;
75
+ if (s < 0 || s > this.length) {
76
+ const atEnd = s > this.length;
77
+ const [ex, ey] = atEnd ? [this.x[n - 1], this.y[n - 1]] : [this.x[0], this.y[0]];
78
+ const [bx, by] = this.pointAtS(atEnd ? Math.max(0, this.length - END_RUN) : Math.min(this.length, END_RUN));
79
+ const L = Math.hypot(ex - bx, ey - by);
80
+ if (L < 1e-6) return [ex, ey];
81
+ const d = atEnd ? s - this.length : -s;
82
+ return [ex + (ex - bx) / L * d, ey + (ey - by) / L * d];
83
+ }
84
  let lo = 0, hi = n - 1;
85
  while (hi - lo > 1) {
86
  const mid = (lo + hi) >> 1;