Prompt specification
Build or customize the Three.js 3D scene "Traction Changes the Toy Outcome" 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: Traction Changes the Toy Outcome
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/traction-changes-the-toy-outcome-x3676
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows a controlled tug model changing one cart's traction limit while keeping its drive command fixed. Show the resulting motion under explicit assumptions rather than human-strength predictions. Voxel style.
Working Three.js Starter (traction-changes-the-toy-outcome-x3676):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Controlled Tug — Traction Limit Experiment</title>
<style>
:root {
--bg: #080d18;
--panel: #101827;
--panel-2: #172235;
--line: #26364d;
--text: #e8f0fb;
--muted: #8fa4bd;
--cyan: #67e8f9;
--blue: #60a5fa;
--orange: #ff9f43;
--yellow: #ffd166;
--green: #55d187;
--red: #ff647c;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#app {
display: grid;
grid-template-columns: 350px minmax(0, 1fr);
width: 100%;
height: 100%;
}
#panel {
position: relative;
z-index: 5;
min-width: 0;
height: 100%;
padding: 20px;
overflow-y: auto;
background:
linear-gradient(180deg, rgba(23, 34, 53, 0.98), rgba(11, 18, 30, 0.98)),
repeating-linear-gradient(
90deg,
transparent 0,
transparent 19px,
rgba(255, 255, 255, 0.018) 20px
);
border-right: 1px solid var(--line);
box-shadow: 12px 0 32px rgba(0, 0, 0, 0.22);
}
.eyebrow {
color: var(--cyan);
font: 700 11px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 8px 0 8px;
font-size: 27px;
line-height: 1.06;
letter-spacing: -0.03em;
}
.subtitle {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.warning {
margin: 16px 0;
padding: 11px 12px;
background: rgba(255, 159, 67, 0.08);
border: 1px solid rgba(255, 159, 67, 0.35);
border-left: 4px solid var(--orange);
color: #ffd7b0;
font-size: 12px;
line-height: 1.45;
}
.section {
margin-top: 18px;
padding-top: 17px;
border-top: 1px solid var(--line);
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
color: #bdcce0;
font: 700 11px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.locked {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 5px 7px;
color: var(--cyan);
background: rgba(103, 232, 249, 0.08);
border: 1px solid rgba(103, 232, 249, 0.24);
font-size: 10px;
letter-spacing: 0.08em;
}
.lock-icon {
width: 9px;
height: 8px;
border: 2px solid currentColor;
border-bottom: 0;
border-radius: 1px 1px 0 0;
position: relative;
}
.lock-icon::after {
content: "";
position: absolute;
width: 3px;
height: 7px;
left: 1px;
top: 5px;
background: currentColor;
}
.command-card {
padding: 14px;
background: var(--panel-2);
border: 1px solid var(--line);
}
.command-value {
margin-top: 7px;
color: var(--cyan);
font: 800 28px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: -0.04em;
}
.command-value small {
margin-left: 4px;
color: var(--muted);
font-size: 12px;
font-weight: 500;
letter-spacing: 0;
}
.command-note {
margin-top: 8px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.control-label {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 12px;
margin-bottom: 10px;
}
.control-label label {
color: #c8d4e5;
font-size: 13px;
font-weight: 650;
}
.control-value {
color: var(--orange);
font: 800 22px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.control-value small {
color: var(--muted);
font-size: 11px;
font-weight: 500;
}
input[type="range"] {
width: 100%;
height: 7px;
margin: 6px 0 12px;
appearance: none;
background:
linear-gradient(
90deg,
var(--orange) 0%,
var(--orange) var(--fill),
#27364b var(--fill),
#27364b 100%
);
border: 0;
outline: none;
cursor: ew-resize;
}
input[type="range"]::-webkit-slider-thumb {
width: 18px;
height: 18px;
appearance: none;
background: var(--orange);
border: 3px solid #101827;
box-shadow: 0 0 0 1px var(--orange);
cursor: ew-resize;
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
background: var(--orange);
border: 3px solid #101827;
box-shadow: 0 0 0 1px var(--orange);
border-radius: 0;
cursor: ew-resize;
}
.presets {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.preset,
.action {
min-height: 35px;
padding: 8px 6px;
color: #cbd7e8;
background: #182437;
border: 1px solid #33465f;
cursor: pointer;
font: 700 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
transition:
background 120ms ease,
border-color 120ms ease,
color 120ms ease;
}
.preset:hover,
.action:hover {
color: white;
background: #23334c;
border-color: #587397;
}
.preset.active {
color: #101827;
background: var(--orange);
border-color: var(--orange);
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 12px;
}
.action.primary {
color: #07121c;
background: var(--cyan);
border-color: var(--cyan);
}
.chart-wrap {
padding: 8px 8px 5px;
background: #0b1321;
border: 1px solid var(--line);
}
.chart-wrap svg {
display: block;
width: 100%;
height: 105px;
overflow: visible;
}
.chart-caption {
display: flex;
justify-content: space-between;
margin-top: 4px;
color: var(--muted);
font-size: 10px;
}
.legend-dot {
display: inline-block;
width: 7px;
height: 7px;
margin-right: 5px;
vertical-align: 1px;
}
details {
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
summary {
color: #cbd7e8;
cursor: pointer;
font-weight: 700;
}
details ol {
margin: 11px 0 0;
padding-left: 19px;
}
details li {
margin: 8px 0;
}
.equation {
margin-top: 12px;
padding: 10px;
color: #d7e8fb;
background: #0b1321;
border-left: 3px solid var(--blue);
font: 11px/1.55 ui-monospace, SFMono-Regular, Menlo, monospace;
overflow-wrap: anywhere;
}
#viewport {
position: relative;
min-width: 0;
height: 100%;
overflow: hidden;
background: #09111f;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.scene-header {
position: absolute;
z-index: 3;
top: 20px;
left: 22px;
right: 22px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 18px;
pointer-events: none;
}
.scene-title {
padding: 12px 14px;
background: rgba(8, 14, 25, 0.76);
border: 1px solid rgba(81, 105, 136, 0.42);
backdrop-filter: blur(8px);
}
.scene-title strong {
display: block;
margin-bottom: 4px;
font: 800 13px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.06em;
}
.scene-title span {
color: var(--muted);
font-size: 11px;
}
.scene-legend {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 7px;
}
.legend-chip {
padding: 7px 9px;
color: #c7d4e5;
background: rgba(8, 14, 25, 0.76);
border: 1px solid rgba(81, 105, 136, 0.42);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.legend-chip i {
display: inline-block;
width: 7px;
height: 7px;
margin-right: 6px;
vertical-align: 1px;
}
.metrics {
position: absolute;
z-index: 3;
right: 20px;
bottom: 20px;
left: 20px;
display: grid;
grid-template-columns: repeat(5, minmax(100px, 1fr));
gap: 8px;
pointer-events: none;
}
.metric {
min-width: 0;
padding: 11px 12px;
background: rgba(8, 14, 25, 0.83);
border: 1px solid rgba(81, 105, 136, 0.42);
backdrop-filter: blur(8px);
}
.metric-label {
overflow: hidden;
color: var(--muted);
font: 700 9px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
.metric-value {
margin-top: 6px;
overflow: hidden;
color: var(--text);
font: 800 18px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
text-overflow: ellipsis;
white-space: nowrap;
}
.metric-value.orange {
color: var(--orange);
}
.metric-value.cyan {
color: var(--cyan);
}
.metric-value.green {
color: var(--green);
}
.world-label {
position: absolute;
z-index: 2;
left: 0;
top: 0;
padding: 5px 7px;
color: #eaf3ff;
background: rgba(7, 12, 22, 0.82);
border: 1px solid rgba(116, 145, 179, 0.52);
font: 700 9px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
pointer-events: none;
white-space: nowrap;
will-change: transform;
}
.world-label.cart {
color: #ffd3ae;
border-color: rgba(255, 159, 67, 0.65);
}
.world-label.ref {
color: #c8f6ff;
border-color: rgba(103, 232, 249, 0.55);
}
.camera-help {
position: absolute;
z-index: 3;
left: 22px;
bottom: 112px;
padding: 7px 9px;
color: #8095ae;
background: rgba(8, 14, 25, 0.68);
border: 1px solid rgba(81, 105, 136, 0.3);
font: 10px/1.3 ui-monospace, SFMono-Regular, Menlo, monospace;
pointer-events: none;
}
.status-line {
position: absolute;
z-index: 3;
top: 82px;
left: 22px;
display: flex;
align-items: center;
gap: 8px;
padding: 7px 9px;
color: var(--muted);
background: rgba(8, 14, 25, 0.72);
border: 1px solid rgba(81, 105, 136, 0.36);
font: 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
pointer-events: none;
}
#statusLight {
width: 7px;
height: 7px;
background: var(--green);
box-shadow: 0 0 9px currentColor;
}
@media (max-width: 980px) {
#app {
grid-template-columns: 300px minmax(0, 1fr);
}
#panel {
padding: 16px;
}
.metrics {
grid-template-columns: repeat(3, 1fr);
}
.metric:nth-child(4),
.metric:nth-child(5) {
display: none;
}
}
@media (max-width: 720px) {
#app {
display: block;
}
#viewport {
width: 100%;
height: 100%;
}
#panel {
position: absolute;
z-index: 10;
top: 10px;
left: 10px;
width: 270px;
max-height: calc(100vh - 20px);
padding: 13px;
background: rgba(12, 20, 34, 0.94);
backdrop-filter: blur(10px);
}
#panel h1 {
font-size: 21px;
}
.scene-header {
left: auto;
width: 220px;
}
.scene-legend,
.camera-help {
display: none;
}
.metrics {
right: 10px;
bottom: 10px;
left: 10px;
grid-template-columns: repeat(2, 1fr);
}
.metric:nth-child(3) {
display: none;
}
}
</style>
</head>
<body>
<div id="app">
<aside id="panel">
<div class="eyebrow">Traction Laboratory / Experiment 01</div>
<h1>Controlled Tug</h1>
<p class="subtitle">
Change one cart’s effective traction budget while the tug’s speed command,
cart mass, and initial condition remain fixed.
</p>
<div class="warning">
<strong>Model comparison, not a human-strength prediction.</strong><br />
This reduced-order simulation makes assumptions explicit and does not
estimate what a person can pull, lift, or safely control.
</div>
<section class="section">
<div class="section-title">
<span>Held Fixed</span>
<span class="locked"><i class="lock-icon"></i> Locked</span>
</div>
<div class="command-card">
<div class="eyebrow">Tug Drive Command</div>
<div class="command-value">
1.35 <small>m/s</small>
</div>
<div class="command-note">
Ideal velocity-controlled tug. Maximum requested drawbar force:
<strong>850 N</strong>.
</div>
</div>
</section>
<section class="section">
<div class="section-title">
<span>Variable</span>
<span>Cart A</span>
</div>
<div class="control-label">
<label for="traction">Effective traction limit</label>
<div class="control-value">
<span id="tractionValue">260</span> <small>N</small>
</div>
</div>
<input
id="traction"
type="range"
min="0"
max="850"
step="10"
value="260"
aria-label="Cart traction limit"
/>
<div class="presets">
<button class="preset" data-cap="80">SLIP / 80</button>
<button class="preset active" data-cap="260">MARGINAL / 260</button>
<button class="preset" data-cap="850">ROLL / 850</button>
</div>
<div class="actions">
<button id="pause" class="action primary">Pause</button>
<button id="restart" class="action">Restart Trial</button>
</div>
<div class="chart-wrap">
<svg viewBox="0 0 280 90" preserveAspectRatio="none" aria-label="Cart speed chart">
<defs>
<linearGradient id="chartFade" x1="0" x2="0" y1="0" y2="1">
<stop offset="0%" stop-color="#ff9f43" stop-opacity="0.22" />
<stop offset="100%" stop-color="#ff9f43" stop-opacity="0" />
</linearGradient>
</defs>
<g stroke="#233249" stroke-width="1">
<line x1="8" y1="12" x2="272" y2="12" />
<line x1="8" y1="34" x2="272" y2="34" />
<line x1="8" y1="56" x2="272" y2="56" />
<line x1="8" y1="78" x2="272" y2="78" />
<line x1="8" y1="8" x2="8" y2="78" />
<line x1="74" y1="8" x2="74" y2="78" />
<line x1="140" y1="8" x2="140" y2="78" />
<line x1="206" y1="8" x2="206" y2="78" />
<line x1="272" y1="8" x2="272" y2="78" />
</g>
<line
x1="8"
y1="12"
x2="272"
y2="12"
stroke="#67e8f9"
stroke-width="1"
stroke-dasharray="4 4"
opacity="0.65"
/>
<polyline
id="chartActual"
fill="none"
stroke="#ff9f43"
stroke-width="2.5"
points=""
/>
<polyline
id="chartReference"
fill="none"
stroke="#67e8f9"
stroke-width="1.5"
stroke-dasharray="4 3"
points=""
/>
<text x="9" y="88" fill="#71869f" font-size="8">0 s</text>
<text x="125" y="88" fill="#71869f" font-size="8">6 s</text>
<text x="253" y="88" fill="#71869f" font-size="8">12 s</text>
<text x="10" y="10" fill="#67e8f9" font-size="7">1.35 m/s</text>
</svg>
<div class="chart-caption">
<span><i class="legend-dot" style="background:#ff9f43"></i>Actual cart</span>
<span><i class="legend-dot" style="background:#67e8f9"></i>850 N reference</span>
</div>
</div>
</section>
<section class="section">
<details open>
<summary>Explicit model assumptions</summary>
<ol>
<li>
Motion is planar. The tug is an ideal velocity-controlled winch
operating at <strong>1.35 m/s</strong>.
</li>
<li>
The tug requests
<strong>F<sub>req</sub> = min(850, 12 + 700 max(0, v<sub>cmd</sub> − v))</strong>
newtons to close the speed error.
</li>
<li>
Cart A’s effective road-traction budget caps the force that can
accelerate it:
<strong>F<sub>cart</sub> = min(F<sub>req</sub>, T<sub>limit</sub>)</strong>.
</li>
<li>
Cart mass is <strong>220 kg</strong> and rolling resistance is
<strong>12 N</strong>. A limit below 12 N cannot start the cart
from rest.
</li>
<li>
Saturation is represented by ideal drawbar extension, not a detailed
tire-slip or structural failure model. The visible span is therefore
a motion-deficit indicator.
</li>
<li>
Grade, aerodynamic drag, shifting cargo, coupler elasticity,
braking, and human exertion are excluded. The cyan ghost is the
same model with an 850 N traction budget.
</li>
</ol>
<div class="equation">
a = (F<sub>cart</sub> − F<sub>roll</sub>) / m<br />
F<sub>cart</sub> ≤ T<sub>limit</sub>
</div>
</details>
</section>
<section class="section">
<div class="section-title"><span>Controls</span><span>Local</span></div>
<div class="command-note">
Drag the scene to orbit. Use the mouse wheel to zoom. Changing the
traction slider restarts the 12-second trial so every run starts from
the same position and velocity.
</div>
</section>
</aside>
<main id="viewport">
<canvas id="scene"></canvas>
<div class="scene-header">
<div class="scene-title">
<strong>FIXED-COMMAND MOTION TEST</strong>
<span>Voxel planar surrogate · live force-limited response</span>
</div>
<div class="scene-legend">
<div class="legend-chip"><i style="background:#67e8f9"></i>REQUESTED</div>
<div class="legend-chip"><i style="background:#ff9f43"></i>DELIVERED</div>
<div class="legend-chip"><i style="background:#c8f6ff"></i>REFERENCE</div>
</div>
</div>
<div class="status-line">
<i id="statusLight"></i>
<span id="statusText">FOLLOWING</span>
<span style="opacity:.45">/</span>
<span id="trialText">TRIAL 00.0 s</span>
</div>
<div id="labelTug" class="world-label">TUG · COMMAND 1.35 m/s</div>
<div id="labelCart" class="world-label cart">CART A · LIMIT 260 N</div>
<div id="labelRef" class="world-label ref">REFERENCE · 850 N</div>
<div class="camera-help">DRAG TO ORBIT · WHEEL TO ZOOM</div>
<div class="metrics">
<div class="metric">
<div class="metric-label">Cart speed</div>
<div id="metricSpeed" class="metric-value orange">0.00 m/s</div>
</div>
<div class="metric">
<div class="metric-label">Delivered force</div>
<div id="metricForce" class="metric-value orange">0 N</div>
</div>
<div class="metric">
<div class="metric-label">Traction use</div>
<div id="metricUse" class="metric-value">0%</div>
</div>
<div class="metric">
<div class="metric-label">Drawbar span</div>
<div id="metricSpan" class="metric-value cyan">2.10 m</div>
</div>
<div class="metric">
<div class="metric-label">Lag vs reference</div>
<div id="metricLag" class="metric-value green">0.00 m</div>
</div>
</div>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Model constants
// -------------------------------------------------------------------------
const MODEL = Object.freeze({
commandSpeed: 1.35, // m/s, locked
maxRequestForce: 850, // N
controllerGain: 700, // N per m/s
cartMass: 220, // kg
rollingResistance: 12, // N
initialCenterDistance: 4.2,
trialDuration: 12,
referenceLimit: 850
});
const COLORS = Object.freeze({
background: 0x09111f,
groundA: 0x202a3b,
groundB: 0x1b2434,
lane: 0x29364a,
laneAlt: 0x243145,
tug: 0x31c7b7,
tugDark: 0x167d7d,
cart: 0xff9f43,
cartDark: 0xb95b26,
cyan: 0x67e8f9,
blue: 0x60a5fa,
yellow: 0xffd166,
tire: 0x101722,
metal: 0x71829a,
cargo: 0xa9845c,
window: 0x173a61
});
const viewport = document.getElementById("viewport");
const canvas = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(COLORS.background);
scene.fog = new THREE.Fog(COLORS.background, 25, 70);
const camera = new THREE.PerspectiveCamera(
42,
viewport.clientWidth / Math.max(1, viewport.clientHeight),
0.1,
180
);
const hemisphere = new THREE.HemisphereLight(0xb8d8ff, 0x182031, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe4bd, 3.2);
sun.position.set(7, 18, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -30;
sun.shadow.camera.right = 30;
sun.shadow.camera.top = 22;
sun.shadow.camera.bottom = -22;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 60;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x60a5fa, 1.3);
rimLight.position.set(-8, 8, -12);
scene.add(rimLight);
const materialCache = new Map();
function material(color, options = {}) {
const key = `${color}-${JSON.stringify(options)}`;
if (!materialCache.has(key)) {
materialCache.set(
key,
new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.08,
...options
})
);
}
return materialCache.get(key);
}
function addBox(parent, size, position, color, options = {}) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material(color, options)
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Voxel tug
// -------------------------------------------------------------------------
function createVoxelTug() {
const group = new THREE.Group();
const wheels = [];
addBox(group, [2.25, 0.30, 1.25], [0, 0.42, 0], COLORS.tugDark);
addBox(group, [1.55, 0.62, 1.12], [0.18, 0.83, 0], COLORS.tug);
addBox(group, [0.95, 0.44, 1.08], [0.75, 1.05, 0], COLORS.tug);
addBox(group, [0.88, 0.90, 1.08], [-0.42, 1.36, 0], COLORS.tugDark);
addBox(group, [1.02, 0.16, 1.20], [-0.42, 1.88, 0], COLORS.tug);
// Windows, deliberately block-shaped.
addBox(group, [0.06, 0.43, 0.86], [0.045, 1.43, 0], COLORS.window, {
emissive: 0x0c243c,
emissiveIntensity: 0.6
});
addBox(group, [0.60, 0.42, 0.06], [-0.42, 1.45, 0.565], COLORS.window, {
emissive: 0x0c243c,
emissiveIntensity: 0.6
});
addBox(group, [0.60, 0.42, 0.06], [-0.42, 1.45, -0.565], COLORS.window, {
emissive: 0x0c243c,
emissiveIntensity: 0.6
});
// Roof beacon and exhaust blocks.
addBox(group, [0.20, 0.17, 0.20], [-0.38, 2.06, 0], COLORS.yellow, {
emissive: 0x6b4300,
emissiveIntensity: 0.8
});
addBox(group, [0.15, 0.50, 0.15], [-0.74, 2.12, -0.31], COLORS.metal);
// Bumpers and hitch.
addBox(group, [0.18, 0.24, 1.42], [1.18, 0.48, 0], COLORS.metal);
addBox(group, [0.18, 0.24, 1.42], [-1.18, 0.48, 0], COLORS.metal);
addBox(group, [0.40, 0.13, 0.16], [-1.36, 0.49, 0], COLORS.yellow);
// Voxel wheels.
for (const x of [-0.72, 0.73]) {
for (const z of [-0.69, 0.69]) {
const wheel = new THREE.Group();
wheel.position.set(x, 0.30, z);
addBox(wheel, [0.48, 0.48, 0.26], [0, 0, 0], COLORS.tire);
addBox(wheel, [0.19, 0.19, 0.29], [0, 0, 0], COLORS.metal);
group.add(wheel);
wheels.push(wheel);
}
}
group.userData.wheels = wheels;
group.userData.hitch = new THREE.Vector3(-1.42, 0.50, 0);
return group;
}
// -------------------------------------------------------------------------
// Voxel cart
// -------------------------------------------------------------------------
function createVoxelCart() {
const group = new THREE.Group();
const wheels = [];
addBox(group, [2.15, 0.23, 1.38], [0, 0.48, 0], COLORS.cartDark);
addBox(group, [1.92, 0.13, 1.20], [0, 0.68, 0], COLORS.cart);
// Cargo stack.
addBox(group, [0.72, 0.70, 0.92], [-0.24, 1.08, 0], COLORS.cargo);
addBox(group, [0.60, 0.48, 0.76], [0.49, 0.96, 0.03], 0x8c6a4a);
addBox(group, [0.46, 0.36, 0.64], [-0.28, 1.58, -0.02], 0xc49b68);
// Voxel straps.
addBox(group, [0.08, 0.76, 0.96], [-0.24, 1.10, 0], 0x314158);
addBox(group, [0.08, 0.50, 0.80], [0.49, 0.97, 0.03], 0x314158);
// Coupler and bumper.
addBox(group, [0.42, 0.12, 0.14], [1.23, 0.50, 0], COLORS.yellow);
addBox(group, [0.15, 0.25, 1.42], [1.10, 0.50, 0], COLORS.metal);
for (const x of [-0.69, 0.70]) {
for (const z of [-0.73, 0.73]) {
const wheel = new THREE.Group();
wheel.position.set(x, 0.29, z);
addBox(wheel, [0.43, 0.43, 0.25], [0, 0, 0], COLORS.tire);
addBox(wheel, [0.17, 0.17, 0.28], [0, 0, 0], COLORS.metal);
group.add(wheel);
wheels.push(wheel);
}
}
group.userData.wheels = wheels;
group.userData.hitch = new THREE.Vector3(1.45, 0.50, 0);
return group;
}
const tug = createVoxelTug();
const cart = createVoxelCart();
const referenceCart = createVoxelCart();
tug.position.set(0, 0, 0.75);
cart.position.set(-MODEL.initialCenterDistance, 0, 0.75);
referenceCart.position.set(-MODEL.initialCenterDistance, 0, -1.85);
scene.add(tug, cart, referenceCart);
// Make the reference visibly translucent without importing extra helpers.
referenceCart.traverse((object) => {
if (!object.isMesh) return;
object.material = object.material.clone();
object.material.transparent = true;
object.material.opacity = 0.23;
object.material.depthWrite = false;
object.castShadow = false;
});
// -------------------------------------------------------------------------
// Voxel environment
// -------------------------------------------------------------------------
const worldGroup = new THREE.Group();
scene.add(worldGroup);
const tileGeometry = new THREE.BoxGeometry(1.24, 0.07, 1.24);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.95,
metalness: 0
});
const tileCountX = 69;
const tileCountZ = 15;
const tiles = new THREE.InstancedMesh(
tileGeometry,
tileMaterial,
tileCountX * tileCountZ
);
tiles.receiveShadow = true;
const matrix = new THREE.Matrix4();
const color = new THREE.Color();
let instanceIndex = 0;
for (let ix = 0; ix < tileCountX; ix++) {
for (let iz = 0; iz < tileCountZ; iz++) {
const x = (ix - tileCountX / 2) * 1.3;
const z = (iz - tileCountZ / 2) * 1.3;
matrix.makeTranslation(x, -0.035, z);
tiles.setMatrixAt(instanceIndex, matrix);
const inMainLane = Math.abs(z - 0.75) < 1.3;
const inReferenceLane = Math.abs(z + 1.85) < 1.25;
const alternating = (ix + iz) % 2 === 0;
if (inMainLane) {
color.setHex(alternating ? COLORS.lane : COLORS.laneAlt);
} else if (inReferenceLane) {
color.setHex(0x1d2c40);
} else {
color.setHex(alternating ? COLORS.groundA : COLORS.groundB);
}
tiles.setColorAt(instanceIndex, color);
instanceIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
if (tiles.instanceColor) tiles.instanceColor.needsUpdate = true;
worldGroup.add(tiles);
// Lane-edge blocks and dashed separators.
const laneEdgeMaterial = material(0x53667c);
const dashMaterial = material(0xb8c4d1);
addBox(worldGroup, [90, 0.08, 0.10], [0, 0.025, 2.55], 0x53667c);
addBox(worldGroup, [90, 0.08, 0.10], [0, 0.025, -1.03], 0x53667c);
for (let x = -43; x <= 43; x += 4) {
addBox(worldGroup, [1.7, 0.06, 0.09], [x, 0.04, 1.77], 0x7c8ca0);
}
// Blocky warehouse silhouettes.
for (let i = 0; i < 8; i++) {
const x = -34 + i * 10;
const h = 3.5 + ((i * 7) % 5) * 0.55;
addBox(worldGroup, [7.2, h, 3.4], [x, h / 2, -9.5], 0x151e2e);
addBox(worldGroup, [7.5, 0.22, 3.7], [x, h + 0.08, -9.5], 0x243249);
for (let w = 0; w < 3; w++) {
addBox(
worldGroup,
[0.75, 0.48, 0.07],
[x - 1.8 + w * 1.8, h * 0.63, -7.76],
0x31506c,
{ emissive: 0x0f263b, emissiveIntensity: 0.35 }
);
}
}
// Utility poles and voxel signal boxes.
for (let x = -24; x <= 24; x += 12) {
addBox(worldGroup, [0.20, 4.6, 0.20], [x, 2.3, 5.6], 0x36465d);
addBox(worldGroup, [0.65, 0.22, 0.22], [x, 4.45, 5.6], 0x64768c);
addBox(worldGroup, [0.36, 0.36, 0.25], [x + 0.22, 4.36, 5.6], COLORS.yellow, {
emissive: 0x5a3d00,
emissiveIntensity: 0.5
});
}
// -------------------------------------------------------------------------
// Drawbar and force indicators
// -------------------------------------------------------------------------
const tether = new THREE.Mesh(
new THREE.BoxGeometry(1, 0.085, 0.085),
material(COLORS.yellow, { emissive: 0x3a2700, emissiveIntensity: 0.25 })
);
tether.castShadow = true;
scene.add(tether);
function createBlockArrow(colorValue) {
const group = new THREE.Group();
const shaft = new THREE.Mesh(
new THREE.BoxGeometry(1, 0.075, 0.075),
material(colorValue, { emissive: colorValue, emissiveIntensity: 0.18 })
);
const head = new THREE.Mesh(
new THREE.BoxGeometry(0.20, 0.23, 0.23),
material(colorValue, { emissive: colorValue, emissiveIntensity: 0.18 })
);
group.add(shaft, head);
scene.add(group);
return { group, shaft, head };
}
const requestedArrow = createBlockArrow(COLORS.cyan);
const deliveredArrow = createBlockArrow(COLORS.cart);
function updateBlockArrow(arrow, startX, z, force) {
const length = Math.max(0.08, (force / MODEL.maxRequestForce) * 3.2);
arrow.group.position.set(startX, 0.98, z);
arrow.shaft.scale.x = length;
arrow.head.position.x = length;
arrow.group.visible = force > 0.5;
}
// -------------------------------------------------------------------------
// Trails
// -------------------------------------------------------------------------
const actualTrailPoints = [];
const referenceTrailPoints = [];
const actualTrailGeometry = new THREE.BufferGeometry();
actualTrailGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(3 * 900), 3)
);
const referenceTrailGeometry = new THREE.BufferGeometry();
referenceTrailGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(3 * 900), 3)
);
const actualTrail = new THREE.Line(
actualTrailGeometry,
new THREE.LineBasicMaterial({
color: COLORS.cart,
transparent: true,
opacity: 0.8
})
);
const referenceTrail = new THREE.Line(
referenceTrailGeometry,
new THREE.LineBasicMaterial({
color: COLORS.cyan,
transparent: true,
opacity: 0.48
})
);
actualTrail.frustumCulled = false;
referenceTrail.frustumCulled = false;
scene.add(actualTrail, referenceTrail);
function writeTrail(line, points) {
const attribute = line.geometry.getAttribute("position");
const count = Math.min(points.length, attribute.count);
for (let i = 0; i < count; i++) {
const point = points[i];
attribute.setXYZ(i, point.x, 0.11, point.z);
}
attribute.needsUpdate = true;
line.geometry.setDrawRange(0, count);
}
// -------------------------------------------------------------------------
// Simulation
// -------------------------------------------------------------------------
let tractionLimit = 260;
let paused = false;
let accumulator = 0;
let trailClock = 0;
const actual = {
tugX: 0,
cartX: -MODEL.initialCenterDistance,
cartV: 0,
requested: 0,
delivered: 0
};
const reference = {
tugX: 0,
cartX: -MODEL.initialCenterDistance,
cartV: 0,
requested: 0,
delivered: 0
};
function integrateCart(state, cap, dt) {
const speedError = Math.max(0, MODEL.commandSpeed - state.cartV);
const requested = Math.min(
MODEL.maxRequestForce,
MODEL.rollingResistance + MODEL.controllerGain * speedError
);
const delivered = Math.min(requested, cap);
let resistance = 0;
if (state.cartV > 0.01) resistance = MODEL.rollingResistance;
if (state.cartV < -0.01) resistance = -MODEL.rollingResistance;
let netForce = delivered - resistance;
// Static rolling resistance can balance a force below its threshold.
if (Math.abs(state.cartV) < 0.01 && netForce < 0) {
netForce = 0;
}
state.cartV += (netForce / MODEL.cartMass) * dt;
state.cartX += state.cartV * dt;
state.requested = requested;
state.delivered = delivered;
}
function resetTrial() {
actual.tugX = 0;
actual.cartX = -MODEL.initialCenterDistance;
actual.cartV = 0;
actual.requested = 0;
actual.delivered = 0;
reference.tugX = 0;
reference.cartX = -MODEL.initialCenterDistance;
reference.cartV = 0;
reference.requested = 0;
reference.delivered = 0;
actualTrailPoints.length = 0;
referenceTrailPoints.length = 0;
trailClock = 0;
accumulator = 0;
actualTrailPoints.push({
x: actual.cartX - actual.tugX,
z: 0.75,
v: actual.cartV,
t: 0
});
referenceTrailPoints.push({
x: reference.cartX - reference.tugX,
z: -1.85,
v: reference.cartV,
t: 0
});
}
function stepSimulation(dt) {
actual.tugX += MODEL.commandSpeed * dt;
reference.tugX += MODEL.commandSpeed * dt;
integrateCart(actual, tractionLimit, dt);
integrateCart(reference, MODEL.referenceLimit, dt);
trailClock += dt;
if (trailClock >= 0.035) {
trailClock -= 0.035;
actualTrailPoints.push({
x: actual.cartX - actual.tugX,
z: 0.75,
v: actual.cartV,
t: actual.tugX / MODEL.commandSpeed
});
referenceTrailPoints.push({
x: reference.cartX - reference.tugX,
z: -1.85,
v: reference.cartV,
t: reference.tugX / MODEL.commandSpeed
});
}
}
resetTrial();
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, and keyboard-free pointer handling
// -------------------------------------------------------------------------
const cameraControl = {
yaw: 0.63,
pitch: 0.48,
zoom: 1,
targetX: -2,
targetZ: 0,
dragging: false,
pointerId: null,
lastX: 0,
lastY: 0
};
canvas.addEventListener("pointerdown", (event) => {
cameraControl.dragging = true;
cameraControl.pointerId = event.pointerId;
cameraControl.lastX = event.clientX;
cameraControl.lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!cameraControl.dragging || event.pointerId !== cameraControl.pointerId) {
return;
}
const dx = event.clientX - cameraControl.lastX;
const dy = event.clientY - cameraControl.lastY;
cameraControl.lastX = event.clientX;
cameraControl.lastY = event.clientY;
cameraControl.yaw -= dx * 0.006;
cameraControl.pitch = THREE.MathUtils.clamp(
cameraControl.pitch + dy * 0.004,
0.18,
1.12
);
});
function endPointer(event) {
if (event.pointerId !== cameraControl.pointerId) return;
cameraControl.dragging = false;
cameraControl.pointerId = null;
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraControl.zoom = THREE.MathUtils.clamp(
cameraControl.zoom * Math.exp(-event.deltaY * 0.001),
0.55,
1.9
);
},
{ passive: false }
);
function resize() {
const width = Math.max(1, viewport.clientWidth);
const height = Math.max(1, viewport.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(viewport);
resize();
// -------------------------------------------------------------------------
// UI controls
// -------------------------------------------------------------------------
const tractionInput = document.getElementById("traction");
const tractionValue = document.getElementById("tractionValue");
const pauseButton = document.getElementById("pause");
const restartButton = document.getElementById("restart");
const presetButtons = [...document.querySelectorAll(".preset")];
function updateTractionUI() {
tractionLimit = Number(tractionInput.value);
tractionValue.textContent = tractionLimit.toString();
const fill =
(tractionLimit / Number(tractionInput.max)) * 100;
tractionInput.style.setProperty("--fill", `${fill}%`);
presetButtons.forEach((button) => {
button.classList.toggle(
"active",
Number(button.dataset.cap) === tractionLimit
);
});
document.getElementById("labelCart").textContent =
`CART A · LIMIT ${tractionLimit} N`;
resetTrial();
}
tractionInput.addEventListener("input", updateTractionUI);
presetButtons.forEach((button) => {
button.addEventListener("click", () => {
tractionInput.value = button.dataset.cap;
updateTractionUI();
});
});
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
});
restartButton.addEventListener("click", resetTrial);
document.addEventListener("keydown", (event) => {
if (event.code === "Space" && event.target !== tractionInput) {
event.preventDefault();
pauseButton.click();
}
});
updateTractionUI();
// -------------------------------------------------------------------------
// HUD and chart
// -------------------------------------------------------------------------
const metricSpeed = document.getElementById("metricSpeed");
const metricForce = document.getElementById("metricForce");
const metricUse = document.getElementById("metricUse");
const metricSpan = document.getElementById("metricSpan");
const metricLag = document.getElementById("metricLag");
const statusText = document.getElementById("statusText");
const statusLight = document.getElementById("statusLight");
const trialText = document.getElementById("trialText");
const chartActual = document.getElementById("chartActual");
const chartReference = document.getElementById("chartReference");
function chartPoint(point) {
const x = 8 + (point.t / MODEL.trialDuration) * 264;
const y = 78 - (THREE.MathUtils.clamp(point.v, 0, 1.6) / 1.6) * 66;
return `${x.toFixed(2)},${y.toFixed(2)}`;
}
function updateHUD() {
const relativeActualX = actual.cartX - actual.tugX;
const relativeReferenceX = reference.cartX - reference.tugX;
const drawbarSpan = Math.max(0, -relativeActualX - 2.1);
const lag = Math.max(0, relativeReferenceX - relativeActualX);
const use = tractionLimit > 0 ? (actual.delivered / tractionLimit) * 100 : 0;
const elapsed = actual.tugX / MODEL.commandSpeed;
metricSpeed.textContent = `${actual.cartV.toFixed(2)} m/s`;
metricForce.textContent = `${Math.round(actual.delivered)} N`;
metricUse.textContent = `${Math.round(use)}%`;
metricSpan.textContent = `${drawbarSpan.toFixed(2)} m`;
metricLag.textContent = `${lag.toFixed(2)} m`;
trialText.textContent = `TRIAL ${elapsed.toFixed(1).padStart(4, "0")} s`;
let status = "FOLLOWING";
let statusColor = "#55d187";
if (tractionLimit === 0) {
status = "NO TRACTION";
statusColor = "#ff647c";
} else if (tractionLimit < MODEL.rollingResistance) {
status = "BELOW RESISTANCE";
statusColor = "#ffd166";
} else if (actual.requested > tractionLimit + 0.5) {
status = "FORCE SATURATED";
statusColor = "#ff9f43";
}
statusText.textContent = status;
statusLight.style.background = statusColor;
statusLight.style.color = statusColor;
chartActual.setAttribute(
"points",
actualTrailPoints.map(chartPoint).join(" ")
);
chartReference.setAttribute(
"points",
referenceTrailPoints.map(chartPoint).join(" ")
);
}
function positionLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const width = viewport.clientWidth;
const height = viewport.clientHeight;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
const visible =
projected.z > -1 &&
projected.z < 1 &&
x > -100 &&
x < width + 100 &&
y > -50 &&
y < height + 50;
element.style.display = visible ? "block" : "none";
element.style.transform =
`translate(-50%, -100%) translate(${x.toFixed(1)}px, ${y.toFixed(1)}px)`;
}
const labelTug = document.getElementById("labelTug");
const labelCart = document.getElementById("labelCart");
const labelRef = document.getElementById("labelRef");
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const fixedStep = 1 / 120;
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
if (!paused) {
accumulator += dt;
while (accumulator >= fixedStep) {
stepSimulation(fixedStep);
accumulator -= fixedStep;
}
if (actual.tugX / MODEL.commandSpeed >= MODEL.trialDuration) {
resetTrial();
}
}
const relativeActualX = actual.cartX - actual.tugX;
const relativeReferenceX = reference.cartX - reference.tugX;
tug.position.x = 0;
cart.position.x = relativeActualX;
referenceCart.position.x = relativeReferenceX;
// The environment shifts while the vehicles are rendered relative to the tug.
worldGroup.position.x = -actual.tugX;
// Rotate block wheels according to traveled distance.
for (const wheel of tug.userData.wheels) {
wheel.rotation.z = -actual.tugX / 0.24;
}
for (const wheel of cart.userData.wheels) {
wheel.rotation.z = -actual.cartX / 0.215;
}
for (const wheel of referenceCart.userData.wheels) {
wheel.rotation.z = -reference.cartX / 0.215;
}
// Tether endpoints in tug-relative coordinates.
const tugHitchX = -1.42;
const cartHitchX = relativeActualX + 1.45;
const tetherLength = Math.max(0.08, tugHitchX - cartHitchX);
const tetherMidpoint = (tugHitchX + cartHitchX) * 0.5;
tether.position.set(tetherMidpoint, 0.57, 0.75);
tether.scale.x = tetherLength;
tether.material.color.setHex(
actual.requested > tractionLimit + 0.5 ? COLORS.cart : COLORS.yellow
);
updateBlockArrow(requestedArrow, 0.25, 0.75, actual.requested);
updateBlockArrow(deliveredArrow, relativeActualX + 0.2, 0.75, actual.delivered);
writeTrail(actualTrail, actualTrailPoints);
writeTrail(referenceTrail, referenceTrailPoints);
// Frame the tug and actual cart, expanding the view as the deficit grows.
const deficitDistance = Math.max(0, -relativeActualX - 4.2);
const desiredDistance = THREE.MathUtils.clamp(
(14.2 + deficitDistance * 0.42) * cameraControl.zoom,
8,
42
);
const desiredTargetX = relativeActualX * 0.52;
cameraControl.targetX = THREE.MathUtils.lerp(
cameraControl.targetX,
desiredTargetX,
0.08
);
const target = new THREE.Vector3(
cameraControl.targetX,
0.9,
cameraControl.targetZ
);
const horizontalDistance =
desiredDistance * Math.cos(cameraControl.pitch);
camera.position.set(
target.x + horizontalDistance * Math.sin(cameraControl.yaw),
target.y + desiredDistance * Math.sin(cameraControl.pitch),
target.z + horizontalDistance * Math.cos(cameraControl.yaw)
);
camera.lookAt(target);
camera.updateMatrixWorld();
positionLabel(
labelTug,
new THREE.Vector3(0, 2.42, 0.75)
);
positionLabel(
labelCart,
new THREE.Vector3(relativeActualX, 2.25, 0.75)
);
positionLabel(
labelRef,
new THREE.Vector3(relativeReferenceX, 1.80, -1.85)
);
updateHUD();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```