Prompt specification
Build or customize the Three.js 3D scene "Billiard momentum teaching table" 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: Billiard momentum teaching table
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/billiard-momentum-teaching-table-x1371
Scene Description Prompt:
Compose a small table with two balls and vector arrows before and after a simplified planar collision; explain the chosen collision model and omit spin effects explicitly. Build this with Three.js from cdnjs as a voxel scene in one self-contained HTML file.
Working Three.js Starter (billiard-momentum-teaching-table-x1371):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Planar Collision</title>
<style>
:root {
color-scheme: dark;
--bg: #070b14;
--panel: #111827;
--panel-2: #0c1422;
--border: #29354a;
--text: #e7edf8;
--muted: #a4b1c7;
--red: #ff5b65;
--blue: #45a9ff;
--yellow: #ffd166;
}
* { box-sizing: border-box; }
body {
margin: 0;
min-height: 100vh;
background:
radial-gradient(circle at 50% 0%, #17243b 0, transparent 48%),
var(--bg);
color: var(--text);
font: 15px/1.5 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
main {
width: min(1180px, calc(100% - 28px));
margin: 0 auto;
padding: 30px 0 42px;
}
header {
display: flex;
align-items: end;
justify-content: space-between;
gap: 20px;
margin-bottom: 18px;
}
h1 {
margin: 0;
font-size: clamp(1.65rem, 3vw, 2.45rem);
line-height: 1.1;
letter-spacing: -0.035em;
}
header p {
max-width: 610px;
margin: 0;
color: var(--muted);
}
.stage {
position: relative;
height: clamp(420px, 54vw, 560px);
overflow: hidden;
border: 1px solid var(--border);
border-radius: 18px;
background: #0b1220;
box-shadow: 0 22px 70px #0007;
}
.stage canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
.stage canvas:active { cursor: grabbing; }
.scene-label {
position: absolute;
top: 18px;
z-index: 2;
padding: 8px 12px;
border: 1px solid #ffffff20;
border-radius: 9px;
background: #07101ccc;
color: #dce7fa;
font-size: 0.78rem;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
pointer-events: none;
backdrop-filter: blur(8px);
}
.scene-label.before { left: 25%; transform: translateX(-50%); }
.scene-label.after { left: 75%; transform: translateX(-50%); }
.legend {
position: absolute;
left: 16px;
bottom: 15px;
z-index: 2;
display: flex;
flex-wrap: wrap;
gap: 13px;
padding: 8px 11px;
border: 1px solid #ffffff16;
border-radius: 9px;
background: #07101cbb;
color: var(--muted);
font-size: 0.78rem;
pointer-events: none;
backdrop-filter: blur(8px);
}
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.swatch {
width: 11px;
height: 11px;
border-radius: 3px;
box-shadow: inset -2px -2px #0004;
}
.controls {
position: absolute;
right: 15px;
bottom: 15px;
z-index: 3;
display: flex;
gap: 6px;
}
button {
min-width: 38px;
height: 34px;
padding: 0 10px;
border: 1px solid #ffffff25;
border-radius: 8px;
background: #172338dd;
color: var(--text);
font: inherit;
cursor: pointer;
}
button:hover { background: #23334d; }
button:focus-visible { outline: 2px solid var(--yellow); outline-offset: 2px; }
.hint {
position: absolute;
top: 19px;
right: 18px;
z-index: 2;
color: #91a1ba;
font-size: 0.76rem;
pointer-events: none;
}
.card {
margin-top: 18px;
padding: 18px;
border: 1px solid var(--border);
border-radius: 14px;
background: linear-gradient(145deg, var(--panel), var(--panel-2));
}
.card h2 {
margin: 0 0 12px;
font-size: 1rem;
letter-spacing: 0.02em;
}
table {
width: 100%;
border-collapse: collapse;
overflow: hidden;
border: 1px solid var(--border);
border-radius: 10px;
}
caption {
padding-bottom: 10px;
color: var(--muted);
text-align: left;
font-size: 0.86rem;
}
th, td {
padding: 13px 15px;
border-bottom: 1px solid var(--border);
text-align: left;
vertical-align: middle;
}
thead th {
background: #182338;
color: #cbd8ec;
font-size: 0.78rem;
letter-spacing: 0.06em;
text-transform: uppercase;
}
tbody tr:last-child th,
tbody tr:last-child td { border-bottom: 0; }
tbody th { color: var(--muted); }
.state {
display: inline-block;
padding: 3px 8px;
border: 1px solid #ffffff20;
border-radius: 999px;
color: #dbe6f7;
font-size: 0.78rem;
}
.visual {
display: flex;
align-items: center;
gap: 9px;
min-width: 180px;
}
.vector {
font-size: 2rem;
font-weight: 800;
line-height: 1;
}
.ball {
flex: 0 0 auto;
width: 22px;
height: 22px;
border: 1px solid #ffffff75;
border-radius: 25%;
box-shadow:
inset -4px -4px #0004,
inset 3px 3px #ffffff35,
0 3px 9px #0005;
}
.ball.red { background: var(--red); }
.ball.blue { background: var(--blue); }
.speed {
margin-left: 5px;
color: #d9e4f7;
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
.model-grid {
display: grid;
grid-template-columns: 1.1fr 0.9fr;
gap: 22px;
}
.model-grid p { margin: 0 0 10px; color: #c2cee1; }
.model-grid p:last-child { margin-bottom: 0; }
code {
padding: 2px 5px;
border-radius: 5px;
background: #02061799;
color: #eaf1ff;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.equation {
margin-top: 10px;
padding: 12px;
border-left: 3px solid var(--yellow);
background: #0a1220;
color: #dce8fb;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.86rem;
overflow-x: auto;
}
.note {
color: #ffd98a;
}
@media (max-width: 760px) {
header { display: block; }
header p { margin-top: 10px; }
.stage { height: 460px; }
.scene-label { top: 10px; font-size: 0.65rem; }
.hint { display: none; }
.legend { right: 14px; bottom: 58px; }
.controls { left: 14px; right: 14px; justify-content: flex-end; }
.model-grid { grid-template-columns: 1fr; }
th, td { padding: 10px 8px; }
.visual { min-width: 125px; gap: 6px; }
.speed { display: block; margin: 4px 0 0; }
}
</style>
</head>
<body>
<main>
<header>
<h1>Voxel Planar Collision</h1>
<p>
A frictionless, perfectly elastic collision of two equal-mass balls.
The tabletop is the planar <code>x–z</code> surface; all motion and impulses
are along the horizontal <code>x</code> axis.
</p>
</header>
<section id="stage" aria-label="Interactive voxel collision scene">
<div class="scene-label before">Before · approach</div>
<div class="scene-label after">After · separated</div>
<div class="hint">Drag to orbit · wheel to zoom</div>
<div class="legend">
<span><i class="swatch" style="background:var(--red)"></i> red ball</span>
<span><i class="swatch" style="background:var(--blue)"></i> blue ball</span>
<span><i class="swatch" style="background:var(--yellow)"></i> collision point</span>
</div>
<div class="controls">
<button id="zoom-out" type="button" aria-label="Zoom out">−</button>
<button id="zoom-in" type="button" aria-label="Zoom in">+</button>
<button id="reset" type="button">Reset view</button>
</div>
</section>
<section class="card">
<h2>Before and after</h2>
<table>
<caption>
Rightward is positive. The arrow beside each ball is its velocity vector;
arrow length is proportional to speed.
</caption>
<thead>
<tr>
<th scope="col">State</th>
<th scope="col">Red ball · m = 1</th>
<th scope="col">Blue ball · m = 1</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row"><span class="state">Before</span></th>
<td>
<div class="visual">
<i class="ball red" aria-hidden="true"></i>
<span class="vector" style="color:var(--red)" aria-label="rightward">→</span>
<span class="speed">u₁ = +2.0 m/s</span>
</div>
</td>
<td>
<div class="visual">
<span class="vector" style="color:var(--blue)" aria-label="leftward">←</span>
<i class="ball blue" aria-hidden="true"></i>
<span class="speed">u₂ = −1.0 m/s</span>
</div>
</td>
</tr>
<tr>
<th scope="row"><span class="state">After</span></th>
<td>
<div class="visual">
<span class="vector" style="color:var(--red)" aria-label="leftward">←</span>
<i class="ball red" aria-hidden="true"></i>
<span class="speed">v₁ = −1.0 m/s</span>
</div>
</td>
<td>
<div class="visual">
<i class="ball blue" aria-hidden="true"></i>
<span class="vector" style="color:var(--blue)" aria-label="rightward">→</span>
<span class="speed">v₂ = +2.0 m/s</span>
</div>
</td>
</tr>
</tbody>
</table>
</section>
<section class="card">
<h2>Chosen collision model</h2>
<div class="model-grid">
<div>
<p>
The balls are treated as equal masses on a smooth, frictionless plane.
Only a normal impulse along <code>x</code> is applied, and the coefficient
of restitution is <code>e = 1</code> (perfectly elastic).
</p>
<p>
For equal masses, the collision exchanges the balls’ velocities.
Momentum and kinetic energy are both conserved.
</p>
<p class="note">
<strong>Spin effects are explicitly omitted:</strong> no angular
velocity, tangential friction impulse, or rotational energy is modeled.
</p>
</div>
<div>
<div class="equation">
v₁ = (m₁ − m₂)/(m₁ + m₂) · u₁<br>
+ 2m₂/(m₁ + m₂) · u₂<br><br>
v₂ = 2m₁/(m₁ + m₂) · u₁<br>
+ (m₂ − m₁)/(m₁ + m₂) · u₂<br><br>
m₁ = m₂ ⇒ v₁ = u₂, v₂ = u₁
</div>
</div>
</div>
</section>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
// ---------------------------------------------------------------------------
// Renderer, camera, and lighting
// ---------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0b1220);
scene.fog = new THREE.Fog(0x0b1220, 25, 45);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Voxel scene showing two equal-mass balls before and after an elastic collision"
);
stage.prepend(renderer.domElement);
const camera = new THREE.OrthographicCamera(-10, 10, 6, -6, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0, 0);
const hemisphere = new THREE.HemisphereLight(0xbdd9ff, 0x151827, 2.2);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(7, 13, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -15;
keyLight.shadow.camera.right = 15;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 40;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x5a8dff, 1.3);
rimLight.position.set(-9, 6, -8);
scene.add(rimLight);
// A quiet floor receives the voxel-table shadows.
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(50, 40),
new THREE.MeshStandardMaterial({
color: 0x0d1524,
roughness: 1,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.27;
floor.receiveShadow = true;
scene.add(floor);
const dummy = new THREE.Object3D();
// ---------------------------------------------------------------------------
// Voxel helpers
// ---------------------------------------------------------------------------
function createVoxelTable(centerX) {
const columns = 13;
const rows = 8;
const step = 0.5;
const geometry = new THREE.BoxGeometry(0.46, 0.24, 0.46);
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.82,
metalness: 0.08
});
const count = columns * rows;
const mesh = new THREE.InstancedMesh(geometry, material, count);
mesh.receiveShadow = true;
mesh.castShadow = true;
let index = 0;
for (let row = 0; row < rows; row++) {
for (let column = 0; column < columns; column++) {
dummy.position.set(
centerX + (column - (columns - 1) / 2) * step,
-0.12,
(row - (rows - 1) / 2) * step
);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
const checker = (row + column) % 2;
const shade = checker ? 0.105 : 0.085;
mesh.setColorAt(
index,
new THREE.Color().setHSL(0.60, 0.20, shade)
);
index++;
}
}
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
scene.add(mesh);
}
function createVoxelBall(color, x, z = 0) {
const group = new THREE.Group();
group.position.set(x, 0.64, z);
const cell = 0.25;
const geometry = new THREE.BoxGeometry(cell * 0.9, cell * 0.9, cell * 0.9);
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.62,
metalness: 0.08
});
const positions = [];
for (let ix = -2; ix <= 2; ix++) {
for (let iy = -2; iy <= 2; iy++) {
for (let iz = -2; iz <= 2; iz++) {
if (ix * ix + iy * iy + iz * iz <= 4.8) {
positions.push([ix, iy, iz]);
}
}
}
}
const mesh = new THREE.InstancedMesh(geometry, material, positions.length);
mesh.castShadow = true;
mesh.receiveShadow = true;
const baseColor = new THREE.Color(color);
positions.forEach((position, index) => {
dummy.position.set(
position[0] * cell,
position[1] * cell,
position[2] * cell
);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
const shadeIndex = (position[0] + position[1] + position[2] + 6) % 3;
const shade = [0.76, 0.88, 1.0][shadeIndex];
const instanceColor = baseColor.clone().multiplyScalar(shade);
mesh.setColorAt(index, instanceColor);
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
group.add(mesh);
scene.add(group);
return group;
}
function createVoxelArrow(startX, direction, length, color, z) {
const group = new THREE.Group();
group.position.z = z;
const step = 0.18;
const shaftLength = Math.max(0.3, length - 0.65);
const shaftCount = Math.max(1, Math.floor(shaftLength / step));
const geometry = new THREE.BoxGeometry(0.17, 0.12, 0.17);
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.12,
roughness: 0.55,
metalness: 0.05
});
// Shaft.
for (let i = 0; i < shaftCount; i++) {
const cube = new THREE.Mesh(geometry, material);
cube.position.set(
startX + direction * (i + 0.5) * step,
0.075,
0
);
cube.castShadow = true;
group.add(cube);
}
// Stepped voxel arrowhead.
const rows = [
{ axial: length - 0.72, width: 3 },
{ axial: length - 0.50, width: 2 },
{ axial: length - 0.28, width: 1 }
];
for (const row of rows) {
for (let lateral = 0; lateral < row.width; lateral++) {
const offset = lateral - (row.width - 1) / 2;
const cube = new THREE.Mesh(geometry, material);
cube.position.set(
startX + direction * row.axial,
0.075,
offset * 0.18
);
cube.castShadow = true;
group.add(cube);
}
}
scene.add(group);
}
function createImpactMarker() {
const group = new THREE.Group();
group.position.set(-5.5, 0.07, 0);
const geometry = new THREE.BoxGeometry(0.19, 0.14, 0.19);
const material = new THREE.MeshStandardMaterial({
color: 0xffd166,
emissive: 0xffa600,
emissiveIntensity: 0.25,
roughness: 0.5
});
const offsets = [
[0, 0], [-0.19, 0], [0.19, 0],
[0, -0.19], [0, 0.19]
];
for (const [x, z] of offsets) {
const cube = new THREE.Mesh(geometry, material);
cube.position.set(x, 0, z);
cube.castShadow = true;
group.add(cube);
}
scene.add(group);
return material;
}
// ---------------------------------------------------------------------------
// Scene layout: before on the left, after on the right
// ---------------------------------------------------------------------------
createVoxelTable(-5.5);
createVoxelTable(5.5);
// Before: red moves right at +2, blue moves left at -1.
createVoxelBall(0xff5b65, -6.55);
createVoxelBall(0x45a9ff, -4.45);
createVoxelArrow(-5.85, +1, 2.55, 0xff5b65, 1.25);
createVoxelArrow(-5.20, -1, 1.35, 0x45a9ff, -1.25);
const impactMaterial = createImpactMarker();
// After: equal masses exchange velocities.
createVoxelBall(0xff5b65, 4.40);
createVoxelBall(0x45a9ff, 6.60);
createVoxelArrow(3.75, -1, 1.35, 0xff5b65, 1.25);
createVoxelArrow(6.20, +1, 2.55, 0x45a9ff, -1.25);
// ---------------------------------------------------------------------------
// Direct camera controls: orbit by dragging, zoom by wheel or buttons
// ---------------------------------------------------------------------------
let currentYaw = 0;
let currentPitch = 0.67;
let currentZoom = 1;
let desiredYaw = currentYaw;
let desiredPitch = currentPitch;
let desiredZoom = currentZoom;
const radius = 23;
function updateCamera(instant = false) {
if (instant) {
currentYaw = desiredYaw;
currentPitch = desiredPitch;
currentZoom = desiredZoom;
}
const horizontalRadius = radius * Math.cos(currentPitch);
camera.position.set(
cameraTarget.x + Math.sin(currentYaw) * horizontalRadius,
cameraTarget.y + Math.sin(currentPitch) * radius,
cameraTarget.z + Math.cos(currentYaw) * horizontalRadius
);
camera.lookAt(cameraTarget);
camera.zoom = currentZoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
const aspect = width / height;
renderer.setSize(width, height, false);
const viewWidth = Math.max(18.5, 10.5 * aspect);
const viewHeight = viewWidth / aspect;
camera.left = -viewWidth / 2;
camera.right = viewWidth / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
const canvas = renderer.domElement;
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
desiredYaw -= dx * 0.006;
desiredPitch = THREE.MathUtils.clamp(
desiredPitch + dy * 0.005,
0.30,
1.22
);
previousX = event.clientX;
previousY = event.clientY;
});
function stopDragging(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
const factor = event.deltaY > 0 ? 0.92 : 1.08;
desiredZoom = THREE.MathUtils.clamp(
desiredZoom * factor,
0.68,
1.75
);
},
{ passive: false }
);
document.getElementById("zoom-in").addEventListener("click", () => {
desiredZoom = THREE.MathUtils.clamp(desiredZoom * 1.12, 0.68, 1.75);
});
document.getElementById("zoom-out").addEventListener("click", () => {
desiredZoom = THREE.MathUtils.clamp(desiredZoom / 1.12, 0.68, 1.75);
});
document.getElementById("reset").addEventListener("click", () => {
desiredYaw = 0;
desiredPitch = 0.67;
desiredZoom = 1;
});
// ---------------------------------------------------------------------------
// Render loop
// ---------------------------------------------------------------------------
resize();
updateCamera(true);
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const elapsed = clock.getElapsedTime();
currentYaw = THREE.MathUtils.lerp(currentYaw, desiredYaw, 0.09);
currentPitch = THREE.MathUtils.lerp(currentPitch, desiredPitch, 0.09);
currentZoom = THREE.MathUtils.lerp(currentZoom, desiredZoom, 0.09);
updateCamera();
impactMaterial.emissiveIntensity =
0.22 + 0.12 * (0.5 + 0.5 * Math.sin(elapsed * 3.2));
renderer.render(scene, camera);
});
</script>
</body>
</html>
```