Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions app/components/map/layers/MapAircraftList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ let init = false;

const visibleSet = useThrottleFn(setVisiblePilots, 1000);

const debouncedUpdate = useThrottleFn(() => {

useRafFn(() => {
if (!canRender.value) {
vectorSource.clear();
linesSource.clear();
Expand All @@ -320,15 +321,13 @@ const debouncedUpdate = useThrottleFn(() => {
tracks: showTracks.value,
});
}
}, 1000, true);
});
Comment on lines 309 to +324
Copy link

Copilot AI Mar 12, 2026

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 useRafFn causes setMapAircraft to run every animation frame even when nothing changes. setMapAircraft iterates 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.

Copilot uses AI. Check for mistakes.

useUpdateCallback(['mandatory', 'short', 'extent', updateRelatedSettings], () => {
if (!init) return;
visibleSet();
});

watch([getShownPilots, canRender, showTracks, renderedPilots], debouncedUpdate);

watch(map, val => {
if (!val) return;

Expand Down
22 changes: 20 additions & 2 deletions app/composables/render/aircraft/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the self-flight branch, the coordinate source is selfCoordinate.value.coordinate (local websocket updates), but the extrapolation timestamp comes from aircraft.last_updated (VATSIM feed). If these clocks/updates are out of sync, the delta time can be wrong (often much larger), causing the self aircraft to be extrapolated far away from its actual position. Consider either not extrapolating when selfCoordinate is present, or using selfCoordinate.value.date as the reference time for extrapolation (and only using VATSIM last_updated for non-self aircraft).

Suggested change
? 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 uses AI. Check for mistakes.
: extrapolateCoordinates([aircraft.longitude, aircraft.latitude], aircraft.heading, aircraft.groundspeed, aircraft.last_updated);

const heading = (isSelfFlight && dataStore.vatsim.selfCoordinate.value)
? dataStore.vatsim.selfCoordinate.value.heading
Expand Down Expand Up @@ -211,3 +211,21 @@ export async function setMapAircraft(settings: {
}
}
}

function extrapolateCoordinates(coordinates: Coordinate, heading: number, groundspeed: number, last_updated: string): Coordinate {
const lastUpdateTime = new Date(last_updated).getTime();
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));
Comment on lines +217 to +228
Copy link

Copilot AI Mar 12, 2026

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).

Suggested change
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;

Copilot uses AI. Check for mistakes.

return [lon + deltaLon, lat + deltaLat];
}
5 changes: 4 additions & 1 deletion app/composables/render/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,13 @@ export function setVatsimMandatoryData(mandatoryData: VatsimMandatoryData) {
if (hasActivePilotFilter()) mandatoryData.pilots = mandatoryData.pilots.filter(x => vatsim.data.pilots.value.some(y => y.cid === x[0]));

vatsim.mandatoryData.value = {
pilots: mandatoryData.pilots.map(([cid, lon, lat, icon, heading]) => {
pilots: mandatoryData.pilots.map(([cid, lon, lat, icon, heading, groundspeed, last_updated]) => {
const cidString = cid.toString();
if (data.keyedPilots.value?.[cidString]) {
data.keyedPilots.value[cidString].longitude = lon;
data.keyedPilots.value[cidString].latitude = lat;
data.keyedPilots.value[cidString].heading = heading;
data.keyedPilots.value[cidString].groundspeed = groundspeed;
}

return {
Expand All @@ -307,6 +308,8 @@ export function setVatsimMandatoryData(mandatoryData: VatsimMandatoryData) {
latitude: lat,
icon,
heading,
groundspeed,
last_updated,
};
}),
controllers: mandatoryData.controllers.map(([cid, callsign, frequency, facility]) => ({
Expand Down
4 changes: 2 additions & 2 deletions app/types/data/vatsim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,13 @@ export type VatsimMandatoryData = {
timestamp: string;
timestampNum: number;
serverTime: number;
pilots: [cid: VatsimPilot['cid'], longitude: VatsimPilot['longitude'], latitude: VatsimPilot['latitude'], icon: AircraftIcon, heading: number][];
pilots: [cid: VatsimPilot['cid'], longitude: VatsimPilot['longitude'], latitude: VatsimPilot['latitude'], icon: AircraftIcon, heading: number, groundspeed: number, last_updated: string][];
controllers: [VatsimController['cid'], VatsimController['callsign'], VatsimController['frequency'], VatsimController['facility']][];
atis: VatsimMandatoryData['controllers'];
};

export type VatsimMandatoryConvertedData = {
pilots: Required<Pick<VatsimPilot, 'cid' | 'longitude' | 'latitude' | 'icon' | 'heading'>>[];
pilots: Required<Pick<VatsimPilot, 'cid' | 'longitude' | 'latitude' | 'icon' | 'heading' | 'groundspeed' | 'last_updated'>>[];
controllers: Pick<VatsimController, 'cid' | 'callsign' | 'frequency' | 'facility'>[];
atis: VatsimMandatoryConvertedData['controllers'];
};
Expand Down
2 changes: 1 addition & 1 deletion app/utils/server/vatsim/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function updateVatsimMandatoryDataStorage() {

for (const pilot of data.pilots) {
const coords = [pilot.longitude, pilot.latitude];
newData.pilots.push([pilot.cid, coords[0], coords[1], getAircraftIcon(pilot).icon, pilot.heading]);
newData.pilots.push([pilot.cid, coords[0], coords[1], getAircraftIcon(pilot).icon, pilot.heading, pilot.groundspeed, pilot.last_updated]);
}

// Maybe no need to implement
Expand Down
Loading