[object Object]

← back to Ventura Bus 3d

fix: direction-aware nextStopFor — westbound bus 'Next stop' now points west

923beeb6d60f11f1714884f83ddb5048c9a56c06 · 2026-05-10 11:48:47 -0700 · Steve

Files touched

Diff

commit 923beeb6d60f11f1714884f83ddb5048c9a56c06
Author: Steve <steve@designerwallcoverings.com>
Date:   Sun May 10 11:48:47 2026 -0700

    fix: direction-aware nextStopFor — westbound bus 'Next stop' now points west
---
 public/js/main.js | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/public/js/main.js b/public/js/main.js
index 217dc72..9536d56 100644
--- a/public/js/main.js
+++ b/public/js/main.js
@@ -361,9 +361,19 @@ function syncLegendVisibility() {
 function nextStopFor(bus) {
   const x = bus.group.position.x;
   const stops = bus.route.stops;
-  // Forward direction along x: find first stop with stop.x > x; if none, wrap to first
-  let next = stops.find(s => s.x > x);
-  if (!next) next = stops[0];
+  // "Next" depends on travel direction. Buses oscillate along x, so a
+  // westbound bus's next stop is the nearest stop with smaller x.
+  // 0.5 m hysteresis avoids flapping when the bus is sitting on a stop.
+  let next;
+  if (bus.dir >= 0) {
+    next = stops.find(s => s.x > x + 0.5);
+    if (!next) next = stops[stops.length - 1];
+  } else {
+    for (let i = stops.length - 1; i >= 0; i--) {
+      if (stops[i].x < x - 0.5) { next = stops[i]; break; }
+    }
+    if (!next) next = stops[0];
+  }
   const idx = stops.indexOf(next);
   return { stop: next, idx };
 }

← e0acce7 add /streetview.html: animated Street View drive down Ventur  ·  back to Ventura Bus 3d  ·  fix(streetview): PlacesService got StreetViewPanorama (must 8f3bd9e →