I need to convert in-game coordinates from the game Euro Truck Simulator 2 to real-world geographic coordinates. The game uses a coordinate system in meters with a scale of 1:19 compared to the real world, where coordinates 0 0 is approximately in originRealCoords
lat and lng. Game provides a map of whole Europe.
The problem is that the longitude and latitude become less and less accurate as the point moves farther away from x: 0, z: 0. Maybe there is another way to convert in-game coordinates into real-life coordinates?
function convertGameCoordsToLatLong(gameCoords, originLat, originLong, originGameCoords, scale) {
// Convert game coordinates to real-world meters
const realX = (gameCoords.x - originGameCoords.x) / scale;
const realZ = (gameCoords.z - originGameCoords.z) / scale;
// Distances in degrees on Earth (approximately)
const metersPerDegreeLat = 111320; // Average distance in meters per 1 degree of latitude
const metersPerDegreeLong = 111320 * Math.cos(originLat * (Math.PI / 180));
// Convert distance to degrees
const deltaLat = realZ / metersPerDegreeLat;
const deltaLong = realX / metersPerDegreeLong;
// Calculate new coordinates
const newLat = originLat - deltaLat;
const newLong = originLong + deltaLong;
return [newLat, newLong];
}
async function getCoords() {
try {
const res = await fetch("http://192.168.0.106:25555/api/ets2/telemetry");
const json = await res.json();
return { x: json.truck.placement.x, z: json.truck.placement.z };
}
catch (error) {
console.error(error.message);
return { x: NaN, z: NaN };
}
}
async function convertCoords() {
// Initial data
const originGameCoords = { x: -5.121521, z: -14.6748238 };
const originRealCoords = { lat: 50.746475, lng: 10.508655 };
const scale = 1 / 19; // Ingame map scale
// New game coordinates
const gameCoords = await getCoords();
// Convert coordinates
return convertGameCoordsToLatLong(
gameCoords,
originRealCoords.lat,
originRealCoords.lng,
originGameCoords,
scale
);
}
convertCoords();