-
-
Notifications
You must be signed in to change notification settings - Fork 85
feat: Extrapolate aircraft positions based on last known location, heading and groundspeed #1466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -140,8 +140,8 @@ export async function setMapAircraft(settings: { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const coordinates = (isSelfFlight && dataStore.vatsim.selfCoordinate.value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? dataStore.vatsim.selfCoordinate.value.coordinate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : [aircraft.longitude, aircraft.latitude]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? extrapolateCoordinates(dataStore.vatsim.selfCoordinate.value.coordinate, aircraft.heading, aircraft.groundspeed, aircraft.last_updated) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? extrapolateCoordinates(dataStore.vatsim.selfCoordinate.value.coordinate, aircraft.heading, aircraft.groundspeed, aircraft.last_updated) | |
| ? extrapolateCoordinates( | |
| dataStore.vatsim.selfCoordinate.value.coordinate, | |
| aircraft.heading, | |
| aircraft.groundspeed, | |
| dataStore.vatsim.selfCoordinate.value.date, | |
| ) |
Copilot
AI
Mar 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extrapolateCoordinates doesn’t guard against invalid/edge inputs: new Date(last_updated).getTime() can be NaN (bad/empty timestamp), deltaTimeHours can be negative (clock skew / future timestamps) or very large (stale data), and Math.cos(degreesToRadians(lat)) can approach 0 near the poles, producing Infinity/NaN longitudes. This can propagate NaN coordinates into OpenLayers and break rendering. Suggest validating the parsed timestamp, clamping deltaTimeHours to a reasonable range (and to >= 0), and protecting against near-zero longitude scaling (or skipping extrapolation when |lat| is too high).
| const currentTime = Date.now(); | |
| const deltaTimeHours = (currentTime - lastUpdateTime) / (1000 * 60 * 60); | |
| const distanceNauticalMiles = groundspeed * deltaTimeHours; | |
| const distanceDegrees = distanceNauticalMiles / 60; | |
| const headingRadians = degreesToRadians(heading); | |
| const [lon, lat] = coordinates; | |
| const deltaLat = distanceDegrees * Math.cos(headingRadians); | |
| const deltaLon = distanceDegrees * Math.sin(headingRadians) / Math.cos(degreesToRadians(lat)); | |
| // If the timestamp is invalid, avoid extrapolation. | |
| if (!Number.isFinite(lastUpdateTime)) { | |
| return coordinates; | |
| } | |
| const currentTime = Date.now(); | |
| let deltaTimeHours = (currentTime - lastUpdateTime) / (1000 * 60 * 60); | |
| // Guard against negative, zero, or non-finite time differences. | |
| if (!Number.isFinite(deltaTimeHours) || deltaTimeHours <= 0) { | |
| return coordinates; | |
| } | |
| // Clamp extrapolation horizon to avoid huge jumps for stale data. | |
| const MAX_EXTRAPOLATION_HOURS = 2; | |
| if (deltaTimeHours > MAX_EXTRAPOLATION_HOURS) { | |
| deltaTimeHours = MAX_EXTRAPOLATION_HOURS; | |
| } | |
| // Guard against non-finite groundspeed/heading. | |
| if (!Number.isFinite(groundspeed) || !Number.isFinite(heading)) { | |
| return coordinates; | |
| } | |
| const distanceNauticalMiles = groundspeed * deltaTimeHours; | |
| const distanceDegrees = distanceNauticalMiles / 60; | |
| const headingRadians = degreesToRadians(heading); | |
| const [lon, lat] = coordinates; | |
| const latRadians = degreesToRadians(lat); | |
| const cosLat = Math.cos(latRadians); | |
| // Avoid division by values close to zero near the poles. | |
| const MIN_COS_LAT = 1e-6; | |
| if (!Number.isFinite(cosLat) || Math.abs(cosLat) < MIN_COS_LAT) { | |
| return coordinates; | |
| } | |
| const deltaLat = distanceDegrees * Math.cos(headingRadians); | |
| const deltaLon = distanceDegrees * Math.sin(headingRadians) / cosLat; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switching from a throttled/watch-driven update to an unconditional
useRafFncausessetMapAircraftto run every animation frame even when nothing changes.setMapAircraftiterates all shown pilots and mutates OpenLayers features, so this is very likely to become a major CPU/battery regression on large traffic sets. If the goal is smoother motion for extrapolation, consider updating at a capped FPS / fixed interval (e.g., 5–10 Hz), only updating geometry coordinates (not full feature props/tracks) on RAF, or keeping the throttled update and adding a lightweight RAF-only position update path.