Prompt specification
Build or customize the Three.js 3D scene "Stopping-time impulse explorer" 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: Stopping-time impulse explorer
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/stopping-time-impulse-explorer-x1400
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing impulse during a controlled stop. Let users vary stopping duration for a fixed initial model momentum; display the required average force and keep the distinction between average and peak force explicit. Voxel style.
Working Three.js Starter (stopping-time-impulse-explorer-x1400):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Impulse Lab — Controlled Stop</title>
<style>
:root {
--paper: #f4f0e6;
--paper-2: #e8e3d7;
--ink: #17242b;
--muted: #657178;
--line: #17242b;
--cyan: #12b8b1;
--cyan-dark: #087f7b;
--red: #ef5350;
--amber: #e7a72e;
--white: #fffdf7;
--shadow: 7px 7px 0 rgba(23, 36, 43, 0.13);
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.app {
width: min(1480px, calc(100% - 32px));
margin: 0 auto;
padding: 22px 0 30px;
}
.header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
margin-bottom: 18px;
}
.brand {
display: flex;
align-items: center;
gap: 15px;
}
.voxel-mark {
display: grid;
grid-template-columns: repeat(3, 13px);
grid-template-rows: repeat(3, 13px);
gap: 3px;
transform: rotate(-3deg);
}
.voxel-mark span {
background: var(--ink);
box-shadow: inset -3px -3px 0 rgba(255, 255, 255, 0.12);
}
.voxel-mark span:nth-child(2),
.voxel-mark span:nth-child(6),
.voxel-mark span:nth-child(8) {
background: var(--cyan);
}
.voxel-mark span:nth-child(5) {
background: var(--red);
}
.eyebrow,
.mono,
.metric-label,
.mini-label,
.status,
.legend,
.stage-hint,
.locked-pill,
.profile-button,
.equation,
.graph-axis,
.graph-legend {
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}
.eyebrow {
color: var(--cyan-dark);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 3px 0 0;
font-size: clamp(22px, 3vw, 34px);
line-height: 1;
letter-spacing: -0.04em;
}
.header-note {
max-width: 430px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
text-align: right;
}
.layout {
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
gap: 18px;
align-items: stretch;
}
.stage {
position: relative;
min-height: 690px;
overflow: hidden;
border: 3px solid var(--line);
background: #dedbd2;
box-shadow: var(--shadow);
cursor: grab;
touch-action: none;
isolation: isolate;
}
.stage.dragging {
cursor: grabbing;
}
#viewport {
position: absolute;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
}
.stage-top {
position: absolute;
z-index: 4;
top: 18px;
left: 18px;
right: 18px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16px;
pointer-events: none;
}
.stage-title {
max-width: 420px;
padding: 13px 15px;
border: 2px solid var(--ink);
background: rgba(255, 253, 247, 0.9);
box-shadow: 4px 4px 0 rgba(23, 36, 43, 0.14);
backdrop-filter: blur(4px);
}
.stage-title h2 {
margin: 5px 0 4px;
font-size: clamp(20px, 2.2vw, 31px);
line-height: 1.05;
letter-spacing: -0.04em;
}
.stage-title p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.stage-tools {
display: flex;
flex-direction: column;
align-items: flex-end;
gap: 8px;
pointer-events: auto;
}
.locked-pill {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 9px 11px;
border: 2px solid var(--ink);
background: var(--cyan);
box-shadow: 4px 4px 0 rgba(23, 36, 43, 0.18);
font-size: 11px;
font-weight: 900;
letter-spacing: 0.04em;
}
.locked-pill::before {
content: "";
width: 8px;
height: 8px;
background: var(--ink);
box-shadow: 2px 2px 0 rgba(255, 255, 255, 0.35);
}
.view-button {
padding: 8px 10px;
border: 2px solid var(--ink);
background: var(--white);
box-shadow: 3px 3px 0 rgba(23, 36, 43, 0.18);
cursor: pointer;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.08em;
}
.view-button:hover {
background: #f8e6a8;
}
.stage-bottom {
position: absolute;
z-index: 4;
left: 18px;
right: 18px;
bottom: 16px;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 18px;
pointer-events: none;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 8px 10px;
border: 2px solid var(--ink);
background: rgba(255, 253, 247, 0.92);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.03em;
}
.legend-swatch {
width: 14px;
height: 8px;
background: var(--cyan);
border: 1px solid var(--ink);
}
.legend-swatch.red {
background: var(--red);
}
.stage-hint {
color: #34434a;
background: rgba(244, 240, 230, 0.82);
padding: 7px 9px;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.05em;
}
.world-label {
position: absolute;
z-index: 3;
transform: translate(-50%, -120%);
padding: 6px 8px;
border: 2px solid var(--ink);
background: var(--white);
box-shadow: 3px 3px 0 rgba(23, 36, 43, 0.18);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.025em;
white-space: nowrap;
pointer-events: none;
}
.world-label.cyan {
border-bottom: 5px solid var(--cyan);
}
.world-label.red {
border-bottom: 5px solid var(--red);
}
.world-label.coral {
border-bottom: 5px solid var(--red);
background: #ffe4df;
}
.panel {
border: 3px solid var(--line);
background: var(--white);
box-shadow: var(--shadow);
}
.panel-section {
padding: 18px;
border-bottom: 2px solid var(--line);
}
.panel-section:last-child {
border-bottom: 0;
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 13px;
}
.section-heading h3 {
margin: 0;
font-size: 12px;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.status {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 5px 7px;
background: #e4e8e7;
border: 2px solid var(--ink);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.07em;
}
.status::before {
content: "";
width: 7px;
height: 7px;
background: var(--amber);
}
.status[data-state="braking"]::before {
background: var(--red);
}
.status[data-state="stopped"]::before {
background: var(--cyan);
}
.model-grid {
display: grid;
grid-template-columns: 1fr 1fr;
border: 2px solid var(--ink);
background: var(--paper);
}
.model-cell {
padding: 10px 11px;
border-right: 2px solid var(--ink);
}
.model-cell:last-child {
border-right: 0;
}
.mini-label {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.model-value {
font-size: 18px;
font-weight: 900;
letter-spacing: -0.04em;
}
.model-value small {
font-size: 11px;
letter-spacing: 0;
}
.momentum-flow {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 10px;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 11px;
font-weight: 800;
}
.momentum-flow .arrow {
flex: 1;
height: 3px;
position: relative;
background: var(--cyan);
}
.momentum-flow .arrow::after {
content: "";
position: absolute;
right: -1px;
top: -4px;
border-left: 8px solid var(--cyan);
border-top: 5px solid transparent;
border-bottom: 5px solid transparent;
}
.control-label {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 12px;
margin-bottom: 10px;
}
.control-label label {
font-size: 12px;
font-weight: 900;
}
.duration-readout {
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 23px;
font-weight: 900;
letter-spacing: -0.05em;
}
.duration-readout small {
color: var(--muted);
font-size: 11px;
letter-spacing: 0;
}
input[type="range"] {
width: 100%;
height: 10px;
margin: 4px 0 8px;
appearance: none;
border: 2px solid var(--ink);
border-radius: 0;
background: linear-gradient(
to right,
var(--cyan) 0%,
var(--cyan) var(--fill, 37.5%),
#d8d7d1 var(--fill, 37.5%),
#d8d7d1 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
appearance: none;
width: 20px;
height: 24px;
border: 3px solid var(--ink);
border-radius: 0;
background: var(--white);
box-shadow: 3px 3px 0 rgba(23, 36, 43, 0.25);
}
input[type="range"]::-moz-range-thumb {
width: 16px;
height: 20px;
border: 3px solid var(--ink);
border-radius: 0;
background: var(--white);
box-shadow: 3px 3px 0 rgba(23, 36, 43, 0.25);
}
.range-limits {
display: flex;
justify-content: space-between;
color: var(--muted);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 9px;
font-weight: 800;
}
.profile-label {
margin: 17px 0 8px;
font-size: 11px;
font-weight: 900;
}
.profiles {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.profile-button {
padding: 9px 8px;
border: 2px solid var(--ink);
background: var(--paper);
box-shadow: 3px 3px 0 rgba(23, 36, 43, 0.12);
cursor: pointer;
font-size: 9px;
font-weight: 900;
line-height: 1.25;
text-align: left;
}
.profile-button:hover {
background: #e9f8f5;
}
.profile-button.active {
background: var(--cyan);
color: #082f2e;
}
.profile-description {
min-height: 44px;
margin: 10px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.play-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 8px;
margin-top: 13px;
}
.play-button,
.reset-button {
border: 2px solid var(--ink);
box-shadow: 3px 3px 0 rgba(23, 36, 43, 0.2);
cursor: pointer;
font-weight: 900;
text-transform: uppercase;
}
.play-button {
padding: 11px 14px;
background: var(--ink);
color: var(--white);
letter-spacing: 0.06em;
}
.play-button:hover {
background: var(--cyan-dark);
}
.reset-button {
padding: 9px 11px;
background: var(--white);
font-size: 10px;
}
.progress-track {
height: 8px;
margin-top: 11px;
border: 2px solid var(--ink);
background: var(--paper-2);
}
.progress-fill {
width: 0;
height: 100%;
background: var(--red);
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.metric {
min-height: 117px;
padding: 12px;
border: 2px solid var(--ink);
background: var(--paper);
}
.metric.average {
background: #e2f5f2;
}
.metric.peak {
background: #ffe5e1;
}
.metric-label {
min-height: 24px;
color: #425159;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.06em;
line-height: 1.35;
text-transform: uppercase;
}
.metric-value {
margin-top: 6px;
font-size: clamp(25px, 3vw, 34px);
font-weight: 950;
letter-spacing: -0.07em;
line-height: 1;
}
.metric-value small {
font-size: 13px;
letter-spacing: 0;
}
.metric-sub {
margin-top: 7px;
color: var(--muted);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 9px;
font-weight: 800;
}
.equation {
margin-top: 10px;
padding: 9px 10px;
border: 2px solid var(--ink);
background: var(--ink);
color: var(--white);
font-size: 10px;
font-weight: 800;
line-height: 1.55;
}
.equation strong {
color: #75e0d8;
}
.graph-wrap {
position: relative;
margin-top: 6px;
background: #f9f6ee;
border: 2px solid var(--ink);
}
.graph-wrap svg {
display: block;
width: 100%;
height: auto;
}
.graph-legend {
position: absolute;
right: 8px;
top: 7px;
display: flex;
gap: 9px;
color: var(--muted);
font-size: 8px;
font-weight: 900;
}
.graph-legend span::before {
content: "";
display: inline-block;
width: 11px;
height: 3px;
margin-right: 4px;
vertical-align: middle;
background: var(--red);
}
.graph-legend .avg-key::before {
background: repeating-linear-gradient(
90deg,
var(--amber) 0,
var(--amber) 4px,
transparent 4px,
transparent 7px
);
}
.impulse-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 9px;
}
.impulse-cell {
padding: 9px;
border: 2px solid var(--ink);
background: var(--paper);
}
.impulse-cell strong {
display: block;
margin-top: 4px;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 15px;
}
.assumption {
margin: 0;
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.assumption strong {
color: var(--ink);
}
footer {
margin-top: 15px;
color: var(--muted);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 9px;
line-height: 1.5;
text-align: right;
}
@media (max-width: 1050px) {
.layout {
grid-template-columns: 1fr;
}
.stage {
min-height: 610px;
}
.panel {
display: grid;
grid-template-columns: 1fr 1fr;
}
.panel-section {
border-right: 2px solid var(--line);
}
.panel-section:nth-child(2n) {
border-right: 0;
}
.panel-section:nth-last-child(-n + 2) {
border-bottom: 0;
}
}
@media (max-width: 680px) {
.app {
width: min(100% - 18px, 1480px);
padding-top: 12px;
}
.header {
align-items: flex-start;
}
.header-note {
display: none;
}
.stage {
min-height: 540px;
}
.stage-top {
top: 10px;
left: 10px;
right: 10px;
}
.stage-title {
max-width: 250px;
padding: 10px;
}
.stage-title p {
display: none;
}
.locked-pill {
font-size: 8px;
}
.stage-bottom {
left: 10px;
right: 10px;
bottom: 10px;
}
.stage-hint {
display: none;
}
.panel {
display: block;
}
.panel-section {
border-right: 0;
border-bottom: 2px solid var(--line) !important;
}
.panel-section:last-child {
border-bottom: 0 !important;
}
.world-label {
font-size: 8px;
}
footer {
text-align: left;
}
}
</style>
</head>
<body>
<div class="app">
<header class="header">
<div class="brand">
<div class="voxel-mark" aria-hidden="true">
<span></span><span></span><span></span>
<span></span><span></span><span></span>
<span></span><span></span><span></span>
</div>
<div>
<div class="eyebrow">Interactive mechanics / module 01</div>
<h1>Voxel Impulse Lab</h1>
</div>
</div>
<div class="header-note">
A fixed initial momentum, a variable stopping time, and a force–time profile.
Impulse determines the <strong>average</strong>; the profile determines the
<strong>peak</strong>.
</div>
</header>
<main class="layout">
<section class="stage" id="stage" aria-label="Interactive 3D controlled-stop model">
<div id="viewport"></div>
<div class="stage-top">
<div class="stage-title">
<div class="eyebrow">Controlled stop / live model</div>
<h2>Impulse, not instant force.</h2>
<p>
The cyan arrow is initial momentum. The red arrow is the changing
net braking force.
</p>
</div>
<div class="stage-tools">
<div class="locked-pill">p₀ = +18.0 N·s / locked</div>
<button class="view-button" id="resetView" type="button">Reset view</button>
</div>
</div>
<div class="world-label cyan" id="momentumLabel">INITIAL MOMENTUM +18.0 N·s</div>
<div class="world-label red" id="forceLabel">BRAKING FORCE 0.00 N</div>
<div class="world-label coral" id="stopLabel">STOP</div>
<div class="stage-bottom">
<div class="legend">
<div class="legend-item">
<span class="legend-swatch"></span>
Initial momentum
</div>
<div class="legend-item">
<span class="legend-swatch red"></span>
Net braking force
</div>
</div>
<div class="stage-hint">DRAG TO ORBIT · SCROLL TO ZOOM</div>
</div>
</section>
<aside class="panel">
<section class="panel-section">
<div class="section-heading">
<h3>Fixed initial model</h3>
<div class="status" id="status" data-state="ready">READY</div>
</div>
<div class="model-grid">
<div class="model-cell">
<span class="mini-label">Mass</span>
<div class="model-value">6.0 <small>kg</small></div>
</div>
<div class="model-cell">
<span class="mini-label">Initial speed</span>
<div class="model-value">+3.0 <small>m/s</small></div>
</div>
</div>
<div class="momentum-flow">
<span>p₀ = +18 N·s</span>
<span class="arrow"></span>
<span>pₓ = 0</span>
</div>
</section>
<section class="panel-section">
<div class="control-label">
<label for="duration">Stopping duration</label>
<div class="duration-readout">
<span id="durationValue">2.0</span> <small>s</small>
</div>
</div>
<input
id="duration"
type="range"
min="0.5"
max="5"
step="0.1"
value="2"
aria-label="Stopping duration in seconds"
/>
<div class="range-limits">
<span>0.5 s / abrupt</span>
<span>5.0 s / gentle</span>
</div>
<div class="profile-label">Force–time profile</div>
<div class="profiles" role="group" aria-label="Force profile">
<button class="profile-button" type="button" data-profile="constant">
CONSTANT
</button>
<button class="profile-button active" type="button" data-profile="smooth">
SMOOTH BELL
</button>
<button class="profile-button" type="button" data-profile="front">
FRONT-LOADED
</button>
<button class="profile-button" type="button" data-profile="late">
LATE RAMP
</button>
</div>
<p class="profile-description" id="profileDescription">
Symmetric smooth pulse. Peak = 1.50 × average.
</p>
<div class="play-row">
<button class="play-button" id="playButton" type="button">Start stop</button>
<button class="reset-button" id="resetSimulation" type="button">Reset</button>
</div>
<div class="progress-track" aria-hidden="true">
<div class="progress-fill" id="progressFill"></div>
</div>
</section>
<section class="panel-section">
<div class="section-heading">
<h3>Required force</h3>
<span class="mini-label">Magnitude / −x direction</span>
</div>
<div class="metrics">
<div class="metric average">
<div class="metric-label">Required average net force</div>
<div class="metric-value">
<span id="averageValue">9.00</span> <small>N</small>
</div>
<div class="metric-sub">SET BY IMPULSE ÷ TIME</div>
</div>
<div class="metric peak">
<div class="metric-label">Peak force for selected profile</div>
<div class="metric-value">
<span id="peakValue">13.50</span> <small>N</small>
</div>
<div class="metric-sub" id="peakSub">1.50 × AVERAGE</div>
</div>
</div>
<div class="equation" id="equation">
F̄ₓ = Δpₓ / Δt = −18 / 2.0 = <strong>−9.00 N</strong>
</div>
</section>
<section class="panel-section">
<div class="section-heading">
<h3>Force–time profile</h3>
<span class="mini-label">Area = impulse</span>
</div>
<div class="graph-wrap">
<svg viewBox="0 0 360 180" role="img" aria-label="Force versus time graph">
<g class="graph-axis" fill="#657178" font-size="9" font-weight="700">
<text x="7" y="17">F (N)</text>
<text x="43" y="166">0</text>
<text x="326" y="166" id="graphEndTime">2.0 s</text>
<text x="137" y="177">TIME</text>
</g>
<path d="M45 143 L45 20 M45 143 L345 143"
fill="none" stroke="#17242b" stroke-width="2"
vector-effect="non-scaling-stroke" />
<path id="forceArea" fill="#ef5350" fill-opacity="0.16" />
<path id="forceLine" fill="none" stroke="#ef5350" stroke-width="3"
stroke-linejoin="round" vector-effect="non-scaling-stroke" />
<line id="peakLine" x1="45" x2="345" stroke="#ef5350" stroke-width="1.5"
stroke-dasharray="4 4" vector-effect="non-scaling-stroke" />
<line id="averageLine" x1="45" x2="345" stroke="#e7a72e" stroke-width="2"
stroke-dasharray="8 5" vector-effect="non-scaling-stroke" />
<text id="peakGraphLabel" x="274" y="29" fill="#c53d3a"
font-family="monospace" font-size="8" font-weight="900">PEAK</text>
<text id="averageGraphLabel" x="52" y="139" fill="#9a6910"
font-family="monospace" font-size="8" font-weight="900">AVG</text>
<line id="graphMarkerLine" x1="45" x2="45" y1="20" y2="143"
stroke="#17242b" stroke-width="1.5" stroke-dasharray="3 3"
vector-effect="non-scaling-stroke" />
<circle id="graphMarker" cx="45" cy="143" r="5"
fill="#fffdf7" stroke="#17242b" stroke-width="2" />
</svg>
<div class="graph-legend">
<span>FORCE</span>
<span class="avg-key">AVERAGE</span>
</div>
</div>
<div class="impulse-row">
<div class="impulse-cell">
<span class="mini-label">Braking impulse</span>
<strong>Jₓ = −18.00 N·s</strong>
</div>
<div class="impulse-cell">
<span class="mini-label">Current speed</span>
<strong id="speedValue">3.00 m/s</strong>
</div>
</div>
</section>
<section class="panel-section">
<p class="assumption">
<strong>Model assumptions:</strong> constant mass and no other horizontal
forces, so “force” means net braking force. Impulse alone determines
average force—not peak force. Peak force depends on the selected
force–time profile. Real actuator force can differ if drag or friction
contributes to the stop.
</p>
</section>
</aside>
</main>
<footer>
Idealized rigid-body model · Units: kg, m, s, N, N·s · Fixed momentum p₀ = 18 N·s
</footer>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const stage = $("stage");
const viewport = $("viewport");
const COLORS = {
ink: 0x17242b,
cyan: 0x12b8b1,
cyanDark: 0x087f7b,
red: 0xef5350,
amber: 0xe7a72e,
paper: 0xf4f0e6,
ground: 0xdedbd2,
tileA: 0xaaa99f,
tileB: 0xb7b5aa,
rail: 0x555d5e,
white: 0xfffdf7
};
// ------------------------------------------------------------------
// Three.js scene
// ------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(COLORS.ground);
scene.fog = new THREE.Fog(COLORS.ground, 24, 46);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Voxel vehicle slowing to a stop under a changing braking force"
);
viewport.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-10, 10, 7, -7, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 1, 0);
let cameraYaw = 0.64;
let cameraPitch = 0.61;
let cameraZoom = 1;
function updateCamera() {
const radius = 23;
camera.position.set(
cameraTarget.x + radius * Math.cos(cameraPitch) * Math.sin(cameraYaw),
cameraTarget.y + radius * Math.sin(cameraPitch),
cameraTarget.z + radius * Math.cos(cameraPitch) * Math.cos(cameraYaw)
);
camera.lookAt(cameraTarget);
camera.zoom = cameraZoom;
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
}
function resize() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
const aspect = width / Math.max(height, 1);
// Always preserve enough horizontal room for the full track.
const viewHeight = Math.max(12.5, 20 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
renderer.setSize(width, height, false);
updateCamera();
}
new ResizeObserver(resize).observe(viewport);
resize();
const hemisphere = new THREE.HemisphereLight(0xffffff, 0x747b78, 2.4);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffffff, 3.2);
sun.position.set(-8, 15, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -14;
sun.shadow.camera.right = 14;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.bias = -0.0004;
scene.add(sun);
const materialCache = new Map();
function material(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.84,
metalness: 0.02
})
);
}
return materialCache.get(color);
}
function box(width, height, depth, color, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material(color)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Ground.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(50, 38),
material(COLORS.ground)
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.08;
ground.receiveShadow = true;
scene.add(ground);
// Voxel track.
box(19.2, 0.12, 3.35, 0x777b75, 0, -0.01, 0);
for (let i = 0; i < 18; i++) {
const x = -8.5 + i;
box(
0.94,
0.12,
2.7,
i % 2 === 0 ? COLORS.tileA : COLORS.tileB,
x,
0.1,
0
);
}
box(19.1, 0.16, 0.22, COLORS.rail, 0, 0.16, -1.48);
box(19.1, 0.16, 0.22, COLORS.rail, 0, 0.16, 1.48);
// Small voxel scenery for depth.
const scenery = [
[-8.8, 0.55, -4.6, 1.4, 1.1, 1.4, 0x8fa5a1],
[-7.3, 0.38, -4.8, 0.8, 0.8, 0.8, 0xc3bca9],
[8.5, 0.8, -4.8, 1.7, 1.6, 1.7, 0x9caaa4],
[7.2, 0.42, -4.3, 0.9, 0.9, 0.9, 0xd0c7b2]
];
scenery.forEach(([x, y, z, w, h, d, c]) => {
box(w, h, d, c, x, y, z);
});
// Voxel vehicle.
const vehicle = new THREE.Group();
scene.add(vehicle);
// Wheels are cubes to maintain the voxel aesthetic.
[-0.72, 0.72].forEach((x) => {
[-0.84, 0.84].forEach((z) => {
box(0.5, 0.48, 0.34, 0x263238, x, 0.39, z, vehicle);
box(0.19, 0.19, 0.08, 0xb8c0bd, x, 0.39, z + Math.sign(z) * 0.2, vehicle);
});
});
box(2.3, 0.48, 1.42, COLORS.ink, 0, 0.78, 0, vehicle);
box(1.7, 0.42, 1.25, 0xe3b83e, -0.15, 1.22, 0, vehicle);
box(0.8, 0.72, 1.05, COLORS.cyan, 0.55, 1.52, 0, vehicle);
box(0.5, 0.4, 0.86, 0x243a43, 0.68, 1.61, 0.02, vehicle);
box(0.13, 0.16, 0.2, 0xfff4bd, 1.17, 0.87, 0.49, vehicle);
box(0.13, 0.16, 0.2, 0xfff4bd, 1.17, 0.87, -0.49, vehicle);
box(0.22, 0.2, 0.22, 0xffffff, 0.55, 1.96, 0, vehicle);
// Initial momentum marker and start flag.
const START_X = -6.5;
function makeMarker(color) {
const group = new THREE.Group();
box(0.22, 0.08, 2.85, color, 0, 0.2, 0, group);
box(0.12, 1.4, 0.12, COLORS.ink, 0, 0.9, -1.15, group);
box(0.72, 0.4, 0.12, color, 0.34, 1.38, -1.15, group);
scene.add(group);
return group;
}
const startMarker = makeMarker(COLORS.cyan);
startMarker.position.x = START_X;
const stopMarker = makeMarker(COLORS.red);
// Stepped block arrows.
function createVoxelArrow(color) {
const group = new THREE.Group();
box(0.95, 0.16, 0.18, color, 0.48, 0, 0, group);
box(0.22, 0.43, 0.43, color, 1.02, 0, 0, group);
box(0.22, 0.29, 0.29, color, 1.2, 0, 0, group);
box(0.22, 0.15, 0.15, color, 1.38, 0, 0, group);
scene.add(group);
return group;
}
const momentumArrow = createVoxelArrow(COLORS.cyan);
const forceArrow = createVoxelArrow(COLORS.red);
forceArrow.rotation.y = Math.PI;
// ------------------------------------------------------------------
// Physics model
// ------------------------------------------------------------------
const model = {
mass: 6.0,
initialSpeed: 3.0,
initialMomentum: 18.0,
sceneDistanceScale: 0.9
};
/*
f(u): normalized force shape, u = elapsed / duration.
g(u): remaining velocity divided by initial velocity.
q(u): normalized distance traveled, ending at the physical stop point.
Each profile has integral f(u) du = 1, so its area is exactly one
average-force interval.
*/
const profiles = {
constant: {
name: "Constant force",
peakFactor: 1,
description: "Constant net braking force. In this special case, peak equals average.",
f: (u) => 1,
g: (u) => 1 - u,
q: (u) => u - 0.5 * u * u
},
smooth: {
name: "Smooth bell",
peakFactor: 1.5,
description: "Symmetric smooth pulse. Peak = 1.50 × average.",
f: (u) => 6 * u * (1 - u),
g: (u) => 1 - 3 * u * u + 2 * u * u * u,
q: (u) => u - u * u * u + 0.5 * u * u * u * u
},
front: {
name: "Front-loaded force",
peakFactor: 3,
description: "Force is greatest at contact, then tapers. Peak = 3.00 × average.",
f: (u) => 3 * (1 - u) * (1 - u),
g: (u) => Math.pow(1 - u, 3),
q: (u) => u - 1.5 * u * u + u * u * u - 0.25 * u * u * u * u
},
late: {
name: "Late ramp",
peakFactor: 3,
description: "Force ramps up late. Peak = 3.00 × average.",
f: (u) => 3 * u * u,
g: (u) => 1 - u * u * u,
q: (u) => u - 0.25 * u * u * u * u
}
};
let duration = 2;
let profileKey = "smooth";
let elapsed = 0;
let playing = false;
let averageForce = 9;
let peakForce = 13.5;
let sceneStopDistance = 0;
let currentForce = 0;
const graph = {
left: 45,
right: 345,
top: 20,
bottom: 143,
width: 300,
height: 123
};
function updateRangeFill() {
const slider = $("duration");
const percent =
((Number(slider.value) - Number(slider.min)) /
(Number(slider.max) - Number(slider.min))) *
100;
slider.style.setProperty("--fill", `${percent}%`);
}
function updateGraph() {
const profile = profiles[profileKey];
const points = [];
const yScale = graph.height / Math.max(peakForce * 1.12, 0.001);
for (let i = 0; i <= 60; i++) {
const u = i / 60;
const x = graph.left + u * graph.width;
const force = averageForce * profile.f(u);
const y = graph.bottom - force * yScale;
points.push([x, y]);
}
const linePath =
"M " +
points
.map(([x, y]) => `${x.toFixed(2)},${y.toFixed(2)}`)
.join(" L ");
const areaPath =
`M ${graph.left},${graph.bottom} L ` +
points
.map(([x, y]) => `${x.toFixed(2)},${y.toFixed(2)}`)
.join(" L ") +
` L ${graph.right},${graph.bottom} Z`;
$("forceLine").setAttribute("d", linePath);
$("forceArea").setAttribute("d", areaPath);
const averageY = graph.bottom - averageForce * yScale;
const peakY = graph.bottom - peakForce * yScale;
$("averageLine").setAttribute("y1", averageY);
$("averageLine").setAttribute("y2", averageY);
$("peakLine").setAttribute("y1", peakY);
$("peakLine").setAttribute("y2", peakY);
$("averageGraphLabel").setAttribute("y", averageY + 13);
$("peakGraphLabel").setAttribute("y", peakY - 6);
$("peakGraphLabel").textContent = `PEAK ${peakForce.toFixed(2)} N`;
$("averageGraphLabel").textContent = `AVG ${averageForce.toFixed(2)} N`;
$("graphEndTime").textContent = `${duration.toFixed(1)} s`;
}
function applySettings() {
duration = Number($("duration").value);
const profile = profiles[profileKey];
averageForce = model.initialMomentum / duration;
peakForce = averageForce * profile.peakFactor;
sceneStopDistance =
model.initialSpeed *
duration *
model.sceneDistanceScale;
elapsed = 0;
playing = false;
$("durationValue").textContent = duration.toFixed(1);
$("averageValue").textContent = averageForce.toFixed(2);
$("peakValue").textContent = peakForce.toFixed(2);
$("peakSub").textContent = `${profile.peakFactor.toFixed(2)} × AVERAGE`;
$("profileDescription").textContent = profile.description;
$("equation").innerHTML =
`F̄ₓ = Δpₓ / Δt = −18 / ${duration.toFixed(1)} = ` +
`<strong>−${averageForce.toFixed(2)} N</strong>`;
document.querySelectorAll(".profile-button").forEach((button) => {
const active = button.dataset.profile === profileKey;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
updateRangeFill();
updateGraph();
updateSimulation();
}
function updateSimulation() {
const u = THREE.MathUtils.clamp(elapsed / duration, 0, 1);
const profile = profiles[profileKey];
const positionRatio = profile.q(u);
const velocityRatio = profile.g(u);
vehicle.position.x = START_X + sceneStopDistance * positionRatio;
momentumArrow.position.set(vehicle.position.x, 3.12, -0.35);
forceArrow.position.set(vehicle.position.x, 2.05, 0.72);
currentForce = averageForce * profile.f(u);
const arrowLength = 0.56 + 0.95 * (1 - Math.exp(-currentForce / 22));
forceArrow.scale.x = arrowLength;
forceArrow.visible = currentForce > 0.015;
stopMarker.position.x = START_X + sceneStopDistance * profile.q(1);
$("forceLabel").textContent = `BRAKING FORCE ${currentForce.toFixed(2)} N`;
$("speedValue").textContent = `${(
model.initialSpeed * velocityRatio
).toFixed(2)} m/s`;
$("progressFill").style.width = `${u * 100}%`;
const markerX = graph.left + u * graph.width;
const yScale = graph.height / Math.max(peakForce * 1.12, 0.001);
const markerY = graph.bottom - currentForce * yScale;
$("graphMarkerLine").setAttribute("x1", markerX);
$("graphMarkerLine").setAttribute("x2", markerX);
$("graphMarker").setAttribute("cx", markerX);
$("graphMarker").setAttribute("cy", markerY);
const status = $("status");
if (elapsed <= 0 && !playing) {
status.textContent = "READY";
status.dataset.state = "ready";
$("playButton").textContent = "Start stop";
} else if (elapsed >= duration) {
status.textContent = "STOPPED";
status.dataset.state = "stopped";
$("playButton").textContent = "Replay";
} else {
status.textContent = "BRAKING";
status.dataset.state = "braking";
$("playButton").textContent = "Pause";
}
}
// ------------------------------------------------------------------
// HTML controls
// ------------------------------------------------------------------
$("duration").addEventListener("input", applySettings);
document.querySelectorAll(".profile-button").forEach((button) => {
button.addEventListener("click", () => {
profileKey = button.dataset.profile;
applySettings();
});
});
$("playButton").addEventListener("click", () => {
if (elapsed >= duration) {
elapsed = 0;
}
playing = !playing;
updateSimulation();
});
$("resetSimulation").addEventListener("click", () => {
elapsed = 0;
playing = false;
updateSimulation();
});
// ------------------------------------------------------------------
// Direct camera controls: orbit, zoom, and reset
// ------------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
stage.addEventListener("pointerdown", (event) => {
if (event.target.closest("button")) return;
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
stage.setPointerCapture(event.pointerId);
stage.classList.add("dragging");
});
stage.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraYaw -= deltaX * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + deltaY * 0.005,
0.25,
1.18
);
updateCamera();
});
function endDrag(event) {
dragging = false;
stage.classList.remove("dragging");
if (event.pointerId !== undefined && stage.hasPointerCapture(event.pointerId)) {
stage.releasePointerCapture(event.pointerId);
}
}
stage.addEventListener("pointerup", endDrag);
stage.addEventListener("pointercancel", endDrag);
stage.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * Math.exp(-event.deltaY * 0.001),
0.72,
1.65
);
updateCamera();
},
{ passive: false }
);
$("resetView").addEventListener("click", () => {
cameraYaw = 0.64;
cameraPitch = 0.61;
cameraZoom = 1;
updateCamera();
});
// ------------------------------------------------------------------
// Project 3D anchor points to HTML labels without addons
// ------------------------------------------------------------------
const projected = new THREE.Vector3();
function placeLabel(element, x, y, z) {
projected.set(x, y, z).project(camera);
const screenX = (projected.x * 0.5 + 0.5) * stage.clientWidth;
const screenY = (-projected.y * 0.5 + 0.5) * stage.clientHeight;
element.style.left = `${screenX}px`;
element.style.top = `${screenY}px`;
}
function updateLabels() {
placeLabel(
$("momentumLabel"),
vehicle.position.x + 1.85,
3.35,
-0.35
);
placeLabel(
$("forceLabel"),
vehicle.position.x - 0.85,
2.25,
0.72
);
placeLabel(
$("stopLabel"),
stopMarker.position.x,
1.75,
-1.15
);
}
// ------------------------------------------------------------------
// Animation loop
// ------------------------------------------------------------------
let previousTime = performance.now();
function animate(now) {
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (playing) {
elapsed = Math.min(duration, elapsed + delta);
if (elapsed >= duration) {
playing = false;
}
}
updateSimulation();
renderer.render(scene, camera);
updateLabels();
requestAnimationFrame(animate);
}
applySettings();
requestAnimationFrame(animate);
</script>
</body>
</html>
```