From 12e2a5370252147ca6fc8a7fda373ddab1c9895a Mon Sep 17 00:00:00 2001 From: HikeMap User Date: Sat, 10 Jan 2026 21:07:56 -0600 Subject: [PATCH] Make WASD movement relative to map rotation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - W now moves toward top of screen (map bearing direction) - S moves toward bottom of screen - A/D move left/right relative to current view - Works for both on-screen buttons and keyboard WASD - Uses destinationPoint() for accurate geographic movement 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- index.html | 55 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/index.html b/index.html index aff01fe..09542db 100644 --- a/index.html +++ b/index.html @@ -6826,21 +6826,30 @@ updateStatus('Test mode enabled via keyboard', 'info'); } - // Move in the specified direction + // Move in the specified direction, accounting for map rotation + const mapBearing = map.getBearing(); + let moveBearing; switch (key) { case 'w': - testPosition.lat += GPS_TEST_STEP; + moveBearing = mapBearing; // Forward + break; + case 'd': + moveBearing = mapBearing + 90; // Right break; case 's': - testPosition.lat -= GPS_TEST_STEP; + moveBearing = mapBearing + 180; // Backward break; case 'a': - testPosition.lng -= GPS_TEST_STEP; - break; - case 'd': - testPosition.lng += GPS_TEST_STEP; + moveBearing = mapBearing + 270; // Left break; } + moveBearing = ((moveBearing % 360) + 360) % 360; + + const stepMeters = GPS_TEST_STEP * 111000; + const currentLngLat = [testPosition.lng, testPosition.lat]; + const newLngLat = destinationPoint(currentLngLat, stepMeters, moveBearing); + testPosition.lat = newLngLat[1]; + testPosition.lng = newLngLat[0]; e.preventDefault(); simulateGpsPosition(); @@ -6900,22 +6909,40 @@ updateStatus('Test mode enabled via controls', 'info'); } - // Move in the specified direction + // Move in the specified direction, accounting for map rotation + // Get current map bearing (0 = north up, 90 = east up, etc.) + const mapBearing = map.getBearing(); + + // Calculate movement bearing based on key pressed + // W = forward (map bearing), D = right (+90), S = back (+180), A = left (+270) + let moveBearing; switch (dir) { case 'w': - testPosition.lat += GPS_TEST_STEP; + moveBearing = mapBearing; // Forward (top of screen) + break; + case 'd': + moveBearing = mapBearing + 90; // Right break; case 's': - testPosition.lat -= GPS_TEST_STEP; + moveBearing = mapBearing + 180; // Backward (bottom of screen) break; case 'a': - testPosition.lng -= GPS_TEST_STEP; - break; - case 'd': - testPosition.lng += GPS_TEST_STEP; + moveBearing = mapBearing + 270; // Left break; } + // Normalize bearing to 0-360 + moveBearing = ((moveBearing % 360) + 360) % 360; + + // Use destinationPoint to calculate new position + // GPS_TEST_STEP is in degrees, convert to approximate meters (1 degree ≈ 111000m) + const stepMeters = GPS_TEST_STEP * 111000; + const currentLngLat = [testPosition.lng, testPosition.lat]; + const newLngLat = destinationPoint(currentLngLat, stepMeters, moveBearing); + + testPosition.lat = newLngLat[1]; + testPosition.lng = newLngLat[0]; + simulateGpsPosition(); };