feat(routing): increase tolerance for off-route

This commit is contained in:
2025-10-02 21:41:27 +02:00
parent 920681f68c
commit 6f0fbac13e

View File

@ -178,7 +178,7 @@ async function tickRoute() {
} }
// Check if the user is on the route // Check if the user is on the route
if (!isOnShape(loc, polyline)) { if (!isOnShape(loc, polyline, 30)) {
console.log("Off route!"); console.log("Off route!");
if (!info.isOffRoute) { if (!info.isOffRoute) {
say(m["routing.off-route"]()); say(m["routing.off-route"]());
@ -303,9 +303,10 @@ function isOnLine(
location: WorldLocation, location: WorldLocation,
from: WorldLocation, from: WorldLocation,
to: WorldLocation, to: WorldLocation,
toleranceMeters = 12,
) { ) {
// Convert the 12-meter tolerance to degrees (approximation) // Convert the tolerance to degrees (approximation)
const tolerance = 12 / 111320; // 1 degree latitude ≈ 111.32 km const tolerance = toleranceMeters / 111320; // 1 degree latitude ≈ 111.32 km
// Calculate the vector components // Calculate the vector components
const dx = to.lon - from.lon; const dx = to.lon - from.lon;
@ -347,10 +348,9 @@ function isOnPoint(location: WorldLocation, point: WorldLocation) {
return distance <= tolerance; return distance <= tolerance;
} }
function isOnShape(location: WorldLocation, shape: WorldLocation[]) { function isOnShape(location: WorldLocation, shape: WorldLocation[], toleranceMeters = 12) {
// Check if the location is on the line between from and to (3 meter tolerance)
for (let i = 0; i < shape.length - 1; i++) { for (let i = 0; i < shape.length - 1; i++) {
if (isOnLine(location, shape[i], shape[i + 1])) { if (isOnLine(location, shape[i], shape[i + 1], toleranceMeters)) {
return true; return true;
} }
} }