fix(selection): a click that follows a contact from straight down keeps the camera's heading

A click keeps the camera's view of the contact it follows (keptViewFrom),
but a follow frames the eye from its offset alone, its heading the way the
offset leans. Seen from straight down, the eye's view at a contact off the
centre leans only as far as the Earth curves between them, towards the eye,
so the follow turned the camera to face the contact's bearing (from north
to 298 degrees on a click 40 km out from 60 km up). followViewFrom keeps the
range and the tilt to the contact's horizon and turns them to the pose's
heading, a view straight down kept a hair off vertical so its heading holds.
Flights, Military Flights and trains follow from it; a world-focus fly-to,
which sets its orientation itself, keeps the view as it was.
This commit is contained in:
Sameh Khamis committed 2026-11-14 13:14:15 -08:00
1 parent d3cc95e9a6
commit b277b55547
7 files changed
+107 -10

No files matched your search

+39
View File
@@ -2,8 +2,10 @@ import {
distance,
eastNorthUp,
inverseFrameVector,
magnitude,
normalize,
scale,
vec,
} from '../geo/index.js';
/**
@@ -36,3 +38,40 @@ export function keptViewFrom(pose, at) {
const look = normalize(inverseFrameVector(eastNorthUp(at), pose.direction));
return scale(look, -range);
}
/**
* The least level share of a followed view: a view straight down keeps its
* heading only through a level part of its own, since a follow's framing
* turns the eye's right out of it (render/camera/followTransition.js).
*/
const MIN_LEVEL = 1e-4;
/**
* `keptViewFrom` for a follow: the same range and the same tilt to the
* target's horizon, turned to the camera's own heading. A follow frames the
* eye from its offset alone, its heading the way the offset leans. Seen from
* straight down, the eye's view leans at a target off the centre only as far
* as the Earth curves between them, and towards the eye: kept as it is, the
* camera would turn to face the target's bearing. Null as `keptViewFrom`.
* @param {{position: object, direction: object, heading?: number}|null|undefined} pose
* The camera port's pose; `heading` in radians, clockwise from north.
* @param {{x: number, y: number, z: number}|null|undefined} at The target.
* @returns {{x: number, y: number, z: number}|null}
*/
export function followViewFrom(pose, at) {
const kept = keptViewFrom(pose, at);
if (!kept || !Number.isFinite(pose.heading)) return kept;
const range = magnitude(kept);
const lookUp = -kept.z / range;
const level = Math.max(
Math.sqrt(Math.max(0, 1 - lookUp * lookUp)),
MIN_LEVEL,
);
const up = Math.sign(lookUp) * Math.sqrt(Math.max(0, 1 - level * level));
const look = vec(
level * Math.sin(pose.heading),
level * Math.cos(pose.heading),
up,
);
return scale(look, -range);
}