feat: migrate simulation to physically-based unit system
Replace arbitrary game units with real-world measurements throughout the simulation and viewer: planet orbits in AU, sizes in km, galaxy positions in light-years. Add SimulationUnits helpers for conversions, separate WarpSpeed from FtlSpeed for ships, fix FTL transit progress to use galaxy-space distances, overhaul Lagrange point placement with Hill sphere approximation, and update the viewer to scale and format all distances correctly. Ships in FTL transit now render in galaxy view. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
import * as THREE from "three";
|
||||
import { getSelectionGroup } from "./viewerSelection";
|
||||
import type { Selectable, SelectionGroup, WorldState } from "./viewerTypes";
|
||||
import { describeHoverLabel, getSelectionGroup } from "./viewerSelection";
|
||||
import { ACTIVE_SYSTEM_DETAIL_SCALE } from "./viewerConstants";
|
||||
import { DISPLAY_UNITS_PER_KILOMETER, DISPLAY_UNITS_PER_LIGHT_YEAR, KILOMETERS_PER_AU, formatAdaptiveDistanceFromKilometers, formatSystemDistance } from "./viewerMath";
|
||||
import type { Selectable, SelectionGroup, WorldState, ZoomLevel } from "./viewerTypes";
|
||||
|
||||
export interface HoverPickResult {
|
||||
selection: Selectable;
|
||||
object: THREE.Object3D;
|
||||
}
|
||||
|
||||
export function pickSelectableAtClientPosition(
|
||||
renderer: THREE.WebGLRenderer,
|
||||
@@ -11,29 +18,49 @@ export function pickSelectableAtClientPosition(
|
||||
clientX: number,
|
||||
clientY: number,
|
||||
) {
|
||||
const hit = pickSelectableHitAtClientPosition(renderer, raycaster, mouse, camera, selectableTargets, clientX, clientY);
|
||||
return hit?.selection;
|
||||
}
|
||||
|
||||
export function pickSelectableHitAtClientPosition(
|
||||
renderer: THREE.WebGLRenderer,
|
||||
raycaster: THREE.Raycaster,
|
||||
mouse: THREE.Vector2,
|
||||
camera: THREE.Camera,
|
||||
selectableTargets: Map<THREE.Object3D, Selectable>,
|
||||
clientX: number,
|
||||
clientY: number,
|
||||
): HoverPickResult | undefined {
|
||||
const bounds = renderer.domElement.getBoundingClientRect();
|
||||
mouse.x = ((clientX - bounds.left) / bounds.width) * 2 - 1;
|
||||
mouse.y = -(((clientY - bounds.top) / bounds.height) * 2 - 1);
|
||||
raycaster.setFromCamera(mouse, camera);
|
||||
const hit = raycaster.intersectObjects([...selectableTargets.keys()], false)[0];
|
||||
return hit ? selectableTargets.get(hit.object) : undefined;
|
||||
const selection = hit ? selectableTargets.get(hit.object) : undefined;
|
||||
return hit && selection
|
||||
? { selection, object: hit.object }
|
||||
: undefined;
|
||||
}
|
||||
|
||||
export function updateHoverLabel(params: {
|
||||
dragMode?: string;
|
||||
hoverLabelEl: HTMLDivElement;
|
||||
selection: Selectable | undefined;
|
||||
hoverPick: HoverPickResult | undefined;
|
||||
activeSystemId?: string;
|
||||
zoomLevel: ZoomLevel;
|
||||
world?: WorldState;
|
||||
point: THREE.Vector2;
|
||||
camera: THREE.Camera;
|
||||
}) {
|
||||
const {
|
||||
dragMode,
|
||||
hoverLabelEl,
|
||||
selection,
|
||||
hoverPick,
|
||||
activeSystemId,
|
||||
zoomLevel,
|
||||
world,
|
||||
point,
|
||||
camera,
|
||||
} = params;
|
||||
|
||||
if (dragMode) {
|
||||
@@ -41,23 +68,60 @@ export function updateHoverLabel(params: {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!selection || selection.kind !== "system" || selection.id === activeSystemId) {
|
||||
if (!hoverPick) {
|
||||
hoverLabelEl.hidden = true;
|
||||
return;
|
||||
}
|
||||
|
||||
const system = world?.systems.get(selection.id);
|
||||
if (!system) {
|
||||
const { selection, object } = hoverPick;
|
||||
const label = describeHoverLabel(world, selection);
|
||||
if (!label) {
|
||||
hoverLabelEl.hidden = true;
|
||||
return;
|
||||
}
|
||||
|
||||
const distance = formatHoverDistance(camera, object, selection, zoomLevel, activeSystemId);
|
||||
|
||||
hoverLabelEl.hidden = false;
|
||||
hoverLabelEl.textContent = system.label;
|
||||
hoverLabelEl.textContent = `${label}\n${distance}`;
|
||||
hoverLabelEl.style.left = `${point.x + 14}px`;
|
||||
hoverLabelEl.style.top = `${point.y + 14}px`;
|
||||
}
|
||||
|
||||
function formatHoverDistance(
|
||||
camera: THREE.Camera,
|
||||
object: THREE.Object3D,
|
||||
selection: Selectable,
|
||||
zoomLevel: ZoomLevel,
|
||||
activeSystemId?: string,
|
||||
) {
|
||||
const worldPosition = object.getWorldPosition(new THREE.Vector3());
|
||||
const displayDistance = camera.position.distanceTo(worldPosition);
|
||||
|
||||
if (selection.kind === "system") {
|
||||
return formatAdaptiveDistanceFromKilometers((displayDistance / DISPLAY_UNITS_PER_LIGHT_YEAR) * 9.4607e12);
|
||||
}
|
||||
|
||||
const inActiveSystem = selection.kind === "planet"
|
||||
? selection.systemId === activeSystemId
|
||||
: selection.kind === "ship"
|
||||
|| selection.kind === "station"
|
||||
|| selection.kind === "node"
|
||||
|| selection.kind === "spatial-node"
|
||||
|| selection.kind === "bubble"
|
||||
|| selection.kind === "claim"
|
||||
|| selection.kind === "construction-site";
|
||||
|
||||
if (inActiveSystem && activeSystemId) {
|
||||
const kilometers = displayDistance / (DISPLAY_UNITS_PER_KILOMETER * ACTIVE_SYSTEM_DETAIL_SCALE);
|
||||
return zoomLevel === "system"
|
||||
? formatSystemDistance(kilometers / KILOMETERS_PER_AU)
|
||||
: formatAdaptiveDistanceFromKilometers(kilometers);
|
||||
}
|
||||
|
||||
return formatAdaptiveDistanceFromKilometers(displayDistance / DISPLAY_UNITS_PER_KILOMETER);
|
||||
}
|
||||
|
||||
export function updateMarqueeBox(
|
||||
marqueeEl: HTMLDivElement,
|
||||
dragStart: THREE.Vector2,
|
||||
|
||||
Reference in New Issue
Block a user