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

+59 -1
View File
@@ -1,7 +1,8 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import * as Cesium from 'cesium';
import { keptViewFrom } from './trackedCamera.js';
import { followViewFrom, keptViewFrom } from './trackedCamera.js';
import { followFramingEye } from '../render/camera/followTransition.js';
import {
distance,
eastNorthUp,
@@ -74,3 +75,60 @@ test('a kept view needs an eye apart from the target', () => {
null,
);
});
/** Heading, radians clockwise from north, of a framing eye's up in its frame. */
const framedHeading = (offset) => {
const { up } = followFramingEye(offset);
return Math.atan2(up.x, up.y);
};
test("a followed view from straight down keeps the camera's heading, not the target's bearing", () => {
// The camera 60 km up, straight down, heading north; the target 40 km to
// the west-north-west of the point under it.
const under = fromDegrees(-122.38, 37.62, 0);
const eye = framePoint(eastNorthUp(under), { x: 0, y: 0, z: 60_000 });
const direction = normalize(
frameVector(eastNorthUp(under), { x: 0, y: 0, z: -1 }),
);
const at = framePoint(eastNorthUp(under), { x: -37_000, y: 15_000, z: 0 });
const pose = { position: eye, direction, heading: 0 };
// Kept as it is, the view leans only as the Earth curves, towards the eye:
// the framing would face the target's bearing.
const kept = keptViewFrom(pose, at);
assert.ok(Math.abs(framedHeading(kept)) > 1, 'kept: turned');
const followed = followViewFrom(pose, at);
assert.ok(Math.abs(framedHeading(followed)) < 1e-9, 'followed: still north');
assert.ok(
Math.abs(magnitude(followed) - magnitude(kept)) < 1e-6,
'its range',
);
for (const heading of [Math.PI / 2, 2.5]) {
const turned = followViewFrom({ ...pose, heading }, at);
const h = framedHeading(turned);
assert.ok(
Math.abs(Math.atan2(Math.sin(h - heading), Math.cos(h - heading))) < 1e-9,
`heading ${heading} kept, not ${h}`,
);
}
});
test("a followed view keeps its range and its tilt to the target's horizon", () => {
const at = fromDegrees(-122.2, 37.7, 600);
const enu = eastNorthUp(at);
const eye = framePoint(enu, { x: 300, y: -1600, z: 1100 });
const direction = normalize(frameVector(enu, { x: -250, y: 1600, z: -1150 }));
const kept = keptViewFrom({ position: eye, direction }, at);
const followed = followViewFrom(
{ position: eye, direction, heading: 0.1 },
at,
);
assert.ok(Math.abs(magnitude(followed) - magnitude(kept)) < 1e-6);
assert.ok(Math.abs(followed.z - kept.z) < 1e-6, 'the same tilt');
assert.ok(
Math.abs(framedHeading(followed) - 0.1) < 1e-9,
"the pose's heading",
);
// A pose with no heading keeps the view as it is.
assert.deepEqual(followViewFrom({ position: eye, direction }, at), kept);
assert.equal(followViewFrom(null, at), null);
});