Prompt specification
Build or customize the Three.js 3D scene "Remote Control Under Delayed Feedback" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Remote Control Under Delayed Feedback
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/remote-control-under-delayed-feedback-x3788
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows finite light-travel time and communication delay. Create a simple fictional rover puzzle with delayed commands and observations. Keep true state and last received state separate so users can inspect why immediate correction assumptions fail. Voxel style.
Working Three.js Starter (remote-control-under-delayed-feedback-x3788):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Delay Lab — Mars Rover Light-Time Puzzle</title>
<style>
:root {
--bg: #071015;
--panel: rgba(8, 20, 27, 0.91);
--panel-soft: rgba(12, 29, 37, 0.75);
--line: #29434c;
--line-bright: #426570;
--text: #e8f3f2;
--muted: #88a3aa;
--cyan: #70f6dc;
--cyan-dim: #2e8f83;
--orange: #ffad5c;
--orange-dim: #a86734;
--red: #ff626c;
--gold: #ffd166;
--purple: #ca8cff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
"Liberation Mono", monospace;
}
body {
user-select: none;
}
button {
font: inherit;
}
[hidden] {
display: none !important;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
outline: none;
touch-action: none;
}
#worldLabels {
position: fixed;
inset: 0;
pointer-events: none;
overflow: hidden;
z-index: 2;
}
.world-label {
position: absolute;
left: 0;
top: 0;
padding: 5px 7px;
border: 1px solid currentColor;
background: rgba(4, 12, 17, 0.82);
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
white-space: nowrap;
transform: translate(-50%, -50%);
box-shadow: 0 5px 20px rgba(0, 0, 0, 0.35);
}
.world-label.orange {
color: var(--orange);
}
.world-label.gold {
color: var(--gold);
}
.world-label.red {
color: var(--red);
}
.world-label.dim {
opacity: 0.72;
}
#topbar {
position: fixed;
z-index: 10;
top: 0;
left: 0;
right: 0;
height: 72px;
display: flex;
align-items: center;
gap: 20px;
padding: 12px 18px;
background: linear-gradient(
180deg,
rgba(4, 13, 18, 0.98),
rgba(4, 13, 18, 0.82),
transparent
);
pointer-events: none;
}
#topbar > * {
pointer-events: auto;
}
.brand {
min-width: 290px;
}
.eyebrow {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 3px 0 0;
font-size: clamp(17px, 2vw, 25px);
line-height: 1;
letter-spacing: -0.04em;
}
.link-spec {
display: flex;
align-items: center;
gap: 8px;
margin-left: auto;
color: var(--muted);
font-size: 11px;
}
.link-spec strong {
color: var(--text);
}
.chip {
display: inline-flex;
align-items: center;
gap: 6px;
border: 1px solid var(--line);
padding: 6px 8px;
background: rgba(10, 26, 33, 0.8);
letter-spacing: 0.08em;
text-transform: uppercase;
font-size: 9px;
font-weight: 800;
}
.chip.cyan {
border-color: var(--cyan-dim);
color: var(--cyan);
}
.chip.orange {
border-color: var(--orange-dim);
color: var(--orange);
}
.dot {
width: 6px;
height: 6px;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
.toolbar {
display: flex;
gap: 6px;
}
.tool-button,
.mode-button {
border: 1px solid var(--line);
background: rgba(8, 21, 28, 0.92);
color: var(--muted);
padding: 8px 10px;
cursor: pointer;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.tool-button:hover,
.mode-button:hover {
border-color: var(--line-bright);
color: var(--text);
}
.tool-button.primary {
color: var(--cyan);
border-color: var(--cyan-dim);
}
.panel {
position: fixed;
z-index: 8;
width: 300px;
border: 1px solid var(--line);
background:
linear-gradient(135deg, rgba(112, 246, 220, 0.035), transparent 30%),
var(--panel);
box-shadow:
0 16px 50px rgba(0, 0, 0, 0.32),
inset 0 1px rgba(255, 255, 255, 0.025);
backdrop-filter: blur(12px);
overflow: hidden;
}
.panel::before {
content: "";
display: block;
height: 2px;
background: var(--cyan);
opacity: 0.75;
}
.panel.orange-panel::before {
background: var(--orange);
}
#earthPanel {
left: 18px;
top: 86px;
}
#truthPanel {
right: 18px;
top: 86px;
width: 278px;
}
#briefPanel {
left: 18px;
bottom: 18px;
width: 330px;
}
#logPanel {
right: 18px;
bottom: 18px;
width: 360px;
}
#timelinePanel {
left: 50%;
bottom: 18px;
transform: translateX(-50%);
width: 440px;
}
.panel-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 12px 13px 10px;
border-bottom: 1px solid var(--line);
}
.panel-title {
font-size: 11px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.panel-subtitle {
margin-top: 4px;
color: var(--muted);
font-size: 9px;
line-height: 1.4;
}
.status {
color: var(--cyan);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
.status.orange {
color: var(--orange);
}
.panel-body {
padding: 12px 13px 13px;
}
.readout-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 8px;
}
.readout {
min-height: 58px;
padding: 9px;
border: 1px solid rgba(41, 67, 76, 0.8);
background: rgba(3, 12, 17, 0.4);
}
.readout label {
display: block;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.readout strong {
display: block;
margin-top: 6px;
color: var(--text);
font-size: 15px;
line-height: 1.05;
}
.readout.cyan strong {
color: var(--cyan);
}
.readout.orange strong {
color: var(--orange);
}
.section-label {
display: flex;
align-items: center;
justify-content: space-between;
margin: 14px 0 8px;
color: var(--muted);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.section-label::after {
content: "";
flex: 1;
height: 1px;
margin-left: 10px;
background: var(--line);
}
.command-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.command-button {
position: relative;
min-height: 43px;
border: 1px solid var(--cyan-dim);
background: rgba(35, 105, 96, 0.11);
color: var(--cyan);
cursor: pointer;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.command-button:hover {
background: rgba(112, 246, 220, 0.15);
box-shadow: inset 0 0 18px rgba(112, 246, 220, 0.08);
}
.command-button:active {
transform: translateY(1px);
}
.command-button:disabled {
opacity: 0.35;
cursor: default;
}
.command-button.brake {
color: var(--red);
border-color: rgba(255, 98, 108, 0.55);
background: rgba(255, 98, 108, 0.07);
}
.command-button.resume {
color: var(--gold);
border-color: rgba(255, 209, 102, 0.55);
background: rgba(255, 209, 102, 0.06);
}
.queue {
min-height: 31px;
border-top: 1px dashed var(--line);
margin-top: 9px;
padding-top: 8px;
}
.queue-empty {
color: #587078;
font-size: 9px;
line-height: 1.5;
}
.queue-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 8px;
padding: 4px 0;
color: var(--cyan);
font-size: 9px;
}
.queue-row span:last-child {
color: var(--muted);
}
.mode-switch {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 5px;
padding: 7px;
border-bottom: 1px solid var(--line);
}
.mode-button.active {
color: var(--cyan);
border-color: var(--cyan-dim);
background: rgba(112, 246, 220, 0.08);
}
.truth-lock {
padding: 22px 14px;
color: var(--muted);
text-align: center;
font-size: 10px;
line-height: 1.7;
background:
repeating-linear-gradient(
-45deg,
transparent,
transparent 7px,
rgba(255, 255, 255, 0.012) 7px,
rgba(255, 255, 255, 0.012) 14px
);
}
.truth-lock strong {
display: block;
margin-bottom: 6px;
color: var(--gold);
font-size: 12px;
}
.divergence {
margin-top: 10px;
padding: 10px;
border: 1px solid rgba(202, 140, 255, 0.35);
background: rgba(202, 140, 255, 0.055);
}
.divergence .label {
color: var(--purple);
font-size: 8px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.divergence .value {
margin-top: 5px;
color: var(--text);
font-size: 14px;
font-weight: 900;
}
.divergence .explanation {
margin-top: 6px;
color: var(--muted);
font-size: 9px;
line-height: 1.45;
}
.objective {
color: var(--text);
font-size: 11px;
line-height: 1.55;
}
.objective strong {
color: var(--gold);
}
.route-list {
margin: 10px 0 0;
padding: 0;
list-style: none;
counter-reset: route;
}
.route-list li {
position: relative;
margin: 7px 0;
padding-left: 26px;
color: var(--muted);
font-size: 9px;
line-height: 1.5;
counter-increment: route;
}
.route-list li::before {
content: counter(route);
position: absolute;
left: 0;
top: 0;
width: 17px;
height: 17px;
border: 1px solid var(--line-bright);
color: var(--cyan);
text-align: center;
line-height: 15px;
font-weight: 900;
}
.hint-button {
width: 100%;
margin-top: 11px;
border: 1px solid var(--line);
background: rgba(255, 209, 102, 0.05);
color: var(--gold);
padding: 8px;
cursor: pointer;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.hint-button:hover {
border-color: var(--orange-dim);
}
#hintText {
display: none;
margin-top: 9px;
padding: 9px;
border-left: 2px solid var(--gold);
background: rgba(255, 209, 102, 0.055);
color: #d8cba8;
font-size: 9px;
line-height: 1.6;
}
.clock-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
}
.clock-cell {
padding: 9px;
border: 1px solid var(--line);
background: rgba(3, 12, 17, 0.35);
}
.clock-cell label {
display: block;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.clock-cell strong {
display: block;
margin-top: 5px;
color: var(--cyan);
font-size: 14px;
}
.clock-cell strong.orange {
color: var(--orange);
}
.timeline-note {
margin-top: 10px;
padding-top: 9px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 9px;
line-height: 1.55;
}
.legend {
display: flex;
gap: 12px;
margin-top: 8px;
color: var(--muted);
font-size: 9px;
}
.legend span::before {
content: "";
display: inline-block;
width: 8px;
height: 8px;
margin-right: 5px;
background: currentColor;
box-shadow: 0 0 8px currentColor;
}
.legend .tx {
color: var(--cyan);
}
.legend .rx {
color: var(--orange);
}
.legend .ghost {
color: var(--purple);
}
#log {
height: 104px;
overflow: hidden;
}
.log-row {
display: grid;
grid-template-columns: 48px 1fr;
gap: 8px;
padding: 5px 0;
border-bottom: 1px solid rgba(41, 67, 76, 0.45);
color: var(--muted);
font-size: 9px;
line-height: 1.4;
}
.log-row time {
color: #55717a;
}
.log-row.command span:last-child {
color: var(--cyan);
}
.log-row.warning span:last-child {
color: var(--gold);
}
.log-row.danger span:last-child {
color: var(--red);
}
.log-row.success span:last-child {
color: var(--gold);
}
#toast {
position: fixed;
z-index: 20;
left: 50%;
top: 88px;
transform: translate(-50%, -12px);
max-width: 420px;
padding: 10px 14px;
border: 1px solid var(--cyan-dim);
background: rgba(5, 17, 22, 0.95);
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.05em;
opacity: 0;
pointer-events: none;
transition:
opacity 0.2s ease,
transform 0.2s ease;
}
#toast.visible {
opacity: 1;
transform: translate(-50%, 0);
}
#resultOverlay {
position: fixed;
z-index: 30;
inset: 0;
display: grid;
place-items: center;
background: rgba(2, 8, 12, 0.72);
backdrop-filter: blur(5px);
}
.result-card {
width: min(440px, calc(100vw - 32px));
border: 1px solid var(--line-bright);
background: #08171d;
box-shadow: 0 30px 100px rgba(0, 0, 0, 0.6);
padding: 26px;
text-align: center;
}
.result-card .eyebrow {
color: var(--gold);
}
.result-card h2 {
margin: 10px 0;
font-size: 27px;
letter-spacing: -0.04em;
}
.result-card p {
margin: 0 0 18px;
color: var(--muted);
font-size: 11px;
line-height: 1.65;
}
.result-card button {
border: 1px solid var(--gold);
background: rgba(255, 209, 102, 0.08);
color: var(--gold);
padding: 11px 18px;
cursor: pointer;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.camera-help {
position: fixed;
z-index: 3;
left: 50%;
top: 78px;
transform: translateX(-50%);
color: rgba(136, 163, 170, 0.7);
font-size: 8px;
letter-spacing: 0.1em;
pointer-events: none;
text-transform: uppercase;
}
@media (max-width: 1180px) {
#logPanel {
display: none;
}
#timelinePanel {
left: auto;
right: 18px;
transform: none;
}
.link-spec {
display: none;
}
}
@media (max-width: 780px) {
.brand {
min-width: 0;
}
.brand .eyebrow {
display: none;
}
h1 {
font-size: 17px;
}
.tool-button {
padding: 7px;
font-size: 8px;
}
#earthPanel {
left: 8px;
top: 78px;
width: 250px;
}
#truthPanel {
right: 8px;
top: 78px;
width: 230px;
}
#briefPanel {
left: 8px;
bottom: 8px;
width: 250px;
}
#timelinePanel {
display: none;
}
.panel-body {
padding: 9px;
}
.readout {
min-height: 48px;
padding: 7px;
}
.readout strong {
font-size: 12px;
}
.camera-help {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel Mars rover light-time puzzle"></canvas>
<div id="worldLabels">
<div id="earthLabel" class="world-label">EARTH CONTROL</div>
<div id="trueLabel" class="world-label">LIVE ROVER</div>
<div id="staleLabel" class="world-label orange">LAST RECEIVED COPY</div>
<div id="wallALabel" class="world-label red">WALL A / NORTH GAP</div>
<div id="wallBLabel" class="world-label red">WALL B / SOUTH GAP</div>
<div id="goalLabel" class="world-label gold">ECHO-3</div>
</div>
<header id="topbar">
<div class="brand">
<div class="eyebrow">Planetary communications simulator</div>
<h1>DELAY LAB // ECHO-3</h1>
</div>
<div class="link-spec">
<span class="chip cyan"><i class="dot"></i> EARTH → MARS</span>
<strong>4.2 s</strong>
<span class="chip orange"><i class="dot"></i> MARS → EARTH</span>
<strong>4.2 s</strong>
<span class="chip">ROUND TRIP 8.4 s</span>
</div>
<div class="toolbar">
<button id="pauseButton" class="tool-button primary">Pause</button>
<button id="resetButton" class="tool-button">Reset</button>
<button id="cameraButton" class="tool-button">View</button>
</div>
</header>
<div class="camera-help">Drag to orbit · wheel to zoom · space to pause · R to reset</div>
<section id="earthPanel" class="panel orange-panel">
<div class="panel-head">
<div>
<div class="panel-title">Earth Control</div>
<div class="panel-subtitle">Last received rover observation</div>
</div>
<div id="earthLinkStatus" class="status orange">Downlink ready</div>
</div>
<div class="panel-body">
<div class="readout-grid">
<div class="readout orange">
<label>Snapshot age</label>
<strong id="earthAge">0.0 s</strong>
</div>
<div class="readout orange">
<label>Observed heading</label>
<strong id="earthHeading">EAST</strong>
</div>
<div class="readout orange">
<label>Observed X</label>
<strong id="earthX">1.40</strong>
</div>
<div class="readout orange">
<label>Observed Z</label>
<strong id="earthZ">1.40</strong>
</div>
</div>
<div class="section-label">Transmit command</div>
<div class="command-grid">
<button class="command-button" data-command="left">↶ Turn left</button>
<button class="command-button" data-command="right">↷ Turn right</button>
<button class="command-button brake" data-command="brake">■ Brake</button>
<button class="command-button resume" data-command="resume">▶ Resume</button>
</div>
<div class="section-label">
Outbound queue
<span id="outboundCount">0 packets</span>
</div>
<div id="commandQueue" class="queue">
<div class="queue-empty">
Clicking a command only launches a light packet. It executes after
the one-way travel time.
</div>
</div>
</div>
</section>
<section id="truthPanel" class="panel">
<div class="mode-switch">
<button class="mode-button active" data-mode="compare">Compare states</button>
<button class="mode-button" data-mode="earth">Earth only</button>
</div>
<div class="panel-head">
<div>
<div class="panel-title">Rover Local Truth</div>
<div class="panel-subtitle">Authoritative state inside the rover</div>
</div>
<div class="status">Live / T+0.0</div>
</div>
<div id="truthValues" class="panel-body">
<div class="readout-grid">
<div class="readout cyan">
<label>True heading</label>
<strong id="truthHeading">EAST</strong>
</div>
<div class="readout cyan">
<label>True speed</label>
<strong id="truthSpeed">1.15</strong>
</div>
<div class="readout cyan">
<label>True X</label>
<strong id="truthX">1.40</strong>
</div>
<div class="readout cyan">
<label>True Z</label>
<strong id="truthZ">1.40</strong>
</div>
</div>
<div class="divergence">
<div class="label">Earth / rover disagreement</div>
<div id="divergenceValue" class="value">0.00 m</div>
<div class="explanation">
Earth is steering from an old position and heading. The purple line
in the scene connects the true rover to the last received copy.
</div>
</div>
</div>
<div id="truthLock" class="truth-lock" hidden>
<strong>LOCAL TRUTH LOCKED</strong>
Solve using only Earth's stale downlink.<br />
Switch back to “Compare states” to inspect the discrepancy.
</div>
</section>
<section id="briefPanel" class="panel">
<div class="panel-head">
<div>
<div class="panel-title">Mission Brief</div>
<div class="panel-subtitle">Autonomous rover ECHO-3</div>
</div>
<div class="status">Goal active</div>
</div>
<div class="panel-body">
<div class="objective">
Guide the rover to the <strong>gold ECHO-3 pad</strong> without
striking either red voxel wall.
</div>
<ol class="route-list">
<li>Pass wall A through its northern gap.</li>
<li>Turn south and pass wall B through its southern gap.</li>
<li>Reach the gold beacon beyond wall B.</li>
</ol>
<button id="hintButton" class="hint-button">Reveal timing hint</button>
<div id="hintText"></div>
</div>
</section>
<section id="timelinePanel" class="panel">
<div class="panel-head">
<div>
<div class="panel-title">Communication Timeline</div>
<div class="panel-subtitle">Finite propagation, no instant correction</div>
</div>
<div id="simState" class="status">Simulation live</div>
</div>
<div class="panel-body">
<div class="clock-grid">
<div class="clock-cell">
<label>Mission time</label>
<strong id="missionTime">00.0 s</strong>
</div>
<div class="clock-cell">
<label>Earth snapshot age</label>
<strong id="timelineAge" class="orange">0.0 s</strong>
</div>
<div class="clock-cell">
<label>Telemetry in flight</label>
<strong id="telemetryCount">0</strong>
</div>
</div>
<div class="timeline-note">
Commands take 4.2 seconds to reach the rover. Observations take another
4.2 seconds to reach Earth. A command based on the newest Earth copy
can already be obsolete by the time it executes.
</div>
<div class="legend">
<span class="tx">Command</span>
<span class="rx">Observation</span>
<span class="ghost">State discrepancy</span>
</div>
</div>
</section>
<section id="logPanel" class="panel">
<div class="panel-head">
<div>
<div class="panel-title">Event Log</div>
<div class="panel-subtitle">Newest event at the bottom</div>
</div>
<div class="status orange">Local monitor</div>
</div>
<div id="log" class="panel-body"></div>
</section>
<div id="toast"></div>
<div id="resultOverlay" hidden>
<div class="result-card">
<div class="eyebrow" id="resultEyebrow">Mission update</div>
<h2 id="resultTitle">Beacon reached</h2>
<p id="resultBody"></p>
<button id="resultReset">Run again</button>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Constants and UI references
// -------------------------------------------------------------------------
const COMMAND_DELAY = 4.2;
const TELEMETRY_DELAY = 4.2;
const TELEMETRY_INTERVAL = 1.4;
const ROVER_SPEED = 1.15;
const ROVER_RADIUS = 0.48;
const FIXED_STEP = 1 / 60;
const START = { x: 1.4, z: 1.4, heading: 0 };
const GOAL = { x: 15.0, z: 9.55 };
const HEADINGS = ["EAST", "SOUTH", "WEST", "NORTH"];
const walls = [
{ xMin: 6.05, xMax: 6.95, zMin: 3.3, zMax: 12.8 },
{ xMin: 10.05, xMax: 10.95, zMin: 0.4, zMax: 8.8 }
];
const ui = {
canvas: document.querySelector("#scene"),
earthAge: document.querySelector("#earthAge"),
earthHeading: document.querySelector("#earthHeading"),
earthX: document.querySelector("#earthX"),
earthZ: document.querySelector("#earthZ"),
earthLinkStatus: document.querySelector("#earthLinkStatus"),
truthHeading: document.querySelector("#truthHeading"),
truthSpeed: document.querySelector("#truthSpeed"),
truthX: document.querySelector("#truthX"),
truthZ: document.querySelector("#truthZ"),
divergenceValue: document.querySelector("#divergenceValue"),
truthValues: document.querySelector("#truthValues"),
truthLock: document.querySelector("#truthLock"),
commandQueue: document.querySelector("#commandQueue"),
outboundCount: document.querySelector("#outboundCount"),
missionTime: document.querySelector("#missionTime"),
timelineAge: document.querySelector("#timelineAge"),
telemetryCount: document.querySelector("#telemetryCount"),
simState: document.querySelector("#simState"),
log: document.querySelector("#log"),
toast: document.querySelector("#toast"),
hintText: document.querySelector("#hintText"),
resultOverlay: document.querySelector("#resultOverlay"),
resultEyebrow: document.querySelector("#resultEyebrow"),
resultTitle: document.querySelector("#resultTitle"),
resultBody: document.querySelector("#resultBody"),
pauseButton: document.querySelector("#pauseButton"),
labels: {
earth: document.querySelector("#earthLabel"),
truth: document.querySelector("#trueLabel"),
stale: document.querySelector("#staleLabel"),
wallA: document.querySelector("#wallALabel"),
wallB: document.querySelector("#wallBLabel"),
goal: document.querySelector("#goalLabel")
}
};
const commandButtons = [
...document.querySelectorAll(".command-button")
];
// -------------------------------------------------------------------------
// Renderer, scene, camera, lighting
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas: ui.canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x050b11);
scene.fog = new THREE.Fog(0x050b11, 34, 65);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
120
);
const cameraState = {
target: new THREE.Vector3(4.6, 0, 6.0),
yaw: 0.64,
pitch: 0.76,
radius: 27
};
function updateCamera() {
const horizontal = Math.cos(cameraState.pitch) * cameraState.radius;
camera.position.set(
cameraState.target.x + Math.sin(cameraState.yaw) * horizontal,
cameraState.target.y + Math.sin(cameraState.pitch) * cameraState.radius,
cameraState.target.z + Math.cos(cameraState.yaw) * horizontal
);
camera.lookAt(cameraState.target);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xc4f4ff, 0x32110c, 1.8);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffe3c2, 3.0);
sun.position.set(-9, 19, 12);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -22;
sun.shadow.camera.right = 22;
sun.shadow.camera.top = 20;
sun.shadow.camera.bottom = -20;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 50;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x4fd6ff, 1.1);
rim.position.set(14, 9, -12);
scene.add(rim);
// -------------------------------------------------------------------------
// Voxel Mars terrain
// -------------------------------------------------------------------------
const platform = new THREE.Mesh(
new THREE.BoxGeometry(17.4, 0.52, 13.4),
new THREE.MeshStandardMaterial({
color: 0x391d18,
roughness: 0.96
})
);
platform.position.set(8.5, -0.36, 6.5);
platform.receiveShadow = true;
scene.add(platform);
const floorGeometry = new THREE.BoxGeometry(0.94, 0.2, 0.94);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
metalness: 0
});
const floor = new THREE.InstancedMesh(
floorGeometry,
floorMaterial,
17 * 13
);
floor.receiveShadow = true;
const transform = new THREE.Object3D();
const floorColor = new THREE.Color();
let floorIndex = 0;
for (let x = 0; x < 17; x++) {
for (let z = 0; z < 13; z++) {
transform.position.set(x + 0.5, -0.13, z + 0.5);
transform.updateMatrix();
floor.setMatrixAt(floorIndex, transform.matrix);
const noise = ((x * 17 + z * 31 + x * z) % 11) / 11;
floorColor.setRGB(
0.30 + noise * 0.065,
0.115 + noise * 0.035,
0.085 + noise * 0.025
);
floor.setColorAt(floorIndex, floorColor);
floorIndex++;
}
}
floor.instanceMatrix.needsUpdate = true;
floor.instanceColor.needsUpdate = true;
scene.add(floor);
const wallMaterials = [
new THREE.MeshStandardMaterial({
color: 0x652d29,
roughness: 0.88
}),
new THREE.MeshStandardMaterial({
color: 0x823a31,
roughness: 0.86
}),
new THREE.MeshStandardMaterial({
color: 0x4b2927,
roughness: 0.92
})
];
const warningMaterial = new THREE.MeshBasicMaterial({
color: 0xff626c
});
function buildWall(x, zStart, zEnd) {
const blockLength = 0.88;
const count = Math.ceil((zEnd - zStart) / blockLength);
for (let i = 0; i < count; i++) {
const z0 = zStart + i * blockLength;
const z1 = Math.min(z0 + blockLength, zEnd);
const height = 1.05 + ((i * 7) % 5) * 0.17;
const depth = z1 - z0;
const block = new THREE.Mesh(
new THREE.BoxGeometry(0.9, height, depth),
wallMaterials[i % wallMaterials.length]
);
block.position.set(x, height / 2 - 0.02, (z0 + z1) / 2);
block.castShadow = true;
block.receiveShadow = true;
scene.add(block);
const warning = new THREE.Mesh(
new THREE.BoxGeometry(0.92, 0.07, 0.09),
warningMaterial
);
warning.position.set(x, height + 0.015, z0 + 0.11);
scene.add(warning);
}
}
buildWall(6.5, 3.3, 12.8);
buildWall(10.5, 0.4, 8.8);
const startPad = new THREE.Mesh(
new THREE.BoxGeometry(1.25, 0.08, 1.25),
new THREE.MeshStandardMaterial({
color: 0x19433f,
emissive: 0x0d3531,
emissiveIntensity: 0.7,
roughness: 0.9
})
);
startPad.position.set(START.x, 0.005, START.z);
scene.add(startPad);
const goalPad = new THREE.Mesh(
new THREE.BoxGeometry(1.8, 0.12, 1.8),
new THREE.MeshStandardMaterial({
color: 0x5a4a1b,
emissive: 0x5e4510,
emissiveIntensity: 0.8,
roughness: 0.75
})
);
goalPad.position.set(GOAL.x, 0.02, GOAL.z);
goalPad.receiveShadow = true;
scene.add(goalPad);
for (let i = 0; i < 4; i++) {
const angle = (i / 4) * Math.PI * 2;
const corner = new THREE.Mesh(
new THREE.BoxGeometry(0.22, 0.18, 0.22),
new THREE.MeshBasicMaterial({ color: 0xffd166 })
);
corner.position.set(
GOAL.x + Math.cos(angle) * 0.78,
0.13,
GOAL.z + Math.sin(angle) * 0.78
);
scene.add(corner);
}
const beacon = new THREE.Group();
beacon.position.set(GOAL.x, 0.12, GOAL.z);
scene.add(beacon);
const beaconCore = new THREE.Mesh(
new THREE.BoxGeometry(0.52, 0.52, 0.52),
new THREE.MeshStandardMaterial({
color: 0xffd166,
emissive: 0xffa900,
emissiveIntensity: 1.4,
roughness: 0.5
})
);
beaconCore.position.y = 0.42;
beaconCore.castShadow = true;
beacon.add(beaconCore);
const beaconLight = new THREE.PointLight(0xffc85c, 2.2, 5);
beaconLight.position.y = 0.7;
beacon.add(beaconLight);
// -------------------------------------------------------------------------
// Voxel Earth and antenna
// -------------------------------------------------------------------------
const earthGroup = new THREE.Group();
earthGroup.position.set(-5, 0, 5.5);
scene.add(earthGroup);
const earthBase = new THREE.Mesh(
new THREE.BoxGeometry(3.8, 0.42, 3.2),
new THREE.MeshStandardMaterial({
color: 0x182830,
roughness: 0.85
})
);
earthBase.position.y = 0.02;
earthBase.receiveShadow = true;
earthGroup.add(earthBase);
const earthVoxels = new THREE.Group();
earthVoxels.position.set(0, 2.05, 0);
earthGroup.add(earthVoxels);
const oceanMaterial = new THREE.MeshStandardMaterial({
color: 0x246ca8,
emissive: 0x0b2a43,
emissiveIntensity: 0.35,
roughness: 0.72
});
const landMaterial = new THREE.MeshStandardMaterial({
color: 0x55a75a,
emissive: 0x12371b,
emissiveIntensity: 0.22,
roughness: 0.92
});
const earthVoxelGeometry = new THREE.BoxGeometry(0.46, 0.46, 0.46);
for (let x = -2; x <= 2; x++) {
for (let y = -2; y <= 2; y++) {
for (let z = -2; z <= 2; z++) {
if (x * x + y * y + z * z > 6) continue;
const isLand =
(x === 0 && z <= 0) ||
(x === -1 && z === 0) ||
(x === 1 && z === -1) ||
(x === 0 && y === 1 && z === 0);
const voxel = new THREE.Mesh(
earthVoxelGeometry,
isLand ? landMaterial : oceanMaterial
);
voxel.position.set(x * 0.44, y * 0.44, z * 0.44);
voxel.castShadow = true;
earthVoxels.add(voxel);
}
}
}
const atmosphere = new THREE.Mesh(
new THREE.BoxGeometry(2.65, 2.65, 2.65),
new THREE.MeshBasicMaterial({
color: 0x53d9ff,
transparent: true,
opacity: 0.11,
wireframe: true
})
);
earthVoxels.add(atmosphere);
const mast = new THREE.Mesh(
new THREE.BoxGeometry(0.16, 3.35, 0.16),
new THREE.MeshStandardMaterial({
color: 0x9ab1b6,
metalness: 0.55,
roughness: 0.45
})
);
mast.position.set(1.1, 1.78, 0);
mast.castShadow = true;
earthGroup.add(mast);
const dish = new THREE.Mesh(
new THREE.BoxGeometry(0.62, 0.16, 0.62),
new THREE.MeshStandardMaterial({
color: 0xd5e5e6,
metalness: 0.45,
roughness: 0.4
})
);
dish.position.set(1.1, 3.48, 0);
dish.rotation.z = -0.35;
earthGroup.add(dish);
const earthOrigin = new THREE.Vector3(-3.9, 3.55, 5.5);
// -------------------------------------------------------------------------
// Voxel rover and stale ghost
// -------------------------------------------------------------------------
function buildRover(isGhost = false) {
const group = new THREE.Group();
const materials = [];
function mat(color, opacity = 1, emissive = 0x000000) {
const material = isGhost
? new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false
})
: new THREE.MeshStandardMaterial({
color,
emissive,
emissiveIntensity: isGhost ? 0 : 0.35,
roughness: 0.66,
metalness: 0.08
});
materials.push(material);
return material;
}
const bodyMaterial = mat(isGhost ? 0xffad5c : 0x70f6dc, 0.24);
const bodyDarkMaterial = mat(isGhost ? 0xa45f28 : 0x18353b, 0.2);
const wheelMaterial = mat(isGhost ? 0xffad5c : 0x10191c, 0.22);
const lightMaterial = mat(isGhost ? 0xffd166 : 0xffb34f, 0.3, 0x7f3900);
const body = new THREE.Mesh(
new THREE.BoxGeometry(1.05, 0.48, 0.9),
bodyMaterial
);
body.position.y = 0.62;
body.castShadow = !isGhost;
group.add(body);
const cabin = new THREE.Mesh(
new THREE.BoxGeometry(0.66, 0.42, 0.66),
bodyDarkMaterial
);
cabin.position.set(-0.12, 1.03, 0);
cabin.castShadow = !isGhost;
group.add(cabin);
const sensor = new THREE.Mesh(
new THREE.BoxGeometry(0.27, 0.2, 0.27),
bodyMaterial
);
sensor.position.set(0.12, 1.34, 0);
group.add(sensor);
const frontLight = new THREE.Mesh(
new THREE.BoxGeometry(0.08, 0.18, 0.35),
lightMaterial
);
frontLight.position.set(0.56, 0.68, 0);
group.add(frontLight);
for (const x of [-0.34, 0.34]) {
for (const z of [-0.54, 0.54]) {
const wheel = new THREE.Mesh(
new THREE.BoxGeometry(0.38, 0.46, 0.28),
wheelMaterial
);
wheel.position.set(x, 0.3, z);
wheel.castShadow = !isGhost;
group.add(wheel);
}
}
const antenna = new THREE.Mesh(
new THREE.BoxGeometry(0.08, 0.7, 0.08),
bodyDarkMaterial
);
antenna.position.set(0.13, 1.62, 0);
group.add(antenna);
const antennaTip = new THREE.Mesh(
new THREE.BoxGeometry(0.15, 0.15, 0.15),
lightMaterial
);
antennaTip.position.set(0.13, 2.0, 0);
group.add(antennaTip);
group.userData.materials = materials;
return group;
}
const trueRover = buildRover(false);
const ghostRover = buildRover(true);
scene.add(trueRover, ghostRover);
// -------------------------------------------------------------------------
// Link line and state-discrepancy line
// -------------------------------------------------------------------------
const linkPositions = new Float32Array(49 * 3);
const linkGeometry = new THREE.BufferGeometry();
linkGeometry.setAttribute(
"position",
new THREE.BufferAttribute(linkPositions, 3)
);
const linkMaterial = new THREE.LineBasicMaterial({
color: 0x70f6dc,
transparent: true,
opacity: 0.22
});
const linkLine = new THREE.Line(linkGeometry, linkMaterial);
linkLine.frustumCulled = false;
scene.add(linkLine);
const discrepancyGeometry = new THREE.BufferGeometry();
discrepancyGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute([0, 0, 0, 0, 0, 0], 3)
);
const discrepancyMaterial = new THREE.LineDashedMaterial({
color: 0xca8cff,
dashSize: 0.22,
gapSize: 0.13,
transparent: true,
opacity: 0.9
});
const discrepancyLine = new THREE.Line(
discrepancyGeometry,
discrepancyMaterial
);
discrepancyLine.frustumCulled = false;
scene.add(discrepancyLine);
function quadraticPoint(source, target, t, result) {
const control = source.clone().add(target).multiplyScalar(0.5);
control.y = Math.max(source.y, target.y) + 5.0;
const oneMinus = 1 - t;
result.set(0, 0, 0);
result.addScaledVector(source, oneMinus * oneMinus);
result.addScaledVector(control, 2 * oneMinus * t);
result.addScaledVector(target, t * t);
return result;
}
const linkTarget = new THREE.Vector3();
const linkPoint = new THREE.Vector3();
function updateLink() {
if (viewMode === "earth") {
linkTarget.set(lastReceived.x, 1.7, lastReceived.z);
linkMaterial.color.setHex(0xffad5c);
} else {
linkTarget.set(truth.x, 1.7, truth.z);
linkMaterial.color.setHex(0x70f6dc);
}
for (let i = 0; i < 49; i++) {
quadraticPoint(earthOrigin, linkTarget, i / 48, linkPoint);
linkPositions[i * 3] = linkPoint.x;
linkPositions[i * 3 + 1] = linkPoint.y;
linkPositions[i * 3 + 2] = linkPoint.z;
}
linkGeometry.attributes.position.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Mission state and packet system
// -------------------------------------------------------------------------
let truth = { ...START, speed: ROVER_SPEED };
let lastReceived = {
x: START.x,
z: START.z,
heading: START.heading,
speed: ROVER_SPEED,
time: 0,
receivedAt: 0
};
let simTime = 0;
let nextTelemetry = TELEMETRY_INTERVAL;
let paused = false;
let crashed = false;
let won = false;
let viewMode = "compare";
let accumulator = 0;
let packetId = 0;
let toastTimer = 0;
const packets = [];
const packetGeometry = new THREE.BoxGeometry(0.22, 0.22, 0.22);
function createPacketVisual(kind) {
const group = new THREE.Group();
const color = kind === "command" ? 0x70f6dc : 0xffad5c;
for (let i = 0; i < 5; i++) {
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 1 - i * 0.16,
depthWrite: false
});
const cube = new THREE.Mesh(packetGeometry, material);
cube.position.x = -i * 0.22;
cube.scale.setScalar(1 - i * 0.12);
group.add(cube);
}
scene.add(group);
return group;
}
function removePacket(packet) {
scene.remove(packet.group);
packet.group.traverse((child) => {
if (child.isMesh && child.material) {
child.material.dispose();
}
});
const index = packets.indexOf(packet);
if (index !== -1) packets.splice(index, 1);
}
function sendCommand(command) {
if (crashed || won) return;
const labels = {
left: "TURN LEFT",
right: "TURN RIGHT",
brake: "BRAKE",
resume: "RESUME"
};
const packet = {
id: ++packetId,
kind: "command",
command,
label: labels[command],
launch: simTime,
arrival: simTime + COMMAND_DELAY,
group: createPacketVisual("command")
};
packets.push(packet);
addLog(`Queued ${labels[command]}`, "command");
showToast(`${labels[command]} packet launched toward ECHO-3`);
}
function emitTelemetry() {
const snapshot = {
x: truth.x,
z: truth.z,
heading: truth.heading,
speed: truth.speed,
time: simTime
};
packets.push({
id: ++packetId,
kind: "telemetry",
snapshot,
origin: new THREE.Vector3(truth.x, 1.85, truth.z),
launch: simTime,
arrival: simTime + TELEMETRY_DELAY,
group: createPacketVisual("telemetry")
});
}
function applyCommand(command) {
switch (command) {
case "left":
truth.heading = (truth.heading + 3) % 4;
break;
case "right":
truth.heading = (truth.heading + 1) % 4;
break;
case "brake":
truth.speed = 0;
break;
case "resume":
truth.speed = ROVER_SPEED;
break;
}
addLog(`Rover executed ${command.toUpperCase()}`, "command");
showToast(`Command executed: ${command.toUpperCase()}`);
}
// -------------------------------------------------------------------------
// Collision and mission logic
// -------------------------------------------------------------------------
function circleIntersectsRect(x, z, radius, rect) {
const nearestX = THREE.MathUtils.clamp(x, rect.xMin, rect.xMax);
const nearestZ = THREE.MathUtils.clamp(z, rect.zMin, rect.zMax);
const dx = x - nearestX;
const dz = z - nearestZ;
return dx * dx + dz * dz < radius * radius;
}
function collides(x, z) {
if (
x < 0.45 ||
x > 16.55 ||
z < 0.45 ||
z > 12.55
) {
return true;
}
return walls.some((wall) =>
circleIntersectsRect(x, z, ROVER_RADIUS, wall)
);
}
function directionForHeading(heading) {
return [
{ x: 1, z: 0 },
{ x: 0, z: 1 },
{ x: -1, z: 0 },
{ x: 0, z: -1 }
][heading];
}
function finishMission(success) {
if (won || crashed) return;
if (success) {
won = true;
truth.speed = 0;
addLog("ECHO-3 beacon reached", "success");
showToast("Mission complete");
} else {
crashed = true;
truth.speed = 0;
addLog("Collision detected. Rover stopped.", "danger");
showToast("Collision detected");
}
setPaused(true);
ui.resultOverlay.hidden = false;
ui.resultEyebrow.textContent = success
? "Mission complete"
: "Mission failed";
ui.resultTitle.textContent = success
? "Beacon reached"
: "Rover damaged";
ui.resultBody.innerHTML = success
? `ECHO-3 reached ECHO-3 in <strong>${simTime.toFixed(1)} seconds</strong>. ` +
`Your commands were planned against a moving rover and a stale downlink.`
: `The rover struck an obstacle at mission time <strong>${simTime.toFixed(1)} seconds</strong>. ` +
`Remember: the command executes 4.2 seconds after you click it.`;
}
function updateSimulation(dt) {
if (paused || won || crashed) return;
simTime += dt;
for (const packet of [...packets]) {
if (simTime < packet.arrival) continue;
if (packet.kind === "command") {
applyCommand(packet.command);
} else {
lastReceived = {
...packet.snapshot,
receivedAt: simTime
};
}
removePacket(packet);
}
if (truth.speed > 0) {
const direction = directionForHeading(truth.heading);
const nextX = truth.x + direction.x * truth.speed * dt;
const nextZ = truth.z + direction.z * truth.speed * dt;
if (collides(nextX, nextZ)) {
truth.x = nextX;
truth.z = nextZ;
finishMission(false);
return;
}
truth.x = nextX;
truth.z = nextZ;
}
const goalDistance = Math.hypot(
truth.x - GOAL.x,
truth.z - GOAL.z
);
if (goalDistance < 0.72) {
finishMission(true);
return;
}
if (simTime >= nextTelemetry) {
emitTelemetry();
nextTelemetry += TELEMETRY_INTERVAL;
}
}
// -------------------------------------------------------------------------
// Packet animation
// -------------------------------------------------------------------------
const packetSource = new THREE.Vector3();
const packetTarget = new THREE.Vector3();
const packetPoint = new THREE.Vector3();
function updatePacketVisuals() {
for (const packet of packets) {
const progress = THREE.MathUtils.clamp(
(simTime - packet.launch) /
Math.max(0.001, packet.arrival - packet.launch),
0,
1
);
if (packet.kind === "command") {
packetSource.copy(earthOrigin);
packetTarget.set(truth.x, 1.9, truth.z);
} else {
packetSource.copy(packet.origin);
packetTarget.copy(earthOrigin);
}
quadraticPoint(
packetSource,
packetTarget,
progress,
packetPoint
);
packet.group.position.copy(packetPoint);
packet.group.rotation.y += 0.08;
packet.group.rotation.x += 0.045;
const pulse = 1 + Math.sin(simTime * 11 + packet.id) * 0.13;
packet.group.scale.setScalar(pulse);
}
}
// -------------------------------------------------------------------------
// Logging, toast, and result display
// -------------------------------------------------------------------------
function addLog(message, type = "") {
const row = document.createElement("div");
row.className = `log-row ${type}`;
const time = document.createElement("time");
time.textContent = `${simTime.toFixed(1)}s`;
const text = document.createElement("span");
text.textContent = message;
row.append(time, text);
ui.log.append(row);
while (ui.log.children.length > 6) {
ui.log.firstElementChild.remove();
}
}
function showToast(message) {
ui.toast.textContent = message;
ui.toast.classList.add("visible");
toastTimer = 2.1;
}
function updateToast(dt) {
if (toastTimer > 0) {
toastTimer -= dt;
if (toastTimer <= 0) {
ui.toast.classList.remove("visible");
}
}
}
// -------------------------------------------------------------------------
// UI state
// -------------------------------------------------------------------------
function setViewMode(mode) {
viewMode = mode;
document.querySelectorAll(".mode-button").forEach((button) => {
button.classList.toggle("active", button.dataset.mode === mode);
});
const compare = mode === "compare";
trueRover.visible = compare;
discrepancyLine.visible = compare;
ui.truthValues.hidden = !compare;
ui.truthLock.hidden = compare;
ui.labels.truth.style.display = compare ? "block" : "none";
for (const material of ghostRover.userData.materials) {
material.opacity = compare ? 0.24 : 0.58;
}
}
function setPaused(value) {
paused = value;
accumulator = 0;
ui.pauseButton.textContent = paused ? "Resume" : "Pause";
ui.simState.textContent = paused
? "Simulation paused"
: won
? "Mission complete"
: crashed
? "Rover stopped"
: "Simulation live";
}
function resetMission() {
for (const packet of [...packets]) {
removePacket(packet);
}
truth = { ...START, speed: ROVER_SPEED };
lastReceived = {
x: START.x,
z: START.z,
heading: START.heading,
speed: ROVER_SPEED,
time: 0,
receivedAt: 0
};
simTime = 0;
nextTelemetry = TELEMETRY_INTERVAL;
crashed = false;
won = false;
accumulator = 0;
packetId = 0;
ui.log.innerHTML = "";
ui.resultOverlay.hidden = true;
ui.hintText.style.display = "none";
ui.hintText.innerHTML = "";
setPaused(false);
addLog("Mission initialized. Rover is moving east.", "command");
updateUI();
}
function renderQueue() {
const commands = packets.filter((packet) => packet.kind === "command");
ui.outboundCount.textContent =
`${commands.length} packet${commands.length === 1 ? "" : "s"}`;
if (commands.length === 0) {
ui.commandQueue.innerHTML =
'<div class="queue-empty">No outbound commands. Click a command to transmit.</div>';
return;
}
ui.commandQueue.innerHTML = commands
.map((packet) => {
const remaining = Math.max(0, packet.arrival - simTime);
return `
<div class="queue-row">
<span>${packet.label}</span>
<span>ARRIVES +${remaining.toFixed(1)}s</span>
</div>
`;
})
.join("");
}
function updateUI() {
const snapshotAge = Math.max(
0,
simTime - lastReceived.time
);
const disagreement = Math.hypot(
truth.x - lastReceived.x,
truth.z - lastReceived.z
);
ui.earthAge.textContent = `${snapshotAge.toFixed(1)} s`;
ui.earthHeading.textContent = HEADINGS[lastReceived.heading];
ui.earthX.textContent = lastReceived.x.toFixed(2);
ui.earthZ.textContent = lastReceived.z.toFixed(2);
ui.truthHeading.textContent = HEADINGS[truth.heading];
ui.truthSpeed.textContent = truth.speed.toFixed(2);
ui.truthX.textContent = truth.x.toFixed(2);
ui.truthZ.textContent = truth.z.toFixed(2);
ui.divergenceValue.textContent = `${disagreement.toFixed(2)} m`;
ui.missionTime.textContent = `${simTime.toFixed(1)} s`;
ui.timelineAge.textContent = `${snapshotAge.toFixed(1)} s`;
ui.telemetryCount.textContent = String(
packets.filter((packet) => packet.kind === "telemetry").length
);
ui.earthLinkStatus.textContent = snapshotAge > 4.0
? "Stale downlink"
: "Downlink ready";
renderQueue();
for (const button of commandButtons) {
button.disabled = won || crashed;
}
}
// -------------------------------------------------------------------------
// World visual state
// -------------------------------------------------------------------------
function updateWorldVisuals(dt, elapsed) {
trueRover.position.set(truth.x, 0, truth.z);
trueRover.rotation.y = -truth.heading * Math.PI / 2;
ghostRover.position.set(lastReceived.x, 0, lastReceived.z);
ghostRover.rotation.y = -lastReceived.heading * Math.PI / 2;
const discrepancy = discrepancyGeometry.attributes.position;
discrepancy.setXYZ(0, truth.x, 0.16, truth.z);
discrepancy.setXYZ(1, lastReceived.x, 0.16, lastReceived.z);
discrepancy.needsUpdate = true;
discrepancyLine.computeLineDistances();
beaconCore.position.y = 0.42 + Math.sin(elapsed * 2.5) * 0.09;
beaconCore.rotation.y = elapsed * 0.7;
beaconLight.intensity = 2 + Math.sin(elapsed * 3.5) * 0.5;
earthVoxels.rotation.y = elapsed * 0.08;
updateLink();
updatePacketVisuals();
}
// -------------------------------------------------------------------------
// Screen-space world labels
// -------------------------------------------------------------------------
const projection = new THREE.Vector3();
function placeLabel(element, x, y, z, visible = true) {
if (!visible) {
element.style.display = "none";
return;
}
projection.set(x, y, z).project(camera);
if (projection.z < -1 || projection.z > 1) {
element.style.display = "none";
return;
}
element.style.display = "block";
element.style.left = `${(projection.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projection.y * 0.5 + 0.5) * window.innerHeight}px`;
}
function updateLabels() {
placeLabel(ui.labels.earth, -5, 4.15, 5.5);
placeLabel(
ui.labels.truth,
truth.x,
2.65,
truth.z,
viewMode === "compare"
);
placeLabel(
ui.labels.stale,
lastReceived.x,
2.45,
lastReceived.z
);
placeLabel(ui.labels.wallA, 6.5, 2.25, 5.5);
placeLabel(ui.labels.wallB, 10.5, 2.25, 4.4);
placeLabel(ui.labels.goal, GOAL.x, 1.65, GOAL.z);
}
// -------------------------------------------------------------------------
// Custom camera controls: no addons
// -------------------------------------------------------------------------
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
ui.canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
ui.canvas.setPointerCapture(event.pointerId);
ui.canvas.style.cursor = "grabbing";
});
ui.canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraState.yaw -= deltaX * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + deltaY * 0.005,
0.32,
1.25
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
ui.canvas.style.cursor = "grab";
if (event && ui.canvas.hasPointerCapture(event.pointerId)) {
ui.canvas.releasePointerCapture(event.pointerId);
}
}
ui.canvas.addEventListener("pointerup", stopDragging);
ui.canvas.addEventListener("pointercancel", stopDragging);
ui.canvas.style.cursor = "grab";
ui.canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.018,
14,
43
);
updateCamera();
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Buttons and keyboard
// -------------------------------------------------------------------------
for (const button of commandButtons) {
button.addEventListener("click", () => {
sendCommand(button.dataset.command);
});
}
document.querySelectorAll(".mode-button").forEach((button) => {
button.addEventListener("click", () => {
setViewMode(button.dataset.mode);
showToast(
viewMode === "compare"
? "Comparison mode: true and last-received states visible"
: "Earth-only mode: local truth hidden"
);
});
});
ui.pauseButton.addEventListener("click", () => {
if (won || crashed) return;
setPaused(!paused);
});
document.querySelector("#resetButton").addEventListener("click", resetMission);
document.querySelector("#resultReset").addEventListener("click", resetMission);
document.querySelector("#cameraButton").addEventListener("click", () => {
cameraState.yaw = 0.64;
cameraState.pitch = 0.76;
cameraState.radius = 27;
cameraState.target.set(4.6, 0, 6.0);
updateCamera();
});
document.querySelector("#hintButton").addEventListener("click", () => {
ui.hintText.style.display = "block";
ui.hintText.innerHTML = `
Queue <strong>TURN RIGHT</strong> at about
<strong>T+2.0 s</strong>; it executes near T+6.2 s, safely beyond
wall A.<br /><br />
Queue <strong>TURN LEFT</strong> at about
<strong>T+9.2 s</strong>; it executes near T+13.4 s, after clearing
wall B's southern gap. The stale Earth display will still show the
rover's older heading during this interval.
`;
});
window.addEventListener("keydown", (event) => {
if (
event.target instanceof HTMLButtonElement ||
event.target instanceof HTMLInputElement
) {
return;
}
if (event.code === "Space") {
event.preventDefault();
if (!won && !crashed) setPaused(!paused);
}
if (event.key.toLowerCase() === "r") {
resetMission();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// -------------------------------------------------------------------------
// Main loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
let uiTimer = 0;
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
if (!paused && !won && !crashed) {
accumulator += dt;
while (accumulator >= FIXED_STEP) {
updateSimulation(FIXED_STEP);
accumulator -= FIXED_STEP;
}
}
updateWorldVisuals(dt, elapsed);
updateCamera();
updateLabels();
updateToast(dt);
uiTimer += dt;
if (uiTimer >= 0.08) {
uiTimer = 0;
updateUI();
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
setViewMode("compare");
resetMission();
animate();
</script>
</body>
</html>
```