Stabilize Earth module and fix satellite TLE handling
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
// utils.js - Utility functions for coordinate conversion
|
||||
|
||||
import * as THREE from 'three';
|
||||
import * as THREE from "three";
|
||||
|
||||
import { CONFIG } from './constants.js';
|
||||
import { CONFIG } from "./constants.js";
|
||||
|
||||
// Convert latitude/longitude to 3D vector
|
||||
export function latLonToVector3(lat, lon, radius = CONFIG.earthRadius) {
|
||||
@@ -18,26 +18,33 @@ export function latLonToVector3(lat, lon, radius = CONFIG.earthRadius) {
|
||||
|
||||
// Convert 3D vector to latitude/longitude
|
||||
export function vector3ToLatLon(vector) {
|
||||
const radius = Math.sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);
|
||||
const lat = 90 - (Math.acos(vector.y / radius) * 180 / Math.PI);
|
||||
|
||||
let lon = (Math.atan2(vector.z, -vector.x) * 180 / Math.PI) - 180;
|
||||
|
||||
const radius = Math.sqrt(
|
||||
vector.x * vector.x + vector.y * vector.y + vector.z * vector.z,
|
||||
);
|
||||
const lat = 90 - (Math.acos(vector.y / radius) * 180) / Math.PI;
|
||||
|
||||
let lon = (Math.atan2(vector.z, -vector.x) * 180) / Math.PI - 180;
|
||||
|
||||
while (lon <= -180) lon += 360;
|
||||
while (lon > 180) lon -= 360;
|
||||
|
||||
return {
|
||||
lat: parseFloat(lat.toFixed(4)),
|
||||
lon: parseFloat(lon.toFixed(4)),
|
||||
alt: radius - CONFIG.earthRadius
|
||||
alt: radius - CONFIG.earthRadius,
|
||||
};
|
||||
}
|
||||
|
||||
// Convert screen coordinates to Earth surface 3D coordinates
|
||||
export function screenToEarthCoords(clientX, clientY, camera, earth, domElement = document.body) {
|
||||
const raycaster = new THREE.Raycaster();
|
||||
const mouse = new THREE.Vector2();
|
||||
|
||||
export function screenToEarthCoords(
|
||||
clientX,
|
||||
clientY,
|
||||
camera,
|
||||
earth,
|
||||
domElement = document.body,
|
||||
raycaster = new THREE.Raycaster(),
|
||||
mouse = new THREE.Vector2(),
|
||||
) {
|
||||
if (domElement === document.body) {
|
||||
mouse.x = (clientX / window.innerWidth) * 2 - 1;
|
||||
mouse.y = -(clientY / window.innerHeight) * 2 + 1;
|
||||
@@ -60,17 +67,26 @@ export function screenToEarthCoords(clientX, clientY, camera, earth, domElement
|
||||
}
|
||||
|
||||
// Calculate accurate spherical distance between two points (Haversine formula)
|
||||
export function calculateDistance(lat1, lon1, lat2, lon2, radius = CONFIG.earthRadius) {
|
||||
export function calculateDistance(
|
||||
lat1,
|
||||
lon1,
|
||||
lat2,
|
||||
lon2,
|
||||
radius = CONFIG.earthRadius,
|
||||
) {
|
||||
const toRad = (angle) => (angle * Math.PI) / 180;
|
||||
|
||||
|
||||
const dLat = toRad(lat2 - lat1);
|
||||
const dLon = toRad(lon2 - lon1);
|
||||
|
||||
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
||||
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
|
||||
Math.sin(dLon / 2) * Math.sin(dLon / 2);
|
||||
|
||||
|
||||
const a =
|
||||
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
||||
Math.cos(toRad(lat1)) *
|
||||
Math.cos(toRad(lat2)) *
|
||||
Math.sin(dLon / 2) *
|
||||
Math.sin(dLon / 2);
|
||||
|
||||
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
|
||||
|
||||
return radius * c;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user