Prompt specification
Build or customize the Three.js 3D scene "Small Vehicle Turning Demonstrator" 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: Small Vehicle Turning Demonstrator
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/small-vehicle-turning-demonstrator-x2023
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a differential between two vehicle wheels. Build a toy vehicle on a curved track with inner and outer wheels. Compare their traveled distances and show why equal wheel speeds conflict with pure rolling. Voxel style.
Working Three.js Starter (small-vehicle-turning-demonstrator-x2023):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Differential — Curved Track Demonstration</title>
<style>
:root {
--bg: #091118;
--panel: rgba(13, 23, 32, 0.88);
--panel-border: rgba(255, 255, 255, 0.12);
--text: #f2f0e8;
--muted: #9eabb4;
--cyan: #48d8e8;
--amber: #ffb84d;
--green: #73e0a1;
--red: #ff6f61;
}
* {
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;
}
canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.brand {
position: absolute;
top: 24px;
left: 26px;
max-width: 440px;
text-shadow: 0 2px 16px rgba(0, 0, 0, 0.75);
}
.eyebrow {
color: var(--amber);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 5px 0 2px;
font-size: clamp(28px, 4vw, 46px);
line-height: 1;
letter-spacing: -0.045em;
}
.brand p {
margin: 8px 0 0;
color: #c0c8ce;
font-size: 14px;
}
.controls {
position: absolute;
top: 24px;
right: 24px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
pointer-events: auto;
}
.control-button {
height: 38px;
padding: 0 14px;
border: 1px solid var(--panel-border);
border-radius: 10px;
color: var(--text);
background: rgba(15, 25, 34, 0.84);
font: inherit;
font-size: 12px;
font-weight: 750;
letter-spacing: 0.02em;
cursor: pointer;
backdrop-filter: blur(12px);
transition:
transform 120ms ease,
border-color 120ms ease,
background 120ms ease;
}
.control-button:hover {
transform: translateY(-1px);
border-color: rgba(255, 255, 255, 0.3);
background: rgba(27, 42, 54, 0.94);
}
.control-button:active {
transform: translateY(0);
}
.panel {
position: absolute;
left: 24px;
bottom: 24px;
width: min(390px, calc(100vw - 48px));
padding: 18px;
border: 1px solid var(--panel-border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(18px);
pointer-events: auto;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.panel h2 {
margin: 0;
font-size: 16px;
letter-spacing: -0.02em;
}
.live-badge {
display: inline-flex;
align-items: center;
gap: 7px;
color: var(--green);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 12px currentColor;
}
.mode-switch {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
padding: 4px;
border-radius: 12px;
background: rgba(0, 0, 0, 0.22);
}
.mode-button {
min-height: 42px;
padding: 8px 9px;
border: 1px solid transparent;
border-radius: 9px;
color: var(--muted);
background: transparent;
font: inherit;
font-size: 12px;
font-weight: 800;
cursor: pointer;
}
.mode-button span {
display: block;
margin-top: 2px;
font-size: 9px;
font-weight: 650;
letter-spacing: 0.04em;
opacity: 0.7;
text-transform: uppercase;
}
.mode-button.active {
border-color: rgba(115, 224, 161, 0.28);
color: #effff4;
background: rgba(115, 224, 161, 0.13);
}
.mode-button.active[data-mode="locked"] {
border-color: rgba(255, 111, 97, 0.35);
background: rgba(255, 111, 97, 0.14);
}
.status {
display: flex;
align-items: center;
gap: 9px;
min-height: 35px;
margin: 12px 0 10px;
color: var(--green);
font-size: 11px;
font-weight: 850;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.status.locked {
color: var(--red);
}
.metric-header {
display: flex;
align-items: baseline;
justify-content: space-between;
margin-bottom: 6px;
color: var(--muted);
font-size: 11px;
}
.metric-header strong {
color: var(--text);
font-size: 13px;
font-variant-numeric: tabular-nums;
}
.bar-row {
margin-bottom: 11px;
}
.bar-track {
height: 8px;
overflow: hidden;
border-radius: 4px;
background: rgba(255, 255, 255, 0.08);
}
.bar {
width: 0;
height: 100%;
border-radius: inherit;
transition: width 100ms linear;
}
.bar.inner {
background: var(--cyan);
box-shadow: 0 0 15px rgba(72, 216, 232, 0.42);
}
.bar.outer {
background: var(--amber);
box-shadow: 0 0 15px rgba(255, 184, 77, 0.42);
}
.mini-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin: 14px 0;
}
.mini-stat {
min-width: 0;
padding: 10px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 10px;
background: rgba(255, 255, 255, 0.035);
}
.mini-stat span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.mini-stat strong {
display: block;
overflow: hidden;
color: var(--text);
font-size: 13px;
text-overflow: ellipsis;
white-space: nowrap;
font-variant-numeric: tabular-nums;
}
.explanation {
padding: 13px;
border-left: 3px solid var(--green);
border-radius: 0 10px 10px 0;
background: rgba(115, 224, 161, 0.08);
}
.explanation.locked {
border-left-color: var(--red);
background: rgba(255, 111, 97, 0.08);
}
.explanation h3 {
margin: 0 0 6px;
font-size: 13px;
}
.explanation p {
margin: 0;
color: #bdc7cd;
font-size: 11.5px;
line-height: 1.55;
}
.speed-row {
display: grid;
grid-template-columns: auto 1fr 38px;
align-items: center;
gap: 10px;
margin-top: 14px;
color: var(--muted);
font-size: 11px;
}
input[type="range"] {
width: 100%;
accent-color: var(--amber);
cursor: pointer;
}
#speedValue {
color: var(--text);
text-align: right;
font-variant-numeric: tabular-nums;
}
.legend {
position: absolute;
right: 24px;
bottom: 24px;
display: grid;
gap: 9px;
padding: 13px 15px;
border: 1px solid var(--panel-border);
border-radius: 13px;
background: rgba(13, 23, 32, 0.78);
color: #c4cdd2;
font-size: 11px;
backdrop-filter: blur(12px);
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
}
.swatch {
width: 18px;
height: 5px;
border-radius: 3px;
}
.swatch.inner {
background: var(--cyan);
box-shadow: 0 0 10px rgba(72, 216, 232, 0.45);
}
.swatch.outer {
background: var(--amber);
box-shadow: 0 0 10px rgba(255, 184, 77, 0.45);
}
.camera-hint {
margin-top: 3px;
padding-top: 9px;
border-top: 1px solid rgba(255, 255, 255, 0.1);
color: #83919b;
font-size: 10px;
}
.world-label {
position: absolute;
z-index: 5;
transform: translate(-50%, -100%);
padding: 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.16);
border-radius: 8px;
color: var(--text);
background: rgba(8, 15, 21, 0.88);
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.32);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.08em;
white-space: nowrap;
pointer-events: none;
}
.world-label.inner {
border-color: rgba(72, 216, 232, 0.5);
color: var(--cyan);
}
.world-label.outer {
border-color: rgba(255, 184, 77, 0.5);
color: var(--amber);
}
.world-label.center {
color: #d8dee2;
}
@media (max-width: 760px) {
.brand {
top: 16px;
left: 16px;
}
.brand p {
display: none;
}
.controls {
top: 16px;
right: 16px;
max-width: 175px;
}
.control-button {
padding: 0 10px;
}
.panel {
left: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 14px;
}
.legend {
display: none;
}
.mini-grid {
grid-template-columns: repeat(3, 1fr);
}
.explanation p {
font-size: 10.5px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel vehicle differential demonstration"></canvas>
<div class="hud">
<header class="brand">
<div class="eyebrow">Mechanics Lab / 01</div>
<h1>The Differential</h1>
<p>One axle. Two paths. Different wheel speeds required.</p>
</header>
<nav class="controls" aria-label="Scene controls">
<button class="control-button" id="pauseButton">Pause</button>
<button class="control-button" id="replayButton">Replay</button>
<button class="control-button" id="cameraButton">Reset camera</button>
</nav>
<section class="panel">
<div class="panel-heading">
<h2>Wheel-path comparison</h2>
<div class="live-badge">
<span class="live-dot"></span>
Live model
</div>
</div>
<div class="mode-switch" role="group" aria-label="Axle mode">
<button class="mode-button active" data-mode="diff" aria-pressed="true">
Differential
<span>Pure rolling</span>
</button>
<button class="mode-button" data-mode="locked" aria-pressed="false">
Equal speeds
<span>Locked axle</span>
</button>
</div>
<div class="status" id="status">
<span class="live-dot"></span>
<span id="statusText">Pure rolling · no slip</span>
</div>
<div class="bar-row">
<div class="metric-header">
<span>Inner wheel path</span>
<strong id="innerDistance">0.0 m</strong>
</div>
<div class="bar-track">
<div class="bar inner" id="innerBar"></div>
</div>
</div>
<div class="bar-row">
<div class="metric-header">
<span>Outer wheel path</span>
<strong id="outerDistance">0.0 m</strong>
</div>
<div class="bar-track">
<div class="bar outer" id="outerBar"></div>
</div>
</div>
<div class="mini-grid">
<div class="mini-stat">
<span>Center path</span>
<strong id="centerDistance">0.0 m</strong>
</div>
<div class="mini-stat">
<span>Inner turns</span>
<strong id="innerTurns">0.0</strong>
</div>
<div class="mini-stat">
<span>Outer turns</span>
<strong id="outerTurns">0.0</strong>
</div>
<div class="mini-stat">
<span>Inner rate</span>
<strong id="innerRate">1.00×</strong>
</div>
<div class="mini-stat">
<span>Outer rate</span>
<strong id="outerRate">1.28×</strong>
</div>
<div class="mini-stat">
<span>Outer slip</span>
<strong id="slipValue">0.0 m</strong>
</div>
</div>
<div class="explanation" id="explanation">
<h3 id="explanationTitle">The differential lets the outer wheel turn faster.</h3>
<p id="explanationText">
The outer lane has a larger radius, so pure rolling requires a faster
outer wheel. The differential supplies that speed difference.
</p>
</div>
<label class="speed-row" for="speedSlider">
<span>Vehicle speed</span>
<input id="speedSlider" type="range" min="0.5" max="1.7" step="0.1" value="1">
<span id="speedValue">1.0×</span>
</label>
</section>
<aside class="legend">
<div class="legend-row">
<span class="swatch inner"></span>
Inner wheel path
</div>
<div class="legend-row">
<span class="swatch outer"></span>
Outer wheel path
</div>
<div class="legend-row">
<span class="swatch" style="background:#f0d264"></span>
Axle midpoint / radius origin
</div>
<div class="camera-hint">Drag to orbit · scroll or pinch to zoom</div>
</aside>
<div class="world-label inner" id="innerLabel">INNER · r 11.4 m</div>
<div class="world-label outer" id="outerLabel">OUTER · r 14.6 m</div>
<div class="world-label center" id="centerLabel">CURVATURE CENTER</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene setup
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x091118);
scene.fog = new THREE.Fog(0x091118, 43, 88);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
140
);
const ambient = new THREE.HemisphereLight(0xb9d9ee, 0x25211d, 2.0);
scene.add(ambient);
const sun = new THREE.DirectionalLight(0xfff1d1, 3.2);
sun.position.set(14, 28, 13);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -29;
sun.shadow.camera.right = 29;
sun.shadow.camera.top = 29;
sun.shadow.camera.bottom = -29;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 75;
sun.shadow.bias = -0.0003;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x58b8d5, 1.15);
rimLight.position.set(-20, 12, -18);
scene.add(rimLight);
const TAU = Math.PI * 2;
const CURVE_RADIUS = 13.0;
const HALF_GAUGE = 1.6;
const INNER_RADIUS = CURVE_RADIUS - HALF_GAUGE;
const OUTER_RADIUS = CURVE_RADIUS + HALF_GAUGE;
const WHEEL_RADIUS = 0.56;
const TRACK_TOP = 0.12;
const ARC_START = -1.58;
const ARC_END = 2.55;
const START_THETA = -1.35;
const END_THETA = 2.48;
const BASE_SPEED = 4.1;
const state = {
mode: "diff",
theta: START_THETA + 0.38,
paused: false,
speedMultiplier: 1,
elapsed: 0
};
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
function makeBox(parent, size, position, color, options = {}) {
const material = new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.78,
metalness: options.metalness ?? 0,
flatShading: true,
transparent: options.transparent ?? false,
opacity: options.opacity ?? 1
});
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addShadowFlags(object) {
object.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
}
// -------------------------------------------------------------------------
// Ground and voxel scenery
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(100, 100),
new THREE.MeshStandardMaterial({
color: 0x101a21,
roughness: 1,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.22;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(90, 45, 0x2d4652, 0x20313a);
grid.position.y = -0.205;
if (grid.material) {
grid.material.transparent = true;
grid.material.opacity = 0.3;
}
scene.add(grid);
const scenery = new THREE.Group();
scene.add(scenery);
function addVoxelTree(x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, -0.18, z);
tree.scale.setScalar(scale);
makeBox(tree, [0.32, 1.6, 0.32], [0, 0.8, 0], 0x6e4932);
makeBox(tree, [1.45, 0.85, 1.45], [0, 1.75, 0], 0x315c50);
makeBox(tree, [1.05, 0.72, 1.05], [0.15, 2.42, 0], 0x3d7160);
makeBox(tree, [0.72, 0.62, 0.72], [-0.2, 2.92, 0.08], 0x4b8068);
scenery.add(tree);
}
const treeAngles = [
-1.35, -0.95, -0.42, 0.12, 0.68, 1.16, 1.72, 2.22,
2.78, 3.18, 3.72, 4.25, 4.74
];
treeAngles.forEach((angle, index) => {
const inside = index % 2 === 0;
const radius = inside ? 8.45 : 17.35;
const jitter = ((index * 17) % 9 - 4) * 0.08;
const x = Math.cos(angle) * (radius + jitter);
const z = Math.sin(angle) * (radius + jitter);
addVoxelTree(x, z, 0.72 + ((index * 7) % 5) * 0.08);
});
for (let i = 0; i < 18; i++) {
const angle = (i / 18) * TAU;
const radius = 6.1 + (i % 3) * 0.7;
const height = 0.35 + (i % 4) * 0.12;
makeBox(
scenery,
[0.65 + (i % 2) * 0.22, height, 0.65 + (i % 3) * 0.12],
[
Math.cos(angle) * radius,
-0.2 + height / 2,
Math.sin(angle) * radius
],
i % 2 ? 0x25343b : 0x2a3d43
);
}
// -------------------------------------------------------------------------
// Curved voxel track
// -------------------------------------------------------------------------
const trackGroup = new THREE.Group();
scene.add(trackGroup);
const trackWidth = 5.2;
const segmentCount = 126;
const arcStep = (ARC_END - ARC_START) / segmentCount;
const segmentLength = CURVE_RADIUS * arcStep * 1.025;
const segmentGeometry = new THREE.BoxGeometry(
trackWidth,
0.28,
segmentLength
);
const trackMaterials = [
new THREE.MeshStandardMaterial({
color: 0x27343a,
roughness: 0.96
}),
new THREE.MeshStandardMaterial({
color: 0x2d3b40,
roughness: 0.96
}),
new THREE.MeshStandardMaterial({
color: 0x243138,
roughness: 0.96
})
];
const innerLaneMaterial = new THREE.MeshStandardMaterial({
color: 0x48d8e8,
emissive: 0x0b515c,
emissiveIntensity: 0.6,
roughness: 0.55
});
const outerLaneMaterial = new THREE.MeshStandardMaterial({
color: 0xffb84d,
emissive: 0x70410d,
emissiveIntensity: 0.55,
roughness: 0.55
});
const centerLaneMaterial = new THREE.MeshStandardMaterial({
color: 0xf0d264,
emissive: 0x55440d,
emissiveIntensity: 0.35,
roughness: 0.65
});
const laneGeometry = new THREE.BoxGeometry(0.11, 0.035, segmentLength * 0.52);
const centerGeometry = new THREE.BoxGeometry(0.09, 0.035, segmentLength * 0.38);
for (let i = 0; i < segmentCount; i++) {
const angle = ARC_START + (i + 0.5) * arcStep;
const x = Math.cos(angle) * CURVE_RADIUS;
const z = Math.sin(angle) * CURVE_RADIUS;
const tile = new THREE.Mesh(
segmentGeometry,
trackMaterials[i % trackMaterials.length]
);
tile.position.set(x, -0.04, z);
tile.rotation.y = -angle;
tile.receiveShadow = true;
tile.castShadow = false;
trackGroup.add(tile);
const innerDash = new THREE.Mesh(laneGeometry, innerLaneMaterial);
innerDash.position.set(
Math.cos(angle) * INNER_RADIUS,
TRACK_TOP + 0.012,
Math.sin(angle) * INNER_RADIUS
);
innerDash.rotation.y = -angle;
trackGroup.add(innerDash);
const outerDash = new THREE.Mesh(laneGeometry, outerLaneMaterial);
outerDash.position.set(
Math.cos(angle) * OUTER_RADIUS,
TRACK_TOP + 0.012,
Math.sin(angle) * OUTER_RADIUS
);
outerDash.rotation.y = -angle;
trackGroup.add(outerDash);
if (i % 3 === 0) {
const centerDash = new THREE.Mesh(centerGeometry, centerLaneMaterial);
centerDash.position.set(
Math.cos(angle) * CURVE_RADIUS,
TRACK_TOP + 0.013,
Math.sin(angle) * CURVE_RADIUS
);
centerDash.rotation.y = -angle;
trackGroup.add(centerDash);
}
}
// -------------------------------------------------------------------------
// Curvature center and animated radius spokes
// -------------------------------------------------------------------------
const centerMarker = new THREE.Group();
scene.add(centerMarker);
makeBox(centerMarker, [1.35, 0.12, 1.35], [0, -0.08, 0], 0x26363c);
makeBox(centerMarker, [0.48, 0.48, 0.48], [0, 0.23, 0], 0xf0d264);
makeBox(centerMarker, [0.18, 1.25, 0.18], [0, 0.88, 0], 0xd6b849);
const radiusGroup = new THREE.Group();
scene.add(radiusGroup);
const innerSpoke = makeBox(
radiusGroup,
[INNER_RADIUS, 0.025, 0.065],
[INNER_RADIUS / 2, TRACK_TOP + 0.035, 0],
0x48d8e8,
{ transparent: true, opacity: 0.72, castShadow: false }
);
const outerSpoke = makeBox(
radiusGroup,
[OUTER_RADIUS, 0.025, 0.065],
[OUTER_RADIUS / 2, TRACK_TOP + 0.037, 0],
0xffb84d,
{ transparent: true, opacity: 0.72, castShadow: false }
);
innerSpoke.renderOrder = 2;
outerSpoke.renderOrder = 2;
// -------------------------------------------------------------------------
// Voxel wheels
// -------------------------------------------------------------------------
const tireGeometry = new THREE.BoxGeometry(0.44, 0.19, 0.19);
const hubGeometry = new THREE.BoxGeometry(0.49, 0.23, 0.23);
const capGeometry = new THREE.BoxGeometry(0.12, 0.29, 0.29);
const tireMaterial = new THREE.MeshStandardMaterial({
color: 0x14191c,
roughness: 0.95
});
function createVoxelWheel(hubColor, markerColor, sideSign = 1) {
const group = new THREE.Group();
const spin = new THREE.Group();
group.add(spin);
const hubMaterial = new THREE.MeshStandardMaterial({
color: hubColor,
roughness: 0.45,
metalness: 0.35
});
const markerMaterial = new THREE.MeshStandardMaterial({
color: markerColor,
roughness: 0.55,
emissive: markerColor,
emissiveIntensity: 0.12
});
for (let iy = -2; iy <= 2; iy++) {
for (let iz = -2; iz <= 2; iz++) {
const y = iy * 0.19;
const z = iz * 0.19;
if (Math.hypot(y, z) <= 0.48) {
const block = new THREE.Mesh(tireGeometry, tireMaterial);
block.position.set(0, y, z);
block.castShadow = true;
block.receiveShadow = true;
spin.add(block);
}
}
}
const hub = new THREE.Mesh(hubGeometry, hubMaterial);
hub.castShadow = true;
spin.add(hub);
const cap = new THREE.Mesh(capGeometry, hubMaterial);
cap.position.x = sideSign * 0.28;
cap.castShadow = true;
spin.add(cap);
const rotationMarker = new THREE.Mesh(tireGeometry, markerMaterial);
rotationMarker.position.set(0, 0.38, 0);
rotationMarker.castShadow = true;
spin.add(rotationMarker);
return { group, spin };
}
// -------------------------------------------------------------------------
// Toy vehicle
// -------------------------------------------------------------------------
const vehicle = new THREE.Group();
scene.add(vehicle);
const wheelY = TRACK_TOP + WHEEL_RADIUS;
const rearInner = createVoxelWheel(0x48d8e8, 0xd8ffff, -1);
rearInner.group.position.set(-HALF_GAUGE, wheelY, 0);
vehicle.add(rearInner.group);
const rearOuter = createVoxelWheel(0xffb84d, 0xfff1c9, 1);
rearOuter.group.position.set(HALF_GAUGE, wheelY, 0);
vehicle.add(rearOuter.group);
const frontLeft = createVoxelWheel(0x87949c, 0xe2e6e7, -1);
frontLeft.group.position.set(-HALF_GAUGE, wheelY, 2.28);
vehicle.add(frontLeft.group);
const frontRight = createVoxelWheel(0x87949c, 0xe2e6e7, 1);
frontRight.group.position.set(HALF_GAUGE, wheelY, 2.28);
vehicle.add(frontRight.group);
// Chassis
makeBox(vehicle, [2.16, 0.25, 3.92], [0, 0.98, 0.78], 0x20282b);
makeBox(vehicle, [2.04, 0.5, 3.58], [0, 1.29, 0.72], 0xe76f32);
makeBox(vehicle, [1.92, 0.18, 3.72], [0, 1.06, 0.73], 0xf28a43);
// Cabin
makeBox(vehicle, [1.67, 0.82, 1.78], [0, 1.9, 0.12], 0xe9dfc5);
const glassMaterial = new THREE.MeshStandardMaterial({
color: 0x163c4c,
roughness: 0.24,
metalness: 0.18
});
makeBox(vehicle, [0.055, 0.43, 1.18], [-0.86, 1.96, 0.25], 0x173e4d);
makeBox(vehicle, [0.055, 0.43, 1.18], [0.86, 1.96, 0.25], 0x173e4d);
makeBox(vehicle, [1.36, 0.43, 0.055], [0, 1.96, 1.025], 0x173e4d);
makeBox(vehicle, [1.32, 0.4, 0.055], [0, 1.94, -0.785], 0x173e4d);
makeBox(vehicle, [1.78, 0.16, 1.88], [0, 2.37, 0.12], 0xf08b46);
makeBox(vehicle, [0.62, 0.22, 0.5], [0, 2.57, 0.02], 0xf0d264);
// Hood and front details
makeBox(vehicle, [1.9, 0.14, 1.25], [0, 1.59, 1.5], 0xf28a43);
makeBox(vehicle, [0.38, 0.23, 0.08], [-0.58, 1.36, 2.54], 0xfff2b5);
makeBox(vehicle, [0.38, 0.23, 0.08], [0.58, 1.36, 2.54], 0xfff2b5);
makeBox(vehicle, [2.18, 0.18, 0.16], [0, 1.03, 2.68], 0x313a3e);
makeBox(vehicle, [2.18, 0.18, 0.16], [0, 1.03, -1.2], 0x313a3e);
// Differential housing and axles
const diffMaterial = new THREE.MeshStandardMaterial({
color: 0x73e0a1,
emissive: 0x123d27,
emissiveIntensity: 0.65,
roughness: 0.42,
metalness: 0.45
});
const diffCase = makeBox(
vehicle,
[0.72, 0.55, 0.65],
[0, 0.73, 0],
0x73e0a1
);
diffCase.material = diffMaterial;
makeBox(vehicle, [0.12, 0.12, 0.12], [-0.42, 0.86, 0.25], 0xdce9df);
makeBox(vehicle, [0.12, 0.12, 0.12], [0.42, 0.86, 0.25], 0xdce9df);
makeBox(vehicle, [0.12, 0.12, 0.12], [-0.42, 0.86, -0.25], 0xdce9df);
makeBox(vehicle, [0.12, 0.12, 0.12], [0.42, 0.86, -0.25], 0xdce9df);
const splitShafts = new THREE.Group();
vehicle.add(splitShafts);
makeBox(splitShafts, [1.23, 0.12, 0.12], [-0.98, wheelY, 0], 0x91a1a4);
makeBox(splitShafts, [1.23, 0.12, 0.12], [0.98, wheelY, 0], 0x91a1a4);
const lockedAxle = makeBox(
vehicle,
[HALF_GAUGE * 2, 0.13, 0.13],
[0, wheelY, 0],
0xff6f61
);
lockedAxle.visible = false;
// Tiny exposed differential gear
const gearGroup = new THREE.Group();
gearGroup.position.set(0, 0.77, 0.345);
vehicle.add(gearGroup);
const gearMaterial = new THREE.MeshStandardMaterial({
color: 0xf0d264,
roughness: 0.4,
metalness: 0.45
});
makeBox(gearGroup, [0.27, 0.27, 0.1], [0, 0, 0], 0xf0d264);
for (let i = 0; i < 8; i++) {
const angle = (i / 8) * TAU;
makeBox(
gearGroup,
[0.16, 0.13, 0.1],
[Math.cos(angle) * 0.24, Math.sin(angle) * 0.24, 0],
0xf0d264
);
}
// -------------------------------------------------------------------------
// Outer-wheel scrub particles for the locked-axle mode
// -------------------------------------------------------------------------
const slipGroup = new THREE.Group();
vehicle.add(slipGroup);
const slipMaterial = new THREE.MeshStandardMaterial({
color: 0xff7a4e,
emissive: 0xa83212,
emissiveIntensity: 1.1,
roughness: 0.6
});
const slipGeometry = new THREE.BoxGeometry(0.08, 0.045, 0.28);
const slipParticles = [];
for (let i = 0; i < 22; i++) {
const particle = new THREE.Mesh(slipGeometry, slipMaterial);
slipGroup.add(particle);
slipParticles.push(particle);
}
slipGroup.visible = false;
// -------------------------------------------------------------------------
// Camera controls — implemented without external addons
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0, 0);
let cameraAzimuth = 0.72;
let cameraElevation = 0.58;
let cameraDistance = 45;
const initialCamera = {
azimuth: cameraAzimuth,
elevation: cameraElevation,
distance: cameraDistance
};
function updateCamera() {
const horizontal = Math.cos(cameraElevation) * cameraDistance;
camera.position.set(
cameraTarget.x + Math.sin(cameraAzimuth) * horizontal,
cameraTarget.y + Math.sin(cameraElevation) * cameraDistance,
cameraTarget.z + Math.cos(cameraAzimuth) * horizontal
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraAzimuth = initialCamera.azimuth;
cameraElevation = initialCamera.elevation;
cameraDistance = initialCamera.distance;
updateCamera();
}
const activePointers = new Map();
let previousPinchDistance = 0;
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (activePointers.size === 2) {
const points = [...activePointers.values()];
previousPinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
canvas.addEventListener("pointermove", (event) => {
if (!activePointers.has(event.pointerId)) return;
const previous = activePointers.get(event.pointerId);
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (activePointers.size === 1) {
cameraAzimuth -= dx * 0.006;
cameraElevation = THREE.MathUtils.clamp(
cameraElevation + dy * 0.005,
0.2,
1.25
);
} else if (activePointers.size === 2) {
const points = [...activePointers.values()];
const pinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (previousPinchDistance > 0 && pinchDistance > 0) {
cameraDistance = THREE.MathUtils.clamp(
cameraDistance * (previousPinchDistance / pinchDistance),
24,
72
);
}
previousPinchDistance = pinchDistance;
}
updateCamera();
});
function releasePointer(event) {
activePointers.delete(event.pointerId);
previousPinchDistance = 0;
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("lostpointercapture", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraDistance = THREE.MathUtils.clamp(
cameraDistance * Math.exp(event.deltaY * 0.001),
24,
72
);
updateCamera();
},
{ passive: false }
);
updateCamera();
// -------------------------------------------------------------------------
// UI
// -------------------------------------------------------------------------
const ui = {
diffButton: document.querySelector('[data-mode="diff"]'),
lockedButton: document.querySelector('[data-mode="locked"]'),
status: document.querySelector("#status"),
statusText: document.querySelector("#statusText"),
centerDistance: document.querySelector("#centerDistance"),
innerDistance: document.querySelector("#innerDistance"),
outerDistance: document.querySelector("#outerDistance"),
innerTurns: document.querySelector("#innerTurns"),
outerTurns: document.querySelector("#outerTurns"),
innerRate: document.querySelector("#innerRate"),
outerRate: document.querySelector("#outerRate"),
slipValue: document.querySelector("#slipValue"),
innerBar: document.querySelector("#innerBar"),
outerBar: document.querySelector("#outerBar"),
explanation: document.querySelector("#explanation"),
explanationTitle: document.querySelector("#explanationTitle"),
explanationText: document.querySelector("#explanationText"),
pauseButton: document.querySelector("#pauseButton"),
replayButton: document.querySelector("#replayButton"),
cameraButton: document.querySelector("#cameraButton"),
speedSlider: document.querySelector("#speedSlider"),
speedValue: document.querySelector("#speedValue"),
innerLabel: document.querySelector("#innerLabel"),
outerLabel: document.querySelector("#outerLabel"),
centerLabel: document.querySelector("#centerLabel")
};
function setMode(mode) {
state.mode = mode;
const differential = mode === "diff";
ui.diffButton.classList.toggle("active", differential);
ui.lockedButton.classList.toggle("active", !differential);
ui.diffButton.setAttribute("aria-pressed", String(differential));
ui.lockedButton.setAttribute("aria-pressed", String(!differential));
ui.status.classList.toggle("locked", !differential);
ui.explanation.classList.toggle("locked", !differential);
diffMaterial.color.set(differential ? 0x73e0a1 : 0xff6f61);
diffMaterial.emissive.set(differential ? 0x123d27 : 0x661812);
splitShafts.visible = differential;
lockedAxle.visible = !differential;
slipGroup.visible = !differential;
updateHUD();
}
ui.diffButton.addEventListener("click", () => setMode("diff"));
ui.lockedButton.addEventListener("click", () => setMode("locked"));
ui.pauseButton.addEventListener("click", () => {
if (state.theta >= END_THETA) {
state.theta = START_THETA;
state.paused = false;
} else {
state.paused = !state.paused;
}
updatePauseButton();
});
function replay() {
state.theta = START_THETA;
state.paused = false;
updatePauseButton();
}
ui.replayButton.addEventListener("click", replay);
ui.cameraButton.addEventListener("click", resetCamera);
ui.speedSlider.addEventListener("input", () => {
state.speedMultiplier = Number(ui.speedSlider.value);
ui.speedValue.textContent = `${state.speedMultiplier.toFixed(1)}×`;
});
function updatePauseButton() {
ui.pauseButton.textContent = state.paused ? "Resume" : "Pause";
}
window.addEventListener("keydown", (event) => {
const tag = event.target?.tagName?.toLowerCase();
if (tag === "input" || tag === "button") return;
if (event.code === "Space") {
event.preventDefault();
ui.pauseButton.click();
}
if (event.key.toLowerCase() === "r") replay();
if (event.key.toLowerCase() === "c") resetCamera();
if (event.key.toLowerCase() === "d") setMode("diff");
if (event.key.toLowerCase() === "l") setMode("locked");
});
// -------------------------------------------------------------------------
// Animation and mathematical model
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
const centerWorld = new THREE.Vector3(0, 1.5, 0);
function placeWorldLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.25 &&
Math.abs(projected.y) < 1.25;
element.style.display = visible ? "block" : "none";
if (!visible) return;
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
function updateVehicle() {
const theta = state.theta;
const centerDistance = (theta - START_THETA) * CURVE_RADIUS;
const innerDistance = centerDistance * (INNER_RADIUS / CURVE_RADIUS);
const outerDistance = centerDistance * (OUTER_RADIUS / CURVE_RADIUS);
const innerTurns = innerDistance / (TAU * WHEEL_RADIUS);
const pureRollingOuterTurns = outerDistance / (TAU * WHEEL_RADIUS);
const actualOuterTurns =
state.mode === "diff" ? pureRollingOuterTurns : innerTurns;
vehicle.position.set(
Math.cos(theta) * CURVE_RADIUS,
0,
Math.sin(theta) * CURVE_RADIUS
);
vehicle.rotation.y = -theta;
radiusGroup.rotation.y = -theta;
rearInner.spin.rotation.x = innerTurns * TAU;
rearOuter.spin.rotation.x = actualOuterTurns * TAU;
const frontTurns =
state.mode === "diff"
? (innerTurns + pureRollingOuterTurns) * 0.5
: innerTurns;
frontLeft.spin.rotation.x = frontTurns * TAU;
frontRight.spin.rotation.x = frontTurns * TAU;
gearGroup.rotation.x = -innerTurns * TAU;
if (state.mode === "locked") {
const phase = state.elapsed * 3.1;
slipParticles.forEach((particle, index) => {
const life = (phase + index * 0.173) % 1;
const lateral = ((index * 13) % 11 - 5) * 0.055;
particle.position.set(
HALF_GAUGE + lateral,
TRACK_TOP + 0.035 + life * 0.025,
-0.45 + ((index * 0.19) % 0.9) + life * 0.48
);
particle.rotation.y = -0.35 + index * 0.07;
particle.scale.setScalar(Math.max(0.15, 1 - life));
});
}
vehicle.updateMatrixWorld(true);
const innerAnchor = vehicle.localToWorld(
new THREE.Vector3(-HALF_GAUGE, 2.0, 0)
);
const outerAnchor = vehicle.localToWorld(
new THREE.Vector3(HALF_GAUGE, 2.0, 0)
);
placeWorldLabel(ui.innerLabel, innerAnchor);
placeWorldLabel(ui.outerLabel, outerAnchor);
placeWorldLabel(ui.centerLabel, centerWorld);
return {
centerDistance,
innerDistance,
outerDistance,
innerTurns,
actualOuterTurns,
slipDistance:
state.mode === "locked" ? outerDistance - innerDistance : 0
};
}
function updateHUD() {
const values = updateVehicle();
const ratio = OUTER_RADIUS / INNER_RADIUS;
ui.centerDistance.textContent = `${values.centerDistance.toFixed(1)} m`;
ui.innerDistance.textContent = `${values.innerDistance.toFixed(1)} m`;
ui.outerDistance.textContent = `${values.outerDistance.toFixed(1)} m`;
ui.innerTurns.textContent = values.innerTurns.toFixed(1);
ui.outerTurns.textContent = values.actualOuterTurns.toFixed(1);
ui.innerRate.textContent = "1.00×";
ui.outerRate.textContent =
state.mode === "diff" ? `${ratio.toFixed(2)}×` : "1.00×";
ui.slipValue.textContent = `${values.slipDistance.toFixed(1)} m`;
const outerMax = Math.max(values.outerDistance, 0.001);
ui.innerBar.style.width = `${(values.innerDistance / outerMax) * 100}%`;
ui.outerBar.style.width = `${(values.outerDistance / outerMax) * 100}%`;
if (state.mode === "diff") {
ui.statusText.textContent = "Pure rolling · no slip";
ui.explanationTitle.textContent =
"The differential lets the outer wheel turn faster.";
ui.explanationText.textContent =
`The outer lane is ${((ratio - 1) * 100).toFixed(1)}% longer. ` +
`Pure rolling therefore requires the outer wheel to rotate ${ratio.toFixed(2)}× ` +
"as fast. The differential provides that speed difference, so both tires " +
"remain momentarily still relative to the track.";
} else {
ui.statusText.textContent =
`Outer scrub · ${values.slipDistance.toFixed(1)} m relative slip`;
ui.explanationTitle.textContent =
"Equal wheel speeds conflict with pure rolling.";
ui.explanationText.textContent =
`A locked axle forces both wheels to rotate at the inner wheel's rate. ` +
`The outer tire must cover ${values.slipDistance.toFixed(1)} m more ground ` +
"than its rotation allows, so it scrubs along the longer path while the " +
"inner wheel rolls cleanly.";
}
}
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
state.elapsed += dt;
if (!state.paused && state.theta < END_THETA) {
state.theta = Math.min(
END_THETA,
state.theta +
(BASE_SPEED * state.speedMultiplier * dt) / CURVE_RADIUS
);
if (state.theta >= END_THETA) {
state.paused = true;
updatePauseButton();
}
}
updateHUD();
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
setMode("diff");
updatePauseButton();
animate();
</script>
</body>
</html>
```