Prompt specification
Build or customize the Three.js 3D scene "Sliding Puck on Two Surface Tiles" 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: Sliding Puck on Two Surface Tiles
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/sliding-puck-on-two-surface-tiles-x3509
Scene Description Prompt:
A stylized puck crosses smooth and rough tiles under toy friction rules, maintaining continuous velocity and stopping without reversing. Three.js from cdnjs, standalone HTML.
Working Three.js Starter (sliding-puck-on-two-surface-tiles-x3509):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tile Motion Lab</title>
<style>
:root {
--ink: #17342e;
--muted: #61736e;
--paper: rgba(255, 255, 255, 0.76);
--line: rgba(23, 52, 46, 0.13);
--mint: #73c9b3;
--coral: #ef896c;
--yellow: #f4bd45;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 18% 15%, #f8fbf7 0, transparent 36%),
radial-gradient(circle at 82% 80%, #dbe9e1 0, transparent 38%),
#e8eee9;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button {
font: inherit;
}
#app,
#app canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#app canvas {
display: block;
touch-action: none;
outline: none;
}
.ui {
position: fixed;
z-index: 10;
pointer-events: none;
}
.panel {
border: 1px solid rgba(255, 255, 255, 0.8);
background: var(--paper);
box-shadow:
0 18px 48px rgba(42, 72, 63, 0.11),
inset 0 0 0 1px var(--line);
backdrop-filter: blur(16px) saturate(1.15);
-webkit-backdrop-filter: blur(16px) saturate(1.15);
}
.brand {
top: 26px;
left: 28px;
width: min(330px, calc(100vw - 56px));
padding: 20px 22px;
border-radius: 22px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: #5a776f;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--mint);
box-shadow: 0 0 0 5px rgba(115, 201, 179, 0.15);
content: "";
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 37px);
font-weight: 760;
letter-spacing: -0.055em;
line-height: 0.98;
}
.brand p {
max-width: 270px;
margin: 12px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.telemetry {
top: 26px;
right: 28px;
width: 225px;
padding: 16px;
border-radius: 20px;
}
.telemetry-row {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 12px;
}
.label {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.speed {
font-variant-numeric: tabular-nums;
font-size: 31px;
font-weight: 760;
letter-spacing: -0.06em;
line-height: 1;
}
.speed small {
margin-left: 3px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0;
}
.meter {
height: 5px;
margin: 15px 0 12px;
overflow: hidden;
border-radius: 99px;
background: rgba(23, 52, 46, 0.1);
}
.meter > span {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--mint), var(--yellow));
transition: width 80ms linear;
}
.tile-status {
display: flex;
align-items: center;
justify-content: space-between;
color: var(--muted);
font-size: 10px;
font-weight: 700;
}
#surfaceName {
color: var(--ink);
}
.progress-track {
height: 3px;
margin-top: 14px;
overflow: hidden;
border-radius: 99px;
background: rgba(23, 52, 46, 0.08);
}
#progress {
display: block;
width: 0;
height: 100%;
background: var(--ink);
}
.legend {
bottom: 28px;
left: 28px;
display: flex;
align-items: center;
gap: 18px;
padding: 13px 17px;
border-radius: 17px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
white-space: nowrap;
}
.swatch {
width: 12px;
height: 12px;
border-radius: 4px;
box-shadow: inset 0 0 0 1px rgba(23, 52, 46, 0.1);
}
.swatch.smooth {
background: #b8ddd2;
}
.swatch.rough {
background:
radial-gradient(circle, #f8c1ae 20%, transparent 23%) 0 0 / 5px 5px,
var(--coral);
}
.hint {
margin-left: 3px;
padding-left: 18px;
border-left: 1px solid var(--line);
color: #81918c;
font-weight: 600;
}
.controls {
right: 28px;
bottom: 28px;
display: flex;
gap: 9px;
pointer-events: auto;
}
.control-button {
display: flex;
min-width: 106px;
height: 45px;
align-items: center;
justify-content: center;
gap: 8px;
padding: 0 16px;
border: 1px solid rgba(255, 255, 255, 0.85);
border-radius: 15px;
outline: none;
background: rgba(255, 255, 255, 0.82);
box-shadow:
0 10px 28px rgba(42, 72, 63, 0.1),
inset 0 0 0 1px var(--line);
color: var(--ink);
cursor: pointer;
font-size: 11px;
font-weight: 750;
transition:
transform 150ms ease,
background 150ms ease,
box-shadow 150ms ease;
}
.control-button:hover {
transform: translateY(-2px);
background: #fff;
box-shadow:
0 14px 32px rgba(42, 72, 63, 0.14),
inset 0 0 0 1px var(--line);
}
.control-button:active {
transform: translateY(0);
}
.control-button.primary {
border-color: transparent;
background: var(--ink);
color: #fff;
}
.control-button.primary:hover {
background: #21483f;
}
.control-button svg {
width: 14px;
height: 14px;
fill: currentColor;
}
@media (max-width: 720px) {
.brand {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 15px 17px;
}
.brand p {
display: none;
}
.telemetry {
top: 122px;
right: 14px;
width: 190px;
}
.legend {
right: 14px;
bottom: 74px;
left: 14px;
justify-content: center;
}
.hint {
display: none;
}
.controls {
right: 14px;
bottom: 14px;
left: 14px;
}
.control-button {
flex: 1;
}
}
</style>
</head>
<body>
<div id="app"></div>
<header class="ui panel brand">
<div class="eyebrow">Kinetic tabletop study</div>
<h1>Tile Motion Lab</h1>
<p>
A momentum-driven puck glides over smooth tiles, loses speed on rough
patches, and never rolls backward.
</p>
</header>
<aside class="ui panel telemetry">
<div class="telemetry-row">
<div>
<div class="label">Current speed</div>
<div class="speed"><span id="speed">3.20</span><small>u/s</small></div>
</div>
<div class="label" id="stateLabel">Running</div>
</div>
<div class="meter"><span id="speedBar"></span></div>
<div class="tile-status">
<span id="surfaceName">Smooth tile</span>
<span id="frictionValue">drag 0.02</span>
</div>
<div class="progress-track"><span id="progress"></span></div>
</aside>
<aside class="ui panel legend">
<div class="legend-item">
<span class="swatch smooth"></span>
Smooth · coast
</div>
<div class="legend-item">
<span class="swatch rough"></span>
Rough · drag
</div>
<div class="hint">Drag to orbit · scroll to zoom</div>
</aside>
<nav class="ui controls" aria-label="Animation controls">
<button class="control-button" id="restart" type="button">
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M12 5a7 7 0 1 1-6.32 4H3l3.5-4L10 9H7.75A5 5 0 1 0 12 7V5z"/>
</svg>
Restart
</button>
<button class="control-button primary" id="pause" type="button">
<svg id="pauseIcon" viewBox="0 0 24 24" aria-hidden="true">
<path d="M6 5h4v14H6zm8 0h4v14h-4z"/>
</svg>
<span id="pauseLabel">Pause</span>
</button>
</nav>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const container = document.getElementById("app");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: 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.04;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8eee9);
scene.fog = new THREE.Fog(0xe8eee9, 28, 55);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
// Camera orbit is implemented directly without addons.
const cameraState = {
target: new THREE.Vector3(0, 0.2, -0.5),
radius: 20.5,
theta: 0.34,
phi: 0.91
};
function updateCamera() {
const { target, radius, theta, phi } = cameraState;
const horizontal = radius * Math.sin(phi);
camera.position.set(
target.x + horizontal * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + horizontal * Math.cos(theta)
);
camera.lookAt(target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xffffff, 0x82968c, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff7e8, 3.1);
keyLight.position.set(-7, 13, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 13;
keyLight.shadow.camera.bottom = -13;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 38;
keyLight.shadow.bias = -0.00035;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xffc5ad, 1.15);
rimLight.position.set(9, 6, -10);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(160, 160),
new THREE.MeshStandardMaterial({
color: 0xe8eee9,
roughness: 1,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.43;
floor.receiveShadow = true;
scene.add(floor);
// ------------------------------------------------------------
// Geometry helpers
// ------------------------------------------------------------
function roundedRectangle(width, height, radius) {
const shape = new THREE.Shape();
const x = -width / 2;
const y = -height / 2;
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
return shape;
}
function roundedExtrusion(width, height, radius, depth, bevel = 0.05) {
const geometry = new THREE.ExtrudeGeometry(
roundedRectangle(width, height, radius),
{
depth,
steps: 1,
curveSegments: 8,
bevelEnabled: bevel > 0,
bevelSegments: 3,
bevelSize: bevel,
bevelThickness: bevel
}
);
geometry.center();
geometry.rotateX(-Math.PI / 2);
return geometry;
}
// ------------------------------------------------------------
// Board and tiles
// ------------------------------------------------------------
const board = new THREE.Mesh(
roundedExtrusion(11.25, 17.25, 0.72, 0.48, 0.08),
new THREE.MeshStandardMaterial({
color: 0x31574d,
roughness: 0.83,
metalness: 0.02
})
);
board.position.y = -0.12;
board.receiveShadow = true;
board.castShadow = true;
scene.add(board);
const boardInset = new THREE.Mesh(
roundedExtrusion(10.72, 16.72, 0.58, 0.08, 0.025),
new THREE.MeshStandardMaterial({
color: 0xdce8df,
roughness: 0.95
})
);
boardInset.position.y = 0.115;
boardInset.receiveShadow = true;
scene.add(boardInset);
const tileGeometry = roundedExtrusion(1.9, 1.9, 0.18, 0.16, 0.045);
const smoothMaterial = new THREE.MeshStandardMaterial({
color: 0xb8ddd2,
roughness: 0.62,
metalness: 0.02
});
const roughMaterial = new THREE.MeshStandardMaterial({
color: 0xef896c,
roughness: 0.96,
metalness: 0
});
const roughTiles = new Set([
"1-0", "1-4",
"2-1", "2-2", "2-3",
"3-0", "3-1", "3-4",
"4-0", "4-1", "4-3", "4-4",
"5-1", "5-2",
"6-0", "6-4"
]);
const bumpPositions = [];
for (let row = 0; row < 8; row++) {
const z = 7 - row * 2;
for (let column = 0; column < 5; column++) {
const x = -4 + column * 2;
const isRough = roughTiles.has(`${row}-${column}`);
const tile = new THREE.Mesh(
tileGeometry,
isRough ? roughMaterial : smoothMaterial
);
tile.position.set(x, 0.205, z);
tile.receiveShadow = true;
tile.castShadow = true;
scene.add(tile);
if (isRough) {
for (let bx = -1; bx <= 1; bx++) {
for (let bz = -1; bz <= 1; bz++) {
bumpPositions.push(
new THREE.Vector3(
x + bx * 0.39 + (bz % 2) * 0.09,
0.315,
z + bz * 0.39 + (bx % 2) * 0.09
)
);
}
}
}
}
}
const bumpGeometry = new THREE.SphereGeometry(0.105, 12, 8);
const bumpMaterial = new THREE.MeshStandardMaterial({
color: 0xffb39d,
roughness: 0.96
});
const bumpMesh = new THREE.InstancedMesh(
bumpGeometry,
bumpMaterial,
bumpPositions.length
);
const bumpDummy = new THREE.Object3D();
bumpPositions.forEach((position, index) => {
bumpDummy.position.copy(position);
bumpDummy.scale.set(1, 0.45, 1);
bumpDummy.updateMatrix();
bumpMesh.setMatrixAt(index, bumpDummy.matrix);
});
bumpMesh.castShadow = true;
bumpMesh.receiveShadow = true;
scene.add(bumpMesh);
// Corner hardware.
const boltGeometry = new THREE.CylinderGeometry(0.105, 0.105, 0.055, 20);
const boltMaterial = new THREE.MeshStandardMaterial({
color: 0xd8c58b,
roughness: 0.38,
metalness: 0.62
});
for (const x of [-5.08, 5.08]) {
for (const z of [-7.9, 7.9]) {
const bolt = new THREE.Mesh(boltGeometry, boltMaterial);
bolt.position.set(x, 0.18, z);
bolt.castShadow = true;
scene.add(bolt);
}
}
// ------------------------------------------------------------
// Route and finish gate
// ------------------------------------------------------------
const startZ = 6.55;
const endZ = -6.25;
function routeX(progress) {
return (
2.25 *
Math.sin(progress * Math.PI * 2.35) *
Math.sin(Math.PI * progress)
);
}
const routePoints = [];
for (let i = 0; i <= 100; i++) {
const progress = i / 100;
routePoints.push(
new THREE.Vector3(
routeX(progress),
0.365,
THREE.MathUtils.lerp(startZ, endZ, progress)
)
);
}
const routeGeometry = new THREE.BufferGeometry().setFromPoints(routePoints);
const route = new THREE.Line(
routeGeometry,
new THREE.LineDashedMaterial({
color: 0xffffff,
dashSize: 0.17,
gapSize: 0.16,
transparent: true,
opacity: 0.58
})
);
route.computeLineDistances();
scene.add(route);
const finishLine = new THREE.Mesh(
new THREE.BoxGeometry(9.55, 0.035, 0.13),
new THREE.MeshStandardMaterial({
color: 0xf4bd45,
roughness: 0.5
})
);
finishLine.position.set(0, 0.35, endZ - 0.03);
finishLine.receiveShadow = true;
scene.add(finishLine);
const gateMaterial = new THREE.MeshStandardMaterial({
color: 0xf4bd45,
roughness: 0.48,
metalness: 0.08
});
for (const x of [-1.08, 1.08]) {
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.065, 0.075, 1.35, 16),
gateMaterial
);
post.position.set(x, 1.02, endZ - 0.24);
post.castShadow = true;
scene.add(post);
}
const gateBar = new THREE.Mesh(
new THREE.BoxGeometry(2.23, 0.075, 0.075),
gateMaterial
);
gateBar.position.set(0, 1.68, endZ - 0.24);
gateBar.castShadow = true;
scene.add(gateBar);
// ------------------------------------------------------------
// Puck and trail
// ------------------------------------------------------------
const puck = new THREE.Group();
const puckBody = new THREE.Mesh(
new THREE.CylinderGeometry(0.49, 0.56, 0.29, 48),
new THREE.MeshStandardMaterial({
color: 0xf4bd45,
roughness: 0.36,
metalness: 0.12
})
);
puckBody.castShadow = true;
puckBody.receiveShadow = true;
puck.add(puckBody);
const puckTop = new THREE.Mesh(
new THREE.CylinderGeometry(0.39, 0.39, 0.055, 48),
new THREE.MeshStandardMaterial({
color: 0xffd86f,
roughness: 0.3,
metalness: 0.05
})
);
puckTop.position.y = 0.17;
puckTop.castShadow = true;
puck.add(puckTop);
const puckRim = new THREE.Mesh(
new THREE.TorusGeometry(0.465, 0.048, 12, 48),
new THREE.MeshStandardMaterial({
color: 0xd99523,
roughness: 0.4,
metalness: 0.16
})
);
puckRim.rotation.x = Math.PI / 2;
puckRim.position.y = 0.13;
puckRim.castShadow = true;
puck.add(puckRim);
const puckMark = new THREE.Mesh(
new THREE.BoxGeometry(0.075, 0.025, 0.29),
new THREE.MeshStandardMaterial({
color: 0x735c2e,
roughness: 0.7
})
);
puckMark.position.set(0.16, 0.205, 0);
puck.add(puckMark);
scene.add(puck);
const trailCount = 28;
const trail = new THREE.InstancedMesh(
new THREE.SphereGeometry(0.09, 10, 8),
new THREE.MeshBasicMaterial({
color: 0xf4bd45,
transparent: true,
opacity: 0.3,
depthWrite: false
}),
trailCount
);
trail.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
scene.add(trail);
const trailHistory = [];
const trailDummy = new THREE.Object3D();
function updateTrail() {
for (let i = 0; i < trailCount; i++) {
const point = trailHistory[i];
if (point) {
trailDummy.position.set(point.x, 0.34, point.z);
const scale = Math.max(0, 1 - i / trailCount) * 0.9;
trailDummy.scale.setScalar(scale);
} else {
trailDummy.position.set(0, -10, 0);
trailDummy.scale.setScalar(0);
}
trailDummy.updateMatrix();
trail.setMatrixAt(i, trailDummy.matrix);
}
trail.instanceMatrix.needsUpdate = true;
}
// ------------------------------------------------------------
// Toy friction simulation
// ------------------------------------------------------------
const simulation = {
x: 0,
z: startZ,
speed: 3.2,
paused: false,
finished: false,
elapsed: 0
};
const speedElement = document.getElementById("speed");
const speedBar = document.getElementById("speedBar");
const surfaceName = document.getElementById("surfaceName");
const frictionValue = document.getElementById("frictionValue");
const stateLabel = document.getElementById("stateLabel");
const progressElement = document.getElementById("progress");
const pauseButton = document.getElementById("pause");
const pauseLabel = document.getElementById("pauseLabel");
const pauseIcon = document.getElementById("pauseIcon");
function tileAt(x, z) {
const row = THREE.MathUtils.clamp(Math.round((7 - z) / 2), 0, 7);
const column = THREE.MathUtils.clamp(Math.round((x + 4) / 2), 0, 4);
const rough = roughTiles.has(`${row}-${column}`);
return { row, column, rough };
}
function resetSimulation() {
simulation.x = 0;
simulation.z = startZ;
simulation.speed = 3.2;
simulation.paused = false;
simulation.finished = false;
simulation.elapsed = 0;
puck.position.set(simulation.x, 0.405, simulation.z);
puck.rotation.y = 0;
trailHistory.length = 0;
updateTrail();
pauseLabel.textContent = "Pause";
pauseIcon.innerHTML =
'<path d="M6 5h4v14H6zm8 0h4v14h-4z"/>';
}
function togglePause() {
if (simulation.finished) return;
simulation.paused = !simulation.paused;
if (simulation.paused) {
pauseLabel.textContent = "Resume";
pauseIcon.innerHTML =
'<path d="M8 5v14l11-7z"/>';
} else {
pauseLabel.textContent = "Pause";
pauseIcon.innerHTML =
'<path d="M6 5h4v14H6zm8 0h4v14h-4z"/>';
}
}
document.getElementById("restart").addEventListener("click", resetSimulation);
pauseButton.addEventListener("click", togglePause);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
togglePause();
}
if (event.key.toLowerCase() === "r") {
resetSimulation();
}
});
function updateSimulation(dt) {
if (simulation.paused || simulation.finished) return;
simulation.elapsed += dt;
const tile = tileAt(simulation.x, simulation.z);
const drag = tile.rough ? 0.92 : 0.02;
// Velocity is integrated continuously and clamped at zero.
// Friction can stop the puck, but can never reverse it.
simulation.speed = Math.max(0, simulation.speed - drag * dt);
simulation.z -= simulation.speed * dt;
const progress = THREE.MathUtils.clamp(
(startZ - simulation.z) / (startZ - endZ),
0,
1
);
const targetX = routeX(progress);
simulation.x = THREE.MathUtils.damp(
simulation.x,
targetX,
5.0,
dt
);
puck.position.set(simulation.x, 0.405, simulation.z);
puck.rotation.y -= simulation.speed * dt * 4.8;
if (
trailHistory.length === 0 ||
puck.position.distanceTo(trailHistory[0]) > 0.13
) {
trailHistory.unshift(puck.position.clone());
if (trailHistory.length > trailCount) {
trailHistory.pop();
}
}
if (simulation.z <= endZ) {
simulation.z = endZ;
simulation.speed = 0;
simulation.finished = true;
pauseLabel.textContent = "Complete";
pauseIcon.innerHTML =
'<path d="M9 16.2 4.8 12l-1.4 1.4L9 19 21 7l-1.4-1.4z"/>';
}
}
function updateHUD() {
const tile = tileAt(simulation.x, simulation.z);
const progress = THREE.MathUtils.clamp(
(startZ - simulation.z) / (startZ - endZ),
0,
1
);
speedElement.textContent = simulation.speed.toFixed(2);
speedBar.style.width = `${Math.min(100, simulation.speed / 3.2 * 100)}%`;
progressElement.style.width = `${progress * 100}%`;
surfaceName.textContent = tile.rough ? "Rough tile" : "Smooth tile";
frictionValue.textContent = tile.rough ? "drag 0.92" : "drag 0.02";
if (simulation.finished) {
stateLabel.textContent = "Arrived";
} else if (simulation.paused) {
stateLabel.textContent = "Paused";
} else if (simulation.speed === 0) {
stateLabel.textContent = "Stopped";
} else {
stateLabel.textContent = tile.rough ? "Braking" : "Coasting";
}
}
// ------------------------------------------------------------
// Direct pointer camera controls
// ------------------------------------------------------------
const pointer = {
active: false,
id: null,
x: 0,
y: 0
};
renderer.domElement.addEventListener("pointerdown", (event) => {
pointer.active = true;
pointer.id = event.pointerId;
pointer.x = event.clientX;
pointer.y = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointer.active || event.pointerId !== pointer.id) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
pointer.x = event.clientX;
pointer.y = event.clientY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.005,
0.48,
1.22
);
updateCamera();
});
function endPointer(event) {
if (!pointer.active || event.pointerId !== pointer.id) return;
pointer.active = false;
pointer.id = null;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
12,
30
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraState.radius = 20.5;
cameraState.theta = 0.34;
cameraState.phi = 0.91;
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// ------------------------------------------------------------
// Render loop
// ------------------------------------------------------------
const clock = new THREE.Clock();
resetSimulation();
renderer.setAnimationLoop(() => {
const dt = Math.min(clock.getDelta(), 0.05);
updateSimulation(dt);
updateTrail();
updateHUD();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```