Prompt specification
Build or customize the Three.js 3D scene "Rolling Versus Sliding Cylinder" 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: Rolling Versus Sliding Cylinder
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/rolling-versus-sliding-cylinder-x3638
Scene Description Prompt:
Make a cutaway Three.js scene in a self-contained HTML file using cdnjs: rolling versus sliding cylinder. Animate two matched cylinders, one obeying pure rolling and one sliding under a declared rule. Compare rim-marker paths and state that friction dynamics are simplified.
Working Three.js Starter (rolling-versus-sliding-cylinder-x3638):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Cutaway: Rolling vs Sliding Cylinders</title>
<style>
:root {
color-scheme: dark;
--bg: #07111c;
--panel: rgba(9, 20, 33, 0.84);
--border: rgba(153, 207, 232, 0.19);
--text: #eaf7ff;
--muted: #91aabd;
--rolling: #39d5e8;
--sliding: #ffad4a;
--good: #6ee7a8;
--danger: #ff647c;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 20%, #14283a 0%, var(--bg) 62%);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--text);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.panel {
position: fixed;
top: 18px;
left: 18px;
width: min(360px, calc(100vw - 36px));
padding: 17px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.34);
backdrop-filter: blur(14px);
pointer-events: none;
user-select: none;
}
.eyebrow {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 5px 0 4px;
font-size: clamp(21px, 3vw, 29px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin: 0 0 14px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.case {
display: grid;
grid-template-columns: 11px 1fr;
gap: 10px;
padding: 11px 0;
border-top: 1px solid rgba(255, 255, 255, 0.08);
}
.swatch {
width: 8px;
height: 100%;
min-height: 64px;
border-radius: 20px;
background: var(--rolling);
box-shadow: 0 0 18px color-mix(in srgb, var(--rolling) 45%, transparent);
}
.case.sliding .swatch {
background: var(--sliding);
box-shadow: 0 0 18px color-mix(in srgb, var(--sliding) 45%, transparent);
}
.case-title {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 8px;
font-size: 13px;
font-weight: 800;
letter-spacing: 0.03em;
}
.case-title span:last-child {
color: var(--muted);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.equation {
margin-top: 5px;
color: #c7dbe8;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 11px;
line-height: 1.55;
}
.path-name {
display: inline-block;
margin-top: 6px;
padding: 3px 7px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 999px;
color: var(--text);
font-size: 10px;
font-weight: 700;
}
.charts {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 12px;
padding-top: 12px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
}
.chart-card {
min-width: 0;
padding: 8px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
}
.chart-title {
display: flex;
align-items: center;
gap: 5px;
margin-bottom: 4px;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--rolling);
box-shadow: 0 0 9px var(--rolling);
}
.chart-card.sliding .dot {
background: var(--sliding);
box-shadow: 0 0 9px var(--sliding);
}
.chart-card svg {
display: block;
width: 100%;
height: 66px;
}
.chart-caption {
margin-top: 3px;
color: var(--muted);
font-size: 9px;
line-height: 1.35;
}
.controls {
position: fixed;
top: 18px;
right: 18px;
display: flex;
gap: 7px;
padding: 7px;
border: 1px solid var(--border);
border-radius: 12px;
background: var(--panel);
backdrop-filter: blur(12px);
box-shadow: 0 14px 35px rgba(0, 0, 0, 0.25);
}
button {
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 8px;
padding: 8px 11px;
background: rgba(255, 255, 255, 0.055);
color: var(--text);
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition: background 150ms, border-color 150ms;
}
button:hover {
background: rgba(255, 255, 255, 0.11);
border-color: rgba(255, 255, 255, 0.26);
}
button.active {
border-color: rgba(57, 213, 232, 0.55);
background: rgba(57, 213, 232, 0.13);
}
.simplification {
position: fixed;
right: 18px;
bottom: 18px;
max-width: 430px;
padding: 12px 14px;
border: 1px solid rgba(255, 100, 124, 0.25);
border-radius: 12px;
background: rgba(30, 12, 20, 0.78);
color: #ffd5dc;
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(12px);
font-size: 11px;
line-height: 1.5;
pointer-events: none;
}
.simplification strong {
color: #ff8da0;
}
.hint {
position: fixed;
left: 50%;
bottom: 18px;
transform: translateX(-50%);
padding: 8px 13px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 999px;
background: rgba(6, 15, 25, 0.72);
color: var(--muted);
font-size: 10px;
white-space: nowrap;
pointer-events: none;
}
#status {
position: fixed;
left: 18px;
bottom: 18px;
color: var(--muted);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 10px;
pointer-events: none;
}
@media (max-width: 760px) {
.panel {
top: 10px;
left: 10px;
width: min(315px, calc(100vw - 20px));
padding: 13px;
}
.charts, .hint {
display: none;
}
.controls {
top: auto;
right: 10px;
bottom: 48px;
flex-direction: column;
}
.simplification {
right: 10px;
bottom: 10px;
left: 10px;
max-width: none;
}
#status {
display: none;
}
}
</style>
</head>
<body>
<section class="panel" aria-label="Experiment information">
<div class="eyebrow">Cutaway dynamics laboratory</div>
<h1>Rolling vs. sliding</h1>
<p class="subtitle">
Matched cylinders: R = 0.80 m, width = 1.15 m, and center speed
v = 1.40 m/s. Colored curves are rim-marker paths.
</p>
<div class="case">
<div class="swatch"></div>
<div>
<div class="case-title">
<span>ROLLING</span>
<span>Pure rolling</span>
</div>
<div class="equation">
v = R|ω|<br>
ω = −1.75 rad/s<br>
contact slip = 0.00 m/s
</div>
<span class="path-name">Rim path: cycloid</span>
</div>
</div>
<div class="case sliding">
<div class="swatch"></div>
<div>
<div class="case-title">
<span>SLIDING</span>
<span>Prescribed rule</span>
</div>
<div class="equation">
v = 1.40 m/s, ω = −1.00 rad/s<br>
u = v + Rω = +0.60 m/s<br>
constant v and constant ω
</div>
<span class="path-name">Rim path: curtate trochoid</span>
</div>
</div>
<div class="charts" aria-label="Static path-shape comparison">
<div class="chart-card">
<div class="chart-title">
<i class="dot"></i>
Rolling
</div>
<svg id="rollingPlot" viewBox="0 0 300 90" aria-label="Cycloid plot"></svg>
<div class="chart-caption">Repeated arches with sharp cusps.</div>
</div>
<div class="chart-card sliding">
<div class="chart-title">
<i class="dot"></i>
Sliding
</div>
<svg id="slidingPlot" viewBox="0 0 300 90" aria-label="Trochoid plot"></svg>
<div class="chart-caption">Broad, smooth waves; no loops.</div>
</div>
</div>
</section>
<div class="controls">
<button id="pauseButton" type="button">Pause</button>
<button id="pathsButton" class="active" type="button">Paths</button>
<button id="resetButton" type="button">Reset</button>
</div>
<div class="simplification">
<strong>Friction dynamics are simplified.</strong>
Rolling no-slip is imposed as a constraint. The sliding case uses prescribed
constant center velocity and angular velocity; friction force, frictional
torque, acceleration, and slip transitions are not solved.
</div>
<div class="hint">Drag to orbit · Wheel to zoom</div>
<div id="status"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Declared experiment
// ------------------------------------------------------------
const R = 0.80; // cylinder radius, m
const WIDTH = 1.15; // cylinder length, m
const V = 1.40; // common center velocity, m/s
// Signed angular velocities about the +Z axis.
const ROLLING_OMEGA = -V / R; // -1.75 rad/s: no-slip rolling
const SLIDING_OMEGA = -1.00; // prescribed sliding rotation
const SLIDING_SLIP = V + R * SLIDING_OMEGA; // +0.60 m/s
const TAU = Math.PI * 2;
const ROLLING_LANE_Z = 1.25;
const SLIDING_LANE_Z = -1.25;
const state = {
time: 0,
running: true,
showPaths: true,
speedScale: 1
};
// ------------------------------------------------------------
// Renderer, scene, lights
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111c);
scene.fog = new THREE.FogExp2(0x07111c, 0.025);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.08,
250
);
scene.add(new THREE.HemisphereLight(0xa9ddff, 0x10141c, 1.7));
const sun = new THREE.DirectionalLight(0xffffff, 3.0);
sun.position.set(-5, 10, 5);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -8;
sun.shadow.camera.right = 8;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.00015;
scene.add(sun);
scene.add(sun.target);
const rimLight = new THREE.DirectionalLight(0x6bcaff, 1.4);
rimLight.position.set(4, 4, -7);
scene.add(rimLight);
// ------------------------------------------------------------
// Ground and lanes
// ------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(300, 70),
new THREE.MeshStandardMaterial({
color: 0x0a1621,
roughness: 0.92,
metalness: 0.06
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.set(30, -0.025, 0);
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(240, 120, 0x23475c, 0x142b3b);
grid.position.set(30, 0.004, 0);
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
function addLane(z, color) {
const lane = new THREE.Mesh(
new THREE.PlaneGeometry(240, 1.85),
new THREE.MeshStandardMaterial({
color,
roughness: 0.95,
transparent: true,
opacity: 0.055,
depthWrite: false
})
);
lane.rotation.x = -Math.PI / 2;
lane.position.set(30, 0.012, z);
scene.add(lane);
const edgeGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-90, 0.025, z - 0.93),
new THREE.Vector3(150, 0.025, z - 0.93)
]);
const edgeMaterial = new THREE.LineDashedMaterial({
color,
dashSize: 0.65,
gapSize: 0.42,
transparent: true,
opacity: 0.32
});
const edge = new THREE.Line(edgeGeometry, edgeMaterial);
edge.computeLineDistances();
scene.add(edge);
}
addLane(ROLLING_LANE_Z, 0x39d5e8);
addLane(SLIDING_LANE_Z, 0xffad4a);
// ------------------------------------------------------------
// Cutaway cylinder construction
//
// The cutaway shell is held fixed as an explanatory section.
// The inner rotor, spokes, and rim marker rotate.
// ------------------------------------------------------------
function radialPoint(theta, radius, z) {
// Matches CylinderGeometry after rotateX(+PI/2).
return new THREE.Vector3(
Math.sin(theta) * radius,
-Math.cos(theta) * radius,
z
);
}
function addArcTube(parent, thetaStart, thetaLength, radius, z, material) {
const points = [];
for (let i = 0; i <= 72; i++) {
const theta = thetaStart + thetaLength * i / 72;
points.push(radialPoint(theta, radius, z));
}
const curve = new THREE.CatmullRomCurve3(points);
const geometry = new THREE.TubeGeometry(curve, 96, 0.028, 8, false);
const mesh = new THREE.Mesh(geometry, material);
parent.add(mesh);
return mesh;
}
function addCutFace(parent, theta, material) {
const inner = R - 0.075;
const outer = R;
const z0 = -WIDTH / 2;
const z1 = WIDTH / 2;
const a = radialPoint(theta, inner, z0);
const b = radialPoint(theta, outer, z0);
const c = radialPoint(theta, outer, z1);
const d = radialPoint(theta, inner, z1);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute([
a.x, a.y, a.z,
b.x, b.y, b.z,
c.x, c.y, c.z,
a.x, a.y, a.z,
c.x, c.y, c.z,
d.x, d.y, d.z
], 3)
);
geometry.computeVertexNormals();
const face = new THREE.Mesh(geometry, material);
face.material.side = THREE.DoubleSide;
parent.add(face);
}
function createCylinder({ laneZ, color, darkColor }) {
const root = new THREE.Group();
root.position.set(0, R, laneZ);
scene.add(root);
const shellMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.07,
metalness: 0.42,
roughness: 0.29,
transparent: true,
opacity: 0.73,
side: THREE.DoubleSide,
depthWrite: false
});
const edgeMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.28,
metalness: 0.72,
roughness: 0.24
});
const cutMaterial = new THREE.MeshStandardMaterial({
color: darkColor,
emissive: color,
emissiveIntensity: 0.08,
metalness: 0.5,
roughness: 0.4,
side: THREE.DoubleSide
});
// A 270-degree shell leaves a cutaway opening on the camera-facing side.
const thetaStart = Math.PI * 1.75;
const thetaLength = Math.PI * 1.50;
const shellGeometry = new THREE.CylinderGeometry(
R, R, WIDTH, 80, 1, true, thetaStart, thetaLength
);
shellGeometry.rotateX(Math.PI / 2);
const shell = new THREE.Mesh(shellGeometry, shellMaterial);
shell.renderOrder = 2;
root.add(shell);
addArcTube(
root, thetaStart, thetaLength, R, -WIDTH / 2, edgeMaterial
);
addArcTube(
root, thetaStart, thetaLength, R, WIDTH / 2, edgeMaterial
);
addCutFace(root, thetaStart, cutMaterial);
addCutFace(root, thetaStart + thetaLength, cutMaterial);
// Rotating internal assembly.
const rotor = new THREE.Group();
root.add(rotor);
const rotorMaterial = new THREE.MeshStandardMaterial({
color: 0xb8cad5,
metalness: 0.88,
roughness: 0.24
});
const spokeMaterial = new THREE.MeshStandardMaterial({
color: darkColor,
emissive: color,
emissiveIntensity: 0.16,
metalness: 0.72,
roughness: 0.32
});
const axleGeometry = new THREE.CylinderGeometry(
0.13, 0.13, WIDTH + 0.34, 28
);
axleGeometry.rotateX(Math.PI / 2);
const axle = new THREE.Mesh(axleGeometry, rotorMaterial);
axle.castShadow = true;
rotor.add(axle);
const hubGeometry = new THREE.CylinderGeometry(
0.24, 0.24, WIDTH * 0.72, 32
);
hubGeometry.rotateX(Math.PI / 2);
const hub = new THREE.Mesh(hubGeometry, rotorMaterial);
hub.castShadow = true;
rotor.add(hub);
for (const z of [-WIDTH * 0.39, WIDTH * 0.39]) {
const bearing = new THREE.Mesh(
new THREE.TorusGeometry(0.29, 0.052, 10, 44),
spokeMaterial
);
bearing.position.z = z;
rotor.add(bearing);
}
// Six visible spokes make the angular motion easy to read.
for (let i = 0; i < 6; i++) {
const pivot = new THREE.Group();
pivot.rotation.z = i * TAU / 6;
const arm = new THREE.Mesh(
new THREE.BoxGeometry(R * 0.74, 0.068, 0.105),
spokeMaterial
);
arm.position.x = R * 0.37;
arm.castShadow = true;
pivot.add(arm);
rotor.add(pivot);
}
// White rim marker with a lane-colored glow.
const marker = new THREE.Mesh(
new THREE.SphereGeometry(0.095, 20, 14),
new THREE.MeshStandardMaterial({
color: 0xffffff,
emissive: color,
emissiveIntensity: 1.7,
metalness: 0.1,
roughness: 0.18
})
);
marker.position.set(0, R, WIDTH / 2 + 0.095);
marker.castShadow = true;
rotor.add(marker);
const markerHalo = new THREE.Mesh(
new THREE.SphereGeometry(0.145, 16, 12),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.13,
depthWrite: false
})
);
markerHalo.position.copy(marker.position);
rotor.add(markerHalo);
return { root, rotor, marker };
}
const rolling = createCylinder({
laneZ: ROLLING_LANE_Z,
color: 0x39d5e8,
darkColor: 0x087b91
});
const sliding = createCylinder({
laneZ: SLIDING_LANE_Z,
color: 0xffad4a,
darkColor: 0x9c4d0b
});
// Give the two cylinders a small longitudinal offset so they do not
// obscure one another at the beginning.
const ROLLING_X0 = -0.85;
const SLIDING_X0 = 0.85;
// Contact-slip indicators.
const rollingContact = new THREE.Mesh(
new THREE.TorusGeometry(0.13, 0.018, 8, 32),
new THREE.MeshBasicMaterial({ color: 0x6ee7a8 })
);
rollingContact.rotation.x = Math.PI / 2;
scene.add(rollingContact);
const slipArrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(),
Math.abs(SLIDING_SLIP),
0xff647c,
0.17,
0.09
);
scene.add(slipArrow);
// ------------------------------------------------------------
// Exact rim-marker trajectories
//
// Marker coordinates for positive phase a:
// x = X + R sin(a)
// y = R + R cos(a)
//
// Rolling: a = (V/R)t gives a cycloid.
// Sliding: a = |omega_s|t gives a curtate trochoid.
// ------------------------------------------------------------
const PATH_SAMPLES = 220;
const PATH_TIME_WINDOW = 3.4;
function createPathLine(color) {
const positions = new Float32Array((PATH_SAMPLES + 1) * 3);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3).setUsage(THREE.DynamicDrawUsage)
);
const material = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.92
});
const line = new THREE.Line(geometry, material);
line.frustumCulled = false;
scene.add(line);
return line;
}
const rollingPath = createPathLine(0x39d5e8);
const slidingPath = createPathLine(0xffad4a);
function updatePath(line, rootX, phase, angularSpeedMagnitude, laneZ) {
const attribute = line.geometry.attributes.position;
for (let i = 0; i <= PATH_SAMPLES; i++) {
const fraction = i / PATH_SAMPLES;
const relativeTime =
-PATH_TIME_WINDOW + PATH_TIME_WINDOW * fraction;
const centerX = rootX + V * relativeTime;
const a = phase + angularSpeedMagnitude * relativeTime;
const x = centerX + R * Math.sin(a);
const y = R + R * Math.cos(a);
const z = laneZ + WIDTH / 2 + 0.095;
attribute.setXYZ(i, x, y, z);
}
attribute.needsUpdate = true;
}
// ------------------------------------------------------------
// HUD path-shape comparison
// ------------------------------------------------------------
function drawPathPlot(svg, angularSpeedMagnitude, color) {
const W = 300;
const H = 90;
const padX = 8;
const padY = 7;
const timeMax = 3;
const maxX = V * timeMax + R;
const points = [];
for (let i = 0; i <= 300; i++) {
const t = timeMax * i / 300;
const x = V * t + R * Math.sin(angularSpeedMagnitude * t);
const y = R + R * Math.cos(angularSpeedMagnitude * t);
const sx = padX + (W - 2 * padX) * x / maxX;
const sy = H - padY - (H - 2 * padY) * y / (2 * R);
points.push(`${i === 0 ? "M" : "L"}${sx.toFixed(2)},${sy.toFixed(2)}`);
}
svg.innerHTML = `
<line
x1="${padX}" y1="${H - padY}"
x2="${W - padX}" y2="${H - padY}"
stroke="rgba(255,255,255,.12)"
stroke-width="1"
/>
<path
d="${points.join(" ")}"
fill="none"
stroke="${color}"
stroke-width="2.4"
stroke-linecap="round"
stroke-linejoin="round"
/>
`;
}
drawPathPlot(
document.getElementById("rollingPlot"),
Math.abs(ROLLING_OMEGA),
"#39d5e8"
);
drawPathPlot(
document.getElementById("slidingPlot"),
Math.abs(SLIDING_OMEGA),
"#ffad4a"
);
// ------------------------------------------------------------
// Camera controls, implemented directly without add-ons
// ------------------------------------------------------------
const cameraControl = {
azimuth: -0.22,
elevation: 0.34,
distance: 10.2,
followX: 0
};
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - previousPointerX;
const dy = event.clientY - previousPointerY;
cameraControl.azimuth -= dx * 0.006;
cameraControl.elevation = THREE.MathUtils.clamp(
cameraControl.elevation + dy * 0.0045,
0.10,
1.02
);
previousPointerX = event.clientX;
previousPointerY = event.clientY;
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraControl.distance = THREE.MathUtils.clamp(
cameraControl.distance * Math.exp(event.deltaY * 0.001),
5.7,
17
);
}, { passive: false });
// ------------------------------------------------------------
// UI controls
// ------------------------------------------------------------
const pauseButton = document.getElementById("pauseButton");
const pathsButton = document.getElementById("pathsButton");
const resetButton = document.getElementById("resetButton");
const status = document.getElementById("status");
pauseButton.addEventListener("click", () => {
state.running = !state.running;
pauseButton.textContent = state.running ? "Pause" : "Resume";
});
pathsButton.addEventListener("click", () => {
state.showPaths = !state.showPaths;
rollingPath.visible = state.showPaths;
slidingPath.visible = state.showPaths;
pathsButton.classList.toggle("active", state.showPaths);
});
resetButton.addEventListener("click", () => {
state.time = 0;
cameraControl.followX = 0;
});
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
function updateExperiment() {
const rollingPhase = Math.abs(ROLLING_OMEGA) * state.time;
const slidingPhase = Math.abs(SLIDING_OMEGA) * state.time;
const rollingX = ROLLING_X0 + V * state.time;
const slidingX = SLIDING_X0 + V * state.time;
rolling.root.position.x = rollingX;
sliding.root.position.x = slidingX;
// Negative Z rotation corresponds to motion toward +X.
rolling.rotor.rotation.z = -rollingPhase;
sliding.rotor.rotation.z = -slidingPhase;
updatePath(
rollingPath,
rollingX,
rollingPhase,
Math.abs(ROLLING_OMEGA),
ROLLING_LANE_Z
);
updatePath(
slidingPath,
slidingX,
slidingPhase,
Math.abs(SLIDING_OMEGA),
SLIDING_LANE_Z
);
rollingContact.position.set(
rollingX,
0.032,
ROLLING_LANE_Z
);
slipArrow.position.set(
slidingX,
0.045,
SLIDING_LANE_Z
);
const averageX = (rollingX + slidingX) * 0.5;
cameraControl.followX +=
(averageX - cameraControl.followX) *
(1 - Math.exp(-3.5 * clock.getDelta()));
sun.position.set(
cameraControl.followX + 5,
10,
5
);
sun.target.position.set(cameraControl.followX, 0, 0);
const target = new THREE.Vector3(
cameraControl.followX,
0.88,
0
);
const horizontalRadius =
cameraControl.distance * Math.cos(cameraControl.elevation);
camera.position.set(
target.x + Math.sin(cameraControl.azimuth) * horizontalRadius,
target.y + Math.sin(cameraControl.elevation) * cameraControl.distance,
target.z + Math.cos(cameraControl.azimuth) * horizontalRadius
);
camera.lookAt(target);
status.textContent =
`t = ${state.time.toFixed(2)} s | ` +
`rolling slip = 0.00 m/s | ` +
`sliding slip = ${SLIDING_SLIP.toFixed(2)} m/s`;
}
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (state.running) state.time += dt * state.speedScale;
updateExperiment();
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
updateExperiment();
animate();
</script>
</body>
</html>
```