我需要将游戏 Euro Truck Simulator 2 中的游戏内坐标转换为现实世界的地理坐标。游戏使用以米为单位的坐标系,比例为 1:19(与真实坐标系相比)
我需要将游戏 Euro Truck Simulator 2 中的游戏内坐标转换为现实世界的地理坐标。游戏使用以米为单位的坐标系,比例为 1:19(与现实世界相比),其中坐标 0 0 大约为 originRealCoords
纬度和经度。
问题是,随着点离 x:0、z:0 越来越远,经度和纬度变得越来越不准确。也许还有其他方法可以将游戏内坐标转换为现实生活中的坐标?
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
const gameCoords = await getCoords();
return convertGameCoordsToLatLong(
gameCoords,
originRealCoords.lat,
originRealCoords.lng,
originGameCoords,
scale
);
}
convertCoords();
使用 originLat 计算经度会得到地球曲率的倾斜表示。
function convertGameCoordsToLatLong(gameCoords, originLat, originLong, originGameCoords, scale) {
const metersPerDegreeLat = 111320;
const deltaRealZ = (gameCoords.z - originGameCoords.z) / scale;
const deltaRealX = (gameCoords.x - originGameCoords.x) / scale;
const newLat = originLat + deltaRealZ / metersPerDegreeLat;
const metersPerDegreeLong = metersPerDegreeLat * Math.cos(newLat * (Math.PI / 180));
const deltaLong = deltaRealX / metersPerDegreeLong;
const newLong = originLong + deltaLong;
return [newLat, newLong];
};