Prompt specification
Build or customize the Three.js 3D scene "Drop Two Balls With Different Restitution" 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: Drop Two Balls With Different Restitution
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/drop-two-balls-with-different-restitution-x3602
Scene Description Prompt:
Drop two voxel balls under equal gravity with different restitution values; show predicted rebounds settling smoothly. Make one HTML scene with Three.js from cdnjs.
Working Three.js Starter (drop-two-balls-with-different-restitution-x3602):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Rebound Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #071019;
--panel: rgba(10, 22, 33, 0.82);
--border: rgba(157, 214, 235, 0.16);
--text: #e8f7ff;
--muted: #86a4b3;
--pink: #ff5f87;
--cyan: #49d9ff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 72% 24%, #173346 0%, transparent 38%),
radial-gradient(circle at 22% 78%, #241629 0%, transparent 40%),
var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
position: absolute;
pointer-events: auto;
border: 1px solid var(--border);
background: var(--panel);
backdrop-filter: blur(16px);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
}
#titlePanel {
top: 22px;
left: 22px;
width: min(360px, calc(100vw - 44px));
padding: 20px;
border-radius: 18px;
}
.eyebrow {
margin-bottom: 8px;
color: #91cfe2;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(24px, 4vw, 34px);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
margin: 10px 0 18px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.readouts {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.readout {
min-width: 0;
padding: 12px;
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 12px;
background: rgba(255, 255, 255, 0.025);
}
.readout-head {
display: flex;
align-items: center;
gap: 7px;
margin-bottom: 8px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 13px currentColor;
}
.pink {
color: var(--pink);
}
.cyan {
color: var(--cyan);
}
.height {
color: var(--text);
font-size: 23px;
font-variant-numeric: tabular-nums;
font-weight: 750;
}
.unit {
margin-left: 3px;
color: var(--muted);
font-size: 11px;
font-weight: 600;
}
.meter {
height: 4px;
margin: 10px 0 8px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.08);
}
.meter i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
.meta {
display: flex;
justify-content: space-between;
gap: 8px;
color: var(--muted);
font-size: 10px;
font-variant-numeric: tabular-nums;
}
#controlsPanel {
right: 22px;
bottom: 22px;
width: min(330px, calc(100vw - 44px));
padding: 17px;
border-radius: 16px;
}
.control-title {
margin-bottom: 13px;
color: #b8d7e3;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.slider-row {
margin: 13px 0;
}
.slider-label {
display: flex;
justify-content: space-between;
margin-bottom: 7px;
color: var(--muted);
font-size: 12px;
}
.slider-label strong {
color: var(--text);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
appearance: none;
border-radius: 99px;
outline: none;
background: rgba(255, 255, 255, 0.12);
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
appearance: none;
border: 3px solid #10202b;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 12px currentColor;
cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 3px solid #10202b;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 12px currentColor;
cursor: pointer;
}
.equation {
margin: 14px 0;
padding: 10px 12px;
border-radius: 10px;
background: rgba(84, 191, 225, 0.07);
color: #a9d9e8;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
text-align: center;
}
.button-row {
display: flex;
gap: 8px;
}
button {
flex: 1;
min-height: 37px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 9px;
background: rgba(255, 255, 255, 0.055);
color: var(--text);
font: inherit;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.04em;
cursor: pointer;
transition: background 150ms ease, transform 150ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.1);
}
button:active {
transform: translateY(1px);
}
#dropButton {
border-color: rgba(73, 217, 255, 0.28);
background: rgba(73, 217, 255, 0.11);
}
#hint {
position: absolute;
bottom: 25px;
left: 22px;
color: rgba(190, 218, 230, 0.58);
font-size: 11px;
pointer-events: none;
}
#gravityBadge {
position: absolute;
top: 24px;
right: 24px;
padding: 10px 13px;
border: 1px solid var(--border);
border-radius: 99px;
background: rgba(10, 22, 33, 0.7);
color: #a8ccda;
font-size: 11px;
font-variant-numeric: tabular-nums;
backdrop-filter: blur(12px);
}
@media (max-width: 700px) {
#titlePanel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 15px;
}
.subtitle {
margin-bottom: 12px;
}
#controlsPanel {
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 13px;
}
#hint {
display: none;
}
#gravityBadge {
top: auto;
right: 14px;
bottom: 230px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div id="hud">
<section id="titlePanel" class="panel">
<div class="eyebrow">Interactive physics study</div>
<h1>Voxel Rebound Lab</h1>
<p class="subtitle">
Two identical voxel balls, one gravity field, different collision responses.
Dashed curves show their predicted ballistic paths.
</p>
<div class="readouts">
<div class="readout pink">
<div class="readout-head">
<span class="dot"></span>
Ball A
</div>
<div>
<span id="heightA" class="height">0.00</span><span class="unit">m</span>
</div>
<div class="meter pink"><i id="meterA"></i></div>
<div class="meta">
<span id="stateA">RELEASED</span>
<span id="impactsA">0 impacts</span>
</div>
</div>
<div class="readout cyan">
<div class="readout-head">
<span class="dot"></span>
Ball B
</div>
<div>
<span id="heightB" class="height">0.00</span><span class="unit">m</span>
</div>
<div class="meter cyan"><i id="meterB"></i></div>
<div class="meta">
<span id="stateB">RELEASED</span>
<span id="impactsB">0 impacts</span>
</div>
</div>
</div>
</section>
<section id="controlsPanel" class="panel">
<div class="control-title">Collision parameters</div>
<div class="slider-row pink">
<label class="slider-label" for="restitutionA">
<span>Ball A restitution</span>
<strong id="restitutionValueA">0.82</strong>
</label>
<input id="restitutionA" type="range" min="0.10" max="0.95" step="0.01" value="0.82" />
</div>
<div class="slider-row cyan">
<label class="slider-label" for="restitutionB">
<span>Ball B restitution</span>
<strong id="restitutionValueB">0.48</strong>
</label>
<input id="restitutionB" type="range" min="0.10" max="0.95" step="0.01" value="0.48" />
</div>
<div class="equation">next rebound height = e² × previous height</div>
<div class="button-row">
<button id="dropButton">Drop again</button>
<button id="pauseButton">Pause</button>
<button id="pathsButton" aria-pressed="true">Hide paths</button>
</div>
</section>
<div id="gravityBadge">Shared gravity · g = 16.0 m/s²</div>
<div id="hint">Drag to orbit · Shift-drag to pan · Scroll to zoom · Double-click to reset view</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const GRAVITY = 16.0;
const REST_Y = 1.12;
const START_Y = 8.4;
const DROP_HEIGHT = START_Y - REST_Y;
const TERMINAL_SPEED = 0.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.Fog(0x071019, 19, 42);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.domElement.style.touchAction = "none";
$("stage").appendChild(renderer.domElement);
// Lighting
scene.add(new THREE.HemisphereLight(0xc9efff, 0x10101c, 2.0));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(8, 14, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 14;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 40;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x49d9ff, 1.25);
rimLight.position.set(-10, 7, -7);
scene.add(rimLight);
const warmLight = new THREE.DirectionalLight(0xff5f87, 0.75);
warmLight.position.set(9, 4, -5);
scene.add(warmLight);
// Ground and platform
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0x08131d,
roughness: 0.96,
metalness: 0.02
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.25;
ground.receiveShadow = true;
scene.add(ground);
const platform = new THREE.Mesh(
new THREE.BoxGeometry(10, 0.24, 6),
new THREE.MeshStandardMaterial({
color: 0x102430,
roughness: 0.68,
metalness: 0.12
})
);
platform.position.y = -0.12;
platform.receiveShadow = true;
scene.add(platform);
const platformEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(platform.geometry),
new THREE.LineBasicMaterial({
color: 0x285267,
transparent: true,
opacity: 0.55
})
);
platformEdges.position.copy(platform.position);
scene.add(platformEdges);
const grid = new THREE.GridHelper(10, 20, 0x32677b, 0x173642);
grid.position.y = 0.008;
grid.scale.z = 0.6;
grid.material.transparent = true;
grid.material.opacity = 0.48;
scene.add(grid);
// A voxel ball made from one InstancedMesh.
function createVoxelBall(color) {
const group = new THREE.Group();
const cubeSize = 0.30;
const spacing = 0.32;
const latticeRadius = 3.25;
const cells = [];
for (let x = -3; x <= 3; x++) {
for (let y = -3; y <= 3; y++) {
for (let z = -3; z <= 3; z++) {
if (Math.hypot(x, y, z) <= latticeRadius) {
cells.push([x, y, z]);
}
}
}
}
const geometry = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
emissive: new THREE.Color(color),
emissiveIntensity: 0.14,
roughness: 0.32,
metalness: 0.08
});
const mesh = new THREE.InstancedMesh(geometry, material, cells.length);
mesh.castShadow = true;
mesh.receiveShadow = true;
const dummy = new THREE.Object3D();
const instanceColor = new THREE.Color();
cells.forEach((cell, index) => {
dummy.position.set(
cell[0] * spacing,
cell[1] * spacing,
cell[2] * spacing
);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
instanceColor.set(color);
instanceColor.offsetHSL(
((index % 7) - 3) * 0.006,
((index % 5) - 2) * 0.018,
((index % 9) - 4) * 0.012
);
mesh.setColorAt(index, instanceColor);
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
group.add(mesh);
const glow = new THREE.PointLight(color, 4.5, 5.5, 2);
group.add(glow);
group.userData.mesh = mesh;
return group;
}
const ballA = createVoxelBall(0xff5f87);
const ballB = createVoxelBall(0x49d9ff);
ballA.position.x = -2.25;
ballB.position.x = 2.25;
ballA.position.y = START_Y;
ballB.position.y = START_Y;
scene.add(ballA, ballB);
// Exact ballistic segments are precomputed from:
// y(t) = y0 + v0*t - 0.5*g*t^2
// A collision reverses velocity and multiplies its magnitude by restitution.
function buildMotion(restitution) {
const segments = [];
const impacts = [];
let time = 0;
let y = START_Y;
let velocity = 0;
for (let iteration = 0; iteration < 60; iteration++) {
const discriminant =
velocity * velocity + 2 * GRAVITY * Math.max(0, y - REST_Y);
if (discriminant <= 0) break;
const duration =
(velocity + Math.sqrt(discriminant)) / GRAVITY;
if (!Number.isFinite(duration) || duration <= 0.000001) break;
const endTime = time + duration;
segments.push({
t0: time,
t1: endTime,
y0: y,
velocity0: velocity,
settle: false
});
const impactVelocity = velocity - GRAVITY * duration;
impacts.push({ time: endTime });
const reboundSpeed = Math.max(0, -impactVelocity * restitution);
time = endTime;
y = REST_Y;
velocity = reboundSpeed;
// A mathematically perfect restitution model can bounce forever.
// Once the bounce is below visual resolution, fold the terminal
// micro-bounce into a short, smooth settling motion.
if (reboundSpeed < TERMINAL_SPEED) {
segments.push({
t0: time,
t1: time + 0.65,
y0: REST_Y,
velocity0: 0,
settle: true
});
break;
}
}
return { segments, impacts };
}
function segmentY(segment, localTime) {
if (segment.settle) {
const p = THREE.MathUtils.clamp(
(localTime) / (segment.t1 - segment.t0),
0,
1
);
return REST_Y + Math.sin(p * Math.PI) * 0.075 * (1 - p);
}
return (
segment.y0 +
segment.velocity0 * localTime -
0.5 * GRAVITY * localTime * localTime
);
}
function sampleMotion(motion, time) {
const last = motion.segments[motion.segments.length - 1];
if (time >= last.t1) {
return {
y: REST_Y,
velocity: 0,
state: "SETTLED"
};
}
for (const segment of motion.segments) {
if (time >= segment.t0 && time <= segment.t1) {
const localTime = time - segment.t0;
if (segment.settle) {
return {
y: segmentY(segment, localTime),
velocity: 0,
state: "SETTLING"
};
}
const velocity =
segment.velocity0 - GRAVITY * localTime;
return {
y: segmentY(segment, localTime),
velocity,
state:
time - segment.t0 < 0.015
? "RELEASED"
: velocity < 0
? "FALLING"
: "RISING"
};
}
}
return { y: REST_Y, velocity: 0, state: "SETTLED" };
}
function createTrajectory(config, motion) {
const points = [];
const times = [];
for (const segment of motion.segments) {
if (segment.settle) continue;
const steps = 28;
for (let i = 0; i <= steps; i++) {
const t = segment.t0 +
((segment.t1 - segment.t0) * i) / steps;
points.push(
new THREE.Vector3(
config.x,
segmentY(segment, t - segment.t0),
0
)
);
times.push(t);
}
}
const predictionGeometry = new THREE.BufferGeometry().setFromPoints(points);
const predictionMaterial = new THREE.LineDashedMaterial({
color: config.color,
transparent: true,
opacity: 0.23,
dashSize: 0.17,
gapSize: 0.11,
depthWrite: false
});
const prediction = new THREE.Line(
predictionGeometry,
predictionMaterial
);
prediction.computeLineDistances();
scene.add(prediction);
const trailGeometry = new THREE.BufferGeometry().setFromPoints(points);
const trailMaterial = new THREE.LineBasicMaterial({
color: config.color,
transparent: true,
opacity: 0.85,
depthWrite: false
});
const trail = new THREE.Line(trailGeometry, trailMaterial);
trail.geometry.setDrawRange(0, 1);
scene.add(trail);
const rings = motion.impacts.map((impact) => {
const ring = new THREE.Mesh(
new THREE.RingGeometry(0.38, 0.46, 48),
new THREE.MeshBasicMaterial({
color: config.color,
transparent: true,
opacity: 0,
side: THREE.DoubleSide,
depthWrite: false
})
);
ring.rotation.x = -Math.PI / 2;
ring.position.set(config.x, 0.018, 0);
ring.visible = false;
ring.userData.impactTime = impact.time;
scene.add(ring);
return ring;
});
return {
config,
motion,
prediction,
trail,
times,
rings,
visiblePoints: 1
};
}
let predictions = [];
function disposeObject(object) {
scene.remove(object);
if (object.geometry) object.geometry.dispose();
if (object.material) {
if (Array.isArray(object.material)) {
object.material.forEach((material) => material.dispose());
} else {
object.material.dispose();
}
}
}
function rebuildPredictions() {
predictions.forEach((prediction) => {
disposeObject(prediction.prediction);
disposeObject(prediction.trail);
prediction.rings.forEach(disposeObject);
});
const configs = [
{
x: -2.25,
color: 0xff5f87,
restitution: Number($("restitutionA").value)
},
{
x: 2.25,
color: 0x49d9ff,
restitution: Number($("restitutionB").value)
}
];
$("restitutionValueA").textContent = configs[0].restitution.toFixed(2);
$("restitutionValueB").textContent = configs[1].restitution.toFixed(2);
predictions = configs.map((config) =>
createTrajectory(config, buildMotion(config.restitution))
);
simulationTime = 0;
paused = false;
$("pauseButton").textContent = "Pause";
}
function updatePrediction(prediction) {
while (
prediction.visiblePoints < prediction.times.length &&
prediction.times[prediction.visiblePoints] <= simulationTime
) {
prediction.visiblePoints++;
}
prediction.trail.geometry.setDrawRange(
0,
Math.max(1, prediction.visiblePoints)
);
for (const ring of prediction.rings) {
const age = simulationTime - ring.userData.impactTime;
if (age >= 0 && age < 0.58) {
const p = age / 0.58;
ring.visible = true;
ring.material.opacity = 0.78 * Math.pow(1 - p, 2);
const scale = 1 + p * 3.2;
ring.scale.setScalar(scale);
} else {
ring.visible = false;
}
}
}
function updateBall(ball, motion, time) {
const sample = sampleMotion(motion, time);
const mesh = ball.userData.mesh;
ball.position.y = sample.y;
mesh.rotation.y = time * 0.32;
mesh.rotation.x = Math.sin(time * 0.72) * 0.075;
let nearestImpact = Infinity;
for (const impact of motion.impacts) {
nearestImpact = Math.min(
nearestImpact,
Math.abs(time - impact.time)
);
}
const squash = Math.max(0, 1 - nearestImpact / 0.13);
ball.scale.set(
1 + squash * 0.075,
1 - squash * 0.15,
1 + squash * 0.075
);
return sample;
}
function updateReadout(prefix, sample, motion) {
const height = Math.max(0, sample.y - REST_Y);
const impacts = motion.impacts.filter(
(impact) => impact.time <= simulationTime
).length;
$(`height${prefix}`).textContent = height.toFixed(2);
$(`meter${prefix}`).style.width =
`${Math.min(100, (height / DROP_HEIGHT) * 100)}%`;
$(`state${prefix}`).textContent = sample.state;
$(`impacts${prefix}`).textContent =
`${impacts} impact${impacts === 1 ? "" : "s"}`;
}
// Camera controls implemented directly with pointer and wheel events.
const cameraTarget = new THREE.Vector3(0, 4.1, 0);
let cameraTheta = 0.55;
let cameraPhi = 1.18;
let cameraRadius = 16;
let dragging = false;
let dragMode = "orbit";
let pointerX = 0;
let pointerY = 0;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
function resetCamera() {
cameraTarget.set(0, 4.1, 0);
cameraTheta = 0.55;
cameraPhi = 1.18;
cameraRadius = 16;
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
dragMode =
event.button === 2 || event.shiftKey ? "pan" : "orbit";
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
if (dragMode === "orbit") {
cameraTheta -= dx * 0.005;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - dy * 0.005,
0.35,
1.46
);
} else {
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
0
);
const up = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
1
);
const panScale = cameraRadius * 0.00125;
cameraTarget
.addScaledVector(right, -dx * panScale)
.addScaledVector(up, dy * panScale);
cameraTarget.y = THREE.MathUtils.clamp(
cameraTarget.y,
0.8,
8.0
);
}
updateCamera();
});
function endDrag(event) {
dragging = false;
if (
renderer.domElement.hasPointerCapture &&
event.pointerId !== undefined
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
8.5,
29
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("dblclick", resetCamera);
// UI
let simulationTime = 0;
let paused = false;
let pathsVisible = true;
$("restitutionA").addEventListener("input", rebuildPredictions);
$("restitutionB").addEventListener("input", rebuildPredictions);
$("dropButton").addEventListener("click", () => {
simulationTime = 0;
paused = false;
$("pauseButton").textContent = "Pause";
});
$("pauseButton").addEventListener("click", () => {
paused = !paused;
$("pauseButton").textContent = paused ? "Resume" : "Pause";
});
$("pathsButton").addEventListener("click", () => {
pathsVisible = !pathsVisible;
predictions.forEach((prediction) => {
prediction.prediction.visible = pathsVisible;
prediction.trail.visible = pathsVisible;
});
$("pathsButton").textContent =
pathsVisible ? "Hide paths" : "Show paths";
$("pathsButton").setAttribute(
"aria-pressed",
String(pathsVisible)
);
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
rebuildPredictions();
updateCamera();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) {
simulationTime += delta;
}
const sampleA = updateBall(
ballA,
predictions[0].motion,
simulationTime
);
const sampleB = updateBall(
ballB,
predictions[1].motion,
simulationTime
);
updatePrediction(predictions[0]);
updatePrediction(predictions[1]);
updateReadout("A", sampleA, predictions[0].motion);
updateReadout("B", sampleB, predictions[1].motion);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```