Files
uconsole_asw_game/data/commander/js/phone/orientation.js
T
Claude ca262e3420 Phone lookout page: pair by code, gyroscope, swipe, voice and tap calls
The /lookout page pairs a phone onto the frigate's lookout or the boat's
periscope, trains the eyepiece with the gyroscope or a swipe, and calls
sightings by voice (Web Speech API) or by tapping them. F9 shows the phone
QR code for the HTTPS address. Calls of sightings already on the bridge
confirm as repeated reports.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018A9rdwgbRHDXhkiwVoADq7
2026-09-28 19:22:09 +00:00

21 lines
1.2 KiB
JavaScript

// The phone's line of sight from DeviceOrientation (W3C: R = Rz(alpha) Rx(beta)
// Ry(gamma)). The camera looks out of the back of the phone, so the line of
// sight is the device's -z axis; heading 0 is the device frame's "north"
// (compass on iOS, arbitrary on many Android browsers) and the view calibrates
// it against the bow. Display only: nothing here is sent but the trained bearing.
const RAD = Math.PI / 180;
export const wrap360 = (value) => ((value % 360) + 360) % 360;
export const wrap180 = (value) => wrap360(value + 180) - 180;
// ``{heading, elevation}`` in degrees for the back camera, or null.
export function lineOfSight(alpha, beta, gamma) {
if (![alpha, beta, gamma].every(Number.isFinite)) return null;
const a = alpha * RAD, b = beta * RAD, g = gamma * RAD;
const east = -Math.cos(a) * Math.sin(g) - Math.sin(a) * Math.sin(b) * Math.cos(g);
const north = -Math.sin(a) * Math.sin(g) + Math.cos(a) * Math.sin(b) * Math.cos(g);
const up = -Math.cos(b) * Math.cos(g);
if (Math.hypot(east, north) < 1e-6) return null; // looking straight up or down
return {heading: wrap360(Math.atan2(east, north) / RAD), elevation: Math.asin(Math.max(-1, Math.min(1, up))) / RAD};
}